Update to new dc_queuecmd syntax.
[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;
649eeeb6 43static GaimPlugin *me;
ee5d122f 44
45static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition);
46
47static 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
60static 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
75static 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
649eeeb6 93static 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
104static 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
ee5d122f 115static void newpeercb(struct dc_fnetpeer *peer)
116{
117 struct conndata *data;
118 GaimConversation *conv;
dc53e482 119 char *buf;
ee5d122f 120
121 data = peer->fn->udata;
122 if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
123 {
c17c238e 124 buf = sprintf2("%s", icswcstombs(peer->nick, "UTF-8", NULL));
dc53e482 125 gaim_conv_chat_add_user(GAIM_CONV_CHAT(conv), buf, NULL, GAIM_CBFLAGS_NONE, TRUE);
126 free(buf);
ee5d122f 127 }
128}
129
130static void delpeercb(struct dc_fnetpeer *peer)
131{
132 struct conndata *data;
133 GaimConversation *conv;
dc53e482 134 char *buf;
ee5d122f 135
136 data = peer->fn->udata;
137 if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
138 {
c17c238e 139 buf = sprintf2("%s", icswcstombs(peer->nick, "UTF-8", NULL));
dc53e482 140 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(conv), buf, NULL);
141 free(buf);
ee5d122f 142 }
143}
144
145static void chpeercb(struct dc_fnetpeer *peer)
146{
147}
148
149static void fillpeerlist(struct dc_fnetnode *fn, int resp, struct conndata *data)
150{
151}
152
153static 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
167static 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);
ee5d122f 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
180static 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")) {
5e1e52f1 196 if(resp->code != 201) {
ee5d122f 197 dc_disconnect();
198 disconnected(data);
199 gaim_connection_error(data->gc, "Server refused connection");
200 return;
5e1e52f1 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);
ee5d122f 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");
d5e00af4 224 msg = g_markup_escape_text(icswcstombs(ires->argv[4].val.str, "UTF-8", NULL), -1);
ee5d122f 225 serv_got_chat_in(data->gc, gaim_conv_chat_get_id(GAIM_CONV_CHAT(conv)), peer, 0, msg, time(NULL));
d5e00af4 226 g_free(msg);
ee5d122f 227 free(peer);
228 }
229 } else {
dc53e482 230 peer = sprintf2("%i:%s", fn->id, icswcstombs(ires->argv[3].val.str, "UTF-8", NULL));
d5e00af4 231 msg = g_markup_escape_text(icswcstombs(ires->argv[4].val.str, "UTF-8", NULL), -1);
878a4636 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))
ee5d122f 233 serv_got_im(data->gc, peer, msg, 0, time(NULL));
d5e00af4 234 g_free(msg);
ee5d122f 235 free(peer);
236 }
237 }
238 dc_freeires(ires);
239 }
240 break;
241 case 601:
242 case 602:
243 case 603:
dc53e482 244 break;
ee5d122f 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 }
dc53e482 257 break;
ee5d122f 258 case 605:
259 break;
260 }
261 }
17221b02 262 dc_freeresp(resp);
ee5d122f 263 }
264 updatewrite(data);
265}
266
878a4636 267static int gi_sendchat(GaimConnection *gc, int id, const char *what, GaimMessageFlags flags)
ee5d122f 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);
0931eb36 279 dc_queuecmd(NULL, NULL, L"sendchat", L"%i", fn->id, L"1", L"", L"%ls", wwhat, NULL);
ee5d122f 280 free(wwhat);
281 updatewrite(data);
282 return(0);
283}
284
878a4636 285static int gi_sendim(GaimConnection *gc, const char *who, const char *what, GaimMessageFlags flags)
ee5d122f 286{
287 struct conndata *data;
dc53e482 288 struct dc_fnetnode *fn;
289 struct dc_fnetpeer *peer;
290 wchar_t *wwho, *wwhat, *p;
291 int en, id;
ee5d122f 292
293 data = gc->proto_data;
294 if((wwho = icmbstowcs((char *)who, "UTF-8")) == NULL)
295 return(-errno);
dc53e482 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);
ee5d122f 305 }
dc53e482 306 for(peer = fn->peers; peer != NULL; peer = peer->next) {
307 if(!wcscmp(peer->nick, p))
308 break;
309 }
310 if(peer == NULL) {
ee5d122f 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 }
0931eb36 319 dc_queuecmd(NULL, NULL, L"sendchat", L"%i", fn->id, L"0", L"%ls", peer->nick, L"%ls", wwhat, NULL);
ee5d122f 320 free(wwho);
321 free(wwhat);
322 updatewrite(data);
323 return(1);
324}
325
326static const char *gi_listicon(GaimAccount *a, GaimBuddy *b)
327{
328 return("dolcon");
329}
330
7f6ee5df 331static 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
649eeeb6 341static void gi_tiptext(GaimBuddy *b, GaimNotifyUserInfo *inf, gboolean full)
7f6ee5df 342{
343 /* Nothing for now */
344}
345
346static 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
ee5d122f 357static 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
368static 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
383static 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);
65bf229b 396 if((data->fd = dc_connect((char *)gaim_account_get_string(act, "server", "localhost"))) < 0)
ee5d122f 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
406static void gi_close(GaimConnection *gc)
407{
408 struct conndata *data;
409
410 data = gc->proto_data;
411 freeconndata(data);
412}
413
414static 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
445static 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
ee5d122f 460static void gi_joinchat(GaimConnection *gc, GHashTable *chatdata)
461{
462 struct conndata *data;
463 struct dc_fnetnode *fn;
dc53e482 464 GaimConversation *conv;
465 struct dc_fnetpeer *peer;
466 char *buf;
7f6ee5df 467 GList *ul, *fl, *c;
ee5d122f 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;
dc53e482 474 conv = serv_got_joined_chat(data->gc, fn->id, icswcstombs(fn->name, "UTF-8", NULL));
7f6ee5df 475 ul = fl = NULL;
dc53e482 476 for(peer = fn->peers; peer != NULL; peer = peer->next) {
d5e00af4 477 buf = icwcstombs(peer->nick, "UTF-8");
7f6ee5df 478 ul = g_list_append(ul, buf);
479 fl = g_list_append(fl, GINT_TO_POINTER(0));
dc53e482 480 }
7f6ee5df 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);
ee5d122f 486}
487
d5e00af4 488static char *gi_cbname(GaimConnection *gc, int id, const char *who)
489{
490 return(g_strdup_printf("%i:%s", id, who));
491}
492
ee5d122f 493static GaimPluginProtocolInfo protinfo = {
494 .options = OPT_PROTO_PASSWORD_OPTIONAL,
495 .icon_spec = NO_BUDDY_ICONS,
496 .list_icon = gi_listicon,
7f6ee5df 497 .status_text = gi_statustext,
498 .tooltip_text = gi_tiptext,
499 .status_types = gi_statustypes,
ee5d122f 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,
d5e00af4 507 .get_cb_real_name = gi_cbname,
ee5d122f 508};
509
510static 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
526static void init(GaimPlugin *pl)
527{
528 GaimAccountOption *opt;
529
530 dc_init();
65bf229b 531 opt = gaim_account_option_string_new("Server", "server", "");
ee5d122f 532 protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
ee5d122f 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);
649eeeb6 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;
ee5d122f 538}
539
540GAIM_INIT_PLUGIN(dolcon, init, info);