5 #include <doldaconnect/uilib.h>
6 #include <doldaconnect/uimisc.h>
7 #include <doldaconnect/utils.h>
11 #include <accountopt.h>
18 int readhdl, writehdl;
20 GaimRoomlist *roomlist;
23 static struct conndata *inuse = NULL;
25 static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition);
27 static void updatewrite(struct conndata *data)
30 if(data->writehdl == -1)
31 data->writehdl = gaim_input_add(data->fd, GAIM_INPUT_WRITE, (void (*)(void *, int, GaimInputCondition))dcfdcb, data);
33 if(data->writehdl != -1) {
34 gaim_input_remove(data->writehdl);
40 static void disconnected(struct conndata *data)
44 if(data->readhdl != -1) {
45 gaim_input_remove(data->readhdl);
48 if(data->writehdl != -1) {
49 gaim_input_remove(data->writehdl);
55 static int loginconv(int type, wchar_t *text, char **resp, struct conndata *data)
58 case DC_LOGIN_CONV_NOECHO:
59 if(data->gc->account->password == NULL) {
63 *resp = sstrdup(data->gc->account->password);
73 static void newpeercb(struct dc_fnetpeer *peer)
75 struct conndata *data;
76 GaimConversation *conv;
78 data = peer->fn->udata;
79 if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
81 gaim_conv_chat_add_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL, GAIM_CBFLAGS_NONE, FALSE);
85 static void delpeercb(struct dc_fnetpeer *peer)
87 struct conndata *data;
88 GaimConversation *conv;
90 data = peer->fn->udata;
91 if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
93 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL);
97 static void chpeercb(struct dc_fnetpeer *peer)
101 static void fillpeerlist(struct dc_fnetnode *fn, int resp, struct conndata *data)
105 static void getfnlistcb(int resp, struct conndata *data)
107 struct dc_fnetnode *fn;
109 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next)
111 dc_getpeerlistasync(fn, (void (*)(struct dc_fnetnode *, int, void *))fillpeerlist, data);
113 fn->newpeercb = newpeercb;
114 fn->delpeercb = delpeercb;
115 fn->chpeercb = chpeercb;
119 static void logincb(int err, wchar_t *reason, struct conndata *data)
121 if(err != DC_LOGIN_ERR_SUCCESS) {
124 gaim_connection_error(data->gc, "Invalid login");
127 gaim_connection_set_state(data->gc, GAIM_CONNECTED);
128 serv_finish_login(data->gc);
129 dc_queuecmd(NULL, NULL, L"notify", L"fn:chat", L"on", L"fn:act", L"on", L"fn:peer", L"on", NULL);
130 dc_getfnlistasync((void (*)(int, void *))getfnlistcb, data);
133 static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition)
135 struct dc_response *resp;
136 struct dc_intresp *ires;
137 struct dc_fnetnode *fn;
138 GaimConversation *conv;
141 if(((condition & GAIM_INPUT_READ) && dc_handleread()) || ((condition & GAIM_INPUT_WRITE) && dc_handlewrite()))
144 gaim_connection_error(data->gc, "Server has disconnected");
147 while((resp = dc_getresp()) != NULL) {
148 if(!wcscmp(resp->cmdname, L".connect")) {
149 if(resp->code == 200) {
150 gaim_connection_update_progress(data->gc, "Authenticating", 2, 3);
151 dc_loginasync(NULL, 1, (int (*)(int, wchar_t *, char **, void *))loginconv, (void (*)(int, wchar_t *, void *))logincb, data);
155 gaim_connection_error(data->gc, "Server refused connection");
158 } else if(!wcscmp(resp->cmdname, L".notify")) {
159 dc_uimisc_handlenotify(resp);
162 if((ires = dc_interpret(resp)) != NULL)
164 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
166 if(ires->argv[1].val.num)
168 /* XXX: Handle different rooms */
169 if((conv = gaim_find_chat(data->gc, fn->id)) != NULL)
171 peer = icwcstombs(ires->argv[3].val.str, "UTF-8");
172 msg = gaim_escape_html(icswcstombs(ires->argv[4].val.str, "UTF-8", NULL));
173 serv_got_chat_in(data->gc, gaim_conv_chat_get_id(GAIM_CONV_CHAT(conv)), peer, 0, msg, time(NULL));
178 peer = icwcstombs(ires->argv[3].val.str, "UTF-8");
179 msg = gaim_escape_html(icswcstombs(ires->argv[4].val.str, "UTF-8", NULL));
180 if(!gaim_account_get_bool(data->gc->account, "represspm", FALSE) || (gaim_find_conversation_with_account(peer, data->gc->account) != NULL))
181 serv_got_im(data->gc, peer, msg, 0, time(NULL));
193 if((ires = dc_interpret(resp)) != NULL)
195 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
198 fn->newpeercb = newpeercb;
199 fn->delpeercb = delpeercb;
200 fn->chpeercb = chpeercb;
212 static int gi_sendchat(GaimConnection *gc, int id, const char *what)
214 struct conndata *data;
215 struct dc_fnetnode *fn;
218 data = gc->proto_data;
219 if((fn = dc_findfnetnode(id)) == NULL)
221 /* XXX: Handle chat rooms */
222 if((wwhat = icmbstowcs((char *)what, "UTF-8")) == NULL)
224 dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", fn->id, L"1", L"", L"%%ls", wwhat, NULL);
230 static int gi_sendim(GaimConnection *gc, const char *who, const char *what, GaimConvImFlags flags)
232 struct conndata *data;
233 struct dc_fnetnode *fn, *tfn;
234 struct dc_fnetpeer *peer, *tpeer;
235 wchar_t *wwho, *wwhat;
238 data = gc->proto_data;
239 if((wwho = icmbstowcs((char *)who, "UTF-8")) == NULL)
242 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next) {
243 for(peer = fn->peers; peer != NULL; peer = peer->next) {
244 if(!wcscmp(wwho, peer->nick)) {
259 if((wwhat = icmbstowcs((char *)what, "UTF-8")) == NULL) {
264 dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", tfn->id, L"0", L"%%ls", wwho, L"%%ls", wwhat, NULL);
271 static const char *gi_listicon(GaimAccount *a, GaimBuddy *b)
276 static struct conndata *newconndata(void)
278 struct conndata *new;
280 new = smalloc(sizeof(*new));
281 memset(new, 0, sizeof(*new));
283 new->readhdl = new->writehdl = -1;
287 static void freeconndata(struct conndata *data)
289 if(data->roomlist != NULL)
290 gaim_roomlist_unref(data->roomlist);
293 if(data->readhdl != -1)
294 gaim_input_remove(data->readhdl);
295 if(data->writehdl != -1)
296 gaim_input_remove(data->writehdl);
302 static void gi_login(GaimAccount *act)
305 struct conndata *data;
307 gc = gaim_account_get_connection(act);
308 gc->proto_data = data = newconndata();
311 gaim_connection_error(gc, "Dolda Connect library already in use");
314 gaim_connection_update_progress(gc, "Connecting", 1, 3);
315 if((data->fd = dc_connect((char *)gaim_account_get_string(act, "server", "localhost"), -1)) < 0)
317 gaim_connection_error(gc, "Could not connect to server");
320 data->readhdl = gaim_input_add(data->fd, GAIM_INPUT_READ, (void (*)(void *, int, GaimInputCondition))dcfdcb, data);
325 static void gi_close(GaimConnection *gc)
327 struct conndata *data;
329 data = gc->proto_data;
333 static GaimRoomlist *gi_getlist(GaimConnection *gc)
335 struct conndata *data;
338 GaimRoomlistField *f;
340 struct dc_fnetnode *fn;
342 data = gc->proto_data;
343 if(data->roomlist != NULL)
344 gaim_roomlist_unref(data->roomlist);
345 data->roomlist = rl = gaim_roomlist_new(gaim_connection_get_account(gc));
347 f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_INT, "", "id", TRUE);
348 fields = g_list_append(fields, f);
349 f = gaim_roomlist_field_new(GAIM_ROOMLIST_FIELD_INT, "Users", "users", FALSE);
350 fields = g_list_append(fields, f);
351 gaim_roomlist_set_fields(rl, fields);
352 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next) {
353 if(fn->state != DC_FNN_STATE_EST)
355 r = gaim_roomlist_room_new(GAIM_ROOMLIST_ROOMTYPE_ROOM, icswcstombs(fn->name, "UTF-8", NULL), NULL);
356 gaim_roomlist_room_add_field(rl, r, GINT_TO_POINTER(fn->id));
357 gaim_roomlist_room_add_field(rl, r, GINT_TO_POINTER(fn->numusers));
358 gaim_roomlist_room_add(rl, r);
360 gaim_roomlist_set_in_progress(rl, FALSE);
364 static void gi_cancelgetlist(GaimRoomlist *rl)
367 struct conndata *data;
369 if((gc = gaim_account_get_connection(rl->account)) == NULL)
371 data = gc->proto_data;
372 gaim_roomlist_set_in_progress(rl, FALSE);
373 if(data->roomlist == rl) {
374 data->roomlist = NULL;
375 gaim_roomlist_unref(rl);
379 static void getpeerlistcb(struct dc_fnetnode *fn, int resp, struct conndata *data)
381 GaimConversation *conv;
382 struct dc_fnetpeer *peer;
386 if(gaim_find_chat(data->gc, fn->id) != NULL)
388 conv = serv_got_joined_chat(data->gc, fn->id, icswcstombs(fn->name, "UTF-8", NULL));
389 for(peer = fn->peers; peer != NULL; peer = peer->next)
390 gaim_conv_chat_add_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL, GAIM_CBFLAGS_NONE, FALSE);
394 static void gi_joinchat(GaimConnection *gc, GHashTable *chatdata)
396 struct conndata *data;
397 struct dc_fnetnode *fn;
399 data = gc->proto_data;
400 if((fn = dc_findfnetnode(GPOINTER_TO_INT(g_hash_table_lookup(chatdata, "id")))) == NULL)
402 if(gaim_find_chat(gc, fn->id) != NULL)
404 dc_getpeerlistasync(fn, (void (*)(struct dc_fnetnode *, int, void *))getpeerlistcb, data);
408 static GaimPluginProtocolInfo protinfo = {
409 .options = OPT_PROTO_PASSWORD_OPTIONAL,
410 .icon_spec = NO_BUDDY_ICONS,
411 .list_icon = gi_listicon,
414 .roomlist_get_list = gi_getlist,
415 .roomlist_cancel = gi_cancelgetlist,
416 .join_chat = gi_joinchat,
417 .chat_send = gi_sendchat,
418 .send_im = gi_sendim,
421 static GaimPluginInfo info = {
422 .magic = GAIM_PLUGIN_MAGIC,
423 .major_version = GAIM_MAJOR_VERSION,
424 .minor_version = GAIM_MINOR_VERSION,
425 .type = GAIM_PLUGIN_PROTOCOL,
426 .priority = GAIM_PRIORITY_DEFAULT,
428 .name = "Dolda Connect",
430 .summary = "Dolda Connect chat plugin",
431 .description = "Allows Gaim to be used as a chat user interface for the Dolda Connect daemon",
432 .author = "Fredrik Tolf <fredrik@dolda2000.com>",
433 .homepage = "http://www.dolda2000.com/~fredrik/doldaconnect/",
434 .extra_info = &protinfo
437 static void init(GaimPlugin *pl)
439 GaimAccountOption *opt;
442 opt = gaim_account_option_string_new("Server", "server", "localhost");
443 protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
444 opt = gaim_account_option_int_new("Port", "port", -1);
445 protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
446 opt = gaim_account_option_bool_new("Do not pop up private messages automatically", "represspm", FALSE);
447 protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
450 GAIM_INIT_PLUGIN(dolcon, init, info);