source: protocols/skype/skype.c @ 58b65b33

Last change on this file since 58b65b33 was 58b65b33, checked in by Miklos Vajna <vmiklos@…>, at 2012-04-12T21:27:54Z

skype: use ssl_disconnect instead of closesocket

Also call ssl_disconnect on logout.

Patch-by: meh on IRC.

  • Property mode set to 100644
File size: 40.5 KB
RevLine 
[7daec06]1/*
2 *  skype.c - Skype plugin for BitlBee
[5adcc65]3 *
[85341dd]4 *  Copyright (c) 2007, 2008, 2009, 2010, 2011, 2012 by Miklos Vajna <vmiklos@frugalware.org>
[f06e3ac]5 *
[7daec06]6 *  This program is free software; you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation; either version 2 of the License, or
9 *  (at your option) any later version.
10 *
11 *  This program is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with this program; if not, write to the Free Software
[5adcc65]18 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
[7daec06]19 *  USA.
[f06e3ac]20 */
[7daec06]21
[f9c3e7b]22#define _XOPEN_SOURCE
[1f4fc80]23#define _BSD_SOURCE
[ed2e37f]24#include <poll.h>
[ff18fc1]25#include <stdio.h>
[f06e3ac]26#include <bitlbee.h>
[7f41495]27#include <ssl_client.h>
[f06e3ac]28
[f5aedd91]29#define SKYPE_DEFAULT_SERVER "localhost"
[4bbd9db]30#define SKYPE_DEFAULT_PORT "2727"
[c213d6b]31#define IRC_LINE_SIZE 1024
[ff436ba]32#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
[f06e3ac]33
[bbba374]34/*
35 * Enumerations
36 */
37
[359f4d9]38enum {
[df9255d]39        SKYPE_CALL_RINGING = 1,
[9db0234]40        SKYPE_CALL_MISSED,
[2eb4b1f]41        SKYPE_CALL_CANCELLED,
[acd9478]42        SKYPE_CALL_FINISHED,
[d87daf3]43        SKYPE_CALL_REFUSED
[5365f84]44};
[bbba374]45
[359f4d9]46enum {
[df9255d]47        SKYPE_FILETRANSFER_NEW = 1,
48        SKYPE_FILETRANSFER_FAILED
[5365f84]49};
[df9255d]50
[7daec06]51/*
52 * Structures
53 */
54
[5adcc65]55struct skype_data {
[f06e3ac]56        struct im_connection *ic;
[a3d6427]57        char *username;
[368861e]58        /* The effective file descriptor. We store it here so any function can
59         * write() to it. */
[f06e3ac]60        int fd;
[368861e]61        /* File descriptor returned by bitlbee. we store it so we know when
62         * we're connected and when we aren't. */
63        int bfd;
[c7304b2]64        /* ssl_getfd() uses this to get the file desciptor. */
65        void *ssl;
[2a0f99c]66        /* When we receive a new message id, we query the properties, finally
67         * the chatname. Store the properties here so that we can use
[c81d0ef]68         * imcb_buddy_msg() when we got the chatname. */
[77c1abe]69        char *handle;
[a7af5f0]70        /* List, because of multiline messages. */
71        GList *body;
[2a0f99c]72        char *type;
[bbba374]73        /* This is necessary because we send a notification when we get the
74         * handle. So we store the state here and then we can send a
75         * notification about the handle is in a given status. */
[359f4d9]76        int call_status;
[2eb4b1f]77        char *call_id;
[48181f0]78        char *call_duration;
[d87daf3]79        /* If the call is outgoing or not */
80        int call_out;
[df9255d]81        /* Same for file transfers. */
[359f4d9]82        int filetransfer_status;
[86278cd]83        /* Using /j #nick we want to have a groupchat with two people. Usually
84         * not (default). */
[5adcc65]85        char *groupchat_with;
[f8674db]86        /* The user who invited us to the chat. */
[5adcc65]87        char *adder;
[a5f76a2]88        /* If we are waiting for a confirmation about we changed the topic. */
89        int topic_wait;
[67454bd]90        /* These are used by the info command. */
91        char *info_fullname;
92        char *info_phonehome;
93        char *info_phoneoffice;
94        char *info_phonemobile;
95        char *info_nrbuddies;
96        char *info_tz;
97        char *info_seen;
98        char *info_birthday;
99        char *info_sex;
100        char *info_language;
101        char *info_country;
102        char *info_province;
103        char *info_city;
104        char *info_homepage;
105        char *info_about;
[b054fad]106        /* When a call fails, we get the reason and later we get the failure
107         * event, so store the failure code here till then */
108        int failurereason;
[d9ce18c]109        /* If this is just an update of an already received message. */
110        int is_edit;
[89d6845]111        /* List of struct skype_group* */
112        GList *groups;
[46641bf]113        /* Pending user which has to be added to the next group which is
114         * created. */
115        char *pending_user;
[f06e3ac]116};
117
[5adcc65]118struct skype_away_state {
[adce2de]119        char *code;
120        char *full_name;
121};
122
[5adcc65]123struct skype_buddy_ask_data {
[7daec06]124        struct im_connection *ic;
[e0074cb]125        /* This is also used for call IDs for simplicity */
[7daec06]126        char *handle;
127};
128
[89d6845]129struct skype_group {
130        int id;
131        char *name;
132        GList *users;
133};
134
[7daec06]135/*
136 * Tables
137 */
138
[5adcc65]139const struct skype_away_state skype_away_state_list[] = {
[bc744df]140        { "AWAY", "Away" },
141        { "NA", "Not available" },
142        { "DND", "Do Not Disturb" },
143        { "INVISIBLE", "Invisible" },
144        { "OFFLINE", "Offline" },
[4b740c2]145        { "SKYPEME", "Skype Me" },
146        { "ONLINE", "Online" },
[23411c6]147        { NULL, NULL}
[adce2de]148};
149
[7daec06]150/*
151 * Functions
152 */
[d3cbd17]153
[7c300bb]154int skype_write(struct im_connection *ic, char *buf, int len)
[f06e3ac]155{
156        struct skype_data *sd = ic->proto_data;
[1fb89e3]157        struct pollfd pfd[1];
158
[e8e2892]159        if (!sd->ssl)
160                return FALSE;
161
[1fb89e3]162        pfd[0].fd = sd->fd;
163        pfd[0].events = POLLOUT;
[f06e3ac]164
[7daec06]165        /* This poll is necessary or we'll get a SIGPIPE when we write() to
166         * sd->fd. */
[1fb89e3]167        poll(pfd, 1, 1000);
[5adcc65]168        if (pfd[0].revents & POLLHUP) {
169                imc_logout(ic, TRUE);
[1fb89e3]170                return FALSE;
171        }
[5adcc65]172        ssl_write(sd->ssl, buf, len);
[f06e3ac]173
[9fd4241]174        return TRUE;
[f06e3ac]175}
176
[1f4fc80]177int skype_printf(struct im_connection *ic, char *fmt, ...)
178{
179        va_list args;
180        char str[IRC_LINE_SIZE];
[5d9db76]181
[1f4fc80]182        va_start(args, fmt);
183        vsnprintf(str, IRC_LINE_SIZE, fmt, args);
184        va_end(args);
185
[7c300bb]186        return skype_write(ic, str, strlen(str));
[1f4fc80]187}
188
[5adcc65]189static void skype_buddy_ask_yes(void *data)
[d3cbd17]190{
[039116a]191        struct skype_buddy_ask_data *bla = data;
[1f4fc80]192        skype_printf(bla->ic, "SET USER %s ISAUTHORIZED TRUE",
[8c09bb3]193                bla->handle);
[d3cbd17]194        g_free(bla->handle);
195        g_free(bla);
196}
197
[5adcc65]198static void skype_buddy_ask_no(void *data)
[d3cbd17]199{
[039116a]200        struct skype_buddy_ask_data *bla = data;
[1f4fc80]201        skype_printf(bla->ic, "SET USER %s ISAUTHORIZED FALSE",
[8c09bb3]202                bla->handle);
[d3cbd17]203        g_free(bla->handle);
204        g_free(bla);
205}
206
[5adcc65]207void skype_buddy_ask(struct im_connection *ic, char *handle, char *message)
[d3cbd17]208{
[8c09bb3]209        struct skype_buddy_ask_data *bla = g_new0(struct skype_buddy_ask_data,
210                1);
[d3cbd17]211        char *buf;
212
213        bla->ic = ic;
214        bla->handle = g_strdup(handle);
215
[8c09bb3]216        buf = g_strdup_printf("The user %s wants to add you to "
217                "his/her buddy list, saying: '%s'.", handle, message);
[5adcc65]218        imcb_ask(ic, buf, bla, skype_buddy_ask_yes, skype_buddy_ask_no);
219        g_free(buf);
[d3cbd17]220}
221
[5adcc65]222static void skype_call_ask_yes(void *data)
[e0074cb]223{
[039116a]224        struct skype_buddy_ask_data *bla = data;
[1f4fc80]225        skype_printf(bla->ic, "SET CALL %s STATUS INPROGRESS",
[8c09bb3]226                bla->handle);
[e0074cb]227        g_free(bla->handle);
228        g_free(bla);
229}
230
[5adcc65]231static void skype_call_ask_no(void *data)
[e0074cb]232{
[039116a]233        struct skype_buddy_ask_data *bla = data;
[1f4fc80]234        skype_printf(bla->ic, "SET CALL %s STATUS FINISHED",
[8c09bb3]235                bla->handle);
[e0074cb]236        g_free(bla->handle);
237        g_free(bla);
238}
239
[5adcc65]240void skype_call_ask(struct im_connection *ic, char *call_id, char *message)
[e0074cb]241{
[8c09bb3]242        struct skype_buddy_ask_data *bla = g_new0(struct skype_buddy_ask_data,
243                1);
[e0074cb]244
245        bla->ic = ic;
246        bla->handle = g_strdup(call_id);
247
[5adcc65]248        imcb_ask(ic, message, bla, skype_call_ask_yes, skype_call_ask_no);
[e0074cb]249}
[72aa7f0]250
[b054fad]251static char *skype_call_strerror(int err)
252{
[5adcc65]253        switch (err) {
254        case 1:
255                return "Miscellaneous error";
256        case 2:
257                return "User or phone number does not exist.";
258        case 3:
259                return "User is offline";
260        case 4:
261                return "No proxy found";
262        case 5:
263                return "Session terminated.";
264        case 6:
265                return "No common codec found.";
266        case 7:
267                return "Sound I/O error.";
268        case 8:
269                return "Problem with remote sound device.";
270        case 9:
271                return "Call blocked by recipient.";
272        case 10:
273                return "Recipient not a friend.";
274        case 11:
275                return "Current user not authorized by recipient.";
276        case 12:
277                return "Sound recording error.";
278        default:
279                return "Unknown error";
[b054fad]280        }
281}
282
[54ca269]283static char *skype_group_by_username(struct im_connection *ic, char *username)
284{
285        struct skype_data *sd = ic->proto_data;
286        int i, j;
287
288        /* NEEDSWORK: we just search for the first group of the user, multiple
289         * groups / user is not yet supported by BitlBee. */
290
291        for (i = 0; i < g_list_length(sd->groups); i++) {
292                struct skype_group *sg = g_list_nth_data(sd->groups, i);
293                for (j = 0; j < g_list_length(sg->users); j++) {
294                        if (!strcmp(g_list_nth_data(sg->users, j), username))
295                                return sg->name;
296                }
297        }
298        return NULL;
299}
300
[46e9822]301static struct skype_group *skype_group_by_name(struct im_connection *ic, char *name)
302{
303        struct skype_data *sd = ic->proto_data;
304        int i;
305
306        for (i = 0; i < g_list_length(sd->groups); i++) {
307                struct skype_group *sg = g_list_nth_data(sd->groups, i);
308                if (!strcmp(sg->name, name))
309                        return sg;
310        }
311        return NULL;
312}
313
[078b0b9]314static void skype_parse_users(struct im_connection *ic, char *line)
315{
[1f4fc80]316        char **i, **nicks;
[078b0b9]317
318        nicks = g_strsplit(line + 6, ", ", 0);
[1f4fc80]319        for (i = nicks; *i; i++)
320                skype_printf(ic, "GET USER %s ONLINESTATUS\n", *i);
[078b0b9]321        g_strfreev(nicks);
322}
323
[6e14204]324static void skype_parse_user(struct im_connection *ic, char *line)
325{
326        int flags = 0;
327        char *ptr;
328        struct skype_data *sd = ic->proto_data;
329        char *user = strchr(line, ' ');
330        char *status = strrchr(line, ' ');
331
332        status++;
333        ptr = strchr(++user, ' ');
334        if (!ptr)
335                return;
336        *ptr = '\0';
337        ptr++;
[49a3c02]338        if (!strncmp(ptr, "ONLINESTATUS ", 13)) {
339                        if (!strcmp(user, sd->username))
340                                return;
341                        if (!set_getbool(&ic->acc->set, "test_join")
342                                && !strcmp(user, "echo123"))
343                                return;
[6e14204]344                ptr = g_strdup_printf("%s@skype.com", user);
[54ca269]345                imcb_add_buddy(ic, ptr, skype_group_by_username(ic, user));
[62f51ee9]346                if (strcmp(status, "OFFLINE") && (strcmp(status, "SKYPEOUT") ||
347                        !set_getbool(&ic->acc->set, "skypeout_offline")))
[6e14204]348                        flags |= OPT_LOGGED_IN;
[62f51ee9]349                if (strcmp(status, "ONLINE") && strcmp(status, "SKYPEME"))
[6e14204]350                        flags |= OPT_AWAY;
351                imcb_buddy_status(ic, ptr, flags, NULL, NULL);
352                g_free(ptr);
353        } else if (!strncmp(ptr, "RECEIVEDAUTHREQUEST ", 20)) {
354                char *message = ptr + 20;
355                if (strlen(message))
356                        skype_buddy_ask(ic, user, message);
357        } else if (!strncmp(ptr, "BUDDYSTATUS ", 12)) {
358                char *st = ptr + 12;
359                if (!strcmp(st, "3")) {
360                        char *buf = g_strdup_printf("%s@skype.com", user);
[54ca269]361                        imcb_add_buddy(ic, buf, skype_group_by_username(ic, user));
[6e14204]362                        g_free(buf);
363                }
[78d22cd0]364        } else if (!strncmp(ptr, "MOOD_TEXT ", 10)) {
[4c674bb]365                char *buf = g_strdup_printf("%s@skype.com", user);
[78d22cd0]366                bee_user_t *bu = bee_user_by_handle(ic->bee, ic, buf);
367                g_free(buf);
368                buf = ptr + 10;
[451f121]369                if (bu)
[78d22cd0]370                        imcb_buddy_status(ic, bu->handle, bu->flags, NULL,
[4ab7225]371                                        *buf ? buf : NULL);
[78d22cd0]372                if (set_getbool(&ic->acc->set, "show_moods"))
373                        imcb_log(ic, "User `%s' changed mood text to `%s'", user, buf);
[3518933]374        } else if (!strncmp(ptr, "FULLNAME ", 9))
[d7938f9]375                sd->info_fullname = g_strdup(ptr + 9);
[6e14204]376        else if (!strncmp(ptr, "PHONE_HOME ", 11))
[d7938f9]377                sd->info_phonehome = g_strdup(ptr + 11);
[6e14204]378        else if (!strncmp(ptr, "PHONE_OFFICE ", 13))
[d7938f9]379                sd->info_phoneoffice = g_strdup(ptr + 13);
[6e14204]380        else if (!strncmp(ptr, "PHONE_MOBILE ", 13))
[d7938f9]381                sd->info_phonemobile = g_strdup(ptr + 13);
[6e14204]382        else if (!strncmp(ptr, "NROF_AUTHED_BUDDIES ", 20))
[d7938f9]383                sd->info_nrbuddies = g_strdup(ptr + 20);
[6e14204]384        else if (!strncmp(ptr, "TIMEZONE ", 9))
[d7938f9]385                sd->info_tz = g_strdup(ptr + 9);
[6e14204]386        else if (!strncmp(ptr, "LASTONLINETIMESTAMP ", 20))
[d7938f9]387                sd->info_seen = g_strdup(ptr + 20);
[6e14204]388        else if (!strncmp(ptr, "SEX ", 4))
[d7938f9]389                sd->info_sex = g_strdup(ptr + 4);
[6e14204]390        else if (!strncmp(ptr, "LANGUAGE ", 9))
[d7938f9]391                sd->info_language = g_strdup(ptr + 9);
[6e14204]392        else if (!strncmp(ptr, "COUNTRY ", 8))
[d7938f9]393                sd->info_country = g_strdup(ptr + 8);
[6e14204]394        else if (!strncmp(ptr, "PROVINCE ", 9))
[d7938f9]395                sd->info_province = g_strdup(ptr + 9);
[6e14204]396        else if (!strncmp(ptr, "CITY ", 5))
[d7938f9]397                sd->info_city = g_strdup(ptr + 5);
[6e14204]398        else if (!strncmp(ptr, "HOMEPAGE ", 9))
[d7938f9]399                sd->info_homepage = g_strdup(ptr + 9);
[6e14204]400        else if (!strncmp(ptr, "ABOUT ", 6)) {
[85341dd]401                /* Support multiple about lines. */
402                if (!sd->info_about)
403                        sd->info_about = g_strdup(ptr + 6);
404                else {
405                        GString *st = g_string_new(sd->info_about);
406                        g_string_append_printf(st, "\n%s", ptr + 6);
407                        g_free(sd->info_about);
408                        sd->info_about = g_strdup(st->str);
409                        g_string_free(st, TRUE);
410                }
411        }
412        else if (!strncmp(ptr, "BIRTHDAY ", 9)) {
413                sd->info_birthday = g_strdup(ptr + 9);
[6e14204]414
415                GString *st = g_string_new("Contact Information\n");
416                g_string_append_printf(st, "Skype Name: %s\n", user);
417                if (sd->info_fullname) {
418                        if (strlen(sd->info_fullname))
[62f51ee9]419                                g_string_append_printf(st, "Full Name: %s\n",
420                                        sd->info_fullname);
[6e14204]421                        g_free(sd->info_fullname);
[7c2daf5f]422                        sd->info_fullname = NULL;
[6e14204]423                }
424                if (sd->info_phonehome) {
425                        if (strlen(sd->info_phonehome))
[62f51ee9]426                                g_string_append_printf(st, "Home Phone: %s\n",
427                                        sd->info_phonehome);
[6e14204]428                        g_free(sd->info_phonehome);
[7c2daf5f]429                        sd->info_phonehome = NULL;
[6e14204]430                }
431                if (sd->info_phoneoffice) {
432                        if (strlen(sd->info_phoneoffice))
[62f51ee9]433                                g_string_append_printf(st, "Office Phone: %s\n",
434                                        sd->info_phoneoffice);
[6e14204]435                        g_free(sd->info_phoneoffice);
[7c2daf5f]436                        sd->info_phoneoffice = NULL;
[6e14204]437                }
438                if (sd->info_phonemobile) {
439                        if (strlen(sd->info_phonemobile))
[62f51ee9]440                                g_string_append_printf(st, "Mobile Phone: %s\n",
441                                        sd->info_phonemobile);
[6e14204]442                        g_free(sd->info_phonemobile);
[7c2daf5f]443                        sd->info_phonemobile = NULL;
[6e14204]444                }
445                g_string_append_printf(st, "Personal Information\n");
446                if (sd->info_nrbuddies) {
447                        if (strlen(sd->info_nrbuddies))
[62f51ee9]448                                g_string_append_printf(st,
449                                        "Contacts: %s\n", sd->info_nrbuddies);
[6e14204]450                        g_free(sd->info_nrbuddies);
[7c2daf5f]451                        sd->info_nrbuddies = NULL;
[6e14204]452                }
453                if (sd->info_tz) {
454                        if (strlen(sd->info_tz)) {
455                                char ib[256];
456                                time_t t = time(NULL);
457                                t += atoi(sd->info_tz)-(60*60*24);
458                                struct tm *gt = gmtime(&t);
459                                strftime(ib, 256, "%H:%M:%S", gt);
[62f51ee9]460                                g_string_append_printf(st,
461                                        "Local Time: %s\n", ib);
[6e14204]462                        }
463                        g_free(sd->info_tz);
[7c2daf5f]464                        sd->info_tz = NULL;
[6e14204]465                }
466                if (sd->info_seen) {
467                        if (strlen(sd->info_seen)) {
468                                char ib[256];
469                                time_t it = atoi(sd->info_seen);
470                                struct tm *tm = localtime(&it);
471                                strftime(ib, 256, ("%Y. %m. %d. %H:%M"), tm);
[62f51ee9]472                                g_string_append_printf(st,
473                                        "Last Seen: %s\n", ib);
[6e14204]474                        }
475                        g_free(sd->info_seen);
[7c2daf5f]476                        sd->info_seen = NULL;
[6e14204]477                }
478                if (sd->info_birthday) {
[62f51ee9]479                        if (strlen(sd->info_birthday) &&
480                                strcmp(sd->info_birthday, "0")) {
[6e14204]481                                char ib[256];
482                                struct tm tm;
483                                strptime(sd->info_birthday, "%Y%m%d", &tm);
484                                strftime(ib, 256, "%B %d, %Y", &tm);
[62f51ee9]485                                g_string_append_printf(st,
486                                        "Birthday: %s\n", ib);
[6e14204]487
488                                strftime(ib, 256, "%Y", &tm);
489                                int year = atoi(ib);
490                                time_t t = time(NULL);
491                                struct tm *lt = localtime(&t);
[62f51ee9]492                                g_string_append_printf(st,
493                                        "Age: %d\n", lt->tm_year+1900-year);
[6e14204]494                        }
495                        g_free(sd->info_birthday);
[7c2daf5f]496                        sd->info_birthday = NULL;
[6e14204]497                }
498                if (sd->info_sex) {
499                        if (strlen(sd->info_sex)) {
500                                char *iptr = sd->info_sex;
501                                while (*iptr++)
502                                        *iptr = tolower(*iptr);
[62f51ee9]503                                g_string_append_printf(st,
504                                        "Gender: %s\n", sd->info_sex);
[6e14204]505                        }
506                        g_free(sd->info_sex);
[7c2daf5f]507                        sd->info_sex = NULL;
[6e14204]508                }
509                if (sd->info_language) {
510                        if (strlen(sd->info_language)) {
511                                char *iptr = strchr(sd->info_language, ' ');
512                                if (iptr)
513                                        iptr++;
514                                else
515                                        iptr = sd->info_language;
[62f51ee9]516                                g_string_append_printf(st,
517                                        "Language: %s\n", iptr);
[6e14204]518                        }
519                        g_free(sd->info_language);
[7c2daf5f]520                        sd->info_language = NULL;
[6e14204]521                }
522                if (sd->info_country) {
523                        if (strlen(sd->info_country)) {
524                                char *iptr = strchr(sd->info_country, ' ');
525                                if (iptr)
526                                        iptr++;
527                                else
528                                        iptr = sd->info_country;
[62f51ee9]529                                g_string_append_printf(st,
530                                        "Country: %s\n", iptr);
[6e14204]531                        }
532                        g_free(sd->info_country);
[7c2daf5f]533                        sd->info_country = NULL;
[6e14204]534                }
535                if (sd->info_province) {
536                        if (strlen(sd->info_province))
[62f51ee9]537                                g_string_append_printf(st,
538                                        "Region: %s\n", sd->info_province);
[6e14204]539                        g_free(sd->info_province);
[7c2daf5f]540                        sd->info_province = NULL;
[6e14204]541                }
542                if (sd->info_city) {
543                        if (strlen(sd->info_city))
[62f51ee9]544                                g_string_append_printf(st,
545                                        "City: %s\n", sd->info_city);
[6e14204]546                        g_free(sd->info_city);
[7c2daf5f]547                        sd->info_city = NULL;
[6e14204]548                }
549                if (sd->info_homepage) {
550                        if (strlen(sd->info_homepage))
[62f51ee9]551                                g_string_append_printf(st,
552                                        "Homepage: %s\n", sd->info_homepage);
[6e14204]553                        g_free(sd->info_homepage);
[7c2daf5f]554                        sd->info_homepage = NULL;
[6e14204]555                }
556                if (sd->info_about) {
557                        if (strlen(sd->info_about))
[62f51ee9]558                                g_string_append_printf(st, "%s\n",
559                                        sd->info_about);
[6e14204]560                        g_free(sd->info_about);
[7c2daf5f]561                        sd->info_about = NULL;
[6e14204]562                }
563                imcb_log(ic, "%s", st->str);
564                g_string_free(st, TRUE);
565        }
566}
567
568static void skype_parse_chatmessage(struct im_connection *ic, char *line)
569{
570        struct skype_data *sd = ic->proto_data;
[c213d6b]571        char buf[IRC_LINE_SIZE];
[6e14204]572        char *id = strchr(line, ' ');
573
[6b9d22a]574        if (!++id)
575                return;
576        char *info = strchr(id, ' ');
577
578        if (!info)
579                return;
580        *info = '\0';
581        info++;
[7825f58]582        if (!strcmp(info, "STATUS RECEIVED") || !strncmp(info, "EDITED_TIMESTAMP", 16)) {
[6b9d22a]583                /* New message ID:
584                 * (1) Request its from field
585                 * (2) Request its body
586                 * (3) Request its type
587                 * (4) Query chatname
588                 */
[1f4fc80]589                skype_printf(ic, "GET CHATMESSAGE %s FROM_HANDLE\n", id);
[d1d5b34]590                if (!strcmp(info, "STATUS RECEIVED"))
591                        skype_printf(ic, "GET CHATMESSAGE %s BODY\n", id);
[d9ce18c]592                else
593                        sd->is_edit = 1;
[1f4fc80]594                skype_printf(ic, "GET CHATMESSAGE %s TYPE\n", id);
595                skype_printf(ic, "GET CHATMESSAGE %s CHATNAME\n", id);
[6b9d22a]596        } else if (!strncmp(info, "FROM_HANDLE ", 12)) {
597                info += 12;
598                /* New from field value. Store
599                 * it, then we can later use it
600                 * when we got the message's
601                 * body. */
602                g_free(sd->handle);
603                sd->handle = g_strdup_printf("%s@skype.com", info);
604        } else if (!strncmp(info, "EDITED_BY ", 10)) {
605                info += 10;
606                /* This is the same as
607                 * FROM_HANDLE, except that we
608                 * never request these lines
609                 * from Skype, we just get
610                 * them. */
611                g_free(sd->handle);
612                sd->handle = g_strdup_printf("%s@skype.com", info);
613        } else if (!strncmp(info, "BODY ", 5)) {
614                info += 5;
615                sd->body = g_list_append(sd->body, g_strdup(info));
616        }       else if (!strncmp(info, "TYPE ", 5)) {
617                info += 5;
618                g_free(sd->type);
619                sd->type = g_strdup(info);
[7825f58]620        } else if (!strncmp(info, "CHATNAME ", 9)) {
621                info += 9;
622                if (sd->handle && sd->body && sd->type) {
[451f121]623                        struct groupchat *gc = bee_chat_by_title(ic->bee, ic, info);
[7825f58]624                        int i;
625                        for (i = 0; i < g_list_length(sd->body); i++) {
626                                char *body = g_list_nth_data(sd->body, i);
627                                if (!strcmp(sd->type, "SAID") ||
628                                        !strcmp(sd->type, "EMOTED")) {
629                                        if (!strcmp(sd->type, "SAID")) {
630                                                if (!sd->is_edit)
631                                                        g_snprintf(buf, IRC_LINE_SIZE, "%s",
632                                                                body);
633                                                else {
634                                                        g_snprintf(buf, IRC_LINE_SIZE, "%s %s",
635                                                                set_getstr(&ic->acc->set, "edit_prefix"),
636                                                                body);
637                                                        sd->is_edit = 0;
638                                                }
639                                        } else
640                                                g_snprintf(buf, IRC_LINE_SIZE, "/me %s",
641                                                        body);
642                                        if (!gc)
643                                                /* Private message */
644                                                imcb_buddy_msg(ic,
645                                                        sd->handle, buf, 0, 0);
646                                        else
647                                                /* Groupchat message */
648                                                imcb_chat_msg(gc,
649                                                        sd->handle, buf, 0, 0);
650                                } else if (!strcmp(sd->type, "SETTOPIC") && gc)
651                                        imcb_chat_topic(gc,
652                                                sd->handle, body, 0);
653                                else if (!strcmp(sd->type, "LEFT") && gc)
654                                        imcb_chat_remove_buddy(gc,
655                                                sd->handle, NULL);
[6e14204]656                        }
[7825f58]657                        g_list_free(sd->body);
658                        sd->body = NULL;
659                }
[6e14204]660        }
661}
662
[9f2f25f]663static void skype_parse_call(struct im_connection *ic, char *line)
664{
665        struct skype_data *sd = ic->proto_data;
666        char *id = strchr(line, ' ');
[c213d6b]667        char buf[IRC_LINE_SIZE];
[9f2f25f]668
[6b9d22a]669        if (!++id)
670                return;
671        char *info = strchr(id, ' ');
672
673        if (!info)
674                return;
675        *info = '\0';
676        info++;
677        if (!strncmp(info, "FAILUREREASON ", 14))
678                sd->failurereason = atoi(strchr(info, ' '));
679        else if (!strcmp(info, "STATUS RINGING")) {
680                if (sd->call_id)
681                        g_free(sd->call_id);
682                sd->call_id = g_strdup(id);
[1f4fc80]683                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
[6b9d22a]684                sd->call_status = SKYPE_CALL_RINGING;
685        } else if (!strcmp(info, "STATUS MISSED")) {
[1f4fc80]686                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
[6b9d22a]687                sd->call_status = SKYPE_CALL_MISSED;
688        } else if (!strcmp(info, "STATUS CANCELLED")) {
[1f4fc80]689                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
[6b9d22a]690                sd->call_status = SKYPE_CALL_CANCELLED;
691        } else if (!strcmp(info, "STATUS FINISHED")) {
[1f4fc80]692                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
[6b9d22a]693                sd->call_status = SKYPE_CALL_FINISHED;
694        } else if (!strcmp(info, "STATUS REFUSED")) {
[1f4fc80]695                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
[6b9d22a]696                sd->call_status = SKYPE_CALL_REFUSED;
697        } else if (!strcmp(info, "STATUS UNPLACED")) {
698                if (sd->call_id)
699                        g_free(sd->call_id);
700                /* Save the ID for later usage (Cancel/Finish). */
701                sd->call_id = g_strdup(id);
702                sd->call_out = TRUE;
703        } else if (!strcmp(info, "STATUS FAILED")) {
[62f51ee9]704                imcb_error(ic, "Call failed: %s",
705                        skype_call_strerror(sd->failurereason));
[6b9d22a]706                sd->call_id = NULL;
707        } else if (!strncmp(info, "DURATION ", 9)) {
708                if (sd->call_duration)
709                        g_free(sd->call_duration);
710                sd->call_duration = g_strdup(info+9);
711        } else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
712                info += 15;
[62f51ee9]713                if (!sd->call_status)
714                        return;
715                switch (sd->call_status) {
716                case SKYPE_CALL_RINGING:
717                        if (sd->call_out)
718                                imcb_log(ic, "You are currently ringing "
719                                        "the user %s.", info);
720                        else {
[c213d6b]721                                g_snprintf(buf, IRC_LINE_SIZE,
[62f51ee9]722                                        "The user %s is currently ringing you.",
723                                        info);
724                                skype_call_ask(ic, sd->call_id, buf);
[9f2f25f]725                        }
[62f51ee9]726                        break;
727                case SKYPE_CALL_MISSED:
728                        imcb_log(ic, "You have missed a call from user %s.",
729                                info);
730                        break;
731                case SKYPE_CALL_CANCELLED:
732                        imcb_log(ic, "You cancelled the call to the user %s.",
733                                info);
[6b9d22a]734                        sd->call_status = 0;
[62f51ee9]735                        sd->call_out = FALSE;
736                        break;
737                case SKYPE_CALL_REFUSED:
738                        if (sd->call_out)
739                                imcb_log(ic, "The user %s refused the call.",
740                                        info);
741                        else
742                                imcb_log(ic,
743                                        "You refused the call from user %s.",
744                                        info);
745                        sd->call_out = FALSE;
746                        break;
747                case SKYPE_CALL_FINISHED:
748                        if (sd->call_duration)
749                                imcb_log(ic,
750                                        "You finished the call to the user %s "
751                                        "(duration: %s seconds).",
752                                        info, sd->call_duration);
753                        else
754                                imcb_log(ic,
755                                        "You finished the call to the user %s.",
756                                        info);
757                        sd->call_out = FALSE;
758                        break;
759                default:
760                        /* Don't be noisy, ignore other statuses for now. */
761                        break;
[9f2f25f]762                }
[62f51ee9]763                sd->call_status = 0;
[9f2f25f]764        }
765}
766
[e200daf]767static void skype_parse_filetransfer(struct im_connection *ic, char *line)
768{
769        struct skype_data *sd = ic->proto_data;
770        char *id = strchr(line, ' ');
771
[6b9d22a]772        if (!++id)
773                return;
774        char *info = strchr(id, ' ');
775
776        if (!info)
777                return;
778        *info = '\0';
779        info++;
780        if (!strcmp(info, "STATUS NEW")) {
[1f4fc80]781                skype_printf(ic, "GET FILETRANSFER %s PARTNER_HANDLE\n",
[62f51ee9]782                        id);
[6b9d22a]783                sd->filetransfer_status = SKYPE_FILETRANSFER_NEW;
784        } else if (!strcmp(info, "STATUS FAILED")) {
[1f4fc80]785                skype_printf(ic, "GET FILETRANSFER %s PARTNER_HANDLE\n",
[62f51ee9]786                        id);
[6b9d22a]787                sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED;
788        } else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
789                info += 15;
[62f51ee9]790                if (!sd->filetransfer_status)
791                        return;
792                switch (sd->filetransfer_status) {
793                case SKYPE_FILETRANSFER_NEW:
794                        imcb_log(ic, "The user %s offered a new file for you.",
795                                info);
796                        break;
797                case SKYPE_FILETRANSFER_FAILED:
798                        imcb_log(ic, "Failed to transfer file from user %s.",
799                                info);
800                        break;
[e200daf]801                }
[62f51ee9]802                sd->filetransfer_status = 0;
[e200daf]803        }
804}
805
[fbb15f2]806static struct skype_group *skype_group_by_id(struct im_connection *ic, int id)
807{
[89d6845]808        struct skype_data *sd = ic->proto_data;
809        int i;
810
811        for (i = 0; i < g_list_length(sd->groups); i++) {
812                struct skype_group *sg = (struct skype_group *)g_list_nth_data(sd->groups, i);
813
814                if (sg->id == id)
815                        return sg;
816        }
817        return NULL;
818}
819
[fbb15f2]820static void skype_group_free(struct skype_group *sg, gboolean usersonly)
821{
[89d6845]822        int i;
[fbb15f2]823
[89d6845]824        for (i = 0; i < g_list_length(sg->users); i++) {
825                char *user = g_list_nth_data(sg->users, i);
826                g_free(user);
827        }
828        sg->users = NULL;
829        if (usersonly)
830                return;
831        g_free(sg->name);
832        g_free(sg);
833}
834
[54ca269]835/* Update the group of each user in this group */
[fbb15f2]836static void skype_group_users(struct im_connection *ic, struct skype_group *sg)
837{
[54ca269]838        int i;
839
840        for (i = 0; i < g_list_length(sg->users); i++) {
841                char *user = g_list_nth_data(sg->users, i);
842                char *buf = g_strdup_printf("%s@skype.com", user);
843                imcb_add_buddy(ic, buf, sg->name);
844                g_free(buf);
845        }
846}
847
[89d6845]848static void skype_parse_group(struct im_connection *ic, char *line)
849{
850        struct skype_data *sd = ic->proto_data;
851        char *id = strchr(line, ' ');
852
853        if (!++id)
854                return;
855
856        char *info = strchr(id, ' ');
857
858        if (!info)
859                return;
860        *info = '\0';
861        info++;
862
863        if (!strncmp(info, "DISPLAYNAME ", 12)) {
864                info += 12;
865
866                /* Name given for a group ID: try to update it or insert a new
867                 * one if not found */
868                struct skype_group *sg = skype_group_by_id(ic, atoi(id));
869                if (sg) {
870                        g_free(sg->name);
871                        sg->name = g_strdup(info);
872                } else {
873                        sg = g_new0(struct skype_group, 1);
874                        sg->id = atoi(id);
875                        sg->name = g_strdup(info);
876                        sd->groups = g_list_append(sd->groups, sg);
877                }
878        } else if (!strncmp(info, "USERS ", 6)) {
879                struct skype_group *sg = skype_group_by_id(ic, atoi(id));
880
881                if (sg) {
882                        char **i;
883                        char **users = g_strsplit(info + 6, ", ", 0);
884
885                        skype_group_free(sg, TRUE);
886                        i = users;
887                        while (*i) {
888                                sg->users = g_list_append(sg->users, g_strdup(*i));
889                                i++;
890                        }
891                        g_strfreev(users);
[54ca269]892                        skype_group_users(ic, sg);
[89d6845]893                } else
[fbb15f2]894                        log_message(LOGLVL_ERROR,
895                                "No skype group with id %s. That's probably a bug.", id);
[46641bf]896        } else if (!strncmp(info, "NROFUSERS ", 10)) {
897                if (!sd->pending_user) {
898                        /* Number of users changed in this group, query its type to see
899                         * if it's a custom one we should care about. */
900                        skype_printf(ic, "GET GROUP %s TYPE", id);
901                        return;
902                }
903
904                /* This is a newly created group, we have a single user
905                 * to add. */
906                struct skype_group *sg = skype_group_by_id(ic, atoi(id));
907
908                if (sg) {
909                        skype_printf(ic, "ALTER GROUP %d ADDUSER %s", sg->id, sd->pending_user);
910                        g_free(sd->pending_user);
911                        sd->pending_user = NULL;
912                } else
913                        log_message(LOGLVL_ERROR,
914                                        "No skype group with id %s. That's probably a bug.", id);
[2b183e4]915        } else if (!strcmp(info, "TYPE CUSTOM_GROUP"))
[cb6d3c9]916                /* This one is interesting, query its users. */
917                skype_printf(ic, "GET GROUP %s USERS", id);
[89d6845]918}
919
[c35bf7a]920static void skype_parse_chat(struct im_connection *ic, char *line)
921{
922        struct skype_data *sd = ic->proto_data;
[c213d6b]923        char buf[IRC_LINE_SIZE];
[c35bf7a]924        char *id = strchr(line, ' ');
925
[6b9d22a]926        if (!++id)
927                return;
928        struct groupchat *gc;
929        char *info = strchr(id, ' ');
[c35bf7a]930
[6b9d22a]931        if (!info)
932                return;
933        *info = '\0';
934        info++;
935        /* Remove fake chat if we created one in skype_chat_with() */
[451f121]936        gc = bee_chat_by_title(ic->bee, ic, "");
[6b9d22a]937        if (gc)
938                imcb_chat_free(gc);
939        if (!strcmp(info, "STATUS MULTI_SUBSCRIBED")) {
[c573f1b]940                gc = bee_chat_by_title(ic->bee, ic, id);
941                if (!gc) {
942                        gc = imcb_chat_new(ic, id);
943                        imcb_chat_name_hint(gc, id);
944                }
[1f4fc80]945                skype_printf(ic, "GET CHAT %s ADDER\n", id);
946                skype_printf(ic, "GET CHAT %s TOPIC\n", id);
[6b9d22a]947        } else if (!strcmp(info, "STATUS DIALOG") && sd->groupchat_with) {
948                gc = imcb_chat_new(ic, id);
[72b60c7e]949                imcb_chat_name_hint(gc, id);
[6b9d22a]950                /* According to the docs this
951                 * is necessary. However it
952                 * does not seem the situation
953                 * and it would open an extra
954                 * window on our client, so
955                 * just leave it out. */
[1f4fc80]956                /*skype_printf(ic, "OPEN CHAT %s\n", id);*/
[5d9db76]957                g_snprintf(buf, IRC_LINE_SIZE, "%s@skype.com",
958                                sd->groupchat_with);
[6b9d22a]959                imcb_chat_add_buddy(gc, buf);
960                imcb_chat_add_buddy(gc, sd->username);
961                g_free(sd->groupchat_with);
962                sd->groupchat_with = NULL;
[1f4fc80]963                skype_printf(ic, "GET CHAT %s ADDER\n", id);
964                skype_printf(ic, "GET CHAT %s TOPIC\n", id);
[6b9d22a]965        } else if (!strcmp(info, "STATUS UNSUBSCRIBED")) {
[451f121]966                gc = bee_chat_by_title(ic->bee, ic, id);
[c35bf7a]967                if (gc)
[6b9d22a]968                        gc->data = (void *)FALSE;
969        } else if (!strncmp(info, "ADDER ", 6)) {
970                info += 6;
971                g_free(sd->adder);
972                sd->adder = g_strdup_printf("%s@skype.com", info);
973        } else if (!strncmp(info, "TOPIC ", 6)) {
974                info += 6;
[451f121]975                gc = bee_chat_by_title(ic->bee, ic, id);
[6b9d22a]976                if (gc && (sd->adder || sd->topic_wait)) {
977                        if (sd->topic_wait) {
978                                sd->adder = g_strdup(sd->username);
979                                sd->topic_wait = 0;
[c35bf7a]980                        }
[6b9d22a]981                        imcb_chat_topic(gc, sd->adder, info, 0);
982                        g_free(sd->adder);
983                        sd->adder = NULL;
984                }
985        } else if (!strncmp(info, "ACTIVEMEMBERS ", 14)) {
986                info += 14;
[451f121]987                gc = bee_chat_by_title(ic->bee, ic, id);
[6b9d22a]988                /* Hack! We set ->data to TRUE
989                 * while we're on the channel
990                 * so that we won't rejoin
991                 * after a /part. */
[62f51ee9]992                if (!gc || gc->data)
993                        return;
994                char **members = g_strsplit(info, " ", 0);
995                int i;
996                for (i = 0; members[i]; i++) {
997                        if (!strcmp(members[i], sd->username))
998                                continue;
[5d9db76]999                        g_snprintf(buf, IRC_LINE_SIZE, "%s@skype.com",
1000                                        members[i]);
[62f51ee9]1001                        if (!g_list_find_custom(gc->in_room, buf,
1002                                (GCompareFunc)strcmp))
1003                                imcb_chat_add_buddy(gc, buf);
[c35bf7a]1004                }
[62f51ee9]1005                imcb_chat_add_buddy(gc, sd->username);
1006                g_strfreev(members);
[c35bf7a]1007        }
1008}
1009
[2709f4c]1010static void skype_parse_password(struct im_connection *ic, char *line)
1011{
1012        if (!strncmp(line+9, "OK", 2))
1013                imcb_connected(ic);
1014        else {
1015                imcb_error(ic, "Authentication Failed");
1016                imc_logout(ic, TRUE);
1017        }
1018}
1019
[607f5e3]1020static void skype_parse_profile(struct im_connection *ic, char *line)
1021{
1022        imcb_log(ic, "SkypeOut balance value is '%s'.", line+21);
1023}
1024
1025static void skype_parse_ping(struct im_connection *ic, char *line)
1026{
[4ae3ffc]1027        /* Unused parameter */
1028        line = line;
[1f4fc80]1029        skype_printf(ic, "PONG\n");
[607f5e3]1030}
1031
1032static void skype_parse_chats(struct im_connection *ic, char *line)
1033{
1034        char **i;
1035        char **chats = g_strsplit(line + 6, ", ", 0);
1036
1037        i = chats;
1038        while (*i) {
[1f4fc80]1039                skype_printf(ic, "GET CHAT %s STATUS\n", *i);
1040                skype_printf(ic, "GET CHAT %s ACTIVEMEMBERS\n", *i);
[607f5e3]1041                i++;
1042        }
1043        g_strfreev(chats);
1044}
1045
[8b8d1bed]1046static void skype_parse_groups(struct im_connection *ic, char *line)
1047{
1048        char **i;
1049        char **groups = g_strsplit(line + 7, ", ", 0);
1050
1051        i = groups;
1052        while (*i) {
1053                skype_printf(ic, "GET GROUP %s DISPLAYNAME\n", *i);
1054                skype_printf(ic, "GET GROUP %s USERS\n", *i);
1055                i++;
1056        }
1057        g_strfreev(groups);
1058}
1059
[46e9822]1060static void skype_parse_alter_group(struct im_connection *ic, char *line)
1061{
1062        char *id = line + strlen("ALTER GROUP");
1063
1064        if (!++id)
1065                return;
1066
1067        char *info = strchr(id, ' ');
1068
1069        if (!info)
1070                return;
1071        *info = '\0';
1072        info++;
1073
1074        if (!strncmp(info, "ADDUSER ", 8)) {
1075                struct skype_group *sg = skype_group_by_id(ic, atoi(id));
1076
1077                info += 8;
1078                if (sg) {
1079                        char *buf = g_strdup_printf("%s@skype.com", info);
1080                        sg->users = g_list_append(sg->users, g_strdup(info));
1081                        imcb_add_buddy(ic, buf, sg->name);
1082                        g_free(buf);
1083                } else
1084                        log_message(LOGLVL_ERROR,
1085                                "No skype group with id %s. That's probably a bug.", id);
1086        }
1087}
1088
[ff436ba]1089typedef void (*skype_parser)(struct im_connection *ic, char *line);
1090
[8c09bb3]1091static gboolean skype_read_callback(gpointer data, gint fd,
1092                                    b_input_condition cond)
[1323e36]1093{
1094        struct im_connection *ic = data;
1095        struct skype_data *sd = ic->proto_data;
[c213d6b]1096        char buf[IRC_LINE_SIZE];
[ff436ba]1097        int st, i;
[6e14204]1098        char **lines, **lineptr, *line;
[ff436ba]1099        static struct parse_map {
1100                char *k;
1101                skype_parser v;
1102        } parsers[] = {
1103                { "USERS ", skype_parse_users },
1104                { "USER ", skype_parse_user },
1105                { "CHATMESSAGE ", skype_parse_chatmessage },
1106                { "CALL ", skype_parse_call },
1107                { "FILETRANSFER ", skype_parse_filetransfer },
1108                { "CHAT ", skype_parse_chat },
[89d6845]1109                { "GROUP ", skype_parse_group },
[ff436ba]1110                { "PASSWORD ", skype_parse_password },
1111                { "PROFILE PSTN_BALANCE ", skype_parse_profile },
1112                { "PING", skype_parse_ping },
1113                { "CHATS ", skype_parse_chats },
[8b8d1bed]1114                { "GROUPS ", skype_parse_groups },
[46e9822]1115                { "ALTER GROUP ", skype_parse_alter_group },
[ff436ba]1116        };
[1323e36]1117
[4ae3ffc]1118        /* Unused parameters */
1119        fd = fd;
1120        cond = cond;
1121
[5adcc65]1122        if (!sd || sd->fd == -1)
[1323e36]1123                return FALSE;
[7daec06]1124        /* Read the whole data. */
[5adcc65]1125        st = ssl_read(sd->ssl, buf, sizeof(buf));
1126        if (st > 0) {
[1323e36]1127                buf[st] = '\0';
[7daec06]1128                /* Then split it up to lines. */
[9fd4241]1129                lines = g_strsplit(buf, "\n", 0);
1130                lineptr = lines;
[5adcc65]1131                while ((line = *lineptr)) {
1132                        if (!strlen(line))
[9fd4241]1133                                break;
[b820226]1134                        if (set_getbool(&ic->acc->set, "skypeconsole_receive"))
1135                                imcb_buddy_msg(ic, "skypeconsole", line, 0, 0);
[6b9d22a]1136                        for (i = 0; i < ARRAY_SIZE(parsers); i++)
1137                                if (!strncmp(line, parsers[i].k,
1138                                        strlen(parsers[i].k))) {
[ff436ba]1139                                        parsers[i].v(ic, line);
1140                                        break;
1141                                }
[9fd4241]1142                        lineptr++;
1143                }
1144                g_strfreev(lines);
[5adcc65]1145        } else if (st == 0 || (st < 0 && !sockerr_again())) {
[58b65b33]1146                ssl_disconnect(sd->ssl);
[1323e36]1147                sd->fd = -1;
[58b65b33]1148                sd->ssl = NULL;
[1323e36]1149
[5adcc65]1150                imcb_error(ic, "Error while reading from server");
1151                imc_logout(ic, TRUE);
[1323e36]1152                return FALSE;
1153        }
1154        return TRUE;
1155}
1156
[5adcc65]1157gboolean skype_start_stream(struct im_connection *ic)
[f06e3ac]1158{
[1323e36]1159        struct skype_data *sd = ic->proto_data;
[f06e3ac]1160        int st;
1161
[5adcc65]1162        if (!sd)
[1fb89e3]1163                return FALSE;
1164
[5adcc65]1165        if (sd->bfd <= 0)
[7670b02]1166                sd->bfd = b_input_add(sd->fd, B_EV_IO_READ,
[8c09bb3]1167                        skype_read_callback, ic);
[1323e36]1168
[a0b206b]1169        /* Log in */
[1f4fc80]1170        skype_printf(ic, "USERNAME %s\n", ic->acc->user);
1171        skype_printf(ic, "PASSWORD %s\n", ic->acc->pass);
[a0b206b]1172
[8b8d1bed]1173        /* This will download all buddies and groups. */
[54ca269]1174        st = skype_printf(ic, "SEARCH GROUPS CUSTOM\n");
1175        skype_printf(ic, "SEARCH FRIENDS\n");
[8b8d1bed]1176
[1f4fc80]1177        skype_printf(ic, "SET USERSTATUS ONLINE\n");
[5acf9ab]1178
1179        /* Auto join to bookmarked chats if requested.*/
[1f4fc80]1180        if (set_getbool(&ic->acc->set, "auto_join"))
1181                skype_printf(ic, "SEARCH BOOKMARKEDCHATS\n");
[f06e3ac]1182        return st;
1183}
1184
[486ddb5]1185gboolean skype_connected(gpointer data, int returncode, void *source, b_input_condition cond)
[f06e3ac]1186{
1187        struct im_connection *ic = data;
[c7304b2]1188        struct skype_data *sd = ic->proto_data;
[4ae3ffc]1189
1190        /* Unused parameter */
1191        cond = cond;
1192
[5adcc65]1193        if (!source) {
[c7304b2]1194                sd->ssl = NULL;
[5adcc65]1195                imcb_error(ic, "Could not connect to server");
1196                imc_logout(ic, TRUE);
[c7304b2]1197                return FALSE;
1198        }
[5adcc65]1199        imcb_log(ic, "Connected to server, logging in");
[4ae3ffc]1200
[f06e3ac]1201        return skype_start_stream(ic);
1202}
1203
[5adcc65]1204static void skype_login(account_t *acc)
[f06e3ac]1205{
[5adcc65]1206        struct im_connection *ic = imcb_new(acc);
1207        struct skype_data *sd = g_new0(struct skype_data, 1);
[f06e3ac]1208
1209        ic->proto_data = sd;
1210
[5adcc65]1211        imcb_log(ic, "Connecting");
[8c09bb3]1212        sd->ssl = ssl_connect(set_getstr(&acc->set, "server"),
[a72dc2b]1213                set_getint(&acc->set, "port"), FALSE, skype_connected, ic);
[5adcc65]1214        sd->fd = sd->ssl ? ssl_getfd(sd->ssl) : -1;
1215        sd->username = g_strdup(acc->user);
[f06e3ac]1216
1217        sd->ic = ic;
[08a355b]1218
1219        if (set_getbool(&acc->set, "skypeconsole"))
1220                imcb_add_buddy(ic, "skypeconsole", NULL);
[f06e3ac]1221}
1222
[5adcc65]1223static void skype_logout(struct im_connection *ic)
[f06e3ac]1224{
1225        struct skype_data *sd = ic->proto_data;
[89d6845]1226        int i;
[98bca36]1227
[1f4fc80]1228        skype_printf(ic, "SET USERSTATUS OFFLINE\n");
[98bca36]1229
[bd11422]1230        while( ic->groupchats )
1231                imcb_chat_free(ic->groupchats->data);
1232
[89d6845]1233        for (i = 0; i < g_list_length(sd->groups); i++) {
1234                struct skype_group *sg = (struct skype_group *)g_list_nth_data(sd->groups, i);
1235                skype_group_free(sg, FALSE);
1236        }
[58b65b33]1237
1238        if (sd->ssl)
1239                ssl_disconnect(sd->ssl);
1240
[a3d6427]1241        g_free(sd->username);
[7613670]1242        g_free(sd->handle);
[f06e3ac]1243        g_free(sd);
[98bca36]1244        ic->proto_data = NULL;
[f06e3ac]1245}
1246
[8c09bb3]1247static int skype_buddy_msg(struct im_connection *ic, char *who, char *message,
1248                           int flags)
[93ece66]1249{
[1f4fc80]1250        char *ptr, *nick;
[93ece66]1251        int st;
1252
[4ae3ffc]1253        /* Unused parameter */
1254        flags = flags;
1255
[cbec0d6]1256        nick = g_strdup(who);
[77c1abe]1257        ptr = strchr(nick, '@');
[5adcc65]1258        if (ptr)
[0bb1b7f]1259                *ptr = '\0';
[93ece66]1260
[08a355b]1261        if (!strncmp(who, "skypeconsole", 12))
[1f4fc80]1262                st = skype_printf(ic, "%s\n", message);
[08a355b]1263        else
[1f4fc80]1264                st = skype_printf(ic, "MESSAGE %s %s\n", nick, message);
[77c1abe]1265        g_free(nick);
[93ece66]1266
1267        return st;
1268}
1269
[5adcc65]1270const struct skype_away_state *skype_away_state_by_name(char *name)
[23411c6]1271{
1272        int i;
1273
[5adcc65]1274        for (i = 0; skype_away_state_list[i].full_name; i++)
1275                if (g_strcasecmp(skype_away_state_list[i].full_name, name) == 0)
1276                        return skype_away_state_list + i;
[23411c6]1277
1278        return NULL;
1279}
1280
[8c09bb3]1281static void skype_set_away(struct im_connection *ic, char *state_txt,
1282                           char *message)
[f06e3ac]1283{
[23411c6]1284        const struct skype_away_state *state;
1285
[4ae3ffc]1286        /* Unused parameter */
1287        message = message;
1288
[4b740c2]1289        if (state_txt == NULL)
1290                state = skype_away_state_by_name("Online");
[23411c6]1291        else
[5adcc65]1292                state = skype_away_state_by_name(state_txt);
[1f4fc80]1293        skype_printf(ic, "SET USERSTATUS %s\n", state->code);
[f06e3ac]1294}
1295
[5adcc65]1296static GList *skype_away_states(struct im_connection *ic)
[f06e3ac]1297{
[5adcc65]1298        static GList *l;
[adce2de]1299        int i;
[5adcc65]1300
[4ae3ffc]1301        /* Unused parameter */
1302        ic = ic;
1303
[5adcc65]1304        if (l == NULL)
1305                for (i = 0; skype_away_state_list[i].full_name; i++)
[8c09bb3]1306                        l = g_list_append(l,
1307                                (void *)skype_away_state_list[i].full_name);
[5adcc65]1308
[f06e3ac]1309        return l;
1310}
1311
[5adcc65]1312static char *skype_set_display_name(set_t *set, char *value)
[93dffea]1313{
1314        account_t *acc = set->data;
1315        struct im_connection *ic = acc->ic;
1316
[1f4fc80]1317        skype_printf(ic, "SET PROFILE FULLNAME %s", value);
[5adcc65]1318        return value;
[93dffea]1319}
1320
[5adcc65]1321static char *skype_set_balance(set_t *set, char *value)
[2af671a]1322{
1323        account_t *acc = set->data;
1324        struct im_connection *ic = acc->ic;
1325
[1f4fc80]1326        skype_printf(ic, "GET PROFILE PSTN_BALANCE");
[5adcc65]1327        return value;
[2af671a]1328}
1329
[fbb15f2]1330static void skype_call(struct im_connection *ic, char *value)
1331{
[71c4bb6]1332        char *nick = g_strdup(value);
1333        char *ptr = strchr(nick, '@');
1334
1335        if (ptr)
1336                *ptr = '\0';
1337        skype_printf(ic, "CALL %s", nick);
1338        g_free(nick);
1339}
1340
1341static void skype_hangup(struct im_connection *ic)
1342{
1343        struct skype_data *sd = ic->proto_data;
1344
1345        if (sd->call_id) {
1346                skype_printf(ic, "SET CALL %s STATUS FINISHED",
1347                                sd->call_id);
1348                g_free(sd->call_id);
1349                sd->call_id = 0;
1350        } else
1351                imcb_error(ic, "There are no active calls currently.");
1352}
1353
[5adcc65]1354static char *skype_set_call(set_t *set, char *value)
[b68b023]1355{
1356        account_t *acc = set->data;
1357        struct im_connection *ic = acc->ic;
1358
[fbb15f2]1359        if (value)
[451f121]1360                skype_call(ic, value);
[fbb15f2]1361        else
[71c4bb6]1362                skype_hangup(ic);
[5adcc65]1363        return value;
[b68b023]1364}
1365
[5adcc65]1366static void skype_add_buddy(struct im_connection *ic, char *who, char *group)
[f06e3ac]1367{
[46641bf]1368        struct skype_data *sd = ic->proto_data;
[1f4fc80]1369        char *nick, *ptr;
[6627d92]1370
[cbec0d6]1371        nick = g_strdup(who);
[6627d92]1372        ptr = strchr(nick, '@');
[5adcc65]1373        if (ptr)
[6627d92]1374                *ptr = '\0';
[46e9822]1375
1376        if (!group) {
1377                skype_printf(ic, "SET USER %s BUDDYSTATUS 2 Please authorize me\n",
1378                                nick);
1379                g_free(nick);
1380        } else {
1381                struct skype_group *sg = skype_group_by_name(ic, group);
1382
1383                if (!sg) {
1384                        /* No such group, we need to create it, then have to
1385                         * add the user once it's created. */
[46641bf]1386                        skype_printf(ic, "CREATE GROUP %s", group);
1387                        sd->pending_user = g_strdup(nick);
[46e9822]1388                } else {
1389                        skype_printf(ic, "ALTER GROUP %d ADDUSER %s", sg->id, nick);
1390                }
1391        }
[f06e3ac]1392}
1393
[5adcc65]1394static void skype_remove_buddy(struct im_connection *ic, char *who, char *group)
[f06e3ac]1395{
[1f4fc80]1396        char *nick, *ptr;
[6627d92]1397
[4ae3ffc]1398        /* Unused parameter */
1399        group = group;
1400
[cbec0d6]1401        nick = g_strdup(who);
[6627d92]1402        ptr = strchr(nick, '@');
[5adcc65]1403        if (ptr)
[6627d92]1404                *ptr = '\0';
[1f4fc80]1405        skype_printf(ic, "SET USER %s BUDDYSTATUS 1\n", nick);
[6627d92]1406        g_free(nick);
[f06e3ac]1407}
1408
[5adcc65]1409void skype_chat_msg(struct groupchat *gc, char *message, int flags)
[66c9558]1410{
[79e20f9]1411        struct im_connection *ic = gc->ic;
[4ae3ffc]1412
1413        /* Unused parameter */
1414        flags = flags;
1415
[1f4fc80]1416        skype_printf(ic, "CHATMESSAGE %s %s\n", gc->title, message);
[66c9558]1417}
1418
[5adcc65]1419void skype_chat_leave(struct groupchat *gc)
[b01dc6c]1420{
1421        struct im_connection *ic = gc->ic;
[1f4fc80]1422        skype_printf(ic, "ALTER CHAT %s LEAVE\n", gc->title);
[5adcc65]1423        gc->data = (void *)TRUE;
[760319d]1424}
1425
1426void skype_chat_invite(struct groupchat *gc, char *who, char *message)
1427{
1428        struct im_connection *ic = gc->ic;
[1f4fc80]1429        char *ptr, *nick;
[4ae3ffc]1430
[17dd2ed]1431        nick = g_strdup(who);
[760319d]1432        ptr = strchr(nick, '@');
[5adcc65]1433        if (ptr)
[760319d]1434                *ptr = '\0';
[1f4fc80]1435        skype_printf(ic, "ALTER CHAT %s ADDMEMBERS %s\n", gc->title, nick);
[760319d]1436        g_free(nick);
[b01dc6c]1437}
1438
[09e2a69]1439void skype_chat_topic(struct groupchat *gc, char *message)
1440{
1441        struct im_connection *ic = gc->ic;
[a5f76a2]1442        struct skype_data *sd = ic->proto_data;
[1f4fc80]1443        skype_printf(ic, "ALTER CHAT %s SETTOPIC %s\n",
[8c09bb3]1444                gc->title, message);
[a5f76a2]1445        sd->topic_wait = 1;
[09e2a69]1446}
1447
[86278cd]1448struct groupchat *skype_chat_with(struct im_connection *ic, char *who)
1449{
1450        struct skype_data *sd = ic->proto_data;
[1f4fc80]1451        char *ptr, *nick;
[86278cd]1452        nick = g_strdup(who);
1453        ptr = strchr(nick, '@');
[5adcc65]1454        if (ptr)
[86278cd]1455                *ptr = '\0';
[1f4fc80]1456        skype_printf(ic, "CHAT CREATE %s\n", nick);
[86278cd]1457        sd->groupchat_with = g_strdup(nick);
1458        g_free(nick);
[5652d43]1459        /* We create a fake chat for now. We will replace it with a real one in
1460         * the real callback. */
[5adcc65]1461        return imcb_chat_new(ic, "");
[86278cd]1462}
1463
[67454bd]1464static void skype_get_info(struct im_connection *ic, char *who)
1465{
[1f4fc80]1466        char *ptr, *nick;
[67454bd]1467        nick = g_strdup(who);
1468        ptr = strchr(nick, '@');
[5adcc65]1469        if (ptr)
[67454bd]1470                *ptr = '\0';
[1f4fc80]1471        skype_printf(ic, "GET USER %s FULLNAME\n", nick);
1472        skype_printf(ic, "GET USER %s PHONE_HOME\n", nick);
1473        skype_printf(ic, "GET USER %s PHONE_OFFICE\n", nick);
1474        skype_printf(ic, "GET USER %s PHONE_MOBILE\n", nick);
1475        skype_printf(ic, "GET USER %s NROF_AUTHED_BUDDIES\n", nick);
1476        skype_printf(ic, "GET USER %s TIMEZONE\n", nick);
1477        skype_printf(ic, "GET USER %s LASTONLINETIMESTAMP\n", nick);
1478        skype_printf(ic, "GET USER %s SEX\n", nick);
1479        skype_printf(ic, "GET USER %s LANGUAGE\n", nick);
1480        skype_printf(ic, "GET USER %s COUNTRY\n", nick);
1481        skype_printf(ic, "GET USER %s PROVINCE\n", nick);
1482        skype_printf(ic, "GET USER %s CITY\n", nick);
1483        skype_printf(ic, "GET USER %s HOMEPAGE\n", nick);
1484        skype_printf(ic, "GET USER %s ABOUT\n", nick);
[85341dd]1485        /*
1486         * Hack: we query the bithday property which is always a single line,
1487         * so we can send the collected properties to the user when we have
1488         * this one.
1489         */
1490        skype_printf(ic, "GET USER %s BIRTHDAY\n", nick);
[67454bd]1491}
1492
[5adcc65]1493static void skype_set_my_name(struct im_connection *ic, char *info)
[93dffea]1494{
[5adcc65]1495        skype_set_display_name(set_find(&ic->acc->set, "display_name"), info);
[93dffea]1496}
1497
[5adcc65]1498static void skype_init(account_t *acc)
[93dffea]1499{
1500        set_t *s;
1501
[8c09bb3]1502        s = set_add(&acc->set, "server", SKYPE_DEFAULT_SERVER, set_eval_account,
1503                acc);
[93dffea]1504        s->flags |= ACC_SET_OFFLINE_ONLY;
1505
[5adcc65]1506        s = set_add(&acc->set, "port", SKYPE_DEFAULT_PORT, set_eval_int, acc);
[93dffea]1507        s->flags |= ACC_SET_OFFLINE_ONLY;
1508
[8c09bb3]1509        s = set_add(&acc->set, "display_name", NULL, skype_set_display_name,
1510                acc);
[93dffea]1511        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
[b68b023]1512
[5adcc65]1513        s = set_add(&acc->set, "call", NULL, skype_set_call, acc);
[b68b023]1514        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
[2af671a]1515
[5adcc65]1516        s = set_add(&acc->set, "balance", NULL, skype_set_balance, acc);
[2af671a]1517        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
[bd417a1]1518
[5adcc65]1519        s = set_add(&acc->set, "skypeout_offline", "true", set_eval_bool, acc);
[08a355b]1520
[5adcc65]1521        s = set_add(&acc->set, "skypeconsole", "false", set_eval_bool, acc);
[08a355b]1522        s->flags |= ACC_SET_OFFLINE_ONLY;
[5acf9ab]1523
[8c09bb3]1524        s = set_add(&acc->set, "skypeconsole_receive", "false", set_eval_bool,
1525                acc);
[b820226]1526        s->flags |= ACC_SET_OFFLINE_ONLY;
1527
[5adcc65]1528        s = set_add(&acc->set, "auto_join", "false", set_eval_bool, acc);
[5acf9ab]1529        s->flags |= ACC_SET_OFFLINE_ONLY;
[f4d37c6]1530
[49a3c02]1531        s = set_add(&acc->set, "test_join", "false", set_eval_bool, acc);
1532        s->flags |= ACC_SET_OFFLINE_ONLY;
1533
[304aa33]1534        s = set_add(&acc->set, "show_moods", "false", set_eval_bool, acc);
1535
[f4d37c6]1536        s = set_add(&acc->set, "edit_prefix", "EDIT:",
[1e3120f]1537                        NULL, acc);
[93dffea]1538}
1539
[c6e0218]1540#if BITLBEE_VERSION_CODE > BITLBEE_VER(3, 0, 1)
[fbb15f2]1541GList *skype_buddy_action_list(bee_user_t *bu)
[71c4bb6]1542{
[fbb15f2]1543        static GList *ret;
[71c4bb6]1544
[a5e6aa1]1545        /* Unused parameter */
1546        bu = bu;
1547
[fbb15f2]1548        if (ret == NULL) {
[71c4bb6]1549                static const struct buddy_action ba[3] = {
1550                        {"CALL", "Initiate a call" },
1551                        {"HANGUP", "Hang up a call" },
1552                };
1553
[fbb15f2]1554                ret = g_list_prepend(ret, (void *) ba + 0);
[71c4bb6]1555        }
1556
1557        return ret;
1558}
1559
[fbb15f2]1560void *skype_buddy_action(struct bee_user *bu, const char *action, char * const args[], void *data)
[71c4bb6]1561{
[a5e6aa1]1562        /* Unused parameters */
1563        args = args;
1564        data = data;
1565
[fbb15f2]1566        if (!g_strcasecmp(action, "CALL"))
[71c4bb6]1567                skype_call(bu->ic, bu->handle);
[fbb15f2]1568        else if (!g_strcasecmp(action, "HANGUP"))
[71c4bb6]1569                skype_hangup(bu->ic);
1570
1571        return NULL;
1572}
1573#endif
1574
[f06e3ac]1575void init_plugin(void)
1576{
[5adcc65]1577        struct prpl *ret = g_new0(struct prpl, 1);
[f06e3ac]1578
1579        ret->name = "skype";
1580        ret->login = skype_login;
1581        ret->init = skype_init;
1582        ret->logout = skype_logout;
[93ece66]1583        ret->buddy_msg = skype_buddy_msg;
[67454bd]1584        ret->get_info = skype_get_info;
[93dffea]1585        ret->set_my_name = skype_set_my_name;
[f06e3ac]1586        ret->away_states = skype_away_states;
[7daec06]1587        ret->set_away = skype_set_away;
[f06e3ac]1588        ret->add_buddy = skype_add_buddy;
1589        ret->remove_buddy = skype_remove_buddy;
[66c9558]1590        ret->chat_msg = skype_chat_msg;
[b01dc6c]1591        ret->chat_leave = skype_chat_leave;
[760319d]1592        ret->chat_invite = skype_chat_invite;
[86278cd]1593        ret->chat_with = skype_chat_with;
[f06e3ac]1594        ret->handle_cmp = g_strcasecmp;
[09e2a69]1595        ret->chat_topic = skype_chat_topic;
[c6e0218]1596#if BITLBEE_VERSION_CODE > BITLBEE_VER(3, 0, 1)
[71c4bb6]1597        ret->buddy_action_list = skype_buddy_action_list;
1598        ret->buddy_action = skype_buddy_action;
1599#endif
[5adcc65]1600        register_protocol(ret);
[f06e3ac]1601}
Note: See TracBrowser for help on using the repository browser.