source: skype/skype.c @ 5258dcc9

Last change on this file since 5258dcc9 was 5258dcc9, checked in by Miklos Vajna <vmiklos@…>, at 2009-01-07T00:35:14Z

updates for 0.7.0

  • Property mode set to 100644
File size: 34.4 KB
Line 
1/*
2 *  skype.c - Skype plugin for BitlBee
3 *
4 *  Copyright (c) 2007, 2008, 2009 by Miklos Vajna <vmiklos@frugalware.org>
5 *
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.
24 */
25
26#define _XOPEN_SOURCE
27#include <stdio.h>
28#include <poll.h>
29#include <bitlbee.h>
30#include <bitlbee/ssl_client.h>
31#include <glib.h>
32
33#define SKYPE_DEFAULT_SERVER "localhost"
34#define SKYPE_DEFAULT_PORT "2727"
35
36/*
37 * Enumerations
38 */
39
40typedef enum {
41        SKYPE_CALL_RINGING = 1,
42        SKYPE_CALL_MISSED,
43        SKYPE_CALL_CANCELLED,
44        SKYPE_CALL_FINISHED,
45        SKYPE_CALL_REFUSED
46} skype_call_status;
47
48typedef enum {
49        SKYPE_FILETRANSFER_NEW = 1,
50        SKYPE_FILETRANSFER_FAILED
51} skype_filetransfer_status;
52
53/*
54 * Structures
55 */
56
57struct skype_data {
58        struct im_connection *ic;
59        char *username;
60        /* The effective file descriptor. We store it here so any function can
61         * write() to it. */
62        int fd;
63        /* File descriptor returned by bitlbee. we store it so we know when
64         * we're connected and when we aren't. */
65        int bfd;
66        /* ssl_getfd() uses this to get the file desciptor. */
67        void *ssl;
68        /* When we receive a new message id, we query the properties, finally
69         * the chatname. Store the properties here so that we can use
70         * imcb_buddy_msg() when we got the chatname. */
71        char *handle;
72        /* List, because of multiline messages. */
73        GList *body;
74        char *type;
75        /* This is necessary because we send a notification when we get the
76         * handle. So we store the state here and then we can send a
77         * notification about the handle is in a given status. */
78        skype_call_status call_status;
79        char *call_id;
80        char *call_duration;
81        /* If the call is outgoing or not */
82        int call_out;
83        /* Same for file transfers. */
84        skype_filetransfer_status filetransfer_status;
85        /* Using /j #nick we want to have a groupchat with two people. Usually
86         * not (default). */
87        char *groupchat_with;
88        /* The user who invited us to the chat. */
89        char *adder;
90        /* If we are waiting for a confirmation about we changed the topic. */
91        int topic_wait;
92        /* These are used by the info command. */
93        char *info_fullname;
94        char *info_phonehome;
95        char *info_phoneoffice;
96        char *info_phonemobile;
97        char *info_nrbuddies;
98        char *info_tz;
99        char *info_seen;
100        char *info_birthday;
101        char *info_sex;
102        char *info_language;
103        char *info_country;
104        char *info_province;
105        char *info_city;
106        char *info_homepage;
107        char *info_about;
108        /* When a call fails, we get the reason and later we get the failure
109         * event, so store the failure code here till then */
110        int failurereason;
111};
112
113struct skype_away_state {
114        char *code;
115        char *full_name;
116};
117
118struct skype_buddy_ask_data {
119        struct im_connection *ic;
120        /* This is also used for call IDs for simplicity */
121        char *handle;
122};
123
124/*
125 * Tables
126 */
127
128const struct skype_away_state skype_away_state_list[] = {
129        { "ONLINE",  "Online" },
130        { "SKYPEME",  "Skype Me" },
131        { "AWAY",   "Away" },
132        { "NA",    "Not available" },
133        { "DND",      "Do Not Disturb" },
134        { "INVISIBLE",      "Invisible" },
135        { "OFFLINE",      "Offline" },
136        { NULL, NULL}
137};
138
139/*
140 * Functions
141 */
142
143int skype_write(struct im_connection *ic, char *buf)
144{
145        struct skype_data *sd = ic->proto_data;
146        struct pollfd pfd[1];
147        int len = strlen(buf);
148
149        pfd[0].fd = sd->fd;
150        pfd[0].events = POLLOUT;
151
152        /* This poll is necessary or we'll get a SIGPIPE when we write() to
153         * sd->fd. */
154        poll(pfd, 1, 1000);
155        if (pfd[0].revents & POLLHUP) {
156                imc_logout(ic, TRUE);
157                return FALSE;
158        }
159        ssl_write(sd->ssl, buf, len);
160
161        return TRUE;
162}
163
164static void skype_buddy_ask_yes(void *data)
165{
166        struct skype_buddy_ask_data *bla = data;
167        char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED TRUE", bla->handle);
168        skype_write(bla->ic, buf);
169        g_free(buf);
170        g_free(bla->handle);
171        g_free(bla);
172}
173
174static void skype_buddy_ask_no(void *data)
175{
176        struct skype_buddy_ask_data *bla = data;
177        char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED FALSE", bla->handle);
178        skype_write(bla->ic, buf);
179        g_free(buf);
180        g_free(bla->handle);
181        g_free(bla);
182}
183
184void skype_buddy_ask(struct im_connection *ic, char *handle, char *message)
185{
186        struct skype_buddy_ask_data *bla = g_new0(struct skype_buddy_ask_data, 1);
187        char *buf;
188
189        bla->ic = ic;
190        bla->handle = g_strdup(handle);
191
192        buf = g_strdup_printf("The user %s wants to add you to his/her buddy list, saying: '%s'.", handle, message);
193        imcb_ask(ic, buf, bla, skype_buddy_ask_yes, skype_buddy_ask_no);
194        g_free(buf);
195}
196
197static void skype_call_ask_yes(void *data)
198{
199        struct skype_buddy_ask_data *bla = data;
200        char *buf = g_strdup_printf("SET CALL %s STATUS INPROGRESS", bla->handle);
201        skype_write(bla->ic, buf);
202        g_free(buf);
203        g_free(bla->handle);
204        g_free(bla);
205}
206
207static void skype_call_ask_no(void *data)
208{
209        struct skype_buddy_ask_data *bla = data;
210        char *buf = g_strdup_printf("SET CALL %s STATUS FINISHED", bla->handle);
211        skype_write(bla->ic, buf);
212        g_free(buf);
213        g_free(bla->handle);
214        g_free(bla);
215}
216
217void skype_call_ask(struct im_connection *ic, char *call_id, char *message)
218{
219        struct skype_buddy_ask_data *bla = g_new0(struct skype_buddy_ask_data, 1);
220
221        bla->ic = ic;
222        bla->handle = g_strdup(call_id);
223
224        imcb_ask(ic, message, bla, skype_call_ask_yes, skype_call_ask_no);
225}
226struct groupchat *skype_chat_by_name(struct im_connection *ic, char *name)
227{
228        struct groupchat *ret;
229
230        for (ret = ic->groupchats; ret; ret = ret->next)
231                if (strcmp(name, ret->title) == 0)
232                        break;
233
234        return ret;
235}
236
237static char *skype_call_strerror(int err)
238{
239        switch (err) {
240        case 1:
241                return "Miscellaneous error";
242        case 2:
243                return "User or phone number does not exist.";
244        case 3:
245                return "User is offline";
246        case 4:
247                return "No proxy found";
248        case 5:
249                return "Session terminated.";
250        case 6:
251                return "No common codec found.";
252        case 7:
253                return "Sound I/O error.";
254        case 8:
255                return "Problem with remote sound device.";
256        case 9:
257                return "Call blocked by recipient.";
258        case 10:
259                return "Recipient not a friend.";
260        case 11:
261                return "Current user not authorized by recipient.";
262        case 12:
263                return "Sound recording error.";
264        default:
265                return "Unknown error";
266        }
267}
268
269static gboolean skype_read_callback(gpointer data, gint fd, b_input_condition cond)
270{
271        struct im_connection *ic = data;
272        struct skype_data *sd = ic->proto_data;
273        char buf[1024];
274        int st;
275        char **lines, **lineptr, *line, *ptr;
276
277        if (!sd || sd->fd == -1)
278                return FALSE;
279        /* Read the whole data. */
280        st = ssl_read(sd->ssl, buf, sizeof(buf));
281        if (st > 0) {
282                buf[st] = '\0';
283                /* Then split it up to lines. */
284                lines = g_strsplit(buf, "\n", 0);
285                lineptr = lines;
286                while ((line = *lineptr)) {
287                        if (!strlen(line))
288                                break;
289                        if (set_getbool(&ic->acc->set, "skypeconsole_receive"))
290                                imcb_buddy_msg(ic, "skypeconsole", line, 0, 0);
291                        if (!strncmp(line, "USERS ", 6)) {
292                                char **i;
293                                char **nicks;
294
295                                nicks = g_strsplit(line + 6, ", ", 0);
296                                i = nicks;
297                                while (*i) {
298                                        g_snprintf(buf, 1024, "GET USER %s ONLINESTATUS\n", *i);
299                                        skype_write(ic, buf);
300                                        i++;
301                                }
302                                g_strfreev(nicks);
303                        } else if (!strncmp(line, "USER ", 5)) {
304                                int flags = 0;
305                                char *status = strrchr(line, ' ');
306                                char *user = strchr(line, ' ');
307                                status++;
308                                ptr = strchr(++user, ' ');
309                                *ptr = '\0';
310                                ptr++;
311                                if (!strncmp(ptr, "ONLINESTATUS ", 13) &&
312                                                strcmp(user, sd->username) != 0
313                                                && strcmp(user, "echo123") != 0) {
314                                        ptr = g_strdup_printf("%s@skype.com", user);
315                                        imcb_add_buddy(ic, ptr, NULL);
316                                        if (strcmp(status, "OFFLINE") && (strcmp(status, "SKYPEOUT") || !set_getbool(&ic->acc->set, "skypeout_offline")))
317                                                flags |= OPT_LOGGED_IN;
318                                        if (strcmp(status, "ONLINE") != 0 && strcmp(status, "SKYPEME") != 0)
319                                                flags |= OPT_AWAY;
320                                        imcb_buddy_status(ic, ptr, flags, NULL, NULL);
321                                        g_free(ptr);
322                                } else if (!strncmp(ptr, "RECEIVEDAUTHREQUEST ", 20)) {
323                                        char *message = ptr + 20;
324                                        if (strlen(message))
325                                                skype_buddy_ask(ic, user, message);
326                                } else if (!strncmp(ptr, "BUDDYSTATUS ", 12)) {
327                                        char *st = ptr + 12;
328                                        if (!strcmp(st, "3")) {
329                                                char *buf = g_strdup_printf("%s@skype.com", user);
330                                                imcb_add_buddy(ic, buf, NULL);
331                                                g_free(buf);
332                                        }
333                                } else if (!strncmp(ptr, "FULLNAME ", 9))
334                                        sd->info_fullname = g_strdup_printf("%s", ptr + 9);
335                                else if (!strncmp(ptr, "PHONE_HOME ", 11))
336                                        sd->info_phonehome = g_strdup_printf("%s", ptr + 11);
337                                else if (!strncmp(ptr, "PHONE_OFFICE ", 13))
338                                        sd->info_phoneoffice = g_strdup_printf("%s", ptr + 13);
339                                else if (!strncmp(ptr, "PHONE_MOBILE ", 13))
340                                        sd->info_phonemobile = g_strdup_printf("%s", ptr + 13);
341                                else if (!strncmp(ptr, "NROF_AUTHED_BUDDIES ", 20))
342                                        sd->info_nrbuddies = g_strdup_printf("%s", ptr + 20);
343                                else if (!strncmp(ptr, "TIMEZONE ", 9))
344                                        sd->info_tz = g_strdup_printf("%s", ptr + 9);
345                                else if (!strncmp(ptr, "LASTONLINETIMESTAMP ", 20))
346                                        sd->info_seen = g_strdup_printf("%s", ptr + 20);
347                                else if (!strncmp(ptr, "BIRTHDAY ", 9))
348                                        sd->info_birthday = g_strdup_printf("%s", ptr + 9);
349                                else if (!strncmp(ptr, "SEX ", 4))
350                                        sd->info_sex = g_strdup_printf("%s", ptr + 4);
351                                else if (!strncmp(ptr, "LANGUAGE ", 9))
352                                        sd->info_language = g_strdup_printf("%s", ptr + 9);
353                                else if (!strncmp(ptr, "COUNTRY ", 8))
354                                        sd->info_country = g_strdup_printf("%s", ptr + 8);
355                                else if (!strncmp(ptr, "PROVINCE ", 9))
356                                        sd->info_province = g_strdup_printf("%s", ptr + 9);
357                                else if (!strncmp(ptr, "CITY ", 5))
358                                        sd->info_city = g_strdup_printf("%s", ptr + 5);
359                                else if (!strncmp(ptr, "HOMEPAGE ", 9))
360                                        sd->info_homepage = g_strdup_printf("%s", ptr + 9);
361                                else if (!strncmp(ptr, "ABOUT ", 6)) {
362                                        sd->info_about = g_strdup_printf("%s", ptr + 6);
363
364                                        GString *st = g_string_new("Contact Information\n");
365                                        g_string_append_printf(st, "Skype Name: %s\n", user);
366                                        if (sd->info_fullname) {
367                                                if (strlen(sd->info_fullname))
368                                                        g_string_append_printf(st, "Full Name: %s\n", sd->info_fullname);
369                                                g_free(sd->info_fullname);
370                                        }
371                                        if (sd->info_phonehome) {
372                                                if (strlen(sd->info_phonehome))
373                                                        g_string_append_printf(st, "Home Phone: %s\n", sd->info_phonehome);
374                                                g_free(sd->info_phonehome);
375                                        }
376                                        if (sd->info_phoneoffice) {
377                                                if (strlen(sd->info_phoneoffice))
378                                                        g_string_append_printf(st, "Office Phone: %s\n", sd->info_phoneoffice);
379                                                g_free(sd->info_phoneoffice);
380                                        }
381                                        if (sd->info_phonemobile) {
382                                                if (strlen(sd->info_phonemobile))
383                                                        g_string_append_printf(st, "Mobile Phone: %s\n", sd->info_phonemobile);
384                                                g_free(sd->info_phonemobile);
385                                        }
386                                        g_string_append_printf(st, "Personal Information\n");
387                                        if (sd->info_nrbuddies) {
388                                                if (strlen(sd->info_nrbuddies))
389                                                        g_string_append_printf(st, "Contacts: %s\n", sd->info_nrbuddies);
390                                                g_free(sd->info_nrbuddies);
391                                        }
392                                        if (sd->info_tz) {
393                                                if (strlen(sd->info_tz)) {
394                                                        char ib[256];
395                                                        time_t t = time(NULL);
396                                                        t += atoi(sd->info_tz)-(60*60*24);
397                                                        struct tm *gt = gmtime(&t);
398                                                        strftime(ib, 256, "%H:%M:%S", gt);
399                                                        g_string_append_printf(st, "Local Time: %s\n", ib);
400                                                }
401                                                g_free(sd->info_tz);
402                                        }
403                                        if (sd->info_seen) {
404                                                if (strlen(sd->info_seen)) {
405                                                        char ib[256];
406                                                        time_t it = atoi(sd->info_seen);
407                                                        struct tm *tm = localtime(&it);
408                                                        strftime(ib, 256, ("%Y. %m. %d. %H:%M"), tm);
409                                                        g_string_append_printf(st, "Last Seen: %s\n", ib);
410                                                }
411                                                g_free(sd->info_seen);
412                                        }
413                                        if (sd->info_birthday) {
414                                                if (strlen(sd->info_birthday) && strcmp(sd->info_birthday, "0")) {
415                                                        char ib[256];
416                                                        struct tm tm;
417                                                        strptime(sd->info_birthday, "%Y%m%d", &tm);
418                                                        strftime(ib, 256, "%B %d, %Y", &tm);
419                                                        g_string_append_printf(st, "Birthday: %s\n", ib);
420
421                                                        strftime(ib, 256, "%Y", &tm);
422                                                        int year = atoi(ib);
423                                                        time_t t = time(NULL);
424                                                        struct tm *lt = localtime(&t);
425                                                        g_string_append_printf(st, "Age: %d\n", lt->tm_year+1900-year);
426                                                }
427                                                g_free(sd->info_birthday);
428                                        }
429                                        if (sd->info_sex) {
430                                                if (strlen(sd->info_sex)) {
431                                                        char *iptr = sd->info_sex;
432                                                        while (*iptr++)
433                                                                *iptr = tolower(*iptr);
434                                                        g_string_append_printf(st, "Gender: %s\n", sd->info_sex);
435                                                }
436                                                g_free(sd->info_sex);
437                                        }
438                                        if (sd->info_language) {
439                                                if (strlen(sd->info_language)) {
440                                                        char *iptr = strchr(sd->info_language, ' ');
441                                                        if (iptr)
442                                                                iptr++;
443                                                        else
444                                                                iptr = sd->info_language;
445                                                        g_string_append_printf(st, "Language: %s\n", iptr);
446                                                }
447                                                g_free(sd->info_language);
448                                        }
449                                        if (sd->info_country) {
450                                                if (strlen(sd->info_country)) {
451                                                        char *iptr = strchr(sd->info_country, ' ');
452                                                        if (iptr)
453                                                                iptr++;
454                                                        else
455                                                                iptr = sd->info_country;
456                                                        g_string_append_printf(st, "Country: %s\n", iptr);
457                                                }
458                                                g_free(sd->info_country);
459                                        }
460                                        if (sd->info_province) {
461                                                if (strlen(sd->info_province))
462                                                        g_string_append_printf(st, "Region: %s\n", sd->info_province);
463                                                g_free(sd->info_province);
464                                        }
465                                        if (sd->info_city) {
466                                                if (strlen(sd->info_city))
467                                                        g_string_append_printf(st, "City: %s\n", sd->info_city);
468                                                g_free(sd->info_city);
469                                        }
470                                        if (sd->info_homepage) {
471                                                if (strlen(sd->info_homepage))
472                                                        g_string_append_printf(st, "Homepage: %s\n", sd->info_homepage);
473                                                g_free(sd->info_homepage);
474                                        }
475                                        if (sd->info_about) {
476                                                if (strlen(sd->info_about))
477                                                        g_string_append_printf(st, "%s\n", sd->info_about);
478                                                g_free(sd->info_about);
479                                        }
480                                        imcb_log(ic, "%s", st->str);
481                                        g_string_free(st, TRUE);
482                                }
483                        } else if (!strncmp(line, "CHATMESSAGE ", 12)) {
484                                char *id = strchr(line, ' ');
485                                if (++id) {
486                                        char *info = strchr(id, ' ');
487                                        *info = '\0';
488                                        info++;
489                                        if (!strcmp(info, "STATUS RECEIVED")) {
490                                                /* New message ID:
491                                                 * (1) Request its from field
492                                                 * (2) Request its body
493                                                 * (3) Request its type
494                                                 * (4) Query chatname
495                                                 */
496                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s FROM_HANDLE\n", id);
497                                                skype_write(ic, buf);
498                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s BODY\n", id);
499                                                skype_write(ic, buf);
500                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s TYPE\n", id);
501                                                skype_write(ic, buf);
502                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s CHATNAME\n", id);
503                                                skype_write(ic, buf);
504                                        } else if (!strncmp(info, "FROM_HANDLE ", 12)) {
505                                                info += 12;
506                                                /* New from field value. Store
507                                                 * it, then we can later use it
508                                                 * when we got the message's
509                                                 * body. */
510                                                g_free(sd->handle);
511                                                sd->handle = g_strdup_printf("%s@skype.com", info);
512                                        } else if (!strncmp(info, "EDITED_BY ", 10)) {
513                                                info += 10;
514                                                /* This is the same as
515                                                 * FROM_HANDLE, except that we
516                                                 * never request these lines
517                                                 * from Skype, we just get
518                                                 * them. */
519                                                g_free(sd->handle);
520                                                sd->handle = g_strdup_printf("%s@skype.com", info);
521                                        } else if (!strncmp(info, "BODY ", 5)) {
522                                                info += 5;
523                                                sd->body = g_list_append(sd->body, g_strdup(info));
524                                        }       else if (!strncmp(info, "TYPE ", 5)) {
525                                                info += 5;
526                                                g_free(sd->type);
527                                                sd->type = g_strdup(info);
528                                        } else if (!strncmp(info, "CHATNAME ", 9)) {
529                                                info += 9;
530                                                if (sd->handle && sd->body && sd->type) {
531                                                        struct groupchat *gc = skype_chat_by_name(ic, info);
532                                                        int i;
533                                                        for (i = 0; i < g_list_length(sd->body); i++) {
534                                                                char *body = g_list_nth_data(sd->body, i);
535                                                                if (!strcmp(sd->type, "SAID") || !strcmp(sd->type, "EMOTED")) {
536                                                                        char *st;
537                                                                        if (!strcmp(sd->type, "SAID"))
538                                                                                st = g_strdup(body);
539                                                                        else
540                                                                                st = g_strdup_printf("/me %s", body);
541                                                                        if (!gc)
542                                                                                /* Private message */
543                                                                                imcb_buddy_msg(ic, sd->handle, st, 0, 0);
544                                                                        else
545                                                                                /* Groupchat message */
546                                                                                imcb_chat_msg(gc, sd->handle, st, 0, 0);
547                                                                        g_free(st);
548                                                                } else if (!strcmp(sd->type, "SETTOPIC")) {
549                                                                        if (gc)
550                                                                                imcb_chat_topic(gc, sd->handle, body, 0);
551                                                                } else if (!strcmp(sd->type, "LEFT")) {
552                                                                        if (gc)
553                                                                                imcb_chat_remove_buddy(gc, sd->handle, NULL);
554                                                                }
555                                                        }
556                                                        g_list_free(sd->body);
557                                                        sd->body = NULL;
558                                                }
559                                        }
560                                }
561                        } else if (!strncmp(line, "CALL ", 5)) {
562                                char *id = strchr(line, ' ');
563                                if (++id) {
564                                        char *info = strchr(id, ' ');
565                                        *info = '\0';
566                                        info++;
567                                        if (!strncmp(info, "FAILUREREASON ", 14))
568                                                sd->failurereason = atoi(strchr(info, ' '));
569                                        else if (!strcmp(info, "STATUS RINGING")) {
570                                                if (sd->call_id)
571                                                        g_free(sd->call_id);
572                                                sd->call_id = g_strdup(id);
573                                                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
574                                                skype_write(ic, buf);
575                                                sd->call_status = SKYPE_CALL_RINGING;
576                                        } else if (!strcmp(info, "STATUS MISSED")) {
577                                                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
578                                                skype_write(ic, buf);
579                                                sd->call_status = SKYPE_CALL_MISSED;
580                                        } else if (!strcmp(info, "STATUS CANCELLED")) {
581                                                        g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
582                                                        skype_write(ic, buf);
583                                                        sd->call_status = SKYPE_CALL_CANCELLED;
584                                        } else if (!strcmp(info, "STATUS FINISHED")) {
585                                                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
586                                                skype_write(ic, buf);
587                                                sd->call_status = SKYPE_CALL_FINISHED;
588                                        } else if (!strcmp(info, "STATUS REFUSED")) {
589                                                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
590                                                skype_write(ic, buf);
591                                                sd->call_status = SKYPE_CALL_REFUSED;
592                                        } else if (!strcmp(info, "STATUS UNPLACED")) {
593                                                if (sd->call_id)
594                                                        g_free(sd->call_id);
595                                                /* Save the ID for later usage (Cancel/Finish). */
596                                                sd->call_id = g_strdup(id);
597                                                sd->call_out = TRUE;
598                                        } else if (!strcmp(info, "STATUS FAILED")) {
599                                                imcb_error(ic, "Call failed: %s", skype_call_strerror(sd->failurereason));
600                                                sd->call_id = NULL;
601                                        } else if (!strncmp(info, "DURATION ", 9)) {
602                                                if (sd->call_duration)
603                                                        g_free(sd->call_duration);
604                                                sd->call_duration = g_strdup(info+9);
605                                        } else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
606                                                info += 15;
607                                                if (sd->call_status) {
608                                                        switch (sd->call_status) {
609                                                        case SKYPE_CALL_RINGING:
610                                                                if (sd->call_out)
611                                                                        imcb_log(ic, "You are currently ringing the user %s.", info);
612                                                                else {
613                                                                        g_snprintf(buf, 1024, "The user %s is currently ringing you.", info);
614                                                                        skype_call_ask(ic, sd->call_id, buf);
615                                                                }
616                                                                break;
617                                                        case SKYPE_CALL_MISSED:
618                                                                imcb_log(ic, "You have missed a call from user %s.", info);
619                                                                break;
620                                                        case SKYPE_CALL_CANCELLED:
621                                                                imcb_log(ic, "You cancelled the call to the user %s.", info);
622                                                                sd->call_status = 0;
623                                                                sd->call_out = FALSE;
624                                                                break;
625                                                        case SKYPE_CALL_REFUSED:
626                                                                if (sd->call_out)
627                                                                        imcb_log(ic, "The user %s refused the call.", info);
628                                                                else
629                                                                        imcb_log(ic, "You refused the call from user %s.", info);
630                                                                sd->call_out = FALSE;
631                                                                break;
632                                                        case SKYPE_CALL_FINISHED:
633                                                                if (sd->call_duration)
634                                                                        imcb_log(ic, "You finished the call to the user %s (duration: %s seconds).", info, sd->call_duration);
635                                                                else
636                                                                        imcb_log(ic, "You finished the call to the user %s.", info);
637                                                                sd->call_out = FALSE;
638                                                                break;
639                                                        default:
640                                                                /* Don't be noisy, ignore other statuses for now. */
641                                                                break;
642                                                        }
643                                                        sd->call_status = 0;
644                                                }
645                                        }
646                                }
647                        } else if (!strncmp(line, "FILETRANSFER ", 13)) {
648                                char *id = strchr(line, ' ');
649                                if (++id) {
650                                        char *info = strchr(id, ' ');
651                                        *info = '\0';
652                                        info++;
653                                        if (!strcmp(info, "STATUS NEW")) {
654                                                g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id);
655                                                skype_write(ic, buf);
656                                                sd->filetransfer_status = SKYPE_FILETRANSFER_NEW;
657                                        } else if (!strcmp(info, "STATUS FAILED")) {
658                                                g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id);
659                                                skype_write(ic, buf);
660                                                sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED;
661                                        } else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
662                                                info += 15;
663                                                if (sd->filetransfer_status) {
664                                                        switch (sd->filetransfer_status) {
665                                                        case SKYPE_FILETRANSFER_NEW:
666                                                                imcb_log(ic, "The user %s offered a new file for you.", info);
667                                                                break;
668                                                        case SKYPE_FILETRANSFER_FAILED:
669                                                                imcb_log(ic, "Failed to transfer file from user %s.", info);
670                                                                break;
671                                                        }
672                                                        sd->filetransfer_status = 0;
673                                                }
674                                        }
675                                }
676                        } else if (!strncmp(line, "CHAT ", 5)) {
677                                char *id = strchr(line, ' ');
678                                if (++id) {
679                                        char *info = strchr(id, ' ');
680                                        if (info)
681                                                *info = '\0';
682                                        info++;
683                                        /* Remove fake chat if we created one in skype_chat_with() */
684                                        struct groupchat *gc = skype_chat_by_name(ic, "");
685                                        if (gc)
686                                                imcb_chat_free(gc);
687                                        if (!strcmp(info, "STATUS MULTI_SUBSCRIBED")) {
688                                                imcb_chat_new(ic, id);
689                                                g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id);
690                                                skype_write(ic, buf);
691                                                g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id);
692                                                skype_write(ic, buf);
693                                        } else if (!strcmp(info, "STATUS DIALOG") && sd->groupchat_with) {
694                                                gc = imcb_chat_new(ic, id);
695                                                /* According to the docs this
696                                                 * is necessary. However it
697                                                 * does not seem the situation
698                                                 * and it would open an extra
699                                                 * window on our client, so
700                                                 * just leave it out. */
701                                                /*g_snprintf(buf, 1024, "OPEN CHAT %s\n", id);
702                                                skype_write(ic, buf);*/
703                                                g_snprintf(buf, 1024, "%s@skype.com", sd->groupchat_with);
704                                                imcb_chat_add_buddy(gc, buf);
705                                                imcb_chat_add_buddy(gc, sd->username);
706                                                g_free(sd->groupchat_with);
707                                                sd->groupchat_with = NULL;
708                                                g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id);
709                                                skype_write(ic, buf);
710                                                g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id);
711                                                skype_write(ic, buf);
712                                        } else if (!strcmp(info, "STATUS UNSUBSCRIBED")) {
713                                                gc = skype_chat_by_name(ic, id);
714                                                if (gc)
715                                                        gc->data = (void *)FALSE;
716                                        } else if (!strncmp(info, "ADDER ", 6)) {
717                                                info += 6;
718                                                g_free(sd->adder);
719                                                sd->adder = g_strdup_printf("%s@skype.com", info);
720                                        } else if (!strncmp(info, "TOPIC ", 6)) {
721                                                info += 6;
722                                                gc = skype_chat_by_name(ic, id);
723                                                if (gc && (sd->adder || sd->topic_wait)) {
724                                                        if (sd->topic_wait) {
725                                                                sd->adder = g_strdup(sd->username);
726                                                                sd->topic_wait = 0;
727                                                        }
728                                                        imcb_chat_topic(gc, sd->adder, info, 0);
729                                                        g_free(sd->adder);
730                                                        sd->adder = NULL;
731                                                }
732                                        } else if (!strncmp(info, "ACTIVEMEMBERS ", 14)) {
733                                                info += 14;
734                                                gc = skype_chat_by_name(ic, id);
735                                                /* Hack! We set ->data to TRUE
736                                                 * while we're on the channel
737                                                 * so that we won't rejoin
738                                                 * after a /part. */
739                                                if (gc && !gc->data) {
740                                                        char **members = g_strsplit(info, " ", 0);
741                                                        int i;
742                                                        for (i = 0; members[i]; i++) {
743                                                                if (!strcmp(members[i], sd->username))
744                                                                        continue;
745                                                                g_snprintf(buf, 1024, "%s@skype.com", members[i]);
746                                                                if (!g_list_find_custom(gc->in_room, buf, (GCompareFunc)strcmp))
747                                                                        imcb_chat_add_buddy(gc, buf);
748                                                        }
749                                                        imcb_chat_add_buddy(gc, sd->username);
750                                                        g_strfreev(members);
751                                                }
752                                        }
753                                }
754                        } else if (!strncmp(line, "PASSWORD ", 9)) {
755                                if (!strncmp(line+9, "OK", 2))
756                                        imcb_connected(ic);
757                                else {
758                                        imcb_error(ic, "Authentication Failed");
759                                        imc_logout(ic, TRUE);
760                                }
761                        } else if (!strncmp(line, "PROFILE PSTN_BALANCE ", 21))
762                                imcb_log(ic, "SkypeOut balance value is '%s'.", line+21);
763                        else if (!strncmp(line, "PING", 4)) {
764                                g_snprintf(buf, 1024, "PONG\n");
765                                skype_write(ic, buf);
766                        } else if (!strncmp(line, "CHATS ", 6)) {
767                                char **i;
768                                char **chats = g_strsplit(line + 6, ", ", 0);
769
770                                i = chats;
771                                while (*i) {
772                                        g_snprintf(buf, 1024, "GET CHAT %s STATUS\n", *i);
773                                        skype_write(ic, buf);
774                                        g_snprintf(buf, 1024, "GET CHAT %s ACTIVEMEMBERS\n", *i);
775                                        skype_write(ic, buf);
776                                        i++;
777                                }
778                                g_strfreev(chats);
779                        }
780                        lineptr++;
781                }
782                g_strfreev(lines);
783        } else if (st == 0 || (st < 0 && !sockerr_again())) {
784                closesocket(sd->fd);
785                sd->fd = -1;
786
787                imcb_error(ic, "Error while reading from server");
788                imc_logout(ic, TRUE);
789                return FALSE;
790        }
791        return TRUE;
792}
793
794gboolean skype_start_stream(struct im_connection *ic)
795{
796        struct skype_data *sd = ic->proto_data;
797        char *buf;
798        int st;
799
800        if (!sd)
801                return FALSE;
802
803        if (sd->bfd <= 0)
804                sd->bfd = b_input_add(sd->fd, GAIM_INPUT_READ, skype_read_callback, ic);
805
806        /* Log in */
807        buf = g_strdup_printf("USERNAME %s\n", ic->acc->user);
808        st = skype_write(ic, buf);
809        g_free(buf);
810        buf = g_strdup_printf("PASSWORD %s\n", ic->acc->pass);
811        st = skype_write(ic, buf);
812        g_free(buf);
813
814        /* This will download all buddies. */
815        buf = g_strdup_printf("SEARCH FRIENDS\n");
816        st = skype_write(ic, buf);
817        g_free(buf);
818        buf = g_strdup_printf("SET USERSTATUS ONLINE\n");
819        skype_write(ic, buf);
820        g_free(buf);
821
822        /* Auto join to bookmarked chats if requested.*/
823        if (set_getbool(&ic->acc->set, "auto_join")) {
824                buf = g_strdup_printf("SEARCH BOOKMARKEDCHATS\n");
825                skype_write(ic, buf);
826                g_free(buf);
827        }
828        return st;
829}
830
831gboolean skype_connected(gpointer data, void *source, b_input_condition cond)
832{
833        struct im_connection *ic = data;
834        struct skype_data *sd = ic->proto_data;
835        if (!source) {
836                sd->ssl = NULL;
837                imcb_error(ic, "Could not connect to server");
838                imc_logout(ic, TRUE);
839                return FALSE;
840        }
841        imcb_log(ic, "Connected to server, logging in");
842        return skype_start_stream(ic);
843}
844
845static void skype_login(account_t *acc)
846{
847        struct im_connection *ic = imcb_new(acc);
848        struct skype_data *sd = g_new0(struct skype_data, 1);
849
850        ic->proto_data = sd;
851
852        imcb_log(ic, "Connecting");
853        sd->ssl = ssl_connect(set_getstr(&acc->set, "server"), set_getint(&acc->set, "port"), skype_connected, ic);
854        sd->fd = sd->ssl ? ssl_getfd(sd->ssl) : -1;
855        sd->username = g_strdup(acc->user);
856
857        sd->ic = ic;
858
859        if (set_getbool(&acc->set, "skypeconsole"))
860                imcb_add_buddy(ic, "skypeconsole", NULL);
861}
862
863static void skype_logout(struct im_connection *ic)
864{
865        struct skype_data *sd = ic->proto_data;
866        char *buf;
867
868        buf = g_strdup_printf("SET USERSTATUS OFFLINE\n");
869        skype_write(ic, buf);
870        g_free(buf);
871
872        g_free(sd->username);
873        g_free(sd->handle);
874        g_free(sd);
875        ic->proto_data = NULL;
876}
877
878static int skype_buddy_msg(struct im_connection *ic, char *who, char *message, int flags)
879{
880        char *buf, *ptr, *nick;
881        int st;
882
883        nick = g_strdup(who);
884        ptr = strchr(nick, '@');
885        if (ptr)
886                *ptr = '\0';
887
888        if (!strncmp(who, "skypeconsole", 12))
889                buf = g_strdup_printf("%s\n", message);
890        else
891                buf = g_strdup_printf("MESSAGE %s %s\n", nick, message);
892        g_free(nick);
893        st = skype_write(ic, buf);
894        g_free(buf);
895
896        return st;
897}
898
899const struct skype_away_state *skype_away_state_by_name(char *name)
900{
901        int i;
902
903        for (i = 0; skype_away_state_list[i].full_name; i++)
904                if (g_strcasecmp(skype_away_state_list[i].full_name, name) == 0)
905                        return skype_away_state_list + i;
906
907        return NULL;
908}
909
910static void skype_set_away(struct im_connection *ic, char *state_txt, char *message)
911{
912        const struct skype_away_state *state;
913        char *buf;
914
915        if (strcmp(state_txt, GAIM_AWAY_CUSTOM) == 0)
916                state = skype_away_state_by_name("Away");
917        else
918                state = skype_away_state_by_name(state_txt);
919        buf = g_strdup_printf("SET USERSTATUS %s\n", state->code);
920        skype_write(ic, buf);
921        g_free(buf);
922}
923
924static GList *skype_away_states(struct im_connection *ic)
925{
926        static GList *l;
927        int i;
928
929        if (l == NULL)
930                for (i = 0; skype_away_state_list[i].full_name; i++)
931                        l = g_list_append(l, (void *)skype_away_state_list[i].full_name);
932
933        return l;
934}
935
936static char *skype_set_display_name(set_t *set, char *value)
937{
938        account_t *acc = set->data;
939        struct im_connection *ic = acc->ic;
940        char *buf;
941
942        buf = g_strdup_printf("SET PROFILE FULLNAME %s", value);
943        skype_write(ic, buf);
944        g_free(buf);
945        return value;
946}
947
948static char *skype_set_balance(set_t *set, char *value)
949{
950        account_t *acc = set->data;
951        struct im_connection *ic = acc->ic;
952        char *buf;
953
954        buf = g_strdup_printf("GET PROFILE PSTN_BALANCE");
955        skype_write(ic, buf);
956        g_free(buf);
957        return value;
958}
959
960static char *skype_set_call(set_t *set, char *value)
961{
962        account_t *acc = set->data;
963        struct im_connection *ic = acc->ic;
964        struct skype_data *sd = ic->proto_data;
965        char *nick, *ptr, *buf;
966
967        if (value) {
968                user_t *u = user_find(acc->irc, value);
969                /* We are starting a call */
970                if (!u)
971                        nick = g_strdup(value);
972                else
973                        nick = g_strdup(u->handle);
974                ptr = strchr(nick, '@');
975                if (ptr)
976                        *ptr = '\0';
977
978                buf = g_strdup_printf("CALL %s", nick);
979                skype_write(ic, buf);
980                g_free(buf);
981                g_free(nick);
982        } else {
983                /* We are ending a call */
984                if (sd->call_id) {
985                        buf = g_strdup_printf("SET CALL %s STATUS FINISHED", sd->call_id);
986                        skype_write(ic, buf);
987                        g_free(buf);
988                        g_free(sd->call_id);
989                        sd->call_id = NULL;
990                } else
991                        imcb_error(ic, "There are no active calls currently.");
992        }
993        return value;
994}
995
996static void skype_add_buddy(struct im_connection *ic, char *who, char *group)
997{
998        char *buf, *nick, *ptr;
999
1000        nick = g_strdup(who);
1001        ptr = strchr(nick, '@');
1002        if (ptr)
1003                *ptr = '\0';
1004        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 2 Please authorize me\n", nick);
1005        skype_write(ic, buf);
1006        g_free(nick);
1007}
1008
1009static void skype_remove_buddy(struct im_connection *ic, char *who, char *group)
1010{
1011        char *buf, *nick, *ptr;
1012
1013        nick = g_strdup(who);
1014        ptr = strchr(nick, '@');
1015        if (ptr)
1016                *ptr = '\0';
1017        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 1\n", nick);
1018        skype_write(ic, buf);
1019        g_free(nick);
1020}
1021
1022void skype_chat_msg(struct groupchat *gc, char *message, int flags)
1023{
1024        struct im_connection *ic = gc->ic;
1025        char *buf;
1026        buf = g_strdup_printf("CHATMESSAGE %s %s\n", gc->title, message);
1027        skype_write(ic, buf);
1028        g_free(buf);
1029}
1030
1031void skype_chat_leave(struct groupchat *gc)
1032{
1033        struct im_connection *ic = gc->ic;
1034        char *buf;
1035        buf = g_strdup_printf("ALTER CHAT %s LEAVE\n", gc->title);
1036        skype_write(ic, buf);
1037        g_free(buf);
1038        gc->data = (void *)TRUE;
1039}
1040
1041void skype_chat_invite(struct groupchat *gc, char *who, char *message)
1042{
1043        struct im_connection *ic = gc->ic;
1044        char *buf, *ptr, *nick;
1045        nick = g_strdup(message);
1046        ptr = strchr(nick, '@');
1047        if (ptr)
1048                *ptr = '\0';
1049        buf = g_strdup_printf("ALTER CHAT %s ADDMEMBERS %s\n", gc->title, nick);
1050        skype_write(ic, buf);
1051        g_free(buf);
1052        g_free(nick);
1053}
1054
1055void skype_chat_topic(struct groupchat *gc, char *message)
1056{
1057        struct im_connection *ic = gc->ic;
1058        struct skype_data *sd = ic->proto_data;
1059        char *buf;
1060        buf = g_strdup_printf("ALTER CHAT %s SETTOPIC %s\n", gc->title, message);
1061        skype_write(ic, buf);
1062        g_free(buf);
1063        sd->topic_wait = 1;
1064}
1065
1066struct groupchat *skype_chat_with(struct im_connection *ic, char *who)
1067{
1068        struct skype_data *sd = ic->proto_data;
1069        char *ptr, *nick, *buf;
1070        nick = g_strdup(who);
1071        ptr = strchr(nick, '@');
1072        if (ptr)
1073                *ptr = '\0';
1074        buf = g_strdup_printf("CHAT CREATE %s\n", nick);
1075        skype_write(ic, buf);
1076        g_free(buf);
1077        sd->groupchat_with = g_strdup(nick);
1078        g_free(nick);
1079        /* We create a fake chat for now. We will replace it with a real one in
1080         * the real callback. */
1081        return imcb_chat_new(ic, "");
1082}
1083
1084static void skype_get_info(struct im_connection *ic, char *who)
1085{
1086        char *ptr, *nick, *buf;
1087        nick = g_strdup(who);
1088        ptr = strchr(nick, '@');
1089        if (ptr)
1090                *ptr = '\0';
1091        buf = g_strdup_printf("GET USER %s FULLNAME\n", nick);
1092        skype_write(ic, buf);
1093        g_free(buf);
1094        buf = g_strdup_printf("GET USER %s PHONE_HOME\n", nick);
1095        skype_write(ic, buf);
1096        g_free(buf);
1097        buf = g_strdup_printf("GET USER %s PHONE_OFFICE\n", nick);
1098        skype_write(ic, buf);
1099        g_free(buf);
1100        buf = g_strdup_printf("GET USER %s PHONE_MOBILE\n", nick);
1101        skype_write(ic, buf);
1102        g_free(buf);
1103        buf = g_strdup_printf("GET USER %s NROF_AUTHED_BUDDIES\n", nick);
1104        skype_write(ic, buf);
1105        g_free(buf);
1106        buf = g_strdup_printf("GET USER %s TIMEZONE\n", nick);
1107        skype_write(ic, buf);
1108        g_free(buf);
1109        buf = g_strdup_printf("GET USER %s LASTONLINETIMESTAMP\n", nick);
1110        skype_write(ic, buf);
1111        g_free(buf);
1112        buf = g_strdup_printf("GET USER %s BIRTHDAY\n", nick);
1113        skype_write(ic, buf);
1114        g_free(buf);
1115        buf = g_strdup_printf("GET USER %s SEX\n", nick);
1116        skype_write(ic, buf);
1117        g_free(buf);
1118        buf = g_strdup_printf("GET USER %s LANGUAGE\n", nick);
1119        skype_write(ic, buf);
1120        g_free(buf);
1121        buf = g_strdup_printf("GET USER %s COUNTRY\n", nick);
1122        skype_write(ic, buf);
1123        g_free(buf);
1124        buf = g_strdup_printf("GET USER %s PROVINCE\n", nick);
1125        skype_write(ic, buf);
1126        g_free(buf);
1127        buf = g_strdup_printf("GET USER %s CITY\n", nick);
1128        skype_write(ic, buf);
1129        g_free(buf);
1130        buf = g_strdup_printf("GET USER %s HOMEPAGE\n", nick);
1131        skype_write(ic, buf);
1132        g_free(buf);
1133        buf = g_strdup_printf("GET USER %s ABOUT\n", nick);
1134        skype_write(ic, buf);
1135        g_free(buf);
1136}
1137
1138static void skype_set_my_name(struct im_connection *ic, char *info)
1139{
1140        skype_set_display_name(set_find(&ic->acc->set, "display_name"), info);
1141}
1142
1143static void skype_init(account_t *acc)
1144{
1145        set_t *s;
1146
1147        s = set_add(&acc->set, "server", SKYPE_DEFAULT_SERVER, set_eval_account, acc);
1148        s->flags |= ACC_SET_OFFLINE_ONLY;
1149
1150        s = set_add(&acc->set, "port", SKYPE_DEFAULT_PORT, set_eval_int, acc);
1151        s->flags |= ACC_SET_OFFLINE_ONLY;
1152
1153        s = set_add(&acc->set, "display_name", NULL, skype_set_display_name, acc);
1154        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
1155
1156        s = set_add(&acc->set, "call", NULL, skype_set_call, acc);
1157        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
1158
1159        s = set_add(&acc->set, "balance", NULL, skype_set_balance, acc);
1160        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
1161
1162        s = set_add(&acc->set, "skypeout_offline", "true", set_eval_bool, acc);
1163
1164        s = set_add(&acc->set, "skypeconsole", "false", set_eval_bool, acc);
1165        s->flags |= ACC_SET_OFFLINE_ONLY;
1166
1167        s = set_add(&acc->set, "skypeconsole_receive", "false", set_eval_bool, acc);
1168        s->flags |= ACC_SET_OFFLINE_ONLY;
1169
1170        s = set_add(&acc->set, "auto_join", "false", set_eval_bool, acc);
1171        s->flags |= ACC_SET_OFFLINE_ONLY;
1172}
1173
1174void init_plugin(void)
1175{
1176        struct prpl *ret = g_new0(struct prpl, 1);
1177
1178        ret->name = "skype";
1179        ret->login = skype_login;
1180        ret->init = skype_init;
1181        ret->logout = skype_logout;
1182        ret->buddy_msg = skype_buddy_msg;
1183        ret->get_info = skype_get_info;
1184        ret->set_my_name = skype_set_my_name;
1185        ret->away_states = skype_away_states;
1186        ret->set_away = skype_set_away;
1187        ret->add_buddy = skype_add_buddy;
1188        ret->remove_buddy = skype_remove_buddy;
1189        ret->chat_msg = skype_chat_msg;
1190        ret->chat_leave = skype_chat_leave;
1191        ret->chat_invite = skype_chat_invite;
1192        ret->chat_with = skype_chat_with;
1193        ret->handle_cmp = g_strcasecmp;
1194        ret->chat_topic = skype_chat_topic;
1195        register_protocol(ret);
1196}
1197
1198/* vim: set ts=2 sw=2 noet: */
Note: See TracBrowser for help on using the repository browser.