Finish porting to Gaim 2.
[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
44 static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition);
45
46 static 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
59 static 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
74 static 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
92 static void newpeercb(struct dc_fnetpeer *peer)
93 {
94     struct conndata *data;
95     GaimConversation *conv;
96     char *buf;
97     
98     data = peer->fn->udata;
99     if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
100     {
101         buf = sprintf2("%i:%s", peer->fn->id, icswcstombs(peer->nick, "UTF-8", NULL));
102         gaim_conv_chat_add_user(GAIM_CONV_CHAT(conv), buf, NULL, GAIM_CBFLAGS_NONE, TRUE);
103         free(buf);
104     }
105 }
106
107 static void delpeercb(struct dc_fnetpeer *peer)
108 {
109     struct conndata *data;
110     GaimConversation *conv;
111     char *buf;
112     
113     data = peer->fn->udata;
114     if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
115     {
116         buf = sprintf2("%i:%s", peer->fn->id, icswcstombs(peer->nick, "UTF-8", NULL));
117         gaim_conv_chat_remove_user(GAIM_CONV_CHAT(conv), buf, NULL);
118         free(buf);
119     }
120 }
121
122 static void chpeercb(struct dc_fnetpeer *peer)
123 {
124 }
125
126 static void fillpeerlist(struct dc_fnetnode *fn, int resp, struct conndata *data)
127 {
128 }
129
130 static 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
144 static 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);
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
157 static 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");
196                                 /* XXX: No more gaim_escape_html?! */
197                                 msg = sstrdup(icswcstombs(ires->argv[4].val.str, "UTF-8", NULL));
198                                 serv_got_chat_in(data->gc, gaim_conv_chat_get_id(GAIM_CONV_CHAT(conv)), peer, 0, msg, time(NULL));
199                                 free(msg);
200                                 free(peer);
201                             }
202                         } else {
203                             peer = sprintf2("%i:%s", fn->id, icswcstombs(ires->argv[3].val.str, "UTF-8", NULL));
204                             msg = sstrdup(icswcstombs(ires->argv[4].val.str, "UTF-8", NULL));
205                             if(!gaim_account_get_bool(data->gc->account, "represspm", FALSE) || (gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, peer, data->gc->account) != NULL))
206                                 serv_got_im(data->gc, peer, msg, 0, time(NULL));
207                             free(msg);
208                             free(peer);
209                         }
210                     }
211                     dc_freeires(ires);
212                 }
213                 break;
214             case 601:
215             case 602:
216             case 603:
217                 break;
218             case 604:
219                 if((ires = dc_interpret(resp)) != NULL)
220                 {
221                     if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
222                     {
223                         fn->udata = data;
224                         fn->newpeercb = newpeercb;
225                         fn->delpeercb = delpeercb;
226                         fn->chpeercb = chpeercb;
227                     }
228                     dc_freeires(ires);
229                 }
230                 break;
231             case 605:
232                 break;
233             }
234         }
235         dc_freeresp(resp);
236     }
237     updatewrite(data);
238 }
239
240 static int gi_sendchat(GaimConnection *gc, int id, const char *what, GaimMessageFlags flags)
241 {
242     struct conndata *data;
243     struct dc_fnetnode *fn;
244     wchar_t *wwhat;
245     
246     data = gc->proto_data;
247     if((fn = dc_findfnetnode(id)) == NULL)
248         return(-EINVAL);
249     /* XXX: Handle chat rooms */
250     if((wwhat = icmbstowcs((char *)what, "UTF-8")) == NULL)
251         return(-errno);
252     dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", fn->id, L"1", L"", L"%%ls", wwhat, NULL);
253     free(wwhat);
254     updatewrite(data);
255     return(0);
256 }
257
258 static int gi_sendim(GaimConnection *gc, const char *who, const char *what, GaimMessageFlags flags)
259 {
260     struct conndata *data;
261     struct dc_fnetnode *fn;
262     struct dc_fnetpeer *peer;
263     wchar_t *wwho, *wwhat, *p;
264     int en, id;
265     
266     data = gc->proto_data;
267     if((wwho = icmbstowcs((char *)who, "UTF-8")) == NULL)
268         return(-errno);
269     if((p = wcschr(wwho, L':')) == NULL) {
270         free(wwho);
271         return(-ESRCH);
272     }
273     *(p++) = L'\0';
274     id = wcstol(wwho, NULL, 10);
275     if((fn = dc_findfnetnode(id)) == NULL) {
276         free(wwho);
277         return(-ESRCH);
278     }
279     for(peer = fn->peers; peer != NULL; peer = peer->next) {
280         if(!wcscmp(peer->nick, p))
281             break;
282     }
283     if(peer == NULL) {
284         free(wwho);
285         return(-ESRCH);
286     }
287     if((wwhat = icmbstowcs((char *)what, "UTF-8")) == NULL) {
288         en = errno;
289         free(wwho);
290         return(-en);
291     }
292     dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", fn->id, L"0", L"%%ls", peer->nick, L"%%ls", wwhat, NULL);
293     free(wwho);
294     free(wwhat);
295     updatewrite(data);
296     return(1);
297 }
298
299 static const char *gi_listicon(GaimAccount *a, GaimBuddy *b)
300 {
301     return("dolcon");
302 }
303
304 static char *gi_statustext(GaimBuddy *b)
305 {
306     GaimPresence *p;
307
308     p = gaim_buddy_get_presence(b);
309     if (gaim_presence_is_online(p) && !gaim_presence_is_available(p))
310         return(g_strdup("Away"));
311     return(NULL);
312 }
313
314 static void gi_tiptext(GaimBuddy *b, GString *buf, gboolean full)
315 {
316     /* Nothing for now */
317 }
318
319 static GList *gi_statustypes(GaimAccount *act)
320 {
321     GList *ret;
322     
323     ret = NULL;
324     ret = g_list_append(ret, gaim_status_type_new(GAIM_STATUS_AVAILABLE, "avail", NULL, TRUE));
325     ret = g_list_append(ret, gaim_status_type_new(GAIM_STATUS_AWAY, "away", NULL, TRUE)); /* Coming up in ADC */
326     ret = g_list_append(ret, gaim_status_type_new(GAIM_STATUS_OFFLINE, "offline", NULL, TRUE));
327     return(ret);
328 }
329
330 static struct conndata *newconndata(void)
331 {
332     struct conndata *new;
333     
334     new = smalloc(sizeof(*new));
335     memset(new, 0, sizeof(*new));
336     new->fd = -1;
337     new->readhdl = new->writehdl = -1;
338     return(new);
339 }
340
341 static void freeconndata(struct conndata *data)
342 {
343     if(data->roomlist != NULL)
344         gaim_roomlist_unref(data->roomlist);
345     if(inuse == data)
346         inuse = NULL;
347     if(data->readhdl != -1)
348         gaim_input_remove(data->readhdl);
349     if(data->writehdl != -1)
350         gaim_input_remove(data->writehdl);
351     if(data->fd >= 0)
352         dc_disconnect();
353     free(data);
354 }
355
356 static void gi_login(GaimAccount *act)
357 {
358     GaimConnection *gc;
359     struct conndata *data;
360     
361     gc = gaim_account_get_connection(act);
362     gc->proto_data = data = newconndata();
363     data->gc = gc;
364     if(inuse != NULL) {
365         gaim_connection_error(gc, "Dolda Connect library already in use");
366         return;
367     }
368     gaim_connection_update_progress(gc, "Connecting", 1, 3);
369     if((data->fd = dc_connect((char *)gaim_account_get_string(act, "server", "localhost"), -1)) < 0)
370     {
371         gaim_connection_error(gc, "Could not connect to server");
372         return;
373     }
374     data->readhdl = gaim_input_add(data->fd, GAIM_INPUT_READ, (void (*)(void *, int, GaimInputCondition))dcfdcb, data);
375     updatewrite(data);
376     inuse = data;
377 }
378
379 static void gi_close(GaimConnection *gc)
380 {
381     struct conndata *data;
382     
383     data = gc->proto_data;
384     freeconndata(data);
385 }
386
387 static GaimRoomlist *gi_getlist(GaimConnection *gc)
388 {
389     struct conndata *data;
390     GList *fields;
391     GaimRoomlist *rl;
392     GaimRoomlistField *f;
393     GaimRoomlistRoom *r;
394     struct dc_fnetnode *fn;
395     
396     data = gc->proto_data;
397     if(data->roomlist != NULL)
398         gaim_roomlist_unref(data->roomlist);
399     data->roomlist = rl = gaim_roomlist_new(gaim_connection_get_account(gc));
400     fields = NULL;
401     f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_INT, "", "id", TRUE);
402     fields = g_list_append(fields, f);
403     f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_INT, "Users", "users", FALSE);
404     fields = g_list_append(fields, f);
405     gaim_roomlist_set_fields(rl, fields);
406     for(fn = dc_fnetnodes; fn != NULL; fn = fn->next) {
407         if(fn->state != DC_FNN_STATE_EST)
408             continue;
409         r = gaim_roomlist_room_new(GAIM_ROOMLIST_ROOMTYPE_ROOM, icswcstombs(fn->name, "UTF-8", NULL), NULL);
410         gaim_roomlist_room_add_field(rl, r, GINT_TO_POINTER(fn->id));
411         gaim_roomlist_room_add_field(rl, r, GINT_TO_POINTER(fn->numusers));
412         gaim_roomlist_room_add(rl, r);
413     }
414     gaim_roomlist_set_in_progress(rl, FALSE);
415     return(rl);
416 }
417
418 static void gi_cancelgetlist(GaimRoomlist *rl)
419 {
420     GaimConnection *gc;
421     struct conndata *data;
422     
423     if((gc = gaim_account_get_connection(rl->account)) == NULL)
424         return;
425     data = gc->proto_data;
426     gaim_roomlist_set_in_progress(rl, FALSE);
427     if(data->roomlist == rl) {
428         data->roomlist = NULL;
429         gaim_roomlist_unref(rl);
430     }
431 }
432
433 static void gi_joinchat(GaimConnection *gc, GHashTable *chatdata)
434 {
435     struct conndata *data;
436     struct dc_fnetnode *fn;
437     GaimConversation *conv;
438     struct dc_fnetpeer *peer;
439     char *buf;
440     GList *ul, *fl, *c;
441     
442     data = gc->proto_data;
443     if((fn = dc_findfnetnode(GPOINTER_TO_INT(g_hash_table_lookup(chatdata, "id")))) == NULL)
444         return;
445     if(gaim_find_chat(gc, fn->id) != NULL)
446         return;
447     conv = serv_got_joined_chat(data->gc, fn->id, icswcstombs(fn->name, "UTF-8", NULL));
448     ul = fl = NULL;
449     for(peer = fn->peers; peer != NULL; peer = peer->next) {
450         buf = sprintf2("%i:%s", fn->id, icswcstombs(peer->nick, "UTF-8", NULL));
451         ul = g_list_append(ul, buf);
452         fl = g_list_append(fl, GINT_TO_POINTER(0));
453     }
454     gaim_conv_chat_add_users(GAIM_CONV_CHAT(conv), ul, NULL, fl, FALSE);
455     for(c = ul; c != NULL; c = c->next)
456         free(c->data);
457     g_list_free(ul);
458     g_list_free(fl);
459 }
460
461 static GaimPluginProtocolInfo protinfo = {
462     .options            = OPT_PROTO_PASSWORD_OPTIONAL,
463     .icon_spec          = NO_BUDDY_ICONS,
464     .list_icon          = gi_listicon,
465     .status_text        = gi_statustext,
466     .tooltip_text       = gi_tiptext,
467     .status_types       = gi_statustypes,
468     .login              = gi_login,
469     .close              = gi_close,
470     .roomlist_get_list  = gi_getlist,
471     .roomlist_cancel    = gi_cancelgetlist,
472     .join_chat          = gi_joinchat,
473     .chat_send          = gi_sendchat,
474     .send_im            = gi_sendim,
475 };
476
477 static GaimPluginInfo info = {
478     .magic              = GAIM_PLUGIN_MAGIC,
479     .major_version      = GAIM_MAJOR_VERSION,
480     .minor_version      = GAIM_MINOR_VERSION,
481     .type               = GAIM_PLUGIN_PROTOCOL,
482     .priority           = GAIM_PRIORITY_DEFAULT,
483     .id                 = "prpl-dolcon",
484     .name               = "Dolda Connect",
485     .version            = VERSION,
486     .summary            = "Dolda Connect chat plugin",
487     .description        = "Allows Gaim to be used as a chat user interface for the Dolda Connect daemon",
488     .author             = "Fredrik Tolf <fredrik@dolda2000.com>",
489     .homepage           = "http://www.dolda2000.com/~fredrik/doldaconnect/",
490     .extra_info         = &protinfo
491 };
492
493 static void init(GaimPlugin *pl)
494 {
495     GaimAccountOption *opt;
496     
497     dc_init();
498     opt = gaim_account_option_string_new("Server", "server", "localhost");
499     protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
500     opt = gaim_account_option_int_new("Port", "port", -1);
501     protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
502     opt = gaim_account_option_bool_new("Do not pop up private messages automatically", "represspm", FALSE);
503     protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
504 }
505
506 GAIM_INIT_PLUGIN(dolcon, init, info);