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