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