[7daec06] | 1 | /* |
---|
| 2 | * skype.c - Skype plugin for BitlBee |
---|
| 3 | * |
---|
| 4 | * Copyright (c) 2007 by Miklos Vajna <vmiklos@frugalware.org> |
---|
[f06e3ac] | 5 | * |
---|
[7daec06] | 6 | * Several ideas are used from the BitlBee Jabber plugin, which is |
---|
| 7 | * |
---|
| 8 | * Copyright (c) 2006 by Wilmer van der Gaast <wilmer@gaast.net> |
---|
| 9 | * |
---|
| 10 | * This program is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 13 | * (at your option) any later version. |
---|
| 14 | * |
---|
| 15 | * This program is distributed in the hope that it will be useful, |
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | * GNU General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU General Public License |
---|
| 21 | * along with this program; if not, write to the Free Software |
---|
| 22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
---|
| 23 | * USA. |
---|
[f06e3ac] | 24 | */ |
---|
[7daec06] | 25 | |
---|
[f06e3ac] | 26 | #include <stdio.h> |
---|
[ed2e37f] | 27 | #include <poll.h> |
---|
[f06e3ac] | 28 | #include <bitlbee.h> |
---|
[72aa7f0] | 29 | #include <glib.h> |
---|
[f06e3ac] | 30 | |
---|
| 31 | #define SKYPE_PORT_DEFAULT "2727" |
---|
| 32 | |
---|
[bbba374] | 33 | /* |
---|
| 34 | * Enumerations |
---|
| 35 | */ |
---|
| 36 | |
---|
| 37 | typedef enum |
---|
| 38 | { |
---|
[df9255d] | 39 | SKYPE_CALL_RINGING = 1, |
---|
[bbba374] | 40 | SKYPE_CALL_MISSED |
---|
| 41 | } skype_call_status; |
---|
| 42 | |
---|
[df9255d] | 43 | typedef enum |
---|
| 44 | { |
---|
| 45 | SKYPE_FILETRANSFER_NEW = 1, |
---|
| 46 | SKYPE_FILETRANSFER_FAILED |
---|
| 47 | } skype_filetransfer_status; |
---|
| 48 | |
---|
[7daec06] | 49 | /* |
---|
| 50 | * Structures |
---|
| 51 | */ |
---|
| 52 | |
---|
[f06e3ac] | 53 | struct skype_data |
---|
| 54 | { |
---|
| 55 | struct im_connection *ic; |
---|
[a3d6427] | 56 | char *username; |
---|
[368861e] | 57 | /* The effective file descriptor. We store it here so any function can |
---|
| 58 | * write() to it. */ |
---|
[f06e3ac] | 59 | int fd; |
---|
[368861e] | 60 | /* File descriptor returned by bitlbee. we store it so we know when |
---|
| 61 | * we're connected and when we aren't. */ |
---|
| 62 | int bfd; |
---|
[2a0f99c] | 63 | /* When we receive a new message id, we query the properties, finally |
---|
| 64 | * the chatname. Store the properties here so that we can use |
---|
[c81d0ef] | 65 | * imcb_buddy_msg() when we got the chatname. */ |
---|
[77c1abe] | 66 | char *handle; |
---|
[a7af5f0] | 67 | /* List, because of multiline messages. */ |
---|
| 68 | GList *body; |
---|
[2a0f99c] | 69 | char *type; |
---|
[bbba374] | 70 | /* This is necessary because we send a notification when we get the |
---|
| 71 | * handle. So we store the state here and then we can send a |
---|
| 72 | * notification about the handle is in a given status. */ |
---|
| 73 | skype_call_status call_status; |
---|
[df9255d] | 74 | /* Same for file transfers. */ |
---|
| 75 | skype_filetransfer_status filetransfer_status; |
---|
[86278cd] | 76 | /* Using /j #nick we want to have a groupchat with two people. Usually |
---|
| 77 | * not (default). */ |
---|
| 78 | char* groupchat_with; |
---|
[f8674db] | 79 | /* The user who invited us to the chat. */ |
---|
| 80 | char* adder; |
---|
[a5f76a2] | 81 | /* If we are waiting for a confirmation about we changed the topic. */ |
---|
| 82 | int topic_wait; |
---|
[f06e3ac] | 83 | }; |
---|
| 84 | |
---|
[adce2de] | 85 | struct skype_away_state |
---|
| 86 | { |
---|
| 87 | char *code; |
---|
| 88 | char *full_name; |
---|
| 89 | }; |
---|
| 90 | |
---|
[7daec06] | 91 | struct skype_buddy_ask_data |
---|
| 92 | { |
---|
| 93 | struct im_connection *ic; |
---|
| 94 | char *handle; |
---|
| 95 | }; |
---|
| 96 | |
---|
| 97 | /* |
---|
| 98 | * Tables |
---|
| 99 | */ |
---|
| 100 | |
---|
[adce2de] | 101 | const struct skype_away_state skype_away_state_list[] = |
---|
| 102 | { |
---|
| 103 | { "ONLINE", "Online" }, |
---|
| 104 | { "SKYPEME", "Skype Me" }, |
---|
| 105 | { "AWAY", "Away" }, |
---|
| 106 | { "NA", "Not available" }, |
---|
| 107 | { "DND", "Do Not Disturb" }, |
---|
| 108 | { "INVISIBLE", "Invisible" }, |
---|
[23411c6] | 109 | { "OFFLINE", "Offline" }, |
---|
| 110 | { NULL, NULL} |
---|
[adce2de] | 111 | }; |
---|
| 112 | |
---|
[7daec06] | 113 | /* |
---|
| 114 | * Functions |
---|
| 115 | */ |
---|
[d3cbd17] | 116 | |
---|
[f06e3ac] | 117 | static void skype_init( account_t *acc ) |
---|
| 118 | { |
---|
| 119 | set_t *s; |
---|
| 120 | |
---|
| 121 | s = set_add( &acc->set, "port", SKYPE_PORT_DEFAULT, set_eval_int, acc ); |
---|
| 122 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 123 | |
---|
| 124 | s = set_add( &acc->set, "server", NULL, set_eval_account, acc ); |
---|
| 125 | s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | int skype_write( struct im_connection *ic, char *buf, int len ) |
---|
| 129 | { |
---|
| 130 | struct skype_data *sd = ic->proto_data; |
---|
[1fb89e3] | 131 | struct pollfd pfd[1]; |
---|
| 132 | |
---|
| 133 | pfd[0].fd = sd->fd; |
---|
| 134 | pfd[0].events = POLLOUT; |
---|
[f06e3ac] | 135 | |
---|
[7daec06] | 136 | /* This poll is necessary or we'll get a SIGPIPE when we write() to |
---|
| 137 | * sd->fd. */ |
---|
[1fb89e3] | 138 | poll(pfd, 1, 1000); |
---|
| 139 | if(pfd[0].revents & POLLHUP) |
---|
| 140 | { |
---|
| 141 | imcb_error( ic, "Could not connect to server" ); |
---|
| 142 | imc_logout( ic, TRUE ); |
---|
| 143 | return FALSE; |
---|
| 144 | } |
---|
[9fd4241] | 145 | write( sd->fd, buf, len ); |
---|
[f06e3ac] | 146 | |
---|
[9fd4241] | 147 | return TRUE; |
---|
[f06e3ac] | 148 | } |
---|
| 149 | |
---|
[d3cbd17] | 150 | static void skype_buddy_ask_yes( gpointer w, struct skype_buddy_ask_data *bla ) |
---|
| 151 | { |
---|
| 152 | char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED TRUE", bla->handle); |
---|
| 153 | skype_write( bla->ic, buf, strlen( buf ) ); |
---|
| 154 | g_free(buf); |
---|
| 155 | g_free(bla->handle); |
---|
| 156 | g_free(bla); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | static void skype_buddy_ask_no( gpointer w, struct skype_buddy_ask_data *bla ) |
---|
| 160 | { |
---|
| 161 | char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED FALSE", bla->handle); |
---|
| 162 | skype_write( bla->ic, buf, strlen( buf ) ); |
---|
| 163 | g_free(buf); |
---|
| 164 | g_free(bla->handle); |
---|
| 165 | g_free(bla); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | void skype_buddy_ask( struct im_connection *ic, char *handle, char *message) |
---|
| 169 | { |
---|
| 170 | struct skype_buddy_ask_data *bla = g_new0( struct skype_buddy_ask_data, 1 ); |
---|
| 171 | char *buf; |
---|
| 172 | |
---|
| 173 | bla->ic = ic; |
---|
| 174 | bla->handle = g_strdup(handle); |
---|
| 175 | |
---|
| 176 | buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list, saying: '%s'.", handle, message); |
---|
| 177 | imcb_ask( ic, buf, bla, skype_buddy_ask_yes, skype_buddy_ask_no ); |
---|
| 178 | g_free( buf ); |
---|
| 179 | } |
---|
| 180 | |
---|
[72aa7f0] | 181 | struct groupchat *skype_chat_by_name( struct im_connection *ic, char *name ) |
---|
| 182 | { |
---|
| 183 | struct groupchat *ret; |
---|
| 184 | |
---|
[de2f05e] | 185 | for( ret = ic->groupchats; ret; ret = ret->next ) |
---|
[72aa7f0] | 186 | { |
---|
| 187 | if(strcmp(name, ret->title ) == 0 ) |
---|
| 188 | break; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | return ret; |
---|
| 192 | } |
---|
| 193 | |
---|
[1323e36] | 194 | static gboolean skype_read_callback( gpointer data, gint fd, b_input_condition cond ) |
---|
| 195 | { |
---|
| 196 | struct im_connection *ic = data; |
---|
| 197 | struct skype_data *sd = ic->proto_data; |
---|
| 198 | char buf[1024]; |
---|
| 199 | int st; |
---|
[9fd4241] | 200 | char **lines, **lineptr, *line, *ptr; |
---|
[1323e36] | 201 | |
---|
[98bca36] | 202 | if( !sd || sd->fd == -1 ) |
---|
[1323e36] | 203 | return FALSE; |
---|
[7daec06] | 204 | /* Read the whole data. */ |
---|
[1323e36] | 205 | st = read( sd->fd, buf, sizeof( buf ) ); |
---|
| 206 | if( st > 0 ) |
---|
| 207 | { |
---|
| 208 | buf[st] = '\0'; |
---|
[7daec06] | 209 | /* Then split it up to lines. */ |
---|
[9fd4241] | 210 | lines = g_strsplit(buf, "\n", 0); |
---|
| 211 | lineptr = lines; |
---|
| 212 | while((line = *lineptr)) |
---|
| 213 | { |
---|
| 214 | if(!strlen(line)) |
---|
| 215 | break; |
---|
| 216 | if(!strncmp(line, "USERS ", 6)) |
---|
| 217 | { |
---|
| 218 | char **i; |
---|
| 219 | char **nicks; |
---|
| 220 | |
---|
| 221 | nicks = g_strsplit(line + 6, ", ", 0); |
---|
| 222 | i = nicks; |
---|
| 223 | while(*i) |
---|
| 224 | { |
---|
| 225 | g_snprintf(buf, 1024, "GET USER %s ONLINESTATUS\n", *i); |
---|
| 226 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 227 | i++; |
---|
| 228 | } |
---|
| 229 | g_strfreev(nicks); |
---|
| 230 | } |
---|
| 231 | else if(!strncmp(line, "USER ", 5)) |
---|
| 232 | { |
---|
[adce2de] | 233 | int flags = 0; |
---|
[be975f8] | 234 | char *status = strrchr(line, ' '); |
---|
| 235 | char *user = strchr(line, ' '); |
---|
[adce2de] | 236 | status++; |
---|
[be975f8] | 237 | ptr = strchr(++user, ' '); |
---|
| 238 | *ptr = '\0'; |
---|
[6627d92] | 239 | ptr++; |
---|
[7daec06] | 240 | if(!strncmp(ptr, "ONLINESTATUS ", 13) && |
---|
| 241 | strcmp(user, sd->username) != 0 |
---|
| 242 | && strcmp(user, "echo123") != 0) |
---|
[a3d6427] | 243 | { |
---|
| 244 | ptr = g_strdup_printf("%s@skype.com", user); |
---|
| 245 | imcb_add_buddy(ic, ptr, NULL); |
---|
| 246 | if(strcmp(status, "OFFLINE") != 0) |
---|
| 247 | flags |= OPT_LOGGED_IN; |
---|
| 248 | if(strcmp(status, "ONLINE") != 0 && strcmp(status, "SKYPEME") != 0) |
---|
| 249 | flags |= OPT_AWAY; |
---|
| 250 | imcb_buddy_status(ic, ptr, flags, NULL, NULL); |
---|
| 251 | g_free(ptr); |
---|
| 252 | } |
---|
[d3cbd17] | 253 | else if(!strncmp(ptr, "RECEIVEDAUTHREQUEST ", 20)) |
---|
| 254 | { |
---|
| 255 | char *message = ptr + 20; |
---|
| 256 | if(strlen(message)) |
---|
| 257 | skype_buddy_ask(ic, user, message); |
---|
| 258 | } |
---|
| 259 | else if(!strncmp(ptr, "BUDDYSTATUS ", 12)) |
---|
| 260 | { |
---|
| 261 | char *st = ptr + 12; |
---|
| 262 | if(!strcmp(st, "3")) |
---|
| 263 | { |
---|
| 264 | char *buf = g_strdup_printf("%s@skype.com", user); |
---|
| 265 | imcb_add_buddy(ic, buf, NULL); |
---|
| 266 | g_free(buf); |
---|
| 267 | } |
---|
| 268 | } |
---|
[9fd4241] | 269 | } |
---|
[77c1abe] | 270 | else if(!strncmp(line, "CHATMESSAGE ", 12)) |
---|
| 271 | { |
---|
| 272 | char *id = strchr(line, ' '); |
---|
| 273 | if(++id) |
---|
| 274 | { |
---|
| 275 | char *info = strchr(id, ' '); |
---|
| 276 | *info = '\0'; |
---|
| 277 | info++; |
---|
| 278 | if(!strcmp(info, "STATUS RECEIVED")) |
---|
| 279 | { |
---|
[7daec06] | 280 | /* New message ID: |
---|
| 281 | * (1) Request its from field |
---|
| 282 | * (2) Request its body |
---|
[2a0f99c] | 283 | * (3) Request its type |
---|
| 284 | * (4) Query chatname |
---|
[7daec06] | 285 | */ |
---|
[77c1abe] | 286 | g_snprintf(buf, 1024, "GET CHATMESSAGE %s FROM_HANDLE\n", id); |
---|
| 287 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 288 | g_snprintf(buf, 1024, "GET CHATMESSAGE %s BODY\n", id); |
---|
| 289 | skype_write( ic, buf, strlen( buf ) ); |
---|
[2a0f99c] | 290 | g_snprintf(buf, 1024, "GET CHATMESSAGE %s TYPE\n", id); |
---|
| 291 | skype_write( ic, buf, strlen( buf ) ); |
---|
[c81d0ef] | 292 | g_snprintf(buf, 1024, "GET CHATMESSAGE %s CHATNAME\n", id); |
---|
| 293 | skype_write( ic, buf, strlen( buf ) ); |
---|
[77c1abe] | 294 | } |
---|
| 295 | else if(!strncmp(info, "FROM_HANDLE ", 12)) |
---|
| 296 | { |
---|
| 297 | info += 12; |
---|
[7daec06] | 298 | /* New from field value. Store |
---|
| 299 | * it, then we can later use it |
---|
| 300 | * when we got the message's |
---|
| 301 | * body. */ |
---|
[7613670] | 302 | g_free(sd->handle); |
---|
[77c1abe] | 303 | sd->handle = g_strdup_printf("%s@skype.com", info); |
---|
[5d1b0774] | 304 | } |
---|
| 305 | else if(!strncmp(info, "EDITED_BY ", 10)) |
---|
| 306 | { |
---|
| 307 | info += 10; |
---|
| 308 | /* This is the same as |
---|
| 309 | * FROM_HANDLE, except that we |
---|
| 310 | * never request these lines |
---|
| 311 | * from Skype, we just get |
---|
| 312 | * them. */ |
---|
| 313 | g_free(sd->handle); |
---|
| 314 | sd->handle = g_strdup_printf("%s@skype.com", info); |
---|
[77c1abe] | 315 | } |
---|
| 316 | else if(!strncmp(info, "BODY ", 5)) |
---|
| 317 | { |
---|
| 318 | info += 5; |
---|
[a7af5f0] | 319 | sd->body = g_list_append(sd->body, g_strdup(info)); |
---|
[c81d0ef] | 320 | } |
---|
[2a0f99c] | 321 | else if(!strncmp(info, "TYPE ", 5)) |
---|
| 322 | { |
---|
| 323 | info += 5; |
---|
| 324 | g_free(sd->type); |
---|
| 325 | sd->type = g_strdup(info); |
---|
| 326 | } |
---|
[c81d0ef] | 327 | else if(!strncmp(info, "CHATNAME ", 9)) |
---|
| 328 | { |
---|
| 329 | info += 9; |
---|
[2a0f99c] | 330 | if(sd->handle && sd->body && sd->type) |
---|
[7daec06] | 331 | { |
---|
[31870ae] | 332 | struct groupchat *gc = skype_chat_by_name(ic, info); |
---|
[a7af5f0] | 333 | int i; |
---|
| 334 | for(i=0;i<g_list_length(sd->body);i++) |
---|
[2a0f99c] | 335 | { |
---|
[a7af5f0] | 336 | char *body = g_list_nth_data(sd->body, i); |
---|
| 337 | if(!strcmp(sd->type, "SAID")) |
---|
| 338 | { |
---|
| 339 | if(!gc) |
---|
| 340 | /* Private message */ |
---|
| 341 | imcb_buddy_msg(ic, sd->handle, body, 0, 0); |
---|
| 342 | else |
---|
| 343 | /* Groupchat message */ |
---|
| 344 | imcb_chat_msg(gc, sd->handle, body, 0, 0); |
---|
| 345 | } |
---|
| 346 | else if(!strcmp(sd->type, "SETTOPIC")) |
---|
| 347 | { |
---|
| 348 | if(gc) |
---|
[de2f05e] | 349 | imcb_chat_topic(gc, sd->handle, body, 0); |
---|
[a7af5f0] | 350 | } |
---|
| 351 | else if(!strcmp(sd->type, "LEFT")) |
---|
| 352 | { |
---|
| 353 | if(gc) |
---|
| 354 | imcb_chat_remove_buddy(gc, sd->handle, NULL); |
---|
| 355 | } |
---|
[2a0f99c] | 356 | } |
---|
[a7af5f0] | 357 | g_list_free(sd->body); |
---|
| 358 | sd->body = NULL; |
---|
[7daec06] | 359 | } |
---|
[77c1abe] | 360 | } |
---|
| 361 | } |
---|
| 362 | } |
---|
[ecfbc5d] | 363 | else if(!strncmp(line, "CALL ", 5)) |
---|
| 364 | { |
---|
| 365 | char *id = strchr(line, ' '); |
---|
| 366 | if(++id) |
---|
| 367 | { |
---|
| 368 | char *info = strchr(id, ' '); |
---|
| 369 | *info = '\0'; |
---|
| 370 | info++; |
---|
| 371 | if(!strcmp(info, "STATUS RINGING")) |
---|
| 372 | { |
---|
| 373 | g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id); |
---|
| 374 | skype_write( ic, buf, strlen( buf ) ); |
---|
[bbba374] | 375 | sd->call_status = SKYPE_CALL_RINGING; |
---|
| 376 | } |
---|
| 377 | else if(!strcmp(info, "STATUS MISSED")) |
---|
| 378 | { |
---|
| 379 | g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id); |
---|
| 380 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 381 | sd->call_status = SKYPE_CALL_MISSED; |
---|
[ecfbc5d] | 382 | } |
---|
| 383 | else if(!strncmp(info, "PARTNER_HANDLE ", 15)) |
---|
| 384 | { |
---|
| 385 | info += 15; |
---|
[df9255d] | 386 | if(sd->call_status) { |
---|
| 387 | switch(sd->call_status) |
---|
| 388 | { |
---|
| 389 | case SKYPE_CALL_RINGING: |
---|
| 390 | imcb_log(ic, "The user %s is currently ringing you.", info); |
---|
| 391 | break; |
---|
| 392 | case SKYPE_CALL_MISSED: |
---|
| 393 | imcb_log(ic, "You have missed a call from user %s.", info); |
---|
| 394 | break; |
---|
| 395 | } |
---|
| 396 | sd->call_status = 0; |
---|
| 397 | } |
---|
| 398 | } |
---|
| 399 | } |
---|
| 400 | } |
---|
| 401 | else if(!strncmp(line, "FILETRANSFER ", 13)) |
---|
| 402 | { |
---|
| 403 | char *id = strchr(line, ' '); |
---|
| 404 | if(++id) |
---|
| 405 | { |
---|
| 406 | char *info = strchr(id, ' '); |
---|
| 407 | *info = '\0'; |
---|
| 408 | info++; |
---|
| 409 | if(!strcmp(info, "STATUS NEW")) |
---|
| 410 | { |
---|
| 411 | g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id); |
---|
| 412 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 413 | sd->filetransfer_status = SKYPE_FILETRANSFER_NEW; |
---|
| 414 | } |
---|
| 415 | else if(!strcmp(info, "STATUS FAILED")) |
---|
| 416 | { |
---|
| 417 | g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id); |
---|
| 418 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 419 | sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED; |
---|
| 420 | } |
---|
| 421 | else if(!strncmp(info, "PARTNER_HANDLE ", 15)) |
---|
| 422 | { |
---|
| 423 | info += 15; |
---|
| 424 | if(sd->filetransfer_status) { |
---|
| 425 | switch(sd->filetransfer_status) |
---|
| 426 | { |
---|
| 427 | case SKYPE_FILETRANSFER_NEW: |
---|
| 428 | imcb_log(ic, "The user %s offered a new file for you.", info); |
---|
| 429 | break; |
---|
| 430 | case SKYPE_FILETRANSFER_FAILED: |
---|
| 431 | imcb_log(ic, "Failed to transfer file from user %s.", info); |
---|
| 432 | break; |
---|
| 433 | } |
---|
| 434 | sd->filetransfer_status = 0; |
---|
[bbba374] | 435 | } |
---|
[ecfbc5d] | 436 | } |
---|
| 437 | } |
---|
| 438 | } |
---|
[2d07803] | 439 | else if(!strncmp(line, "CHAT ", 5)) |
---|
| 440 | { |
---|
| 441 | char *id = strchr(line, ' '); |
---|
| 442 | if(++id) |
---|
| 443 | { |
---|
| 444 | char *info = strchr(id, ' '); |
---|
[86f2683] | 445 | if(info) |
---|
| 446 | *info = '\0'; |
---|
[2d07803] | 447 | info++; |
---|
[5652d43] | 448 | /* Remove fake chat if we created one in skype_chat_with() */ |
---|
| 449 | struct groupchat *gc = skype_chat_by_name(ic, ""); |
---|
| 450 | if(gc) |
---|
| 451 | imcb_chat_free(gc); |
---|
[5a61e43f] | 452 | if(!strcmp(info, "STATUS MULTI_SUBSCRIBED")) |
---|
[72aa7f0] | 453 | { |
---|
[86278cd] | 454 | imcb_chat_new( ic, id ); |
---|
[f8674db] | 455 | g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id); |
---|
| 456 | skype_write(ic, buf, strlen(buf)); |
---|
| 457 | g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id); |
---|
| 458 | skype_write(ic, buf, strlen(buf)); |
---|
[86278cd] | 459 | } |
---|
| 460 | else if(!strcmp(info, "STATUS DIALOG") && sd->groupchat_with) |
---|
| 461 | { |
---|
[5652d43] | 462 | gc = imcb_chat_new( ic, id ); |
---|
[86278cd] | 463 | /* According to the docs this |
---|
| 464 | * is necessary. However it |
---|
| 465 | * does not seem the situation |
---|
| 466 | * and it would open an extra |
---|
| 467 | * window on our client, so |
---|
| 468 | * just leave it out. */ |
---|
| 469 | /*g_snprintf(buf, 1024, "OPEN CHAT %s\n", id); |
---|
| 470 | skype_write(ic, buf, strlen(buf));*/ |
---|
| 471 | g_snprintf(buf, 1024, "%s@skype.com", sd->groupchat_with); |
---|
| 472 | imcb_chat_add_buddy(gc, buf); |
---|
| 473 | imcb_chat_add_buddy(gc, sd->username); |
---|
| 474 | g_free(sd->groupchat_with); |
---|
| 475 | sd->groupchat_with = NULL; |
---|
[f8674db] | 476 | g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id); |
---|
| 477 | skype_write(ic, buf, strlen(buf)); |
---|
| 478 | g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id); |
---|
| 479 | skype_write(ic, buf, strlen(buf)); |
---|
[72aa7f0] | 480 | } |
---|
[ec159f1] | 481 | else if(!strcmp(info, "STATUS UNSUBSCRIBED")) |
---|
| 482 | { |
---|
[5652d43] | 483 | gc = skype_chat_by_name(ic, id); |
---|
[ec159f1] | 484 | if(gc) |
---|
| 485 | gc->data = (void*)FALSE; |
---|
| 486 | } |
---|
[f8674db] | 487 | else if(!strncmp(info, "ADDER ", 6)) |
---|
| 488 | { |
---|
| 489 | info += 6; |
---|
| 490 | g_free(sd->adder); |
---|
| 491 | sd->adder = g_strdup_printf("%s@skype.com", info); |
---|
| 492 | } |
---|
[d601f9b] | 493 | else if(!strncmp(info, "TOPIC ", 6)) |
---|
| 494 | { |
---|
| 495 | info += 6; |
---|
[5652d43] | 496 | gc = skype_chat_by_name(ic, id); |
---|
[a5f76a2] | 497 | if(gc && (sd->adder || sd->topic_wait)) |
---|
[f8674db] | 498 | { |
---|
[a5f76a2] | 499 | if(sd->topic_wait) |
---|
| 500 | { |
---|
| 501 | sd->adder = g_strdup(sd->username); |
---|
| 502 | sd->topic_wait = 0; |
---|
| 503 | } |
---|
| 504 | imcb_chat_topic(gc, sd->adder, info, 0); |
---|
[f8674db] | 505 | g_free(sd->adder); |
---|
| 506 | sd->adder = NULL; |
---|
| 507 | } |
---|
[d601f9b] | 508 | } |
---|
[72aa7f0] | 509 | else if(!strncmp(info, "ACTIVEMEMBERS ", 14)) |
---|
| 510 | { |
---|
| 511 | info += 14; |
---|
[5652d43] | 512 | gc = skype_chat_by_name(ic, id); |
---|
[ec159f1] | 513 | /* Hack! We set ->data to TRUE |
---|
| 514 | * while we're on the channel |
---|
| 515 | * so that we won't rejoin |
---|
| 516 | * after a /part. */ |
---|
| 517 | if(gc && !gc->data) |
---|
[72aa7f0] | 518 | { |
---|
[349ee4a] | 519 | char **members = g_strsplit(info, " ", 0); |
---|
| 520 | int i; |
---|
| 521 | for(i=0;members[i];i++) |
---|
| 522 | { |
---|
| 523 | if(!strcmp(members[i], sd->username)) |
---|
| 524 | continue; |
---|
| 525 | g_snprintf(buf, 1024, "%s@skype.com", members[i]); |
---|
[c09d327] | 526 | if(!g_list_find_custom(gc->in_room, buf, (GCompareFunc)strcmp)) |
---|
| 527 | imcb_chat_add_buddy(gc, buf); |
---|
[349ee4a] | 528 | } |
---|
| 529 | imcb_chat_add_buddy(gc, sd->username); |
---|
| 530 | g_strfreev(members); |
---|
[72aa7f0] | 531 | } |
---|
| 532 | } |
---|
[2d07803] | 533 | } |
---|
| 534 | } |
---|
[9fd4241] | 535 | lineptr++; |
---|
| 536 | } |
---|
| 537 | g_strfreev(lines); |
---|
[1323e36] | 538 | } |
---|
| 539 | else if( st == 0 || ( st < 0 && !sockerr_again() ) ) |
---|
| 540 | { |
---|
| 541 | closesocket( sd->fd ); |
---|
| 542 | sd->fd = -1; |
---|
| 543 | |
---|
| 544 | imcb_error( ic, "Error while reading from server" ); |
---|
| 545 | imc_logout( ic, TRUE ); |
---|
| 546 | return FALSE; |
---|
| 547 | } |
---|
| 548 | return TRUE; |
---|
| 549 | } |
---|
| 550 | |
---|
[f06e3ac] | 551 | gboolean skype_start_stream( struct im_connection *ic ) |
---|
| 552 | { |
---|
[1323e36] | 553 | struct skype_data *sd = ic->proto_data; |
---|
[f06e3ac] | 554 | char *buf; |
---|
| 555 | int st; |
---|
| 556 | |
---|
[1fb89e3] | 557 | if(!sd) |
---|
| 558 | return FALSE; |
---|
| 559 | |
---|
[368861e] | 560 | if( sd->bfd <= 0 ) |
---|
| 561 | sd->bfd = b_input_add( sd->fd, GAIM_INPUT_READ, skype_read_callback, ic ); |
---|
[1323e36] | 562 | |
---|
[7daec06] | 563 | /* This will download all buddies. */ |
---|
[9fd4241] | 564 | buf = g_strdup_printf("SEARCH FRIENDS\n"); |
---|
[f06e3ac] | 565 | st = skype_write( ic, buf, strlen( buf ) ); |
---|
| 566 | g_free(buf); |
---|
[348a3a2] | 567 | buf = g_strdup_printf("SET USERSTATUS ONLINE\n"); |
---|
| 568 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 569 | g_free(buf); |
---|
[f06e3ac] | 570 | return st; |
---|
| 571 | } |
---|
| 572 | |
---|
| 573 | gboolean skype_connected( gpointer data, gint source, b_input_condition cond ) |
---|
| 574 | { |
---|
| 575 | struct im_connection *ic = data; |
---|
[ed2e37f] | 576 | imcb_connected(ic); |
---|
[f06e3ac] | 577 | return skype_start_stream(ic); |
---|
| 578 | } |
---|
| 579 | |
---|
| 580 | static void skype_login( account_t *acc ) |
---|
| 581 | { |
---|
| 582 | struct im_connection *ic = imcb_new( acc ); |
---|
| 583 | struct skype_data *sd = g_new0( struct skype_data, 1 ); |
---|
| 584 | |
---|
| 585 | ic->proto_data = sd; |
---|
| 586 | |
---|
| 587 | imcb_log( ic, "Connecting" ); |
---|
| 588 | sd->fd = proxy_connect(acc->server, set_getint( &acc->set, "port" ), skype_connected, ic ); |
---|
[a3d6427] | 589 | sd->username = g_strdup( acc->user ); |
---|
[f06e3ac] | 590 | |
---|
| 591 | sd->ic = ic; |
---|
| 592 | } |
---|
| 593 | |
---|
| 594 | static void skype_logout( struct im_connection *ic ) |
---|
| 595 | { |
---|
| 596 | struct skype_data *sd = ic->proto_data; |
---|
[98bca36] | 597 | char *buf; |
---|
| 598 | |
---|
| 599 | buf = g_strdup_printf("SET USERSTATUS OFFLINE\n"); |
---|
| 600 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 601 | g_free(buf); |
---|
| 602 | |
---|
[a3d6427] | 603 | g_free(sd->username); |
---|
[7613670] | 604 | g_free(sd->handle); |
---|
[f06e3ac] | 605 | g_free(sd); |
---|
[98bca36] | 606 | ic->proto_data = NULL; |
---|
[f06e3ac] | 607 | } |
---|
| 608 | |
---|
[93ece66] | 609 | static int skype_buddy_msg( struct im_connection *ic, char *who, char *message, int flags ) |
---|
| 610 | { |
---|
[77c1abe] | 611 | char *buf, *ptr, *nick; |
---|
[93ece66] | 612 | int st; |
---|
| 613 | |
---|
[cbec0d6] | 614 | nick = g_strdup(who); |
---|
[77c1abe] | 615 | ptr = strchr(nick, '@'); |
---|
[0bb1b7f] | 616 | if(ptr) |
---|
| 617 | *ptr = '\0'; |
---|
[93ece66] | 618 | |
---|
[77c1abe] | 619 | buf = g_strdup_printf("MESSAGE %s %s\n", nick, message); |
---|
| 620 | g_free(nick); |
---|
[93ece66] | 621 | st = skype_write( ic, buf, strlen( buf ) ); |
---|
| 622 | g_free(buf); |
---|
| 623 | |
---|
| 624 | return st; |
---|
| 625 | } |
---|
| 626 | |
---|
[23411c6] | 627 | const struct skype_away_state *skype_away_state_by_name( char *name ) |
---|
| 628 | { |
---|
| 629 | int i; |
---|
| 630 | |
---|
| 631 | for( i = 0; skype_away_state_list[i].full_name; i ++ ) |
---|
| 632 | if( g_strcasecmp( skype_away_state_list[i].full_name, name ) == 0 ) |
---|
| 633 | return( skype_away_state_list + i ); |
---|
| 634 | |
---|
| 635 | return NULL; |
---|
| 636 | } |
---|
| 637 | |
---|
[f06e3ac] | 638 | static void skype_set_away( struct im_connection *ic, char *state_txt, char *message ) |
---|
| 639 | { |
---|
[23411c6] | 640 | const struct skype_away_state *state; |
---|
| 641 | char *buf; |
---|
| 642 | |
---|
| 643 | if( strcmp( state_txt, GAIM_AWAY_CUSTOM ) == 0 ) |
---|
| 644 | state = skype_away_state_by_name( "Away" ); |
---|
| 645 | else |
---|
| 646 | state = skype_away_state_by_name( state_txt ); |
---|
| 647 | buf = g_strdup_printf("SET USERSTATUS %s\n", state->code); |
---|
| 648 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 649 | g_free(buf); |
---|
[f06e3ac] | 650 | } |
---|
| 651 | |
---|
| 652 | static GList *skype_away_states( struct im_connection *ic ) |
---|
| 653 | { |
---|
[de2f05e] | 654 | static GList *l = NULL; |
---|
[adce2de] | 655 | int i; |
---|
| 656 | |
---|
[de2f05e] | 657 | if( l == NULL ) |
---|
| 658 | for( i = 0; skype_away_state_list[i].full_name; i ++ ) |
---|
| 659 | l = g_list_append( l, (void*) skype_away_state_list[i].full_name ); |
---|
[adce2de] | 660 | |
---|
[f06e3ac] | 661 | return l; |
---|
| 662 | } |
---|
| 663 | |
---|
| 664 | static void skype_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 665 | { |
---|
[6627d92] | 666 | char *buf, *nick, *ptr; |
---|
| 667 | |
---|
[cbec0d6] | 668 | nick = g_strdup(who); |
---|
[6627d92] | 669 | ptr = strchr(nick, '@'); |
---|
| 670 | if(ptr) |
---|
| 671 | *ptr = '\0'; |
---|
| 672 | buf = g_strdup_printf("SET USER %s BUDDYSTATUS 2 Please authorize me\n", nick); |
---|
| 673 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 674 | g_free(nick); |
---|
[f06e3ac] | 675 | } |
---|
| 676 | |
---|
| 677 | static void skype_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 678 | { |
---|
[6627d92] | 679 | char *buf, *nick, *ptr; |
---|
| 680 | |
---|
[cbec0d6] | 681 | nick = g_strdup(who); |
---|
[6627d92] | 682 | ptr = strchr(nick, '@'); |
---|
| 683 | if(ptr) |
---|
| 684 | *ptr = '\0'; |
---|
| 685 | buf = g_strdup_printf("SET USER %s BUDDYSTATUS 1\n", nick); |
---|
| 686 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 687 | g_free(nick); |
---|
[f06e3ac] | 688 | } |
---|
| 689 | |
---|
[79e20f9] | 690 | void skype_chat_msg( struct groupchat *gc, char *message, int flags ) |
---|
[66c9558] | 691 | { |
---|
[79e20f9] | 692 | struct im_connection *ic = gc->ic; |
---|
| 693 | char *buf; |
---|
| 694 | buf = g_strdup_printf("CHATMESSAGE %s %s\n", gc->title, message); |
---|
| 695 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 696 | g_free(buf); |
---|
[66c9558] | 697 | } |
---|
| 698 | |
---|
[b01dc6c] | 699 | void skype_chat_leave( struct groupchat *gc ) |
---|
| 700 | { |
---|
| 701 | struct im_connection *ic = gc->ic; |
---|
| 702 | char *buf; |
---|
| 703 | buf = g_strdup_printf("ALTER CHAT %s LEAVE\n", gc->title); |
---|
| 704 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 705 | g_free(buf); |
---|
[760319d] | 706 | gc->data = (void*)TRUE; |
---|
| 707 | } |
---|
| 708 | |
---|
| 709 | void skype_chat_invite(struct groupchat *gc, char *who, char *message) |
---|
| 710 | { |
---|
| 711 | struct im_connection *ic = gc->ic; |
---|
| 712 | char *buf, *ptr, *nick; |
---|
| 713 | nick = g_strdup(message); |
---|
| 714 | ptr = strchr(nick, '@'); |
---|
| 715 | if(ptr) |
---|
| 716 | *ptr = '\0'; |
---|
| 717 | buf = g_strdup_printf("ALTER CHAT %s ADDMEMBERS %s\n", gc->title, nick); |
---|
| 718 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 719 | g_free(buf); |
---|
| 720 | g_free(nick); |
---|
[b01dc6c] | 721 | } |
---|
| 722 | |
---|
[09e2a69] | 723 | void skype_chat_topic(struct groupchat *gc, char *message) |
---|
| 724 | { |
---|
| 725 | struct im_connection *ic = gc->ic; |
---|
[a5f76a2] | 726 | struct skype_data *sd = ic->proto_data; |
---|
[09e2a69] | 727 | char *buf; |
---|
| 728 | buf = g_strdup_printf("ALTER CHAT %s SETTOPIC %s\n", gc->title, message); |
---|
| 729 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 730 | g_free(buf); |
---|
[a5f76a2] | 731 | sd->topic_wait = 1; |
---|
[09e2a69] | 732 | } |
---|
| 733 | |
---|
[86278cd] | 734 | struct groupchat *skype_chat_with(struct im_connection *ic, char *who) |
---|
| 735 | { |
---|
| 736 | struct skype_data *sd = ic->proto_data; |
---|
| 737 | char *ptr, *nick, *buf; |
---|
| 738 | nick = g_strdup(who); |
---|
| 739 | ptr = strchr(nick, '@'); |
---|
| 740 | if(ptr) |
---|
| 741 | *ptr = '\0'; |
---|
| 742 | buf = g_strdup_printf("CHAT CREATE %s\n", nick); |
---|
| 743 | skype_write(ic, buf, strlen(buf)); |
---|
| 744 | g_free(buf); |
---|
| 745 | sd->groupchat_with = g_strdup(nick); |
---|
| 746 | g_free(nick); |
---|
[5652d43] | 747 | /* We create a fake chat for now. We will replace it with a real one in |
---|
| 748 | * the real callback. */ |
---|
| 749 | return(imcb_chat_new( ic, "" )); |
---|
[86278cd] | 750 | } |
---|
| 751 | |
---|
[f06e3ac] | 752 | void init_plugin(void) |
---|
| 753 | { |
---|
| 754 | struct prpl *ret = g_new0( struct prpl, 1 ); |
---|
| 755 | |
---|
| 756 | ret->name = "skype"; |
---|
| 757 | ret->login = skype_login; |
---|
| 758 | ret->init = skype_init; |
---|
| 759 | ret->logout = skype_logout; |
---|
[93ece66] | 760 | ret->buddy_msg = skype_buddy_msg; |
---|
[f06e3ac] | 761 | ret->away_states = skype_away_states; |
---|
[7daec06] | 762 | ret->set_away = skype_set_away; |
---|
[f06e3ac] | 763 | ret->add_buddy = skype_add_buddy; |
---|
| 764 | ret->remove_buddy = skype_remove_buddy; |
---|
[66c9558] | 765 | ret->chat_msg = skype_chat_msg; |
---|
[b01dc6c] | 766 | ret->chat_leave = skype_chat_leave; |
---|
[760319d] | 767 | ret->chat_invite = skype_chat_invite; |
---|
[86278cd] | 768 | ret->chat_with = skype_chat_with; |
---|
[f06e3ac] | 769 | ret->handle_cmp = g_strcasecmp; |
---|
[09e2a69] | 770 | ret->chat_topic = skype_chat_topic; |
---|
[f06e3ac] | 771 | register_protocol( ret ); |
---|
| 772 | } |
---|