Fix stupid typos.
[doldaconnect.git] / clients / gaim / gaim-dolcon.c
CommitLineData
0af3c916 1/*
2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2005 Fredrik Tolf (fredrik@dolda2000.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
ccea96e5 19
ee5d122f 20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23#include <string.h>
24#include <doldaconnect/uilib.h>
25#include <doldaconnect/uimisc.h>
26#include <doldaconnect/utils.h>
27#include <gaim.h>
28#include <plugin.h>
29#include <version.h>
30#include <accountopt.h>
31#include <roomlist.h>
32#include <util.h>
33#include <errno.h>
34
35struct conndata {
36 int fd;
37 int readhdl, writehdl;
38 GaimConnection *gc;
39 GaimRoomlist *roomlist;
40};
41
42static struct conndata *inuse = NULL;
43
44static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition);
45
46static void updatewrite(struct conndata *data)
47{
48 if(dc_wantwrite()) {
49 if(data->writehdl == -1)
50 data->writehdl = gaim_input_add(data->fd, GAIM_INPUT_WRITE, (void (*)(void *, int, GaimInputCondition))dcfdcb, data);
51 } else {
52 if(data->writehdl != -1) {
53 gaim_input_remove(data->writehdl);
54 data->writehdl = -1;
55 }
56 }
57}
58
59static void disconnected(struct conndata *data)
60{
61 if(inuse == data)
62 inuse = NULL;
63 if(data->readhdl != -1) {
64 gaim_input_remove(data->readhdl);
65 data->readhdl = -1;
66 }
67 if(data->writehdl != -1) {
68 gaim_input_remove(data->writehdl);
69 data->writehdl = -1;
70 }
71 data->fd = -1;
72}
73
74static int loginconv(int type, wchar_t *text, char **resp, struct conndata *data)
75{
76 switch(type) {
77 case DC_LOGIN_CONV_NOECHO:
78 if(data->gc->account->password == NULL) {
79 updatewrite(data);
80 return(1);
81 } else {
82 *resp = sstrdup(data->gc->account->password);
83 updatewrite(data);
84 return(0);
85 }
86 default:
87 updatewrite(data);
88 return(1);
89 }
90}
91
92static void newpeercb(struct dc_fnetpeer *peer)
93{
94 struct conndata *data;
95 GaimConversation *conv;
dc53e482 96 char *buf;
ee5d122f 97
98 data = peer->fn->udata;
99 if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
100 {
c17c238e 101 buf = sprintf2("%s", icswcstombs(peer->nick, "UTF-8", NULL));
dc53e482 102 gaim_conv_chat_add_user(GAIM_CONV_CHAT(conv), buf, NULL, GAIM_CBFLAGS_NONE, TRUE);
103 free(buf);
ee5d122f 104 }
105}
106
107static void delpeercb(struct dc_fnetpeer *peer)
108{
109 struct conndata *data;
110 GaimConversation *conv;
dc53e482 111 char *buf;
ee5d122f 112
113 data = peer->fn->udata;
114 if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
115 {
c17c238e 116 buf = sprintf2("%s", icswcstombs(peer->nick, "UTF-8", NULL));
dc53e482 117 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(conv), buf, NULL);
118 free(buf);
ee5d122f 119 }
120}
121
122static void chpeercb(struct dc_fnetpeer *peer)
123{
124}
125
126static void fillpeerlist(struct dc_fnetnode *fn, int resp, struct conndata *data)
127{
128}
129
130static void getfnlistcb(int resp, struct conndata *data)
131{
132 struct dc_fnetnode *fn;
133
134 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next)
135 {
136 dc_getpeerlistasync(fn, (void (*)(struct dc_fnetnode *, int, void *))fillpeerlist, data);
137 fn->udata = data;
138 fn->newpeercb = newpeercb;
139 fn->delpeercb = delpeercb;
140 fn->chpeercb = chpeercb;
141 }
142}
143
144static void logincb(int err, wchar_t *reason, struct conndata *data)
145{
146 if(err != DC_LOGIN_ERR_SUCCESS) {
147 dc_disconnect();
148 disconnected(data);
149 gaim_connection_error(data->gc, "Invalid login");
150 return;
151 }
152 gaim_connection_set_state(data->gc, GAIM_CONNECTED);
ee5d122f 153 dc_queuecmd(NULL, NULL, L"notify", L"fn:chat", L"on", L"fn:act", L"on", L"fn:peer", L"on", NULL);
154 dc_getfnlistasync((void (*)(int, void *))getfnlistcb, data);
155}
156
157static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition)
158{
159 struct dc_response *resp;
160 struct dc_intresp *ires;
161 struct dc_fnetnode *fn;
162 GaimConversation *conv;
163 char *peer, *msg;
164
165 if(((condition & GAIM_INPUT_READ) && dc_handleread()) || ((condition & GAIM_INPUT_WRITE) && dc_handlewrite()))
166 {
167 disconnected(data);
168 gaim_connection_error(data->gc, "Server has disconnected");
169 return;
170 }
171 while((resp = dc_getresp()) != NULL) {
172 if(!wcscmp(resp->cmdname, L".connect")) {
173 if(resp->code == 200) {
174 gaim_connection_update_progress(data->gc, "Authenticating", 2, 3);
175 dc_loginasync(NULL, 1, (int (*)(int, wchar_t *, char **, void *))loginconv, (void (*)(int, wchar_t *, void *))logincb, data);
176 } else {
177 dc_disconnect();
178 disconnected(data);
179 gaim_connection_error(data->gc, "Server refused connection");
180 return;
181 }
182 } else if(!wcscmp(resp->cmdname, L".notify")) {
183 dc_uimisc_handlenotify(resp);
184 switch(resp->code) {
185 case 600:
186 if((ires = dc_interpret(resp)) != NULL)
187 {
188 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
189 {
190 if(ires->argv[1].val.num)
191 {
192 /* XXX: Handle different rooms */
193 if((conv = gaim_find_chat(data->gc, fn->id)) != NULL)
194 {
195 peer = icwcstombs(ires->argv[3].val.str, "UTF-8");
d5e00af4 196 msg = g_markup_escape_text(icswcstombs(ires->argv[4].val.str, "UTF-8", NULL), -1);
ee5d122f 197 serv_got_chat_in(data->gc, gaim_conv_chat_get_id(GAIM_CONV_CHAT(conv)), peer, 0, msg, time(NULL));
d5e00af4 198 g_free(msg);
ee5d122f 199 free(peer);
200 }
201 } else {
dc53e482 202 peer = sprintf2("%i:%s", fn->id, icswcstombs(ires->argv[3].val.str, "UTF-8", NULL));
d5e00af4 203 msg = g_markup_escape_text(icswcstombs(ires->argv[4].val.str, "UTF-8", NULL), -1);
878a4636 204 if(!gaim_account_get_bool(data->gc->account, "represspm", FALSE) || (gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, peer, data->gc->account) != NULL))
ee5d122f 205 serv_got_im(data->gc, peer, msg, 0, time(NULL));
d5e00af4 206 g_free(msg);
ee5d122f 207 free(peer);
208 }
209 }
210 dc_freeires(ires);
211 }
212 break;
213 case 601:
214 case 602:
215 case 603:
dc53e482 216 break;
ee5d122f 217 case 604:
218 if((ires = dc_interpret(resp)) != NULL)
219 {
220 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
221 {
222 fn->udata = data;
223 fn->newpeercb = newpeercb;
224 fn->delpeercb = delpeercb;
225 fn->chpeercb = chpeercb;
226 }
227 dc_freeires(ires);
228 }
dc53e482 229 break;
ee5d122f 230 case 605:
231 break;
232 }
233 }
17221b02 234 dc_freeresp(resp);
ee5d122f 235 }
236 updatewrite(data);
237}
238
878a4636 239static int gi_sendchat(GaimConnection *gc, int id, const char *what, GaimMessageFlags flags)
ee5d122f 240{
241 struct conndata *data;
242 struct dc_fnetnode *fn;
243 wchar_t *wwhat;
244
245 data = gc->proto_data;
246 if((fn = dc_findfnetnode(id)) == NULL)
247 return(-EINVAL);
248 /* XXX: Handle chat rooms */
249 if((wwhat = icmbstowcs((char *)what, "UTF-8")) == NULL)
250 return(-errno);
251 dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", fn->id, L"1", L"", L"%%ls", wwhat, NULL);
252 free(wwhat);
253 updatewrite(data);
254 return(0);
255}
256
878a4636 257static int gi_sendim(GaimConnection *gc, const char *who, const char *what, GaimMessageFlags flags)
ee5d122f 258{
259 struct conndata *data;
dc53e482 260 struct dc_fnetnode *fn;
261 struct dc_fnetpeer *peer;
262 wchar_t *wwho, *wwhat, *p;
263 int en, id;
ee5d122f 264
265 data = gc->proto_data;
266 if((wwho = icmbstowcs((char *)who, "UTF-8")) == NULL)
267 return(-errno);
dc53e482 268 if((p = wcschr(wwho, L':')) == NULL) {
269 free(wwho);
270 return(-ESRCH);
271 }
272 *(p++) = L'\0';
273 id = wcstol(wwho, NULL, 10);
274 if((fn = dc_findfnetnode(id)) == NULL) {
275 free(wwho);
276 return(-ESRCH);
ee5d122f 277 }
dc53e482 278 for(peer = fn->peers; peer != NULL; peer = peer->next) {
279 if(!wcscmp(peer->nick, p))
280 break;
281 }
282 if(peer == NULL) {
ee5d122f 283 free(wwho);
284 return(-ESRCH);
285 }
286 if((wwhat = icmbstowcs((char *)what, "UTF-8")) == NULL) {
287 en = errno;
288 free(wwho);
289 return(-en);
290 }
dc53e482 291 dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", fn->id, L"0", L"%%ls", peer->nick, L"%%ls", wwhat, NULL);
ee5d122f 292 free(wwho);
293 free(wwhat);
294 updatewrite(data);
295 return(1);
296}
297
298static const char *gi_listicon(GaimAccount *a, GaimBuddy *b)
299{
300 return("dolcon");
301}
302
7f6ee5df 303static char *gi_statustext(GaimBuddy *b)
304{
305 GaimPresence *p;
306
307 p = gaim_buddy_get_presence(b);
308 if (gaim_presence_is_online(p) && !gaim_presence_is_available(p))
309 return(g_strdup("Away"));
310 return(NULL);
311}
312
313static void gi_tiptext(GaimBuddy *b, GString *buf, gboolean full)
314{
315 /* Nothing for now */
316}
317
318static GList *gi_statustypes(GaimAccount *act)
319{
320 GList *ret;
321
322 ret = NULL;
323 ret = g_list_append(ret, gaim_status_type_new(GAIM_STATUS_AVAILABLE, "avail", NULL, TRUE));
324 ret = g_list_append(ret, gaim_status_type_new(GAIM_STATUS_AWAY, "away", NULL, TRUE)); /* Coming up in ADC */
325 ret = g_list_append(ret, gaim_status_type_new(GAIM_STATUS_OFFLINE, "offline", NULL, TRUE));
326 return(ret);
327}
328
ee5d122f 329static struct conndata *newconndata(void)
330{
331 struct conndata *new;
332
333 new = smalloc(sizeof(*new));
334 memset(new, 0, sizeof(*new));
335 new->fd = -1;
336 new->readhdl = new->writehdl = -1;
337 return(new);
338}
339
340static void freeconndata(struct conndata *data)
341{
342 if(data->roomlist != NULL)
343 gaim_roomlist_unref(data->roomlist);
344 if(inuse == data)
345 inuse = NULL;
346 if(data->readhdl != -1)
347 gaim_input_remove(data->readhdl);
348 if(data->writehdl != -1)
349 gaim_input_remove(data->writehdl);
350 if(data->fd >= 0)
351 dc_disconnect();
352 free(data);
353}
354
355static void gi_login(GaimAccount *act)
356{
357 GaimConnection *gc;
358 struct conndata *data;
359
360 gc = gaim_account_get_connection(act);
361 gc->proto_data = data = newconndata();
362 data->gc = gc;
363 if(inuse != NULL) {
364 gaim_connection_error(gc, "Dolda Connect library already in use");
365 return;
366 }
367 gaim_connection_update_progress(gc, "Connecting", 1, 3);
368 if((data->fd = dc_connect((char *)gaim_account_get_string(act, "server", "localhost"), -1)) < 0)
369 {
370 gaim_connection_error(gc, "Could not connect to server");
371 return;
372 }
373 data->readhdl = gaim_input_add(data->fd, GAIM_INPUT_READ, (void (*)(void *, int, GaimInputCondition))dcfdcb, data);
374 updatewrite(data);
375 inuse = data;
376}
377
378static void gi_close(GaimConnection *gc)
379{
380 struct conndata *data;
381
382 data = gc->proto_data;
383 freeconndata(data);
384}
385
386static GaimRoomlist *gi_getlist(GaimConnection *gc)
387{
388 struct conndata *data;
389 GList *fields;
390 GaimRoomlist *rl;
391 GaimRoomlistField *f;
392 GaimRoomlistRoom *r;
393 struct dc_fnetnode *fn;
394
395 data = gc->proto_data;
396 if(data->roomlist != NULL)
397 gaim_roomlist_unref(data->roomlist);
398 data->roomlist = rl = gaim_roomlist_new(gaim_connection_get_account(gc));
399 fields = NULL;
400 f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_INT, "", "id", TRUE);
401 fields = g_list_append(fields, f);
402 f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_INT, "Users", "users", FALSE);
403 fields = g_list_append(fields, f);
404 gaim_roomlist_set_fields(rl, fields);
405 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next) {
406 if(fn->state != DC_FNN_STATE_EST)
407 continue;
408 r = gaim_roomlist_room_new(GAIM_ROOMLIST_ROOMTYPE_ROOM, icswcstombs(fn->name, "UTF-8", NULL), NULL);
409 gaim_roomlist_room_add_field(rl, r, GINT_TO_POINTER(fn->id));
410 gaim_roomlist_room_add_field(rl, r, GINT_TO_POINTER(fn->numusers));
411 gaim_roomlist_room_add(rl, r);
412 }
413 gaim_roomlist_set_in_progress(rl, FALSE);
414 return(rl);
415}
416
417static void gi_cancelgetlist(GaimRoomlist *rl)
418{
419 GaimConnection *gc;
420 struct conndata *data;
421
422 if((gc = gaim_account_get_connection(rl->account)) == NULL)
423 return;
424 data = gc->proto_data;
425 gaim_roomlist_set_in_progress(rl, FALSE);
426 if(data->roomlist == rl) {
427 data->roomlist = NULL;
428 gaim_roomlist_unref(rl);
429 }
430}
431
ee5d122f 432static void gi_joinchat(GaimConnection *gc, GHashTable *chatdata)
433{
434 struct conndata *data;
435 struct dc_fnetnode *fn;
dc53e482 436 GaimConversation *conv;
437 struct dc_fnetpeer *peer;
438 char *buf;
7f6ee5df 439 GList *ul, *fl, *c;
ee5d122f 440
441 data = gc->proto_data;
442 if((fn = dc_findfnetnode(GPOINTER_TO_INT(g_hash_table_lookup(chatdata, "id")))) == NULL)
443 return;
444 if(gaim_find_chat(gc, fn->id) != NULL)
445 return;
dc53e482 446 conv = serv_got_joined_chat(data->gc, fn->id, icswcstombs(fn->name, "UTF-8", NULL));
7f6ee5df 447 ul = fl = NULL;
dc53e482 448 for(peer = fn->peers; peer != NULL; peer = peer->next) {
d5e00af4 449 buf = icwcstombs(peer->nick, "UTF-8");
7f6ee5df 450 ul = g_list_append(ul, buf);
451 fl = g_list_append(fl, GINT_TO_POINTER(0));
dc53e482 452 }
7f6ee5df 453 gaim_conv_chat_add_users(GAIM_CONV_CHAT(conv), ul, NULL, fl, FALSE);
454 for(c = ul; c != NULL; c = c->next)
455 free(c->data);
456 g_list_free(ul);
457 g_list_free(fl);
ee5d122f 458}
459
d5e00af4 460static char *gi_cbname(GaimConnection *gc, int id, const char *who)
461{
462 return(g_strdup_printf("%i:%s", id, who));
463}
464
ee5d122f 465static GaimPluginProtocolInfo protinfo = {
466 .options = OPT_PROTO_PASSWORD_OPTIONAL,
467 .icon_spec = NO_BUDDY_ICONS,
468 .list_icon = gi_listicon,
7f6ee5df 469 .status_text = gi_statustext,
470 .tooltip_text = gi_tiptext,
471 .status_types = gi_statustypes,
ee5d122f 472 .login = gi_login,
473 .close = gi_close,
474 .roomlist_get_list = gi_getlist,
475 .roomlist_cancel = gi_cancelgetlist,
476 .join_chat = gi_joinchat,
477 .chat_send = gi_sendchat,
478 .send_im = gi_sendim,
d5e00af4 479 .get_cb_real_name = gi_cbname,
ee5d122f 480};
481
482static GaimPluginInfo info = {
483 .magic = GAIM_PLUGIN_MAGIC,
484 .major_version = GAIM_MAJOR_VERSION,
485 .minor_version = GAIM_MINOR_VERSION,
486 .type = GAIM_PLUGIN_PROTOCOL,
487 .priority = GAIM_PRIORITY_DEFAULT,
488 .id = "prpl-dolcon",
489 .name = "Dolda Connect",
490 .version = VERSION,
491 .summary = "Dolda Connect chat plugin",
492 .description = "Allows Gaim to be used as a chat user interface for the Dolda Connect daemon",
493 .author = "Fredrik Tolf <fredrik@dolda2000.com>",
494 .homepage = "http://www.dolda2000.com/~fredrik/doldaconnect/",
495 .extra_info = &protinfo
496};
497
498static void init(GaimPlugin *pl)
499{
500 GaimAccountOption *opt;
501
502 dc_init();
503 opt = gaim_account_option_string_new("Server", "server", "localhost");
504 protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
505 opt = gaim_account_option_int_new("Port", "port", -1);
506 protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
507 opt = gaim_account_option_bool_new("Do not pop up private messages automatically", "represspm", FALSE);
508 protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
509}
510
511GAIM_INIT_PLUGIN(dolcon, init, info);