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;
44 static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition);
46 static void updatewrite(struct conndata *data)
49 if(data->writehdl == -1)
50 data->writehdl = gaim_input_add(data->fd, GAIM_INPUT_WRITE, (void (*)(void *, int, GaimInputCondition))dcfdcb, data);
52 if(data->writehdl != -1) {
53 gaim_input_remove(data->writehdl);
59 static void disconnected(struct conndata *data)
63 if(data->readhdl != -1) {
64 gaim_input_remove(data->readhdl);
67 if(data->writehdl != -1) {
68 gaim_input_remove(data->writehdl);
74 static int loginconv(int type, wchar_t *text, char **resp, struct conndata *data)
77 case DC_LOGIN_CONV_NOECHO:
78 if(data->gc->account->password == NULL) {
82 *resp = sstrdup(data->gc->account->password);
92 static void newpeercb(struct dc_fnetpeer *peer)
94 struct conndata *data;
95 GaimConversation *conv;
97 data = peer->fn->udata;
98 if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
100 gaim_conv_chat_add_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL, GAIM_CBFLAGS_NONE, TRUE);
104 static void delpeercb(struct dc_fnetpeer *peer)
106 struct conndata *data;
107 GaimConversation *conv;
109 data = peer->fn->udata;
110 if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
112 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL);
116 static void chpeercb(struct dc_fnetpeer *peer)
120 static void fillpeerlist(struct dc_fnetnode *fn, int resp, struct conndata *data)
124 static void getfnlistcb(int resp, struct conndata *data)
126 struct dc_fnetnode *fn;
128 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next)
130 dc_getpeerlistasync(fn, (void (*)(struct dc_fnetnode *, int, void *))fillpeerlist, data);
132 fn->newpeercb = newpeercb;
133 fn->delpeercb = delpeercb;
134 fn->chpeercb = chpeercb;
138 static void logincb(int err, wchar_t *reason, struct conndata *data)
140 if(err != DC_LOGIN_ERR_SUCCESS) {
143 gaim_connection_error(data->gc, "Invalid login");
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);
152 static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition)
154 struct dc_response *resp;
155 struct dc_intresp *ires;
156 struct dc_fnetnode *fn;
157 GaimConversation *conv;
160 if(((condition & GAIM_INPUT_READ) && dc_handleread()) || ((condition & GAIM_INPUT_WRITE) && dc_handlewrite()))
163 gaim_connection_error(data->gc, "Server has disconnected");
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);
174 gaim_connection_error(data->gc, "Server refused connection");
177 } else if(!wcscmp(resp->cmdname, L".notify")) {
178 dc_uimisc_handlenotify(resp);
181 if((ires = dc_interpret(resp)) != NULL)
183 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
185 if(ires->argv[1].val.num)
187 /* XXX: Handle different rooms */
188 if((conv = gaim_find_chat(data->gc, fn->id)) != NULL)
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));
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));
212 if((ires = dc_interpret(resp)) != NULL)
214 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
217 fn->newpeercb = newpeercb;
218 fn->delpeercb = delpeercb;
219 fn->chpeercb = chpeercb;
232 static int gi_sendchat(GaimConnection *gc, int id, const char *what)
234 struct conndata *data;
235 struct dc_fnetnode *fn;
238 data = gc->proto_data;
239 if((fn = dc_findfnetnode(id)) == NULL)
241 /* XXX: Handle chat rooms */
242 if((wwhat = icmbstowcs((char *)what, "UTF-8")) == NULL)
244 dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", fn->id, L"1", L"", L"%%ls", wwhat, NULL);
250 static int gi_sendim(GaimConnection *gc, const char *who, const char *what, GaimConvImFlags flags)
252 struct conndata *data;
253 struct dc_fnetnode *fn, *tfn;
254 struct dc_fnetpeer *peer, *tpeer;
255 wchar_t *wwho, *wwhat;
258 data = gc->proto_data;
259 if((wwho = icmbstowcs((char *)who, "UTF-8")) == NULL)
262 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next) {
263 for(peer = fn->peers; peer != NULL; peer = peer->next) {
264 if(!wcscmp(wwho, peer->nick)) {
279 if((wwhat = icmbstowcs((char *)what, "UTF-8")) == NULL) {
284 dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", tfn->id, L"0", L"%%ls", wwho, L"%%ls", wwhat, NULL);
291 static const char *gi_listicon(GaimAccount *a, GaimBuddy *b)
296 static struct conndata *newconndata(void)
298 struct conndata *new;
300 new = smalloc(sizeof(*new));
301 memset(new, 0, sizeof(*new));
303 new->readhdl = new->writehdl = -1;
307 static void freeconndata(struct conndata *data)
309 if(data->roomlist != NULL)
310 gaim_roomlist_unref(data->roomlist);
313 if(data->readhdl != -1)
314 gaim_input_remove(data->readhdl);
315 if(data->writehdl != -1)
316 gaim_input_remove(data->writehdl);
322 static void gi_login(GaimAccount *act)
325 struct conndata *data;
327 gc = gaim_account_get_connection(act);
328 gc->proto_data = data = newconndata();
331 gaim_connection_error(gc, "Dolda Connect library already in use");
334 gaim_connection_update_progress(gc, "Connecting", 1, 3);
335 if((data->fd = dc_connect((char *)gaim_account_get_string(act, "server", "localhost"), -1)) < 0)
337 gaim_connection_error(gc, "Could not connect to server");
340 data->readhdl = gaim_input_add(data->fd, GAIM_INPUT_READ, (void (*)(void *, int, GaimInputCondition))dcfdcb, data);
345 static void gi_close(GaimConnection *gc)
347 struct conndata *data;
349 data = gc->proto_data;
353 static GaimRoomlist *gi_getlist(GaimConnection *gc)
355 struct conndata *data;
358 GaimRoomlistField *f;
360 struct dc_fnetnode *fn;
362 data = gc->proto_data;
363 if(data->roomlist != NULL)
364 gaim_roomlist_unref(data->roomlist);
365 data->roomlist = rl = gaim_roomlist_new(gaim_connection_get_account(gc));
367 f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_INT, "", "id", TRUE);
368 fields = g_list_append(fields, f);
369 f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_INT, "Users", "users", FALSE);
370 fields = g_list_append(fields, f);
371 gaim_roomlist_set_fields(rl, fields);
372 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next) {
373 if(fn->state != DC_FNN_STATE_EST)
375 r = gaim_roomlist_room_new(GAIM_ROOMLIST_ROOMTYPE_ROOM, icswcstombs(fn->name, "UTF-8", NULL), NULL);
376 gaim_roomlist_room_add_field(rl, r, GINT_TO_POINTER(fn->id));
377 gaim_roomlist_room_add_field(rl, r, GINT_TO_POINTER(fn->numusers));
378 gaim_roomlist_room_add(rl, r);
380 gaim_roomlist_set_in_progress(rl, FALSE);
384 static void gi_cancelgetlist(GaimRoomlist *rl)
387 struct conndata *data;
389 if((gc = gaim_account_get_connection(rl->account)) == NULL)
391 data = gc->proto_data;
392 gaim_roomlist_set_in_progress(rl, FALSE);
393 if(data->roomlist == rl) {
394 data->roomlist = NULL;
395 gaim_roomlist_unref(rl);
399 static void getpeerlistcb(struct dc_fnetnode *fn, int resp, struct conndata *data)
401 GaimConversation *conv;
402 struct dc_fnetpeer *peer;
406 if(gaim_find_chat(data->gc, fn->id) != NULL)
408 conv = serv_got_joined_chat(data->gc, fn->id, icswcstombs(fn->name, "UTF-8", NULL));
409 for(peer = fn->peers; peer != NULL; peer = peer->next)
410 gaim_conv_chat_add_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL, GAIM_CBFLAGS_NONE, FALSE);
414 static void gi_joinchat(GaimConnection *gc, GHashTable *chatdata)
416 struct conndata *data;
417 struct dc_fnetnode *fn;
419 data = gc->proto_data;
420 if((fn = dc_findfnetnode(GPOINTER_TO_INT(g_hash_table_lookup(chatdata, "id")))) == NULL)
422 if(gaim_find_chat(gc, fn->id) != NULL)
424 dc_getpeerlistasync(fn, (void (*)(struct dc_fnetnode *, int, void *))getpeerlistcb, data);
428 static GaimPluginProtocolInfo protinfo = {
429 .options = OPT_PROTO_PASSWORD_OPTIONAL,
430 .icon_spec = NO_BUDDY_ICONS,
431 .list_icon = gi_listicon,
434 .roomlist_get_list = gi_getlist,
435 .roomlist_cancel = gi_cancelgetlist,
436 .join_chat = gi_joinchat,
437 .chat_send = gi_sendchat,
438 .send_im = gi_sendim,
441 static GaimPluginInfo info = {
442 .magic = GAIM_PLUGIN_MAGIC,
443 .major_version = GAIM_MAJOR_VERSION,
444 .minor_version = GAIM_MINOR_VERSION,
445 .type = GAIM_PLUGIN_PROTOCOL,
446 .priority = GAIM_PRIORITY_DEFAULT,
448 .name = "Dolda Connect",
450 .summary = "Dolda Connect chat plugin",
451 .description = "Allows Gaim to be used as a chat user interface for the Dolda Connect daemon",
452 .author = "Fredrik Tolf <fredrik@dolda2000.com>",
453 .homepage = "http://www.dolda2000.com/~fredrik/doldaconnect/",
454 .extra_info = &protinfo
457 static void init(GaimPlugin *pl)
459 GaimAccountOption *opt;
462 opt = gaim_account_option_string_new("Server", "server", "localhost");
463 protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
464 opt = gaim_account_option_int_new("Port", "port", -1);
465 protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
466 opt = gaim_account_option_bool_new("Do not pop up private messages automatically", "represspm", FALSE);
467 protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
470 GAIM_INIT_PLUGIN(dolcon, init, info);