[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; |
---|
[7daec06] | 63 | /* When we receive a new message id, we query the handle, then the |
---|
| 64 | * body. Store the handle here so that we imcb_buddy_msg() when we got |
---|
| 65 | * the body. */ |
---|
[77c1abe] | 66 | char *handle; |
---|
[bbba374] | 67 | /* This is necessary because we send a notification when we get the |
---|
| 68 | * handle. So we store the state here and then we can send a |
---|
| 69 | * notification about the handle is in a given status. */ |
---|
| 70 | skype_call_status call_status; |
---|
[df9255d] | 71 | /* Same for file transfers. */ |
---|
| 72 | skype_filetransfer_status filetransfer_status; |
---|
[2d07803] | 73 | /* True if the next message will be a topic */ |
---|
| 74 | int topic; |
---|
[f06e3ac] | 75 | }; |
---|
| 76 | |
---|
[adce2de] | 77 | struct skype_away_state |
---|
| 78 | { |
---|
| 79 | char *code; |
---|
| 80 | char *full_name; |
---|
| 81 | }; |
---|
| 82 | |
---|
[7daec06] | 83 | struct skype_buddy_ask_data |
---|
| 84 | { |
---|
| 85 | struct im_connection *ic; |
---|
| 86 | char *handle; |
---|
| 87 | }; |
---|
| 88 | |
---|
| 89 | /* |
---|
| 90 | * Tables |
---|
| 91 | */ |
---|
| 92 | |
---|
[adce2de] | 93 | const struct skype_away_state skype_away_state_list[] = |
---|
| 94 | { |
---|
| 95 | { "ONLINE", "Online" }, |
---|
| 96 | { "SKYPEME", "Skype Me" }, |
---|
| 97 | { "AWAY", "Away" }, |
---|
| 98 | { "NA", "Not available" }, |
---|
| 99 | { "DND", "Do Not Disturb" }, |
---|
| 100 | { "INVISIBLE", "Invisible" }, |
---|
[23411c6] | 101 | { "OFFLINE", "Offline" }, |
---|
| 102 | { NULL, NULL} |
---|
[adce2de] | 103 | }; |
---|
| 104 | |
---|
[7daec06] | 105 | /* |
---|
| 106 | * Functions |
---|
| 107 | */ |
---|
[d3cbd17] | 108 | |
---|
[f06e3ac] | 109 | static void skype_init( account_t *acc ) |
---|
| 110 | { |
---|
| 111 | set_t *s; |
---|
| 112 | |
---|
| 113 | s = set_add( &acc->set, "port", SKYPE_PORT_DEFAULT, set_eval_int, acc ); |
---|
| 114 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 115 | |
---|
| 116 | s = set_add( &acc->set, "server", NULL, set_eval_account, acc ); |
---|
| 117 | s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | int skype_write( struct im_connection *ic, char *buf, int len ) |
---|
| 121 | { |
---|
| 122 | struct skype_data *sd = ic->proto_data; |
---|
[1fb89e3] | 123 | struct pollfd pfd[1]; |
---|
| 124 | |
---|
| 125 | pfd[0].fd = sd->fd; |
---|
| 126 | pfd[0].events = POLLOUT; |
---|
[f06e3ac] | 127 | |
---|
[7daec06] | 128 | /* This poll is necessary or we'll get a SIGPIPE when we write() to |
---|
| 129 | * sd->fd. */ |
---|
[1fb89e3] | 130 | poll(pfd, 1, 1000); |
---|
| 131 | if(pfd[0].revents & POLLHUP) |
---|
| 132 | { |
---|
| 133 | imcb_error( ic, "Could not connect to server" ); |
---|
| 134 | imc_logout( ic, TRUE ); |
---|
| 135 | return FALSE; |
---|
| 136 | } |
---|
[9fd4241] | 137 | write( sd->fd, buf, len ); |
---|
[f06e3ac] | 138 | |
---|
[9fd4241] | 139 | return TRUE; |
---|
[f06e3ac] | 140 | } |
---|
| 141 | |
---|
[d3cbd17] | 142 | static void skype_buddy_ask_yes( gpointer w, struct skype_buddy_ask_data *bla ) |
---|
| 143 | { |
---|
| 144 | char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED TRUE", bla->handle); |
---|
| 145 | skype_write( bla->ic, buf, strlen( buf ) ); |
---|
| 146 | g_free(buf); |
---|
| 147 | g_free(bla->handle); |
---|
| 148 | g_free(bla); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | static void skype_buddy_ask_no( gpointer w, struct skype_buddy_ask_data *bla ) |
---|
| 152 | { |
---|
| 153 | char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED FALSE", bla->handle); |
---|
| 154 | skype_write( bla->ic, buf, strlen( buf ) ); |
---|
| 155 | g_free(buf); |
---|
| 156 | g_free(bla->handle); |
---|
| 157 | g_free(bla); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | void skype_buddy_ask( struct im_connection *ic, char *handle, char *message) |
---|
| 161 | { |
---|
| 162 | struct skype_buddy_ask_data *bla = g_new0( struct skype_buddy_ask_data, 1 ); |
---|
| 163 | char *buf; |
---|
| 164 | |
---|
| 165 | bla->ic = ic; |
---|
| 166 | bla->handle = g_strdup(handle); |
---|
| 167 | |
---|
| 168 | buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list, saying: '%s'.", handle, message); |
---|
| 169 | imcb_ask( ic, buf, bla, skype_buddy_ask_yes, skype_buddy_ask_no ); |
---|
| 170 | g_free( buf ); |
---|
| 171 | } |
---|
| 172 | |
---|
[72aa7f0] | 173 | struct groupchat *skype_chat_by_name( struct im_connection *ic, char *name ) |
---|
| 174 | { |
---|
| 175 | struct groupchat *ret; |
---|
| 176 | |
---|
| 177 | for( ret = ic->conversations; ret; ret = ret->next ) |
---|
| 178 | { |
---|
| 179 | if(strcmp(name, ret->title ) == 0 ) |
---|
| 180 | break; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | return ret; |
---|
| 184 | } |
---|
| 185 | |
---|
[1323e36] | 186 | static gboolean skype_read_callback( gpointer data, gint fd, b_input_condition cond ) |
---|
| 187 | { |
---|
| 188 | struct im_connection *ic = data; |
---|
| 189 | struct skype_data *sd = ic->proto_data; |
---|
| 190 | char buf[1024]; |
---|
| 191 | int st; |
---|
[9fd4241] | 192 | char **lines, **lineptr, *line, *ptr; |
---|
[1323e36] | 193 | |
---|
[98bca36] | 194 | if( !sd || sd->fd == -1 ) |
---|
[1323e36] | 195 | return FALSE; |
---|
[7daec06] | 196 | /* Read the whole data. */ |
---|
[1323e36] | 197 | st = read( sd->fd, buf, sizeof( buf ) ); |
---|
| 198 | if( st > 0 ) |
---|
| 199 | { |
---|
| 200 | buf[st] = '\0'; |
---|
[7daec06] | 201 | /* Then split it up to lines. */ |
---|
[9fd4241] | 202 | lines = g_strsplit(buf, "\n", 0); |
---|
| 203 | lineptr = lines; |
---|
| 204 | while((line = *lineptr)) |
---|
| 205 | { |
---|
| 206 | if(!strlen(line)) |
---|
| 207 | break; |
---|
| 208 | if(!strncmp(line, "USERS ", 6)) |
---|
| 209 | { |
---|
| 210 | char **i; |
---|
| 211 | char **nicks; |
---|
| 212 | |
---|
| 213 | nicks = g_strsplit(line + 6, ", ", 0); |
---|
| 214 | i = nicks; |
---|
| 215 | while(*i) |
---|
| 216 | { |
---|
| 217 | g_snprintf(buf, 1024, "GET USER %s ONLINESTATUS\n", *i); |
---|
| 218 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 219 | i++; |
---|
| 220 | } |
---|
| 221 | g_strfreev(nicks); |
---|
| 222 | } |
---|
| 223 | else if(!strncmp(line, "USER ", 5)) |
---|
| 224 | { |
---|
[adce2de] | 225 | int flags = 0; |
---|
[be975f8] | 226 | char *status = strrchr(line, ' '); |
---|
| 227 | char *user = strchr(line, ' '); |
---|
[adce2de] | 228 | status++; |
---|
[be975f8] | 229 | ptr = strchr(++user, ' '); |
---|
| 230 | *ptr = '\0'; |
---|
[6627d92] | 231 | ptr++; |
---|
[7daec06] | 232 | if(!strncmp(ptr, "ONLINESTATUS ", 13) && |
---|
| 233 | strcmp(user, sd->username) != 0 |
---|
| 234 | && strcmp(user, "echo123") != 0) |
---|
[a3d6427] | 235 | { |
---|
| 236 | ptr = g_strdup_printf("%s@skype.com", user); |
---|
| 237 | imcb_add_buddy(ic, ptr, NULL); |
---|
| 238 | if(strcmp(status, "OFFLINE") != 0) |
---|
| 239 | flags |= OPT_LOGGED_IN; |
---|
| 240 | if(strcmp(status, "ONLINE") != 0 && strcmp(status, "SKYPEME") != 0) |
---|
| 241 | flags |= OPT_AWAY; |
---|
| 242 | imcb_buddy_status(ic, ptr, flags, NULL, NULL); |
---|
| 243 | g_free(ptr); |
---|
| 244 | } |
---|
[d3cbd17] | 245 | else if(!strncmp(ptr, "RECEIVEDAUTHREQUEST ", 20)) |
---|
| 246 | { |
---|
| 247 | char *message = ptr + 20; |
---|
| 248 | if(strlen(message)) |
---|
| 249 | skype_buddy_ask(ic, user, message); |
---|
| 250 | } |
---|
| 251 | else if(!strncmp(ptr, "BUDDYSTATUS ", 12)) |
---|
| 252 | { |
---|
| 253 | char *st = ptr + 12; |
---|
| 254 | if(!strcmp(st, "3")) |
---|
| 255 | { |
---|
| 256 | char *buf = g_strdup_printf("%s@skype.com", user); |
---|
| 257 | imcb_add_buddy(ic, buf, NULL); |
---|
| 258 | g_free(buf); |
---|
| 259 | } |
---|
| 260 | } |
---|
[9fd4241] | 261 | } |
---|
[77c1abe] | 262 | else if(!strncmp(line, "CHATMESSAGE ", 12)) |
---|
| 263 | { |
---|
| 264 | char *id = strchr(line, ' '); |
---|
| 265 | if(++id) |
---|
| 266 | { |
---|
| 267 | char *info = strchr(id, ' '); |
---|
| 268 | *info = '\0'; |
---|
| 269 | info++; |
---|
| 270 | if(!strcmp(info, "STATUS RECEIVED")) |
---|
| 271 | { |
---|
[7daec06] | 272 | /* New message ID: |
---|
| 273 | * (1) Request its from field |
---|
| 274 | * (2) Request its body |
---|
| 275 | * (3) Mark it as seen |
---|
| 276 | */ |
---|
[77c1abe] | 277 | g_snprintf(buf, 1024, "GET CHATMESSAGE %s FROM_HANDLE\n", id); |
---|
| 278 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 279 | g_snprintf(buf, 1024, "GET CHATMESSAGE %s BODY\n", id); |
---|
| 280 | skype_write( ic, buf, strlen( buf ) ); |
---|
[62bb4e4] | 281 | g_snprintf(buf, 1024, "SET CHATMESSAGE %s SEEN\n", id); |
---|
| 282 | skype_write( ic, buf, strlen( buf ) ); |
---|
[77c1abe] | 283 | } |
---|
| 284 | else if(!strncmp(info, "FROM_HANDLE ", 12)) |
---|
| 285 | { |
---|
| 286 | info += 12; |
---|
[7daec06] | 287 | /* New from field value. Store |
---|
| 288 | * it, then we can later use it |
---|
| 289 | * when we got the message's |
---|
| 290 | * body. */ |
---|
[7613670] | 291 | g_free(sd->handle); |
---|
[77c1abe] | 292 | sd->handle = g_strdup_printf("%s@skype.com", info); |
---|
[5d1b0774] | 293 | } |
---|
| 294 | else if(!strncmp(info, "EDITED_BY ", 10)) |
---|
| 295 | { |
---|
| 296 | info += 10; |
---|
| 297 | /* This is the same as |
---|
| 298 | * FROM_HANDLE, except that we |
---|
| 299 | * never request these lines |
---|
| 300 | * from Skype, we just get |
---|
| 301 | * them. */ |
---|
| 302 | g_free(sd->handle); |
---|
| 303 | sd->handle = g_strdup_printf("%s@skype.com", info); |
---|
[77c1abe] | 304 | } |
---|
| 305 | else if(!strncmp(info, "BODY ", 5)) |
---|
| 306 | { |
---|
| 307 | info += 5; |
---|
[4eaff41] | 308 | if(sd->handle && strlen(info)) |
---|
[7daec06] | 309 | { |
---|
| 310 | /* New body, we have everything to use imcb_buddy_msg() now! */ |
---|
[2d07803] | 311 | if(sd->topic) |
---|
| 312 | { |
---|
| 313 | imcb_log(ic, "%s has changed the chat topic to \"%s\"", sd->handle, info); |
---|
| 314 | sd->topic = 0; |
---|
| 315 | } |
---|
| 316 | else |
---|
| 317 | imcb_buddy_msg(ic, sd->handle, info, 0, 0); |
---|
[7daec06] | 318 | } |
---|
[77c1abe] | 319 | } |
---|
| 320 | } |
---|
| 321 | } |
---|
[ecfbc5d] | 322 | else if(!strncmp(line, "CALL ", 5)) |
---|
| 323 | { |
---|
| 324 | char *id = strchr(line, ' '); |
---|
| 325 | if(++id) |
---|
| 326 | { |
---|
| 327 | char *info = strchr(id, ' '); |
---|
| 328 | *info = '\0'; |
---|
| 329 | info++; |
---|
| 330 | if(!strcmp(info, "STATUS RINGING")) |
---|
| 331 | { |
---|
| 332 | g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id); |
---|
| 333 | skype_write( ic, buf, strlen( buf ) ); |
---|
[bbba374] | 334 | sd->call_status = SKYPE_CALL_RINGING; |
---|
| 335 | } |
---|
| 336 | else if(!strcmp(info, "STATUS MISSED")) |
---|
| 337 | { |
---|
| 338 | g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id); |
---|
| 339 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 340 | sd->call_status = SKYPE_CALL_MISSED; |
---|
[ecfbc5d] | 341 | } |
---|
| 342 | else if(!strncmp(info, "PARTNER_HANDLE ", 15)) |
---|
| 343 | { |
---|
| 344 | info += 15; |
---|
[df9255d] | 345 | if(sd->call_status) { |
---|
| 346 | switch(sd->call_status) |
---|
| 347 | { |
---|
| 348 | case SKYPE_CALL_RINGING: |
---|
| 349 | imcb_log(ic, "The user %s is currently ringing you.", info); |
---|
| 350 | break; |
---|
| 351 | case SKYPE_CALL_MISSED: |
---|
| 352 | imcb_log(ic, "You have missed a call from user %s.", info); |
---|
| 353 | break; |
---|
| 354 | } |
---|
| 355 | sd->call_status = 0; |
---|
| 356 | } |
---|
| 357 | } |
---|
| 358 | } |
---|
| 359 | } |
---|
| 360 | else if(!strncmp(line, "FILETRANSFER ", 13)) |
---|
| 361 | { |
---|
| 362 | char *id = strchr(line, ' '); |
---|
| 363 | if(++id) |
---|
| 364 | { |
---|
| 365 | char *info = strchr(id, ' '); |
---|
| 366 | *info = '\0'; |
---|
| 367 | info++; |
---|
| 368 | if(!strcmp(info, "STATUS NEW")) |
---|
| 369 | { |
---|
| 370 | g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id); |
---|
| 371 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 372 | sd->filetransfer_status = SKYPE_FILETRANSFER_NEW; |
---|
| 373 | } |
---|
| 374 | else if(!strcmp(info, "STATUS FAILED")) |
---|
| 375 | { |
---|
| 376 | g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id); |
---|
| 377 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 378 | sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED; |
---|
| 379 | } |
---|
| 380 | else if(!strncmp(info, "PARTNER_HANDLE ", 15)) |
---|
| 381 | { |
---|
| 382 | info += 15; |
---|
| 383 | if(sd->filetransfer_status) { |
---|
| 384 | switch(sd->filetransfer_status) |
---|
| 385 | { |
---|
| 386 | case SKYPE_FILETRANSFER_NEW: |
---|
| 387 | imcb_log(ic, "The user %s offered a new file for you.", info); |
---|
| 388 | break; |
---|
| 389 | case SKYPE_FILETRANSFER_FAILED: |
---|
| 390 | imcb_log(ic, "Failed to transfer file from user %s.", info); |
---|
| 391 | break; |
---|
| 392 | } |
---|
| 393 | sd->filetransfer_status = 0; |
---|
[bbba374] | 394 | } |
---|
[ecfbc5d] | 395 | } |
---|
| 396 | } |
---|
| 397 | } |
---|
[2d07803] | 398 | else if(!strncmp(line, "CHAT ", 5)) |
---|
| 399 | { |
---|
| 400 | char *id = strchr(line, ' '); |
---|
| 401 | if(++id) |
---|
| 402 | { |
---|
| 403 | char *info = strchr(id, ' '); |
---|
| 404 | *info = '\0'; |
---|
| 405 | info++; |
---|
| 406 | if(!strncmp(info, "TOPIC ", 6)) |
---|
| 407 | sd->topic = 1; |
---|
[72aa7f0] | 408 | else if(!strcmp(info, "STATUS MULTI_SUBSCRIBED")) |
---|
| 409 | { |
---|
| 410 | struct groupchat *gc; |
---|
| 411 | gc = imcb_chat_new( ic, id ); |
---|
| 412 | } |
---|
| 413 | else if(!strncmp(info, "ACTIVEMEMBERS ", 14)) |
---|
| 414 | { |
---|
| 415 | info += 14; |
---|
| 416 | struct groupchat *gc = skype_chat_by_name(ic, id); |
---|
| 417 | if(!gc) |
---|
| 418 | gc = imcb_chat_new( ic, id ); |
---|
| 419 | char **members = g_strsplit(info, " ", 0); |
---|
| 420 | int i; |
---|
| 421 | for(i=0;members[i];i++) |
---|
| 422 | { |
---|
| 423 | if(!strcmp(members[i], sd->username)) |
---|
| 424 | continue; |
---|
| 425 | g_snprintf(buf, 1024, "%s@skype.com", members[i]); |
---|
| 426 | imcb_chat_add_buddy(gc, buf); |
---|
| 427 | } |
---|
| 428 | imcb_chat_add_buddy(gc, sd->username); |
---|
| 429 | g_strfreev(members); |
---|
| 430 | } |
---|
[2d07803] | 431 | } |
---|
| 432 | } |
---|
[9fd4241] | 433 | lineptr++; |
---|
| 434 | } |
---|
| 435 | g_strfreev(lines); |
---|
[1323e36] | 436 | } |
---|
| 437 | else if( st == 0 || ( st < 0 && !sockerr_again() ) ) |
---|
| 438 | { |
---|
| 439 | closesocket( sd->fd ); |
---|
| 440 | sd->fd = -1; |
---|
| 441 | |
---|
| 442 | imcb_error( ic, "Error while reading from server" ); |
---|
| 443 | imc_logout( ic, TRUE ); |
---|
| 444 | return FALSE; |
---|
| 445 | } |
---|
| 446 | return TRUE; |
---|
| 447 | } |
---|
| 448 | |
---|
[f06e3ac] | 449 | gboolean skype_start_stream( struct im_connection *ic ) |
---|
| 450 | { |
---|
[1323e36] | 451 | struct skype_data *sd = ic->proto_data; |
---|
[f06e3ac] | 452 | char *buf; |
---|
| 453 | int st; |
---|
| 454 | |
---|
[1fb89e3] | 455 | if(!sd) |
---|
| 456 | return FALSE; |
---|
| 457 | |
---|
[368861e] | 458 | if( sd->bfd <= 0 ) |
---|
| 459 | sd->bfd = b_input_add( sd->fd, GAIM_INPUT_READ, skype_read_callback, ic ); |
---|
[1323e36] | 460 | |
---|
[7daec06] | 461 | /* This will download all buddies. */ |
---|
[9fd4241] | 462 | buf = g_strdup_printf("SEARCH FRIENDS\n"); |
---|
[f06e3ac] | 463 | st = skype_write( ic, buf, strlen( buf ) ); |
---|
| 464 | g_free(buf); |
---|
[348a3a2] | 465 | buf = g_strdup_printf("SET USERSTATUS ONLINE\n"); |
---|
| 466 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 467 | g_free(buf); |
---|
[f06e3ac] | 468 | return st; |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | gboolean skype_connected( gpointer data, gint source, b_input_condition cond ) |
---|
| 472 | { |
---|
| 473 | struct im_connection *ic = data; |
---|
[ed2e37f] | 474 | imcb_connected(ic); |
---|
[f06e3ac] | 475 | return skype_start_stream(ic); |
---|
| 476 | } |
---|
| 477 | |
---|
| 478 | static void skype_login( account_t *acc ) |
---|
| 479 | { |
---|
| 480 | struct im_connection *ic = imcb_new( acc ); |
---|
| 481 | struct skype_data *sd = g_new0( struct skype_data, 1 ); |
---|
| 482 | |
---|
| 483 | ic->proto_data = sd; |
---|
| 484 | |
---|
| 485 | imcb_log( ic, "Connecting" ); |
---|
| 486 | sd->fd = proxy_connect(acc->server, set_getint( &acc->set, "port" ), skype_connected, ic ); |
---|
[a3d6427] | 487 | sd->username = g_strdup( acc->user ); |
---|
[f06e3ac] | 488 | |
---|
| 489 | sd->ic = ic; |
---|
| 490 | } |
---|
| 491 | |
---|
| 492 | static void skype_logout( struct im_connection *ic ) |
---|
| 493 | { |
---|
| 494 | struct skype_data *sd = ic->proto_data; |
---|
[98bca36] | 495 | char *buf; |
---|
| 496 | |
---|
| 497 | buf = g_strdup_printf("SET USERSTATUS OFFLINE\n"); |
---|
| 498 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 499 | g_free(buf); |
---|
| 500 | |
---|
[a3d6427] | 501 | g_free(sd->username); |
---|
[7613670] | 502 | g_free(sd->handle); |
---|
[f06e3ac] | 503 | g_free(sd); |
---|
[98bca36] | 504 | ic->proto_data = NULL; |
---|
[f06e3ac] | 505 | } |
---|
| 506 | |
---|
[93ece66] | 507 | static int skype_buddy_msg( struct im_connection *ic, char *who, char *message, int flags ) |
---|
| 508 | { |
---|
[77c1abe] | 509 | char *buf, *ptr, *nick; |
---|
[93ece66] | 510 | int st; |
---|
| 511 | |
---|
[cbec0d6] | 512 | nick = g_strdup(who); |
---|
[77c1abe] | 513 | ptr = strchr(nick, '@'); |
---|
[0bb1b7f] | 514 | if(ptr) |
---|
| 515 | *ptr = '\0'; |
---|
[93ece66] | 516 | |
---|
[77c1abe] | 517 | buf = g_strdup_printf("MESSAGE %s %s\n", nick, message); |
---|
| 518 | g_free(nick); |
---|
[93ece66] | 519 | st = skype_write( ic, buf, strlen( buf ) ); |
---|
| 520 | g_free(buf); |
---|
| 521 | |
---|
| 522 | return st; |
---|
| 523 | } |
---|
| 524 | |
---|
[23411c6] | 525 | const struct skype_away_state *skype_away_state_by_name( char *name ) |
---|
| 526 | { |
---|
| 527 | int i; |
---|
| 528 | |
---|
| 529 | for( i = 0; skype_away_state_list[i].full_name; i ++ ) |
---|
| 530 | if( g_strcasecmp( skype_away_state_list[i].full_name, name ) == 0 ) |
---|
| 531 | return( skype_away_state_list + i ); |
---|
| 532 | |
---|
| 533 | return NULL; |
---|
| 534 | } |
---|
| 535 | |
---|
[f06e3ac] | 536 | static void skype_set_away( struct im_connection *ic, char *state_txt, char *message ) |
---|
| 537 | { |
---|
[23411c6] | 538 | const struct skype_away_state *state; |
---|
| 539 | char *buf; |
---|
| 540 | |
---|
| 541 | if( strcmp( state_txt, GAIM_AWAY_CUSTOM ) == 0 ) |
---|
| 542 | state = skype_away_state_by_name( "Away" ); |
---|
| 543 | else |
---|
| 544 | state = skype_away_state_by_name( state_txt ); |
---|
| 545 | buf = g_strdup_printf("SET USERSTATUS %s\n", state->code); |
---|
| 546 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 547 | g_free(buf); |
---|
[f06e3ac] | 548 | } |
---|
| 549 | |
---|
| 550 | static GList *skype_away_states( struct im_connection *ic ) |
---|
| 551 | { |
---|
[23411c6] | 552 | GList *l = NULL; |
---|
[adce2de] | 553 | int i; |
---|
| 554 | |
---|
[23411c6] | 555 | for( i = 0; skype_away_state_list[i].full_name; i ++ ) |
---|
| 556 | l = g_list_append( l, (void*) skype_away_state_list[i].full_name ); |
---|
[adce2de] | 557 | |
---|
[f06e3ac] | 558 | return l; |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | static void skype_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 562 | { |
---|
[6627d92] | 563 | char *buf, *nick, *ptr; |
---|
| 564 | |
---|
[cbec0d6] | 565 | nick = g_strdup(who); |
---|
[6627d92] | 566 | ptr = strchr(nick, '@'); |
---|
| 567 | if(ptr) |
---|
| 568 | *ptr = '\0'; |
---|
| 569 | buf = g_strdup_printf("SET USER %s BUDDYSTATUS 2 Please authorize me\n", nick); |
---|
| 570 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 571 | g_free(nick); |
---|
[f06e3ac] | 572 | } |
---|
| 573 | |
---|
| 574 | static void skype_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 575 | { |
---|
[6627d92] | 576 | char *buf, *nick, *ptr; |
---|
| 577 | |
---|
[cbec0d6] | 578 | nick = g_strdup(who); |
---|
[6627d92] | 579 | ptr = strchr(nick, '@'); |
---|
| 580 | if(ptr) |
---|
| 581 | *ptr = '\0'; |
---|
| 582 | buf = g_strdup_printf("SET USER %s BUDDYSTATUS 1\n", nick); |
---|
| 583 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 584 | g_free(nick); |
---|
[f06e3ac] | 585 | } |
---|
| 586 | |
---|
[66c9558] | 587 | int skype_chat_msg( struct groupchat *c, char *message, int flags ) |
---|
| 588 | { |
---|
| 589 | // TODO: this is just here atm to prevent a segfault |
---|
| 590 | } |
---|
| 591 | |
---|
[f06e3ac] | 592 | void init_plugin(void) |
---|
| 593 | { |
---|
| 594 | struct prpl *ret = g_new0( struct prpl, 1 ); |
---|
| 595 | |
---|
| 596 | ret->name = "skype"; |
---|
| 597 | ret->login = skype_login; |
---|
| 598 | ret->init = skype_init; |
---|
| 599 | ret->logout = skype_logout; |
---|
[93ece66] | 600 | ret->buddy_msg = skype_buddy_msg; |
---|
[f06e3ac] | 601 | ret->away_states = skype_away_states; |
---|
[7daec06] | 602 | ret->set_away = skype_set_away; |
---|
[f06e3ac] | 603 | ret->add_buddy = skype_add_buddy; |
---|
| 604 | ret->remove_buddy = skype_remove_buddy; |
---|
[66c9558] | 605 | ret->chat_msg = skype_chat_msg; |
---|
[f06e3ac] | 606 | ret->handle_cmp = g_strcasecmp; |
---|
| 607 | register_protocol( ret ); |
---|
| 608 | } |
---|