2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2005 Fredrik Tolf (fredrik@dolda2000.com)
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.
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.
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
24 #include <doldaconnect/uilib.h>
25 #include <doldaconnect/uimisc.h>
26 #include <doldaconnect/utils.h>
30 #include <accountopt.h>
37 int readhdl, writehdl;
39 GaimRoomlist *roomlist;
42 static struct conndata *inuse = NULL;
43 static GaimPlugin *me;
45 static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition);
47 static void updatewrite(struct conndata *data)
50 if(data->writehdl == -1)
51 data->writehdl = gaim_input_add(data->fd, GAIM_INPUT_WRITE, (void (*)(void *, int, GaimInputCondition))dcfdcb, data);
53 if(data->writehdl != -1) {
54 gaim_input_remove(data->writehdl);
60 static void disconnected(struct conndata *data)
64 if(data->readhdl != -1) {
65 gaim_input_remove(data->readhdl);
68 if(data->writehdl != -1) {
69 gaim_input_remove(data->writehdl);
75 static int loginconv(int type, wchar_t *text, char **resp, struct conndata *data)
78 case DC_LOGIN_CONV_NOECHO:
79 if(data->gc->account->password == NULL) {
83 *resp = sstrdup(data->gc->account->password);
93 static gboolean gi_chatjoincb(GaimConversation *conv, const char *user, GaimConvChatBuddyFlags flags, void *uudata)
97 if((c = gaim_conversation_get_gc(conv)) == NULL)
104 static gboolean gi_chatleavecb(GaimConversation *conv, const char *user, const char *reason, void *uudata)
108 if((c = gaim_conversation_get_gc(conv)) == NULL)
115 static void newpeercb(struct dc_fnetpeer *peer)
117 struct conndata *data;
118 GaimConversation *conv;
121 data = peer->fn->udata;
122 if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
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);
130 static void delpeercb(struct dc_fnetpeer *peer)
132 struct conndata *data;
133 GaimConversation *conv;
136 data = peer->fn->udata;
137 if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
139 buf = sprintf2("%s", icswcstombs(peer->nick, "UTF-8", NULL));
140 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(conv), buf, NULL);
145 static void chpeercb(struct dc_fnetpeer *peer)
149 static void fillpeerlist(struct dc_fnetnode *fn, int resp, struct conndata *data)
153 static void getfnlistcb(int resp, struct conndata *data)
155 struct dc_fnetnode *fn;
157 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next)
159 dc_getpeerlistasync(fn, (void (*)(struct dc_fnetnode *, int, void *))fillpeerlist, data);
161 fn->newpeercb = newpeercb;
162 fn->delpeercb = delpeercb;
163 fn->chpeercb = chpeercb;
167 static void logincb(int err, wchar_t *reason, struct conndata *data)
169 if(err != DC_LOGIN_ERR_SUCCESS) {
172 gaim_connection_error(data->gc, "Invalid login");
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);
180 static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition)
182 struct dc_response *resp;
183 struct dc_intresp *ires;
184 struct dc_fnetnode *fn;
185 GaimConversation *conv;
188 if(((condition & GAIM_INPUT_READ) && dc_handleread()) || ((condition & GAIM_INPUT_WRITE) && dc_handlewrite()))
191 gaim_connection_error(data->gc, "Server has disconnected");
194 while((resp = dc_getresp()) != NULL) {
195 if(!wcscmp(resp->cmdname, L".connect")) {
196 if(resp->code != 201) {
199 gaim_connection_error(data->gc, "Server refused connection");
201 } else if(dc_checkprotocol(resp, DC_LATEST)) {
204 gaim_connection_error(data->gc, "Server protocol revision mismatch");
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);
210 } else if(!wcscmp(resp->cmdname, L".notify")) {
211 dc_uimisc_handlenotify(resp);
214 if((ires = dc_interpret(resp)) != NULL)
216 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
218 if(ires->argv[1].val.num)
220 /* XXX: Handle different rooms */
221 if((conv = gaim_find_chat(data->gc, fn->id)) != NULL)
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));
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));
246 if((ires = dc_interpret(resp)) != NULL)
248 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
251 fn->newpeercb = newpeercb;
252 fn->delpeercb = delpeercb;
253 fn->chpeercb = chpeercb;
267 static int gi_sendchat(GaimConnection *gc, int id, const char *what, GaimMessageFlags flags)
269 struct conndata *data;
270 struct dc_fnetnode *fn;
273 data = gc->proto_data;
274 if((fn = dc_findfnetnode(id)) == NULL)
276 /* XXX: Handle chat rooms */
277 if((wwhat = icmbstowcs((char *)what, "UTF-8")) == NULL)
279 dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", fn->id, L"1", L"", L"%%ls", wwhat, NULL);
285 static int gi_sendim(GaimConnection *gc, const char *who, const char *what, GaimMessageFlags flags)
287 struct conndata *data;
288 struct dc_fnetnode *fn;
289 struct dc_fnetpeer *peer;
290 wchar_t *wwho, *wwhat, *p;
293 data = gc->proto_data;
294 if((wwho = icmbstowcs((char *)who, "UTF-8")) == NULL)
296 if((p = wcschr(wwho, L':')) == NULL) {
301 id = wcstol(wwho, NULL, 10);
302 if((fn = dc_findfnetnode(id)) == NULL) {
306 for(peer = fn->peers; peer != NULL; peer = peer->next) {
307 if(!wcscmp(peer->nick, p))
314 if((wwhat = icmbstowcs((char *)what, "UTF-8")) == NULL) {
319 dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", fn->id, L"0", L"%%ls", peer->nick, L"%%ls", wwhat, NULL);
326 static const char *gi_listicon(GaimAccount *a, GaimBuddy *b)
331 static char *gi_statustext(GaimBuddy *b)
335 p = gaim_buddy_get_presence(b);
336 if (gaim_presence_is_online(p) && !gaim_presence_is_available(p))
337 return(g_strdup("Away"));
341 static void gi_tiptext(GaimBuddy *b, GaimNotifyUserInfo *inf, gboolean full)
343 /* Nothing for now */
346 static GList *gi_statustypes(GaimAccount *act)
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));
357 static struct conndata *newconndata(void)
359 struct conndata *new;
361 new = smalloc(sizeof(*new));
362 memset(new, 0, sizeof(*new));
364 new->readhdl = new->writehdl = -1;
368 static void freeconndata(struct conndata *data)
370 if(data->roomlist != NULL)
371 gaim_roomlist_unref(data->roomlist);
374 if(data->readhdl != -1)
375 gaim_input_remove(data->readhdl);
376 if(data->writehdl != -1)
377 gaim_input_remove(data->writehdl);
383 static void gi_login(GaimAccount *act)
386 struct conndata *data;
388 gc = gaim_account_get_connection(act);
389 gc->proto_data = data = newconndata();
392 gaim_connection_error(gc, "Dolda Connect library already in use");
395 gaim_connection_update_progress(gc, "Connecting", 1, 3);
396 if((data->fd = dc_connect((char *)gaim_account_get_string(act, "server", "localhost"))) < 0)
398 gaim_connection_error(gc, "Could not connect to server");
401 data->readhdl = gaim_input_add(data->fd, GAIM_INPUT_READ, (void (*)(void *, int, GaimInputCondition))dcfdcb, data);
406 static void gi_close(GaimConnection *gc)
408 struct conndata *data;
410 data = gc->proto_data;
414 static GaimRoomlist *gi_getlist(GaimConnection *gc)
416 struct conndata *data;
419 GaimRoomlistField *f;
421 struct dc_fnetnode *fn;
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));
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)
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);
441 gaim_roomlist_set_in_progress(rl, FALSE);
445 static void gi_cancelgetlist(GaimRoomlist *rl)
448 struct conndata *data;
450 if((gc = gaim_account_get_connection(rl->account)) == NULL)
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);
460 static void gi_joinchat(GaimConnection *gc, GHashTable *chatdata)
462 struct conndata *data;
463 struct dc_fnetnode *fn;
464 GaimConversation *conv;
465 struct dc_fnetpeer *peer;
469 data = gc->proto_data;
470 if((fn = dc_findfnetnode(GPOINTER_TO_INT(g_hash_table_lookup(chatdata, "id")))) == NULL)
472 if(gaim_find_chat(gc, fn->id) != NULL)
474 conv = serv_got_joined_chat(data->gc, fn->id, icswcstombs(fn->name, "UTF-8", 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));
481 gaim_conv_chat_add_users(GAIM_CONV_CHAT(conv), ul, NULL, fl, FALSE);
482 for(c = ul; c != NULL; c = c->next)
488 static char *gi_cbname(GaimConnection *gc, int id, const char *who)
490 return(g_strdup_printf("%i:%s", id, who));
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,
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,
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,
517 .name = "Dolda Connect",
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
526 static void init(GaimPlugin *pl)
528 GaimAccountOption *opt;
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);
540 GAIM_INIT_PLUGIN(dolcon, init, info);