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