| 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 | */ |
| 19 | #ifdef HAVE_CONFIG_H |
| 20 | #include <config.h> |
| 21 | #endif |
| 22 | #include <string.h> |
| 23 | #include <doldaconnect/uilib.h> |
| 24 | #include <doldaconnect/uimisc.h> |
| 25 | #include <doldaconnect/utils.h> |
| 26 | #include <gaim.h> |
| 27 | #include <plugin.h> |
| 28 | #include <version.h> |
| 29 | #include <accountopt.h> |
| 30 | #include <roomlist.h> |
| 31 | #include <util.h> |
| 32 | #include <errno.h> |
| 33 | |
| 34 | struct conndata { |
| 35 | int fd; |
| 36 | int readhdl, writehdl; |
| 37 | GaimConnection *gc; |
| 38 | GaimRoomlist *roomlist; |
| 39 | }; |
| 40 | |
| 41 | static struct conndata *inuse = NULL; |
| 42 | |
| 43 | static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition); |
| 44 | |
| 45 | static void updatewrite(struct conndata *data) |
| 46 | { |
| 47 | if(dc_wantwrite()) { |
| 48 | if(data->writehdl == -1) |
| 49 | data->writehdl = gaim_input_add(data->fd, GAIM_INPUT_WRITE, (void (*)(void *, int, GaimInputCondition))dcfdcb, data); |
| 50 | } else { |
| 51 | if(data->writehdl != -1) { |
| 52 | gaim_input_remove(data->writehdl); |
| 53 | data->writehdl = -1; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static void disconnected(struct conndata *data) |
| 59 | { |
| 60 | if(inuse == data) |
| 61 | inuse = NULL; |
| 62 | if(data->readhdl != -1) { |
| 63 | gaim_input_remove(data->readhdl); |
| 64 | data->readhdl = -1; |
| 65 | } |
| 66 | if(data->writehdl != -1) { |
| 67 | gaim_input_remove(data->writehdl); |
| 68 | data->writehdl = -1; |
| 69 | } |
| 70 | data->fd = -1; |
| 71 | } |
| 72 | |
| 73 | static int loginconv(int type, wchar_t *text, char **resp, struct conndata *data) |
| 74 | { |
| 75 | switch(type) { |
| 76 | case DC_LOGIN_CONV_NOECHO: |
| 77 | if(data->gc->account->password == NULL) { |
| 78 | updatewrite(data); |
| 79 | return(1); |
| 80 | } else { |
| 81 | *resp = sstrdup(data->gc->account->password); |
| 82 | updatewrite(data); |
| 83 | return(0); |
| 84 | } |
| 85 | default: |
| 86 | updatewrite(data); |
| 87 | return(1); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | static void newpeercb(struct dc_fnetpeer *peer) |
| 92 | { |
| 93 | struct conndata *data; |
| 94 | GaimConversation *conv; |
| 95 | |
| 96 | data = peer->fn->udata; |
| 97 | if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL) |
| 98 | { |
| 99 | gaim_conv_chat_add_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL, GAIM_CBFLAGS_NONE, TRUE); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | static void delpeercb(struct dc_fnetpeer *peer) |
| 104 | { |
| 105 | struct conndata *data; |
| 106 | GaimConversation *conv; |
| 107 | |
| 108 | data = peer->fn->udata; |
| 109 | if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL) |
| 110 | { |
| 111 | gaim_conv_chat_remove_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | static void chpeercb(struct dc_fnetpeer *peer) |
| 116 | { |
| 117 | } |
| 118 | |
| 119 | static void fillpeerlist(struct dc_fnetnode *fn, int resp, struct conndata *data) |
| 120 | { |
| 121 | } |
| 122 | |
| 123 | static void getfnlistcb(int resp, struct conndata *data) |
| 124 | { |
| 125 | struct dc_fnetnode *fn; |
| 126 | |
| 127 | for(fn = dc_fnetnodes; fn != NULL; fn = fn->next) |
| 128 | { |
| 129 | dc_getpeerlistasync(fn, (void (*)(struct dc_fnetnode *, int, void *))fillpeerlist, data); |
| 130 | fn->udata = data; |
| 131 | fn->newpeercb = newpeercb; |
| 132 | fn->delpeercb = delpeercb; |
| 133 | fn->chpeercb = chpeercb; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static void logincb(int err, wchar_t *reason, struct conndata *data) |
| 138 | { |
| 139 | if(err != DC_LOGIN_ERR_SUCCESS) { |
| 140 | dc_disconnect(); |
| 141 | disconnected(data); |
| 142 | gaim_connection_error(data->gc, "Invalid login"); |
| 143 | return; |
| 144 | } |
| 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); |
| 149 | } |
| 150 | |
| 151 | static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition) |
| 152 | { |
| 153 | struct dc_response *resp; |
| 154 | struct dc_intresp *ires; |
| 155 | struct dc_fnetnode *fn; |
| 156 | GaimConversation *conv; |
| 157 | char *peer, *msg; |
| 158 | |
| 159 | if(((condition & GAIM_INPUT_READ) && dc_handleread()) || ((condition & GAIM_INPUT_WRITE) && dc_handlewrite())) |
| 160 | { |
| 161 | disconnected(data); |
| 162 | gaim_connection_error(data->gc, "Server has disconnected"); |
| 163 | return; |
| 164 | } |
| 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); |
| 170 | } else { |
| 171 | dc_disconnect(); |
| 172 | disconnected(data); |
| 173 | gaim_connection_error(data->gc, "Server refused connection"); |
| 174 | return; |
| 175 | } |
| 176 | } else if(!wcscmp(resp->cmdname, L".notify")) { |
| 177 | dc_uimisc_handlenotify(resp); |
| 178 | switch(resp->code) { |
| 179 | case 600: |
| 180 | if((ires = dc_interpret(resp)) != NULL) |
| 181 | { |
| 182 | if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL) |
| 183 | { |
| 184 | if(ires->argv[1].val.num) |
| 185 | { |
| 186 | /* XXX: Handle different rooms */ |
| 187 | if((conv = gaim_find_chat(data->gc, fn->id)) != NULL) |
| 188 | { |
| 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)); |
| 192 | g_free(msg); |
| 193 | free(peer); |
| 194 | } |
| 195 | } else { |
| 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)); |
| 200 | g_free(msg); |
| 201 | free(peer); |
| 202 | } |
| 203 | } |
| 204 | dc_freeires(ires); |
| 205 | } |
| 206 | break; |
| 207 | case 601: |
| 208 | case 602: |
| 209 | case 603: |
| 210 | case 604: |
| 211 | if((ires = dc_interpret(resp)) != NULL) |
| 212 | { |
| 213 | if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL) |
| 214 | { |
| 215 | fn->udata = data; |
| 216 | fn->newpeercb = newpeercb; |
| 217 | fn->delpeercb = delpeercb; |
| 218 | fn->chpeercb = chpeercb; |
| 219 | } |
| 220 | dc_freeires(ires); |
| 221 | } |
| 222 | case 605: |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | updatewrite(data); |
| 228 | } |
| 229 | |
| 230 | static int gi_sendchat(GaimConnection *gc, int id, const char *what) |
| 231 | { |
| 232 | struct conndata *data; |
| 233 | struct dc_fnetnode *fn; |
| 234 | wchar_t *wwhat; |
| 235 | |
| 236 | data = gc->proto_data; |
| 237 | if((fn = dc_findfnetnode(id)) == NULL) |
| 238 | return(-EINVAL); |
| 239 | /* XXX: Handle chat rooms */ |
| 240 | if((wwhat = icmbstowcs((char *)what, "UTF-8")) == NULL) |
| 241 | return(-errno); |
| 242 | dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", fn->id, L"1", L"", L"%%ls", wwhat, NULL); |
| 243 | free(wwhat); |
| 244 | updatewrite(data); |
| 245 | return(0); |
| 246 | } |
| 247 | |
| 248 | static int gi_sendim(GaimConnection *gc, const char *who, const char *what, GaimConvImFlags flags) |
| 249 | { |
| 250 | struct conndata *data; |
| 251 | struct dc_fnetnode *fn, *tfn; |
| 252 | struct dc_fnetpeer *peer, *tpeer; |
| 253 | wchar_t *wwho, *wwhat; |
| 254 | int en; |
| 255 | |
| 256 | data = gc->proto_data; |
| 257 | if((wwho = icmbstowcs((char *)who, "UTF-8")) == NULL) |
| 258 | return(-errno); |
| 259 | tpeer = 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)) { |
| 263 | if(tpeer == NULL) { |
| 264 | tpeer = peer; |
| 265 | tfn = fn; |
| 266 | } else { |
| 267 | free(wwho); |
| 268 | return(-ESRCH); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | if(tpeer == NULL) { |
| 274 | free(wwho); |
| 275 | return(-ESRCH); |
| 276 | } |
| 277 | if((wwhat = icmbstowcs((char *)what, "UTF-8")) == NULL) { |
| 278 | en = errno; |
| 279 | free(wwho); |
| 280 | return(-en); |
| 281 | } |
| 282 | dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", tfn->id, L"0", L"%%ls", wwho, L"%%ls", wwhat, NULL); |
| 283 | free(wwho); |
| 284 | free(wwhat); |
| 285 | updatewrite(data); |
| 286 | return(1); |
| 287 | } |
| 288 | |
| 289 | static const char *gi_listicon(GaimAccount *a, GaimBuddy *b) |
| 290 | { |
| 291 | return("dolcon"); |
| 292 | } |
| 293 | |
| 294 | static struct conndata *newconndata(void) |
| 295 | { |
| 296 | struct conndata *new; |
| 297 | |
| 298 | new = smalloc(sizeof(*new)); |
| 299 | memset(new, 0, sizeof(*new)); |
| 300 | new->fd = -1; |
| 301 | new->readhdl = new->writehdl = -1; |
| 302 | return(new); |
| 303 | } |
| 304 | |
| 305 | static void freeconndata(struct conndata *data) |
| 306 | { |
| 307 | if(data->roomlist != NULL) |
| 308 | gaim_roomlist_unref(data->roomlist); |
| 309 | if(inuse == data) |
| 310 | inuse = NULL; |
| 311 | if(data->readhdl != -1) |
| 312 | gaim_input_remove(data->readhdl); |
| 313 | if(data->writehdl != -1) |
| 314 | gaim_input_remove(data->writehdl); |
| 315 | if(data->fd >= 0) |
| 316 | dc_disconnect(); |
| 317 | free(data); |
| 318 | } |
| 319 | |
| 320 | static void gi_login(GaimAccount *act) |
| 321 | { |
| 322 | GaimConnection *gc; |
| 323 | struct conndata *data; |
| 324 | |
| 325 | gc = gaim_account_get_connection(act); |
| 326 | gc->proto_data = data = newconndata(); |
| 327 | data->gc = gc; |
| 328 | if(inuse != NULL) { |
| 329 | gaim_connection_error(gc, "Dolda Connect library already in use"); |
| 330 | return; |
| 331 | } |
| 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) |
| 334 | { |
| 335 | gaim_connection_error(gc, "Could not connect to server"); |
| 336 | return; |
| 337 | } |
| 338 | data->readhdl = gaim_input_add(data->fd, GAIM_INPUT_READ, (void (*)(void *, int, GaimInputCondition))dcfdcb, data); |
| 339 | updatewrite(data); |
| 340 | inuse = data; |
| 341 | } |
| 342 | |
| 343 | static void gi_close(GaimConnection *gc) |
| 344 | { |
| 345 | struct conndata *data; |
| 346 | |
| 347 | data = gc->proto_data; |
| 348 | freeconndata(data); |
| 349 | } |
| 350 | |
| 351 | static GaimRoomlist *gi_getlist(GaimConnection *gc) |
| 352 | { |
| 353 | struct conndata *data; |
| 354 | GList *fields; |
| 355 | GaimRoomlist *rl; |
| 356 | GaimRoomlistField *f; |
| 357 | GaimRoomlistRoom *r; |
| 358 | struct dc_fnetnode *fn; |
| 359 | |
| 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)); |
| 364 | fields = NULL; |
| 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) |
| 372 | continue; |
| 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); |
| 377 | } |
| 378 | gaim_roomlist_set_in_progress(rl, FALSE); |
| 379 | return(rl); |
| 380 | } |
| 381 | |
| 382 | static void gi_cancelgetlist(GaimRoomlist *rl) |
| 383 | { |
| 384 | GaimConnection *gc; |
| 385 | struct conndata *data; |
| 386 | |
| 387 | if((gc = gaim_account_get_connection(rl->account)) == NULL) |
| 388 | return; |
| 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); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | static void getpeerlistcb(struct dc_fnetnode *fn, int resp, struct conndata *data) |
| 398 | { |
| 399 | GaimConversation *conv; |
| 400 | struct dc_fnetpeer *peer; |
| 401 | |
| 402 | if(resp == 200) |
| 403 | { |
| 404 | if(gaim_find_chat(data->gc, fn->id) != NULL) |
| 405 | return; |
| 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); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | static void gi_joinchat(GaimConnection *gc, GHashTable *chatdata) |
| 413 | { |
| 414 | struct conndata *data; |
| 415 | struct dc_fnetnode *fn; |
| 416 | |
| 417 | data = gc->proto_data; |
| 418 | if((fn = dc_findfnetnode(GPOINTER_TO_INT(g_hash_table_lookup(chatdata, "id")))) == NULL) |
| 419 | return; |
| 420 | if(gaim_find_chat(gc, fn->id) != NULL) |
| 421 | return; |
| 422 | dc_getpeerlistasync(fn, (void (*)(struct dc_fnetnode *, int, void *))getpeerlistcb, data); |
| 423 | updatewrite(data); |
| 424 | } |
| 425 | |
| 426 | static GaimPluginProtocolInfo protinfo = { |
| 427 | .options = OPT_PROTO_PASSWORD_OPTIONAL, |
| 428 | .icon_spec = NO_BUDDY_ICONS, |
| 429 | .list_icon = gi_listicon, |
| 430 | .login = gi_login, |
| 431 | .close = gi_close, |
| 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, |
| 437 | }; |
| 438 | |
| 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, |
| 445 | .id = "prpl-dolcon", |
| 446 | .name = "Dolda Connect", |
| 447 | .version = VERSION, |
| 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 |
| 453 | }; |
| 454 | |
| 455 | static void init(GaimPlugin *pl) |
| 456 | { |
| 457 | GaimAccountOption *opt; |
| 458 | |
| 459 | dc_init(); |
| 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); |
| 466 | } |
| 467 | |
| 468 | GAIM_INIT_PLUGIN(dolcon, init, info); |