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
23 #include <doldaconnect/uilib.h>
24 #include <doldaconnect/uimisc.h>
25 #include <doldaconnect/utils.h>
29 #include <accountopt.h>
36 int readhdl, writehdl;
38 GaimRoomlist *roomlist;
41 static struct conndata *inuse = NULL;
43 static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition);
45 static void updatewrite(struct conndata *data)
48 if(data->writehdl == -1)
49 data->writehdl = gaim_input_add(data->fd, GAIM_INPUT_WRITE, (void (*)(void *, int, GaimInputCondition))dcfdcb, data);
51 if(data->writehdl != -1) {
52 gaim_input_remove(data->writehdl);
58 static void disconnected(struct conndata *data)
62 if(data->readhdl != -1) {
63 gaim_input_remove(data->readhdl);
66 if(data->writehdl != -1) {
67 gaim_input_remove(data->writehdl);
73 static int loginconv(int type, wchar_t *text, char **resp, struct conndata *data)
76 case DC_LOGIN_CONV_NOECHO:
77 if(data->gc->account->password == NULL) {
81 *resp = sstrdup(data->gc->account->password);
91 static void newpeercb(struct dc_fnetpeer *peer)
93 struct conndata *data;
94 GaimConversation *conv;
96 data = peer->fn->udata;
97 if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
99 gaim_conv_chat_add_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL, GAIM_CBFLAGS_NONE, TRUE);
103 static void delpeercb(struct dc_fnetpeer *peer)
105 struct conndata *data;
106 GaimConversation *conv;
108 data = peer->fn->udata;
109 if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
111 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL);
115 static void chpeercb(struct dc_fnetpeer *peer)
119 static void fillpeerlist(struct dc_fnetnode *fn, int resp, struct conndata *data)
123 static void getfnlistcb(int resp, struct conndata *data)
125 struct dc_fnetnode *fn;
127 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next)
129 dc_getpeerlistasync(fn, (void (*)(struct dc_fnetnode *, int, void *))fillpeerlist, data);
131 fn->newpeercb = newpeercb;
132 fn->delpeercb = delpeercb;
133 fn->chpeercb = chpeercb;
137 static void logincb(int err, wchar_t *reason, struct conndata *data)
139 if(err != DC_LOGIN_ERR_SUCCESS) {
142 gaim_connection_error(data->gc, "Invalid login");
145 gaim_connection_set_state(data->gc, GAIM_CONNECTED);
146 serv_finish_login(data->gc);
147 dc_queuecmd(NULL, NULL, L"notify", L"fn:chat", L"on", L"fn:act", L"on", L"fn:peer", L"on", NULL);
148 dc_getfnlistasync((void (*)(int, void *))getfnlistcb, data);
151 static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition)
153 struct dc_response *resp;
154 struct dc_intresp *ires;
155 struct dc_fnetnode *fn;
156 GaimConversation *conv;
159 if(((condition & GAIM_INPUT_READ) && dc_handleread()) || ((condition & GAIM_INPUT_WRITE) && dc_handlewrite()))
162 gaim_connection_error(data->gc, "Server has disconnected");
165 while((resp = dc_getresp()) != NULL) {
166 if(!wcscmp(resp->cmdname, L".connect")) {
167 if(resp->code == 200) {
168 gaim_connection_update_progress(data->gc, "Authenticating", 2, 3);
169 dc_loginasync(NULL, 1, (int (*)(int, wchar_t *, char **, void *))loginconv, (void (*)(int, wchar_t *, void *))logincb, data);
173 gaim_connection_error(data->gc, "Server refused connection");
176 } else if(!wcscmp(resp->cmdname, L".notify")) {
177 dc_uimisc_handlenotify(resp);
180 if((ires = dc_interpret(resp)) != NULL)
182 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
184 if(ires->argv[1].val.num)
186 /* XXX: Handle different rooms */
187 if((conv = gaim_find_chat(data->gc, fn->id)) != NULL)
189 peer = icwcstombs(ires->argv[3].val.str, "UTF-8");
190 msg = gaim_escape_html(icswcstombs(ires->argv[4].val.str, "UTF-8", NULL));
191 serv_got_chat_in(data->gc, gaim_conv_chat_get_id(GAIM_CONV_CHAT(conv)), peer, 0, msg, time(NULL));
196 peer = icwcstombs(ires->argv[3].val.str, "UTF-8");
197 msg = gaim_escape_html(icswcstombs(ires->argv[4].val.str, "UTF-8", NULL));
198 if(!gaim_account_get_bool(data->gc->account, "represspm", FALSE) || (gaim_find_conversation_with_account(peer, data->gc->account) != NULL))
199 serv_got_im(data->gc, peer, msg, 0, time(NULL));
211 if((ires = dc_interpret(resp)) != NULL)
213 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
216 fn->newpeercb = newpeercb;
217 fn->delpeercb = delpeercb;
218 fn->chpeercb = chpeercb;
230 static int gi_sendchat(GaimConnection *gc, int id, const char *what)
232 struct conndata *data;
233 struct dc_fnetnode *fn;
236 data = gc->proto_data;
237 if((fn = dc_findfnetnode(id)) == NULL)
239 /* XXX: Handle chat rooms */
240 if((wwhat = icmbstowcs((char *)what, "UTF-8")) == NULL)
242 dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", fn->id, L"1", L"", L"%%ls", wwhat, NULL);
248 static int gi_sendim(GaimConnection *gc, const char *who, const char *what, GaimConvImFlags flags)
250 struct conndata *data;
251 struct dc_fnetnode *fn, *tfn;
252 struct dc_fnetpeer *peer, *tpeer;
253 wchar_t *wwho, *wwhat;
256 data = gc->proto_data;
257 if((wwho = icmbstowcs((char *)who, "UTF-8")) == NULL)
260 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next) {
261 for(peer = fn->peers; peer != NULL; peer = peer->next) {
262 if(!wcscmp(wwho, peer->nick)) {
277 if((wwhat = icmbstowcs((char *)what, "UTF-8")) == NULL) {
282 dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", tfn->id, L"0", L"%%ls", wwho, L"%%ls", wwhat, NULL);
289 static const char *gi_listicon(GaimAccount *a, GaimBuddy *b)
294 static struct conndata *newconndata(void)
296 struct conndata *new;
298 new = smalloc(sizeof(*new));
299 memset(new, 0, sizeof(*new));
301 new->readhdl = new->writehdl = -1;
305 static void freeconndata(struct conndata *data)
307 if(data->roomlist != NULL)
308 gaim_roomlist_unref(data->roomlist);
311 if(data->readhdl != -1)
312 gaim_input_remove(data->readhdl);
313 if(data->writehdl != -1)
314 gaim_input_remove(data->writehdl);
320 static void gi_login(GaimAccount *act)
323 struct conndata *data;
325 gc = gaim_account_get_connection(act);
326 gc->proto_data = data = newconndata();
329 gaim_connection_error(gc, "Dolda Connect library already in use");
332 gaim_connection_update_progress(gc, "Connecting", 1, 3);
333 if((data->fd = dc_connect((char *)gaim_account_get_string(act, "server", "localhost"), -1)) < 0)
335 gaim_connection_error(gc, "Could not connect to server");
338 data->readhdl = gaim_input_add(data->fd, GAIM_INPUT_READ, (void (*)(void *, int, GaimInputCondition))dcfdcb, data);
343 static void gi_close(GaimConnection *gc)
345 struct conndata *data;
347 data = gc->proto_data;
351 static GaimRoomlist *gi_getlist(GaimConnection *gc)
353 struct conndata *data;
356 GaimRoomlistField *f;
358 struct dc_fnetnode *fn;
360 data = gc->proto_data;
361 if(data->roomlist != NULL)
362 gaim_roomlist_unref(data->roomlist);
363 data->roomlist = rl = gaim_roomlist_new(gaim_connection_get_account(gc));
365 f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_INT, "", "id", TRUE);
366 fields = g_list_append(fields, f);
367 f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_INT, "Users", "users", FALSE);
368 fields = g_list_append(fields, f);
369 gaim_roomlist_set_fields(rl, fields);
370 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next) {
371 if(fn->state != DC_FNN_STATE_EST)
373 r = gaim_roomlist_room_new(GAIM_ROOMLIST_ROOMTYPE_ROOM, icswcstombs(fn->name, "UTF-8", NULL), NULL);
374 gaim_roomlist_room_add_field(rl, r, GINT_TO_POINTER(fn->id));
375 gaim_roomlist_room_add_field(rl, r, GINT_TO_POINTER(fn->numusers));
376 gaim_roomlist_room_add(rl, r);
378 gaim_roomlist_set_in_progress(rl, FALSE);
382 static void gi_cancelgetlist(GaimRoomlist *rl)
385 struct conndata *data;
387 if((gc = gaim_account_get_connection(rl->account)) == NULL)
389 data = gc->proto_data;
390 gaim_roomlist_set_in_progress(rl, FALSE);
391 if(data->roomlist == rl) {
392 data->roomlist = NULL;
393 gaim_roomlist_unref(rl);
397 static void getpeerlistcb(struct dc_fnetnode *fn, int resp, struct conndata *data)
399 GaimConversation *conv;
400 struct dc_fnetpeer *peer;
404 if(gaim_find_chat(data->gc, fn->id) != NULL)
406 conv = serv_got_joined_chat(data->gc, fn->id, icswcstombs(fn->name, "UTF-8", NULL));
407 for(peer = fn->peers; peer != NULL; peer = peer->next)
408 gaim_conv_chat_add_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL, GAIM_CBFLAGS_NONE, FALSE);
412 static void gi_joinchat(GaimConnection *gc, GHashTable *chatdata)
414 struct conndata *data;
415 struct dc_fnetnode *fn;
417 data = gc->proto_data;
418 if((fn = dc_findfnetnode(GPOINTER_TO_INT(g_hash_table_lookup(chatdata, "id")))) == NULL)
420 if(gaim_find_chat(gc, fn->id) != NULL)
422 dc_getpeerlistasync(fn, (void (*)(struct dc_fnetnode *, int, void *))getpeerlistcb, data);
426 static GaimPluginProtocolInfo protinfo = {
427 .options = OPT_PROTO_PASSWORD_OPTIONAL,
428 .icon_spec = NO_BUDDY_ICONS,
429 .list_icon = gi_listicon,
432 .roomlist_get_list = gi_getlist,
433 .roomlist_cancel = gi_cancelgetlist,
434 .join_chat = gi_joinchat,
435 .chat_send = gi_sendchat,
436 .send_im = gi_sendim,
439 static GaimPluginInfo info = {
440 .magic = GAIM_PLUGIN_MAGIC,
441 .major_version = GAIM_MAJOR_VERSION,
442 .minor_version = GAIM_MINOR_VERSION,
443 .type = GAIM_PLUGIN_PROTOCOL,
444 .priority = GAIM_PRIORITY_DEFAULT,
446 .name = "Dolda Connect",
448 .summary = "Dolda Connect chat plugin",
449 .description = "Allows Gaim to be used as a chat user interface for the Dolda Connect daemon",
450 .author = "Fredrik Tolf <fredrik@dolda2000.com>",
451 .homepage = "http://www.dolda2000.com/~fredrik/doldaconnect/",
452 .extra_info = &protinfo
455 static void init(GaimPlugin *pl)
457 GaimAccountOption *opt;
460 opt = gaim_account_option_string_new("Server", "server", "localhost");
461 protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
462 opt = gaim_account_option_int_new("Port", "port", -1);
463 protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
464 opt = gaim_account_option_bool_new("Do not pop up private messages automatically", "represspm", FALSE);
465 protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
468 GAIM_INIT_PLUGIN(dolcon, init, info);