Improve whitespace.
[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     
97     data = peer->fn->udata;
98     if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
99     {
100         gaim_conv_chat_add_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL, GAIM_CBFLAGS_NONE, TRUE);
101     }
102 }
103
104 static void delpeercb(struct dc_fnetpeer *peer)
105 {
106     struct conndata *data;
107     GaimConversation *conv;
108     
109     data = peer->fn->udata;
110     if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
111     {
112         gaim_conv_chat_remove_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL);
113     }
114 }
115
116 static void chpeercb(struct dc_fnetpeer *peer)
117 {
118 }
119
120 static void fillpeerlist(struct dc_fnetnode *fn, int resp, struct conndata *data)
121 {
122 }
123
124 static void getfnlistcb(int resp, struct conndata *data)
125 {
126     struct dc_fnetnode *fn;
127     
128     for(fn = dc_fnetnodes; fn != NULL; fn = fn->next)
129     {
130         dc_getpeerlistasync(fn, (void (*)(struct dc_fnetnode *, int, void *))fillpeerlist, data);
131         fn->udata = data;
132         fn->newpeercb = newpeercb;
133         fn->delpeercb = delpeercb;
134         fn->chpeercb = chpeercb;
135     }
136 }
137
138 static void logincb(int err, wchar_t *reason, struct conndata *data)
139 {
140     if(err != DC_LOGIN_ERR_SUCCESS) {
141         dc_disconnect();
142         disconnected(data);
143         gaim_connection_error(data->gc, "Invalid login");
144         return;
145     }
146     gaim_connection_set_state(data->gc, GAIM_CONNECTED);
147     serv_finish_login(data->gc);
148     dc_queuecmd(NULL, NULL, L"notify", L"fn:chat", L"on", L"fn:act", L"on", L"fn:peer", L"on", NULL);
149     dc_getfnlistasync((void (*)(int, void *))getfnlistcb, data);
150 }
151
152 static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition)
153 {
154     struct dc_response *resp;
155     struct dc_intresp *ires;
156     struct dc_fnetnode *fn;
157     GaimConversation *conv;
158     char *peer, *msg;
159     
160     if(((condition & GAIM_INPUT_READ) && dc_handleread()) || ((condition & GAIM_INPUT_WRITE) && dc_handlewrite()))
161     {
162         disconnected(data);
163         gaim_connection_error(data->gc, "Server has disconnected");
164         return;
165     }
166     while((resp = dc_getresp()) != NULL) {
167         if(!wcscmp(resp->cmdname, L".connect")) {
168             if(resp->code == 200) {
169                 gaim_connection_update_progress(data->gc, "Authenticating", 2, 3);
170                 dc_loginasync(NULL, 1, (int (*)(int, wchar_t *, char **, void *))loginconv, (void (*)(int, wchar_t *, void *))logincb, data);
171             } else {
172                 dc_disconnect();
173                 disconnected(data);
174                 gaim_connection_error(data->gc, "Server refused connection");
175                 return;
176             }
177         } else if(!wcscmp(resp->cmdname, L".notify")) {
178             dc_uimisc_handlenotify(resp);
179             switch(resp->code) {
180             case 600:
181                 if((ires = dc_interpret(resp)) != NULL)
182                 {
183                     if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
184                     {
185                         if(ires->argv[1].val.num)
186                         {
187                             /* XXX: Handle different rooms */
188                             if((conv = gaim_find_chat(data->gc, fn->id)) != NULL)
189                             {
190                                 peer = icwcstombs(ires->argv[3].val.str, "UTF-8");
191                                 msg = gaim_escape_html(icswcstombs(ires->argv[4].val.str, "UTF-8", NULL));
192                                 serv_got_chat_in(data->gc, gaim_conv_chat_get_id(GAIM_CONV_CHAT(conv)), peer, 0, msg, time(NULL));
193                                 g_free(msg);
194                                 free(peer);
195                             }
196                         } else {
197                             peer = icwcstombs(ires->argv[3].val.str, "UTF-8");
198                             msg = gaim_escape_html(icswcstombs(ires->argv[4].val.str, "UTF-8", NULL));
199                             if(!gaim_account_get_bool(data->gc->account, "represspm", FALSE) || (gaim_find_conversation_with_account(peer, data->gc->account) != NULL))
200                                 serv_got_im(data->gc, peer, msg, 0, time(NULL));
201                             g_free(msg);
202                             free(peer);
203                         }
204                     }
205                     dc_freeires(ires);
206                 }
207                 break;
208             case 601:
209             case 602:
210             case 603:
211             case 604:
212                 if((ires = dc_interpret(resp)) != NULL)
213                 {
214                     if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
215                     {
216                         fn->udata = data;
217                         fn->newpeercb = newpeercb;
218                         fn->delpeercb = delpeercb;
219                         fn->chpeercb = chpeercb;
220                     }
221                     dc_freeires(ires);
222                 }
223             case 605:
224                 break;
225             }
226         }
227     }
228     updatewrite(data);
229 }
230
231 static int gi_sendchat(GaimConnection *gc, int id, const char *what)
232 {
233     struct conndata *data;
234     struct dc_fnetnode *fn;
235     wchar_t *wwhat;
236     
237     data = gc->proto_data;
238     if((fn = dc_findfnetnode(id)) == NULL)
239         return(-EINVAL);
240     /* XXX: Handle chat rooms */
241     if((wwhat = icmbstowcs((char *)what, "UTF-8")) == NULL)
242         return(-errno);
243     dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", fn->id, L"1", L"", L"%%ls", wwhat, NULL);
244     free(wwhat);
245     updatewrite(data);
246     return(0);
247 }
248
249 static int gi_sendim(GaimConnection *gc, const char *who, const char *what, GaimConvImFlags flags)
250 {
251     struct conndata *data;
252     struct dc_fnetnode *fn, *tfn;
253     struct dc_fnetpeer *peer, *tpeer;
254     wchar_t *wwho, *wwhat;
255     int en;
256     
257     data = gc->proto_data;
258     if((wwho = icmbstowcs((char *)who, "UTF-8")) == NULL)
259         return(-errno);
260     tpeer = NULL;
261     for(fn = dc_fnetnodes; fn != NULL; fn = fn->next) {
262         for(peer = fn->peers; peer != NULL; peer = peer->next) {
263             if(!wcscmp(wwho, peer->nick)) {
264                 if(tpeer == NULL) {
265                     tpeer = peer;
266                     tfn = fn;
267                 } else {
268                     free(wwho);
269                     return(-ESRCH);
270                 }
271             }
272         }
273     }
274     if(tpeer == NULL) {
275         free(wwho);
276         return(-ESRCH);
277     }
278     if((wwhat = icmbstowcs((char *)what, "UTF-8")) == NULL) {
279         en = errno;
280         free(wwho);
281         return(-en);
282     }
283     dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", tfn->id, L"0", L"%%ls", wwho, L"%%ls", wwhat, NULL);
284     free(wwho);
285     free(wwhat);
286     updatewrite(data);
287     return(1);
288 }
289
290 static const char *gi_listicon(GaimAccount *a, GaimBuddy *b)
291 {
292     return("dolcon");
293 }
294
295 static struct conndata *newconndata(void)
296 {
297     struct conndata *new;
298     
299     new = smalloc(sizeof(*new));
300     memset(new, 0, sizeof(*new));
301     new->fd = -1;
302     new->readhdl = new->writehdl = -1;
303     return(new);
304 }
305
306 static void freeconndata(struct conndata *data)
307 {
308     if(data->roomlist != NULL)
309         gaim_roomlist_unref(data->roomlist);
310     if(inuse == data)
311         inuse = NULL;
312     if(data->readhdl != -1)
313         gaim_input_remove(data->readhdl);
314     if(data->writehdl != -1)
315         gaim_input_remove(data->writehdl);
316     if(data->fd >= 0)
317         dc_disconnect();
318     free(data);
319 }
320
321 static void gi_login(GaimAccount *act)
322 {
323     GaimConnection *gc;
324     struct conndata *data;
325     
326     gc = gaim_account_get_connection(act);
327     gc->proto_data = data = newconndata();
328     data->gc = gc;
329     if(inuse != NULL) {
330         gaim_connection_error(gc, "Dolda Connect library already in use");
331         return;
332     }
333     gaim_connection_update_progress(gc, "Connecting", 1, 3);
334     if((data->fd = dc_connect((char *)gaim_account_get_string(act, "server", "localhost"), -1)) < 0)
335     {
336         gaim_connection_error(gc, "Could not connect to server");
337         return;
338     }
339     data->readhdl = gaim_input_add(data->fd, GAIM_INPUT_READ, (void (*)(void *, int, GaimInputCondition))dcfdcb, data);
340     updatewrite(data);
341     inuse = data;
342 }
343
344 static void gi_close(GaimConnection *gc)
345 {
346     struct conndata *data;
347     
348     data = gc->proto_data;
349     freeconndata(data);
350 }
351
352 static GaimRoomlist *gi_getlist(GaimConnection *gc)
353 {
354     struct conndata *data;
355     GList *fields;
356     GaimRoomlist *rl;
357     GaimRoomlistField *f;
358     GaimRoomlistRoom *r;
359     struct dc_fnetnode *fn;
360     
361     data = gc->proto_data;
362     if(data->roomlist != NULL)
363         gaim_roomlist_unref(data->roomlist);
364     data->roomlist = rl = gaim_roomlist_new(gaim_connection_get_account(gc));
365     fields = NULL;
366     f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_INT, "", "id", TRUE);
367     fields = g_list_append(fields, f);
368     f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_INT, "Users", "users", FALSE);
369     fields = g_list_append(fields, f);
370     gaim_roomlist_set_fields(rl, fields);
371     for(fn = dc_fnetnodes; fn != NULL; fn = fn->next) {
372         if(fn->state != DC_FNN_STATE_EST)
373             continue;
374         r = gaim_roomlist_room_new(GAIM_ROOMLIST_ROOMTYPE_ROOM, icswcstombs(fn->name, "UTF-8", NULL), NULL);
375         gaim_roomlist_room_add_field(rl, r, GINT_TO_POINTER(fn->id));
376         gaim_roomlist_room_add_field(rl, r, GINT_TO_POINTER(fn->numusers));
377         gaim_roomlist_room_add(rl, r);
378     }
379     gaim_roomlist_set_in_progress(rl, FALSE);
380     return(rl);
381 }
382
383 static void gi_cancelgetlist(GaimRoomlist *rl)
384 {
385     GaimConnection *gc;
386     struct conndata *data;
387     
388     if((gc = gaim_account_get_connection(rl->account)) == NULL)
389         return;
390     data = gc->proto_data;
391     gaim_roomlist_set_in_progress(rl, FALSE);
392     if(data->roomlist == rl) {
393         data->roomlist = NULL;
394         gaim_roomlist_unref(rl);
395     }
396 }
397
398 static void getpeerlistcb(struct dc_fnetnode *fn, int resp, struct conndata *data)
399 {
400     GaimConversation *conv;
401     struct dc_fnetpeer *peer;
402     
403     if(resp == 200)
404     {
405         if(gaim_find_chat(data->gc, fn->id) != NULL)
406             return;
407         conv = serv_got_joined_chat(data->gc, fn->id, icswcstombs(fn->name, "UTF-8", NULL));
408         for(peer = fn->peers; peer != NULL; peer = peer->next)
409             gaim_conv_chat_add_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL, GAIM_CBFLAGS_NONE, FALSE);
410     }
411 }
412
413 static void gi_joinchat(GaimConnection *gc, GHashTable *chatdata)
414 {
415     struct conndata *data;
416     struct dc_fnetnode *fn;
417     
418     data = gc->proto_data;
419     if((fn = dc_findfnetnode(GPOINTER_TO_INT(g_hash_table_lookup(chatdata, "id")))) == NULL)
420         return;
421     if(gaim_find_chat(gc, fn->id) != NULL)
422         return;
423     dc_getpeerlistasync(fn, (void (*)(struct dc_fnetnode *, int, void *))getpeerlistcb, data);
424     updatewrite(data);
425 }
426
427 static GaimPluginProtocolInfo protinfo = {
428     .options            = OPT_PROTO_PASSWORD_OPTIONAL,
429     .icon_spec          = NO_BUDDY_ICONS,
430     .list_icon          = gi_listicon,
431     .login              = gi_login,
432     .close              = gi_close,
433     .roomlist_get_list  = gi_getlist,
434     .roomlist_cancel    = gi_cancelgetlist,
435     .join_chat          = gi_joinchat,
436     .chat_send          = gi_sendchat,
437     .send_im            = gi_sendim,
438 };
439
440 static GaimPluginInfo info = {
441     .magic              = GAIM_PLUGIN_MAGIC,
442     .major_version      = GAIM_MAJOR_VERSION,
443     .minor_version      = GAIM_MINOR_VERSION,
444     .type               = GAIM_PLUGIN_PROTOCOL,
445     .priority           = GAIM_PRIORITY_DEFAULT,
446     .id                 = "prpl-dolcon",
447     .name               = "Dolda Connect",
448     .version            = VERSION,
449     .summary            = "Dolda Connect chat plugin",
450     .description        = "Allows Gaim to be used as a chat user interface for the Dolda Connect daemon",
451     .author             = "Fredrik Tolf <fredrik@dolda2000.com>",
452     .homepage           = "http://www.dolda2000.com/~fredrik/doldaconnect/",
453     .extra_info         = &protinfo
454 };
455
456 static void init(GaimPlugin *pl)
457 {
458     GaimAccountOption *opt;
459     
460     dc_init();
461     opt = gaim_account_option_string_new("Server", "server", "localhost");
462     protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
463     opt = gaim_account_option_int_new("Port", "port", -1);
464     protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
465     opt = gaim_account_option_bool_new("Do not pop up private messages automatically", "represspm", FALSE);
466     protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
467 }
468
469 GAIM_INIT_PLUGIN(dolcon, init, info);