source: protocols/skype/skype.c @ 7c2daf5f

Last change on this file since 7c2daf5f was 7c2daf5f, checked in by Miklos Vajna <vmiklos@…>, at 2012-01-27T23:16:15Z

skype: don't crash in skype_parse_user() if the user has multiple about lines

  • Property mode set to 100644
File size: 40.0 KB
RevLine 
[7daec06]1/*
2 *  skype.c - Skype plugin for BitlBee
[5adcc65]3 *
[89d6845]4 *  Copyright (c) 2007, 2008, 2009, 2010, 2011 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, "BIRTHDAY ", 9))
[d7938f9]389                sd->info_birthday = g_strdup(ptr + 9);
[6e14204]390        else if (!strncmp(ptr, "SEX ", 4))
[d7938f9]391                sd->info_sex = g_strdup(ptr + 4);
[6e14204]392        else if (!strncmp(ptr, "LANGUAGE ", 9))
[d7938f9]393                sd->info_language = g_strdup(ptr + 9);
[6e14204]394        else if (!strncmp(ptr, "COUNTRY ", 8))
[d7938f9]395                sd->info_country = g_strdup(ptr + 8);
[6e14204]396        else if (!strncmp(ptr, "PROVINCE ", 9))
[d7938f9]397                sd->info_province = g_strdup(ptr + 9);
[6e14204]398        else if (!strncmp(ptr, "CITY ", 5))
[d7938f9]399                sd->info_city = g_strdup(ptr + 5);
[6e14204]400        else if (!strncmp(ptr, "HOMEPAGE ", 9))
[d7938f9]401                sd->info_homepage = g_strdup(ptr + 9);
[6e14204]402        else if (!strncmp(ptr, "ABOUT ", 6)) {
[d7938f9]403                sd->info_about = g_strdup(ptr + 6);
[6e14204]404
405                GString *st = g_string_new("Contact Information\n");
406                g_string_append_printf(st, "Skype Name: %s\n", user);
407                if (sd->info_fullname) {
408                        if (strlen(sd->info_fullname))
[62f51ee9]409                                g_string_append_printf(st, "Full Name: %s\n",
410                                        sd->info_fullname);
[6e14204]411                        g_free(sd->info_fullname);
[7c2daf5f]412                        sd->info_fullname = NULL;
[6e14204]413                }
414                if (sd->info_phonehome) {
415                        if (strlen(sd->info_phonehome))
[62f51ee9]416                                g_string_append_printf(st, "Home Phone: %s\n",
417                                        sd->info_phonehome);
[6e14204]418                        g_free(sd->info_phonehome);
[7c2daf5f]419                        sd->info_phonehome = NULL;
[6e14204]420                }
421                if (sd->info_phoneoffice) {
422                        if (strlen(sd->info_phoneoffice))
[62f51ee9]423                                g_string_append_printf(st, "Office Phone: %s\n",
424                                        sd->info_phoneoffice);
[6e14204]425                        g_free(sd->info_phoneoffice);
[7c2daf5f]426                        sd->info_phoneoffice = NULL;
[6e14204]427                }
428                if (sd->info_phonemobile) {
429                        if (strlen(sd->info_phonemobile))
[62f51ee9]430                                g_string_append_printf(st, "Mobile Phone: %s\n",
431                                        sd->info_phonemobile);
[6e14204]432                        g_free(sd->info_phonemobile);
[7c2daf5f]433                        sd->info_phonemobile = NULL;
[6e14204]434                }
435                g_string_append_printf(st, "Personal Information\n");
436                if (sd->info_nrbuddies) {
437                        if (strlen(sd->info_nrbuddies))
[62f51ee9]438                                g_string_append_printf(st,
439                                        "Contacts: %s\n", sd->info_nrbuddies);
[6e14204]440                        g_free(sd->info_nrbuddies);
[7c2daf5f]441                        sd->info_nrbuddies = NULL;
[6e14204]442                }
443                if (sd->info_tz) {
444                        if (strlen(sd->info_tz)) {
445                                char ib[256];
446                                time_t t = time(NULL);
447                                t += atoi(sd->info_tz)-(60*60*24);
448                                struct tm *gt = gmtime(&t);
449                                strftime(ib, 256, "%H:%M:%S", gt);
[62f51ee9]450                                g_string_append_printf(st,
451                                        "Local Time: %s\n", ib);
[6e14204]452                        }
453                        g_free(sd->info_tz);
[7c2daf5f]454                        sd->info_tz = NULL;
[6e14204]455                }
456                if (sd->info_seen) {
457                        if (strlen(sd->info_seen)) {
458                                char ib[256];
459                                time_t it = atoi(sd->info_seen);
460                                struct tm *tm = localtime(&it);
461                                strftime(ib, 256, ("%Y. %m. %d. %H:%M"), tm);
[62f51ee9]462                                g_string_append_printf(st,
463                                        "Last Seen: %s\n", ib);
[6e14204]464                        }
465                        g_free(sd->info_seen);
[7c2daf5f]466                        sd->info_seen = NULL;
[6e14204]467                }
468                if (sd->info_birthday) {
[62f51ee9]469                        if (strlen(sd->info_birthday) &&
470                                strcmp(sd->info_birthday, "0")) {
[6e14204]471                                char ib[256];
472                                struct tm tm;
473                                strptime(sd->info_birthday, "%Y%m%d", &tm);
474                                strftime(ib, 256, "%B %d, %Y", &tm);
[62f51ee9]475                                g_string_append_printf(st,
476                                        "Birthday: %s\n", ib);
[6e14204]477
478                                strftime(ib, 256, "%Y", &tm);
479                                int year = atoi(ib);
480                                time_t t = time(NULL);
481                                struct tm *lt = localtime(&t);
[62f51ee9]482                                g_string_append_printf(st,
483                                        "Age: %d\n", lt->tm_year+1900-year);
[6e14204]484                        }
485                        g_free(sd->info_birthday);
[7c2daf5f]486                        sd->info_birthday = NULL;
[6e14204]487                }
488                if (sd->info_sex) {
489                        if (strlen(sd->info_sex)) {
490                                char *iptr = sd->info_sex;
491                                while (*iptr++)
492                                        *iptr = tolower(*iptr);
[62f51ee9]493                                g_string_append_printf(st,
494                                        "Gender: %s\n", sd->info_sex);
[6e14204]495                        }
496                        g_free(sd->info_sex);
[7c2daf5f]497                        sd->info_sex = NULL;
[6e14204]498                }
499                if (sd->info_language) {
500                        if (strlen(sd->info_language)) {
501                                char *iptr = strchr(sd->info_language, ' ');
502                                if (iptr)
503                                        iptr++;
504                                else
505                                        iptr = sd->info_language;
[62f51ee9]506                                g_string_append_printf(st,
507                                        "Language: %s\n", iptr);
[6e14204]508                        }
509                        g_free(sd->info_language);
[7c2daf5f]510                        sd->info_language = NULL;
[6e14204]511                }
512                if (sd->info_country) {
513                        if (strlen(sd->info_country)) {
514                                char *iptr = strchr(sd->info_country, ' ');
515                                if (iptr)
516                                        iptr++;
517                                else
518                                        iptr = sd->info_country;
[62f51ee9]519                                g_string_append_printf(st,
520                                        "Country: %s\n", iptr);
[6e14204]521                        }
522                        g_free(sd->info_country);
[7c2daf5f]523                        sd->info_country = NULL;
[6e14204]524                }
525                if (sd->info_province) {
526                        if (strlen(sd->info_province))
[62f51ee9]527                                g_string_append_printf(st,
528                                        "Region: %s\n", sd->info_province);
[6e14204]529                        g_free(sd->info_province);
[7c2daf5f]530                        sd->info_province = NULL;
[6e14204]531                }
532                if (sd->info_city) {
533                        if (strlen(sd->info_city))
[62f51ee9]534                                g_string_append_printf(st,
535                                        "City: %s\n", sd->info_city);
[6e14204]536                        g_free(sd->info_city);
[7c2daf5f]537                        sd->info_city = NULL;
[6e14204]538                }
539                if (sd->info_homepage) {
540                        if (strlen(sd->info_homepage))
[62f51ee9]541                                g_string_append_printf(st,
542                                        "Homepage: %s\n", sd->info_homepage);
[6e14204]543                        g_free(sd->info_homepage);
[7c2daf5f]544                        sd->info_homepage = NULL;
[6e14204]545                }
546                if (sd->info_about) {
547                        if (strlen(sd->info_about))
[62f51ee9]548                                g_string_append_printf(st, "%s\n",
549                                        sd->info_about);
[6e14204]550                        g_free(sd->info_about);
[7c2daf5f]551                        sd->info_about = NULL;
[6e14204]552                }
553                imcb_log(ic, "%s", st->str);
554                g_string_free(st, TRUE);
555        }
556}
557
558static void skype_parse_chatmessage(struct im_connection *ic, char *line)
559{
560        struct skype_data *sd = ic->proto_data;
[c213d6b]561        char buf[IRC_LINE_SIZE];
[6e14204]562        char *id = strchr(line, ' ');
563
[6b9d22a]564        if (!++id)
565                return;
566        char *info = strchr(id, ' ');
567
568        if (!info)
569                return;
570        *info = '\0';
571        info++;
[7825f58]572        if (!strcmp(info, "STATUS RECEIVED") || !strncmp(info, "EDITED_TIMESTAMP", 16)) {
[6b9d22a]573                /* New message ID:
574                 * (1) Request its from field
575                 * (2) Request its body
576                 * (3) Request its type
577                 * (4) Query chatname
578                 */
[1f4fc80]579                skype_printf(ic, "GET CHATMESSAGE %s FROM_HANDLE\n", id);
[d1d5b34]580                if (!strcmp(info, "STATUS RECEIVED"))
581                        skype_printf(ic, "GET CHATMESSAGE %s BODY\n", id);
[d9ce18c]582                else
583                        sd->is_edit = 1;
[1f4fc80]584                skype_printf(ic, "GET CHATMESSAGE %s TYPE\n", id);
585                skype_printf(ic, "GET CHATMESSAGE %s CHATNAME\n", id);
[6b9d22a]586        } else if (!strncmp(info, "FROM_HANDLE ", 12)) {
587                info += 12;
588                /* New from field value. Store
589                 * it, then we can later use it
590                 * when we got the message's
591                 * body. */
592                g_free(sd->handle);
593                sd->handle = g_strdup_printf("%s@skype.com", info);
594        } else if (!strncmp(info, "EDITED_BY ", 10)) {
595                info += 10;
596                /* This is the same as
597                 * FROM_HANDLE, except that we
598                 * never request these lines
599                 * from Skype, we just get
600                 * them. */
601                g_free(sd->handle);
602                sd->handle = g_strdup_printf("%s@skype.com", info);
603        } else if (!strncmp(info, "BODY ", 5)) {
604                info += 5;
605                sd->body = g_list_append(sd->body, g_strdup(info));
606        }       else if (!strncmp(info, "TYPE ", 5)) {
607                info += 5;
608                g_free(sd->type);
609                sd->type = g_strdup(info);
[7825f58]610        } else if (!strncmp(info, "CHATNAME ", 9)) {
611                info += 9;
612                if (sd->handle && sd->body && sd->type) {
[451f121]613                        struct groupchat *gc = bee_chat_by_title(ic->bee, ic, info);
[7825f58]614                        int i;
615                        for (i = 0; i < g_list_length(sd->body); i++) {
616                                char *body = g_list_nth_data(sd->body, i);
617                                if (!strcmp(sd->type, "SAID") ||
618                                        !strcmp(sd->type, "EMOTED")) {
619                                        if (!strcmp(sd->type, "SAID")) {
620                                                if (!sd->is_edit)
621                                                        g_snprintf(buf, IRC_LINE_SIZE, "%s",
622                                                                body);
623                                                else {
624                                                        g_snprintf(buf, IRC_LINE_SIZE, "%s %s",
625                                                                set_getstr(&ic->acc->set, "edit_prefix"),
626                                                                body);
627                                                        sd->is_edit = 0;
628                                                }
629                                        } else
630                                                g_snprintf(buf, IRC_LINE_SIZE, "/me %s",
631                                                        body);
632                                        if (!gc)
633                                                /* Private message */
634                                                imcb_buddy_msg(ic,
635                                                        sd->handle, buf, 0, 0);
636                                        else
637                                                /* Groupchat message */
638                                                imcb_chat_msg(gc,
639                                                        sd->handle, buf, 0, 0);
640                                } else if (!strcmp(sd->type, "SETTOPIC") && gc)
641                                        imcb_chat_topic(gc,
642                                                sd->handle, body, 0);
643                                else if (!strcmp(sd->type, "LEFT") && gc)
644                                        imcb_chat_remove_buddy(gc,
645                                                sd->handle, NULL);
[6e14204]646                        }
[7825f58]647                        g_list_free(sd->body);
648                        sd->body = NULL;
649                }
[6e14204]650        }
651}
652
[9f2f25f]653static void skype_parse_call(struct im_connection *ic, char *line)
654{
655        struct skype_data *sd = ic->proto_data;
656        char *id = strchr(line, ' ');
[c213d6b]657        char buf[IRC_LINE_SIZE];
[9f2f25f]658
[6b9d22a]659        if (!++id)
660                return;
661        char *info = strchr(id, ' ');
662
663        if (!info)
664                return;
665        *info = '\0';
666        info++;
667        if (!strncmp(info, "FAILUREREASON ", 14))
668                sd->failurereason = atoi(strchr(info, ' '));
669        else if (!strcmp(info, "STATUS RINGING")) {
670                if (sd->call_id)
671                        g_free(sd->call_id);
672                sd->call_id = g_strdup(id);
[1f4fc80]673                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
[6b9d22a]674                sd->call_status = SKYPE_CALL_RINGING;
675        } else if (!strcmp(info, "STATUS MISSED")) {
[1f4fc80]676                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
[6b9d22a]677                sd->call_status = SKYPE_CALL_MISSED;
678        } else if (!strcmp(info, "STATUS CANCELLED")) {
[1f4fc80]679                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
[6b9d22a]680                sd->call_status = SKYPE_CALL_CANCELLED;
681        } else if (!strcmp(info, "STATUS FINISHED")) {
[1f4fc80]682                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
[6b9d22a]683                sd->call_status = SKYPE_CALL_FINISHED;
684        } else if (!strcmp(info, "STATUS REFUSED")) {
[1f4fc80]685                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
[6b9d22a]686                sd->call_status = SKYPE_CALL_REFUSED;
687        } else if (!strcmp(info, "STATUS UNPLACED")) {
688                if (sd->call_id)
689                        g_free(sd->call_id);
690                /* Save the ID for later usage (Cancel/Finish). */
691                sd->call_id = g_strdup(id);
692                sd->call_out = TRUE;
693        } else if (!strcmp(info, "STATUS FAILED")) {
[62f51ee9]694                imcb_error(ic, "Call failed: %s",
695                        skype_call_strerror(sd->failurereason));
[6b9d22a]696                sd->call_id = NULL;
697        } else if (!strncmp(info, "DURATION ", 9)) {
698                if (sd->call_duration)
699                        g_free(sd->call_duration);
700                sd->call_duration = g_strdup(info+9);
701        } else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
702                info += 15;
[62f51ee9]703                if (!sd->call_status)
704                        return;
705                switch (sd->call_status) {
706                case SKYPE_CALL_RINGING:
707                        if (sd->call_out)
708                                imcb_log(ic, "You are currently ringing "
709                                        "the user %s.", info);
710                        else {
[c213d6b]711                                g_snprintf(buf, IRC_LINE_SIZE,
[62f51ee9]712                                        "The user %s is currently ringing you.",
713                                        info);
714                                skype_call_ask(ic, sd->call_id, buf);
[9f2f25f]715                        }
[62f51ee9]716                        break;
717                case SKYPE_CALL_MISSED:
718                        imcb_log(ic, "You have missed a call from user %s.",
719                                info);
720                        break;
721                case SKYPE_CALL_CANCELLED:
722                        imcb_log(ic, "You cancelled the call to the user %s.",
723                                info);
[6b9d22a]724                        sd->call_status = 0;
[62f51ee9]725                        sd->call_out = FALSE;
726                        break;
727                case SKYPE_CALL_REFUSED:
728                        if (sd->call_out)
729                                imcb_log(ic, "The user %s refused the call.",
730                                        info);
731                        else
732                                imcb_log(ic,
733                                        "You refused the call from user %s.",
734                                        info);
735                        sd->call_out = FALSE;
736                        break;
737                case SKYPE_CALL_FINISHED:
738                        if (sd->call_duration)
739                                imcb_log(ic,
740                                        "You finished the call to the user %s "
741                                        "(duration: %s seconds).",
742                                        info, sd->call_duration);
743                        else
744                                imcb_log(ic,
745                                        "You finished the call to the user %s.",
746                                        info);
747                        sd->call_out = FALSE;
748                        break;
749                default:
750                        /* Don't be noisy, ignore other statuses for now. */
751                        break;
[9f2f25f]752                }
[62f51ee9]753                sd->call_status = 0;
[9f2f25f]754        }
755}
756
[e200daf]757static void skype_parse_filetransfer(struct im_connection *ic, char *line)
758{
759        struct skype_data *sd = ic->proto_data;
760        char *id = strchr(line, ' ');
761
[6b9d22a]762        if (!++id)
763                return;
764        char *info = strchr(id, ' ');
765
766        if (!info)
767                return;
768        *info = '\0';
769        info++;
770        if (!strcmp(info, "STATUS NEW")) {
[1f4fc80]771                skype_printf(ic, "GET FILETRANSFER %s PARTNER_HANDLE\n",
[62f51ee9]772                        id);
[6b9d22a]773                sd->filetransfer_status = SKYPE_FILETRANSFER_NEW;
774        } else if (!strcmp(info, "STATUS FAILED")) {
[1f4fc80]775                skype_printf(ic, "GET FILETRANSFER %s PARTNER_HANDLE\n",
[62f51ee9]776                        id);
[6b9d22a]777                sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED;
778        } else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
779                info += 15;
[62f51ee9]780                if (!sd->filetransfer_status)
781                        return;
782                switch (sd->filetransfer_status) {
783                case SKYPE_FILETRANSFER_NEW:
784                        imcb_log(ic, "The user %s offered a new file for you.",
785                                info);
786                        break;
787                case SKYPE_FILETRANSFER_FAILED:
788                        imcb_log(ic, "Failed to transfer file from user %s.",
789                                info);
790                        break;
[e200daf]791                }
[62f51ee9]792                sd->filetransfer_status = 0;
[e200daf]793        }
794}
795
[fbb15f2]796static struct skype_group *skype_group_by_id(struct im_connection *ic, int id)
797{
[89d6845]798        struct skype_data *sd = ic->proto_data;
799        int i;
800
801        for (i = 0; i < g_list_length(sd->groups); i++) {
802                struct skype_group *sg = (struct skype_group *)g_list_nth_data(sd->groups, i);
803
804                if (sg->id == id)
805                        return sg;
806        }
807        return NULL;
808}
809
[fbb15f2]810static void skype_group_free(struct skype_group *sg, gboolean usersonly)
811{
[89d6845]812        int i;
[fbb15f2]813
[89d6845]814        for (i = 0; i < g_list_length(sg->users); i++) {
815                char *user = g_list_nth_data(sg->users, i);
816                g_free(user);
817        }
818        sg->users = NULL;
819        if (usersonly)
820                return;
821        g_free(sg->name);
822        g_free(sg);
823}
824
[54ca269]825/* Update the group of each user in this group */
[fbb15f2]826static void skype_group_users(struct im_connection *ic, struct skype_group *sg)
827{
[54ca269]828        int i;
829
830        for (i = 0; i < g_list_length(sg->users); i++) {
831                char *user = g_list_nth_data(sg->users, i);
832                char *buf = g_strdup_printf("%s@skype.com", user);
833                imcb_add_buddy(ic, buf, sg->name);
834                g_free(buf);
835        }
836}
837
[89d6845]838static void skype_parse_group(struct im_connection *ic, char *line)
839{
840        struct skype_data *sd = ic->proto_data;
841        char *id = strchr(line, ' ');
842
843        if (!++id)
844                return;
845
846        char *info = strchr(id, ' ');
847
848        if (!info)
849                return;
850        *info = '\0';
851        info++;
852
853        if (!strncmp(info, "DISPLAYNAME ", 12)) {
854                info += 12;
855
856                /* Name given for a group ID: try to update it or insert a new
857                 * one if not found */
858                struct skype_group *sg = skype_group_by_id(ic, atoi(id));
859                if (sg) {
860                        g_free(sg->name);
861                        sg->name = g_strdup(info);
862                } else {
863                        sg = g_new0(struct skype_group, 1);
864                        sg->id = atoi(id);
865                        sg->name = g_strdup(info);
866                        sd->groups = g_list_append(sd->groups, sg);
867                }
868        } else if (!strncmp(info, "USERS ", 6)) {
869                struct skype_group *sg = skype_group_by_id(ic, atoi(id));
870
871                if (sg) {
872                        char **i;
873                        char **users = g_strsplit(info + 6, ", ", 0);
874
875                        skype_group_free(sg, TRUE);
876                        i = users;
877                        while (*i) {
878                                sg->users = g_list_append(sg->users, g_strdup(*i));
879                                i++;
880                        }
881                        g_strfreev(users);
[54ca269]882                        skype_group_users(ic, sg);
[89d6845]883                } else
[fbb15f2]884                        log_message(LOGLVL_ERROR,
885                                "No skype group with id %s. That's probably a bug.", id);
[46641bf]886        } else if (!strncmp(info, "NROFUSERS ", 10)) {
887                if (!sd->pending_user) {
888                        /* Number of users changed in this group, query its type to see
889                         * if it's a custom one we should care about. */
890                        skype_printf(ic, "GET GROUP %s TYPE", id);
891                        return;
892                }
893
894                /* This is a newly created group, we have a single user
895                 * to add. */
896                struct skype_group *sg = skype_group_by_id(ic, atoi(id));
897
898                if (sg) {
899                        skype_printf(ic, "ALTER GROUP %d ADDUSER %s", sg->id, sd->pending_user);
900                        g_free(sd->pending_user);
901                        sd->pending_user = NULL;
902                } else
903                        log_message(LOGLVL_ERROR,
904                                        "No skype group with id %s. That's probably a bug.", id);
[2b183e4]905        } else if (!strcmp(info, "TYPE CUSTOM_GROUP"))
[cb6d3c9]906                /* This one is interesting, query its users. */
907                skype_printf(ic, "GET GROUP %s USERS", id);
[89d6845]908}
909
[c35bf7a]910static void skype_parse_chat(struct im_connection *ic, char *line)
911{
912        struct skype_data *sd = ic->proto_data;
[c213d6b]913        char buf[IRC_LINE_SIZE];
[c35bf7a]914        char *id = strchr(line, ' ');
915
[6b9d22a]916        if (!++id)
917                return;
918        struct groupchat *gc;
919        char *info = strchr(id, ' ');
[c35bf7a]920
[6b9d22a]921        if (!info)
922                return;
923        *info = '\0';
924        info++;
925        /* Remove fake chat if we created one in skype_chat_with() */
[451f121]926        gc = bee_chat_by_title(ic->bee, ic, "");
[6b9d22a]927        if (gc)
928                imcb_chat_free(gc);
929        if (!strcmp(info, "STATUS MULTI_SUBSCRIBED")) {
[c573f1b]930                gc = bee_chat_by_title(ic->bee, ic, id);
931                if (!gc) {
932                        gc = imcb_chat_new(ic, id);
933                        imcb_chat_name_hint(gc, id);
934                }
[1f4fc80]935                skype_printf(ic, "GET CHAT %s ADDER\n", id);
936                skype_printf(ic, "GET CHAT %s TOPIC\n", id);
[6b9d22a]937        } else if (!strcmp(info, "STATUS DIALOG") && sd->groupchat_with) {
938                gc = imcb_chat_new(ic, id);
[72b60c7e]939                imcb_chat_name_hint(gc, id);
[6b9d22a]940                /* According to the docs this
941                 * is necessary. However it
942                 * does not seem the situation
943                 * and it would open an extra
944                 * window on our client, so
945                 * just leave it out. */
[1f4fc80]946                /*skype_printf(ic, "OPEN CHAT %s\n", id);*/
[5d9db76]947                g_snprintf(buf, IRC_LINE_SIZE, "%s@skype.com",
948                                sd->groupchat_with);
[6b9d22a]949                imcb_chat_add_buddy(gc, buf);
950                imcb_chat_add_buddy(gc, sd->username);
951                g_free(sd->groupchat_with);
952                sd->groupchat_with = NULL;
[1f4fc80]953                skype_printf(ic, "GET CHAT %s ADDER\n", id);
954                skype_printf(ic, "GET CHAT %s TOPIC\n", id);
[6b9d22a]955        } else if (!strcmp(info, "STATUS UNSUBSCRIBED")) {
[451f121]956                gc = bee_chat_by_title(ic->bee, ic, id);
[c35bf7a]957                if (gc)
[6b9d22a]958                        gc->data = (void *)FALSE;
959        } else if (!strncmp(info, "ADDER ", 6)) {
960                info += 6;
961                g_free(sd->adder);
962                sd->adder = g_strdup_printf("%s@skype.com", info);
963        } else if (!strncmp(info, "TOPIC ", 6)) {
964                info += 6;
[451f121]965                gc = bee_chat_by_title(ic->bee, ic, id);
[6b9d22a]966                if (gc && (sd->adder || sd->topic_wait)) {
967                        if (sd->topic_wait) {
968                                sd->adder = g_strdup(sd->username);
969                                sd->topic_wait = 0;
[c35bf7a]970                        }
[6b9d22a]971                        imcb_chat_topic(gc, sd->adder, info, 0);
972                        g_free(sd->adder);
973                        sd->adder = NULL;
974                }
975        } else if (!strncmp(info, "ACTIVEMEMBERS ", 14)) {
976                info += 14;
[451f121]977                gc = bee_chat_by_title(ic->bee, ic, id);
[6b9d22a]978                /* Hack! We set ->data to TRUE
979                 * while we're on the channel
980                 * so that we won't rejoin
981                 * after a /part. */
[62f51ee9]982                if (!gc || gc->data)
983                        return;
984                char **members = g_strsplit(info, " ", 0);
985                int i;
986                for (i = 0; members[i]; i++) {
987                        if (!strcmp(members[i], sd->username))
988                                continue;
[5d9db76]989                        g_snprintf(buf, IRC_LINE_SIZE, "%s@skype.com",
990                                        members[i]);
[62f51ee9]991                        if (!g_list_find_custom(gc->in_room, buf,
992                                (GCompareFunc)strcmp))
993                                imcb_chat_add_buddy(gc, buf);
[c35bf7a]994                }
[62f51ee9]995                imcb_chat_add_buddy(gc, sd->username);
996                g_strfreev(members);
[c35bf7a]997        }
998}
999
[2709f4c]1000static void skype_parse_password(struct im_connection *ic, char *line)
1001{
1002        if (!strncmp(line+9, "OK", 2))
1003                imcb_connected(ic);
1004        else {
1005                imcb_error(ic, "Authentication Failed");
1006                imc_logout(ic, TRUE);
1007        }
1008}
1009
[607f5e3]1010static void skype_parse_profile(struct im_connection *ic, char *line)
1011{
1012        imcb_log(ic, "SkypeOut balance value is '%s'.", line+21);
1013}
1014
1015static void skype_parse_ping(struct im_connection *ic, char *line)
1016{
[4ae3ffc]1017        /* Unused parameter */
1018        line = line;
[1f4fc80]1019        skype_printf(ic, "PONG\n");
[607f5e3]1020}
1021
1022static void skype_parse_chats(struct im_connection *ic, char *line)
1023{
1024        char **i;
1025        char **chats = g_strsplit(line + 6, ", ", 0);
1026
1027        i = chats;
1028        while (*i) {
[1f4fc80]1029                skype_printf(ic, "GET CHAT %s STATUS\n", *i);
1030                skype_printf(ic, "GET CHAT %s ACTIVEMEMBERS\n", *i);
[607f5e3]1031                i++;
1032        }
1033        g_strfreev(chats);
1034}
1035
[8b8d1bed]1036static void skype_parse_groups(struct im_connection *ic, char *line)
1037{
1038        char **i;
1039        char **groups = g_strsplit(line + 7, ", ", 0);
1040
1041        i = groups;
1042        while (*i) {
1043                skype_printf(ic, "GET GROUP %s DISPLAYNAME\n", *i);
1044                skype_printf(ic, "GET GROUP %s USERS\n", *i);
1045                i++;
1046        }
1047        g_strfreev(groups);
1048}
1049
[46e9822]1050static void skype_parse_alter_group(struct im_connection *ic, char *line)
1051{
1052        char *id = line + strlen("ALTER GROUP");
1053
1054        if (!++id)
1055                return;
1056
1057        char *info = strchr(id, ' ');
1058
1059        if (!info)
1060                return;
1061        *info = '\0';
1062        info++;
1063
1064        if (!strncmp(info, "ADDUSER ", 8)) {
1065                struct skype_group *sg = skype_group_by_id(ic, atoi(id));
1066
1067                info += 8;
1068                if (sg) {
1069                        char *buf = g_strdup_printf("%s@skype.com", info);
1070                        sg->users = g_list_append(sg->users, g_strdup(info));
1071                        imcb_add_buddy(ic, buf, sg->name);
1072                        g_free(buf);
1073                } else
1074                        log_message(LOGLVL_ERROR,
1075                                "No skype group with id %s. That's probably a bug.", id);
1076        }
1077}
1078
[ff436ba]1079typedef void (*skype_parser)(struct im_connection *ic, char *line);
1080
[8c09bb3]1081static gboolean skype_read_callback(gpointer data, gint fd,
1082                                    b_input_condition cond)
[1323e36]1083{
1084        struct im_connection *ic = data;
1085        struct skype_data *sd = ic->proto_data;
[c213d6b]1086        char buf[IRC_LINE_SIZE];
[ff436ba]1087        int st, i;
[6e14204]1088        char **lines, **lineptr, *line;
[ff436ba]1089        static struct parse_map {
1090                char *k;
1091                skype_parser v;
1092        } parsers[] = {
1093                { "USERS ", skype_parse_users },
1094                { "USER ", skype_parse_user },
1095                { "CHATMESSAGE ", skype_parse_chatmessage },
1096                { "CALL ", skype_parse_call },
1097                { "FILETRANSFER ", skype_parse_filetransfer },
1098                { "CHAT ", skype_parse_chat },
[89d6845]1099                { "GROUP ", skype_parse_group },
[ff436ba]1100                { "PASSWORD ", skype_parse_password },
1101                { "PROFILE PSTN_BALANCE ", skype_parse_profile },
1102                { "PING", skype_parse_ping },
1103                { "CHATS ", skype_parse_chats },
[8b8d1bed]1104                { "GROUPS ", skype_parse_groups },
[46e9822]1105                { "ALTER GROUP ", skype_parse_alter_group },
[ff436ba]1106        };
[1323e36]1107
[4ae3ffc]1108        /* Unused parameters */
1109        fd = fd;
1110        cond = cond;
1111
[5adcc65]1112        if (!sd || sd->fd == -1)
[1323e36]1113                return FALSE;
[7daec06]1114        /* Read the whole data. */
[5adcc65]1115        st = ssl_read(sd->ssl, buf, sizeof(buf));
1116        if (st > 0) {
[1323e36]1117                buf[st] = '\0';
[7daec06]1118                /* Then split it up to lines. */
[9fd4241]1119                lines = g_strsplit(buf, "\n", 0);
1120                lineptr = lines;
[5adcc65]1121                while ((line = *lineptr)) {
1122                        if (!strlen(line))
[9fd4241]1123                                break;
[b820226]1124                        if (set_getbool(&ic->acc->set, "skypeconsole_receive"))
1125                                imcb_buddy_msg(ic, "skypeconsole", line, 0, 0);
[6b9d22a]1126                        for (i = 0; i < ARRAY_SIZE(parsers); i++)
1127                                if (!strncmp(line, parsers[i].k,
1128                                        strlen(parsers[i].k))) {
[ff436ba]1129                                        parsers[i].v(ic, line);
1130                                        break;
1131                                }
[9fd4241]1132                        lineptr++;
1133                }
1134                g_strfreev(lines);
[5adcc65]1135        } else if (st == 0 || (st < 0 && !sockerr_again())) {
1136                closesocket(sd->fd);
[1323e36]1137                sd->fd = -1;
1138
[5adcc65]1139                imcb_error(ic, "Error while reading from server");
1140                imc_logout(ic, TRUE);
[1323e36]1141                return FALSE;
1142        }
1143        return TRUE;
1144}
1145
[5adcc65]1146gboolean skype_start_stream(struct im_connection *ic)
[f06e3ac]1147{
[1323e36]1148        struct skype_data *sd = ic->proto_data;
[f06e3ac]1149        int st;
1150
[5adcc65]1151        if (!sd)
[1fb89e3]1152                return FALSE;
1153
[5adcc65]1154        if (sd->bfd <= 0)
[7670b02]1155                sd->bfd = b_input_add(sd->fd, B_EV_IO_READ,
[8c09bb3]1156                        skype_read_callback, ic);
[1323e36]1157
[a0b206b]1158        /* Log in */
[1f4fc80]1159        skype_printf(ic, "USERNAME %s\n", ic->acc->user);
1160        skype_printf(ic, "PASSWORD %s\n", ic->acc->pass);
[a0b206b]1161
[8b8d1bed]1162        /* This will download all buddies and groups. */
[54ca269]1163        st = skype_printf(ic, "SEARCH GROUPS CUSTOM\n");
1164        skype_printf(ic, "SEARCH FRIENDS\n");
[8b8d1bed]1165
[1f4fc80]1166        skype_printf(ic, "SET USERSTATUS ONLINE\n");
[5acf9ab]1167
1168        /* Auto join to bookmarked chats if requested.*/
[1f4fc80]1169        if (set_getbool(&ic->acc->set, "auto_join"))
1170                skype_printf(ic, "SEARCH BOOKMARKEDCHATS\n");
[f06e3ac]1171        return st;
1172}
1173
[486ddb5]1174gboolean skype_connected(gpointer data, int returncode, void *source, b_input_condition cond)
[f06e3ac]1175{
1176        struct im_connection *ic = data;
[c7304b2]1177        struct skype_data *sd = ic->proto_data;
[4ae3ffc]1178
1179        /* Unused parameter */
1180        cond = cond;
1181
[5adcc65]1182        if (!source) {
[c7304b2]1183                sd->ssl = NULL;
[5adcc65]1184                imcb_error(ic, "Could not connect to server");
1185                imc_logout(ic, TRUE);
[c7304b2]1186                return FALSE;
1187        }
[5adcc65]1188        imcb_log(ic, "Connected to server, logging in");
[4ae3ffc]1189
[f06e3ac]1190        return skype_start_stream(ic);
1191}
1192
[5adcc65]1193static void skype_login(account_t *acc)
[f06e3ac]1194{
[5adcc65]1195        struct im_connection *ic = imcb_new(acc);
1196        struct skype_data *sd = g_new0(struct skype_data, 1);
[f06e3ac]1197
1198        ic->proto_data = sd;
1199
[5adcc65]1200        imcb_log(ic, "Connecting");
[8c09bb3]1201        sd->ssl = ssl_connect(set_getstr(&acc->set, "server"),
[a72dc2b]1202                set_getint(&acc->set, "port"), FALSE, skype_connected, ic);
[5adcc65]1203        sd->fd = sd->ssl ? ssl_getfd(sd->ssl) : -1;
1204        sd->username = g_strdup(acc->user);
[f06e3ac]1205
1206        sd->ic = ic;
[08a355b]1207
1208        if (set_getbool(&acc->set, "skypeconsole"))
1209                imcb_add_buddy(ic, "skypeconsole", NULL);
[f06e3ac]1210}
1211
[5adcc65]1212static void skype_logout(struct im_connection *ic)
[f06e3ac]1213{
1214        struct skype_data *sd = ic->proto_data;
[89d6845]1215        int i;
[98bca36]1216
[1f4fc80]1217        skype_printf(ic, "SET USERSTATUS OFFLINE\n");
[98bca36]1218
[bd11422]1219        while( ic->groupchats )
1220                imcb_chat_free(ic->groupchats->data);
1221
[89d6845]1222        for (i = 0; i < g_list_length(sd->groups); i++) {
1223                struct skype_group *sg = (struct skype_group *)g_list_nth_data(sd->groups, i);
1224                skype_group_free(sg, FALSE);
1225        }
[a3d6427]1226        g_free(sd->username);
[7613670]1227        g_free(sd->handle);
[f06e3ac]1228        g_free(sd);
[98bca36]1229        ic->proto_data = NULL;
[f06e3ac]1230}
1231
[8c09bb3]1232static int skype_buddy_msg(struct im_connection *ic, char *who, char *message,
1233                           int flags)
[93ece66]1234{
[1f4fc80]1235        char *ptr, *nick;
[93ece66]1236        int st;
1237
[4ae3ffc]1238        /* Unused parameter */
1239        flags = flags;
1240
[cbec0d6]1241        nick = g_strdup(who);
[77c1abe]1242        ptr = strchr(nick, '@');
[5adcc65]1243        if (ptr)
[0bb1b7f]1244                *ptr = '\0';
[93ece66]1245
[08a355b]1246        if (!strncmp(who, "skypeconsole", 12))
[1f4fc80]1247                st = skype_printf(ic, "%s\n", message);
[08a355b]1248        else
[1f4fc80]1249                st = skype_printf(ic, "MESSAGE %s %s\n", nick, message);
[77c1abe]1250        g_free(nick);
[93ece66]1251
1252        return st;
1253}
1254
[5adcc65]1255const struct skype_away_state *skype_away_state_by_name(char *name)
[23411c6]1256{
1257        int i;
1258
[5adcc65]1259        for (i = 0; skype_away_state_list[i].full_name; i++)
1260                if (g_strcasecmp(skype_away_state_list[i].full_name, name) == 0)
1261                        return skype_away_state_list + i;
[23411c6]1262
1263        return NULL;
1264}
1265
[8c09bb3]1266static void skype_set_away(struct im_connection *ic, char *state_txt,
1267                           char *message)
[f06e3ac]1268{
[23411c6]1269        const struct skype_away_state *state;
1270
[4ae3ffc]1271        /* Unused parameter */
1272        message = message;
1273
[4b740c2]1274        if (state_txt == NULL)
1275                state = skype_away_state_by_name("Online");
[23411c6]1276        else
[5adcc65]1277                state = skype_away_state_by_name(state_txt);
[1f4fc80]1278        skype_printf(ic, "SET USERSTATUS %s\n", state->code);
[f06e3ac]1279}
1280
[5adcc65]1281static GList *skype_away_states(struct im_connection *ic)
[f06e3ac]1282{
[5adcc65]1283        static GList *l;
[adce2de]1284        int i;
[5adcc65]1285
[4ae3ffc]1286        /* Unused parameter */
1287        ic = ic;
1288
[5adcc65]1289        if (l == NULL)
1290                for (i = 0; skype_away_state_list[i].full_name; i++)
[8c09bb3]1291                        l = g_list_append(l,
1292                                (void *)skype_away_state_list[i].full_name);
[5adcc65]1293
[f06e3ac]1294        return l;
1295}
1296
[5adcc65]1297static char *skype_set_display_name(set_t *set, char *value)
[93dffea]1298{
1299        account_t *acc = set->data;
1300        struct im_connection *ic = acc->ic;
1301
[1f4fc80]1302        skype_printf(ic, "SET PROFILE FULLNAME %s", value);
[5adcc65]1303        return value;
[93dffea]1304}
1305
[5adcc65]1306static char *skype_set_balance(set_t *set, char *value)
[2af671a]1307{
1308        account_t *acc = set->data;
1309        struct im_connection *ic = acc->ic;
1310
[1f4fc80]1311        skype_printf(ic, "GET PROFILE PSTN_BALANCE");
[5adcc65]1312        return value;
[2af671a]1313}
1314
[fbb15f2]1315static void skype_call(struct im_connection *ic, char *value)
1316{
[71c4bb6]1317        char *nick = g_strdup(value);
1318        char *ptr = strchr(nick, '@');
1319
1320        if (ptr)
1321                *ptr = '\0';
1322        skype_printf(ic, "CALL %s", nick);
1323        g_free(nick);
1324}
1325
1326static void skype_hangup(struct im_connection *ic)
1327{
1328        struct skype_data *sd = ic->proto_data;
1329
1330        if (sd->call_id) {
1331                skype_printf(ic, "SET CALL %s STATUS FINISHED",
1332                                sd->call_id);
1333                g_free(sd->call_id);
1334                sd->call_id = 0;
1335        } else
1336                imcb_error(ic, "There are no active calls currently.");
1337}
1338
[5adcc65]1339static char *skype_set_call(set_t *set, char *value)
[b68b023]1340{
1341        account_t *acc = set->data;
1342        struct im_connection *ic = acc->ic;
1343
[fbb15f2]1344        if (value)
[451f121]1345                skype_call(ic, value);
[fbb15f2]1346        else
[71c4bb6]1347                skype_hangup(ic);
[5adcc65]1348        return value;
[b68b023]1349}
1350
[5adcc65]1351static void skype_add_buddy(struct im_connection *ic, char *who, char *group)
[f06e3ac]1352{
[46641bf]1353        struct skype_data *sd = ic->proto_data;
[1f4fc80]1354        char *nick, *ptr;
[6627d92]1355
[cbec0d6]1356        nick = g_strdup(who);
[6627d92]1357        ptr = strchr(nick, '@');
[5adcc65]1358        if (ptr)
[6627d92]1359                *ptr = '\0';
[46e9822]1360
1361        if (!group) {
1362                skype_printf(ic, "SET USER %s BUDDYSTATUS 2 Please authorize me\n",
1363                                nick);
1364                g_free(nick);
1365        } else {
1366                struct skype_group *sg = skype_group_by_name(ic, group);
1367
1368                if (!sg) {
1369                        /* No such group, we need to create it, then have to
1370                         * add the user once it's created. */
[46641bf]1371                        skype_printf(ic, "CREATE GROUP %s", group);
1372                        sd->pending_user = g_strdup(nick);
[46e9822]1373                } else {
1374                        skype_printf(ic, "ALTER GROUP %d ADDUSER %s", sg->id, nick);
1375                }
1376        }
[f06e3ac]1377}
1378
[5adcc65]1379static void skype_remove_buddy(struct im_connection *ic, char *who, char *group)
[f06e3ac]1380{
[1f4fc80]1381        char *nick, *ptr;
[6627d92]1382
[4ae3ffc]1383        /* Unused parameter */
1384        group = group;
1385
[cbec0d6]1386        nick = g_strdup(who);
[6627d92]1387        ptr = strchr(nick, '@');
[5adcc65]1388        if (ptr)
[6627d92]1389                *ptr = '\0';
[1f4fc80]1390        skype_printf(ic, "SET USER %s BUDDYSTATUS 1\n", nick);
[6627d92]1391        g_free(nick);
[f06e3ac]1392}
1393
[5adcc65]1394void skype_chat_msg(struct groupchat *gc, char *message, int flags)
[66c9558]1395{
[79e20f9]1396        struct im_connection *ic = gc->ic;
[4ae3ffc]1397
1398        /* Unused parameter */
1399        flags = flags;
1400
[1f4fc80]1401        skype_printf(ic, "CHATMESSAGE %s %s\n", gc->title, message);
[66c9558]1402}
1403
[5adcc65]1404void skype_chat_leave(struct groupchat *gc)
[b01dc6c]1405{
1406        struct im_connection *ic = gc->ic;
[1f4fc80]1407        skype_printf(ic, "ALTER CHAT %s LEAVE\n", gc->title);
[5adcc65]1408        gc->data = (void *)TRUE;
[760319d]1409}
1410
1411void skype_chat_invite(struct groupchat *gc, char *who, char *message)
1412{
1413        struct im_connection *ic = gc->ic;
[1f4fc80]1414        char *ptr, *nick;
[4ae3ffc]1415
[17dd2ed]1416        nick = g_strdup(who);
[760319d]1417        ptr = strchr(nick, '@');
[5adcc65]1418        if (ptr)
[760319d]1419                *ptr = '\0';
[1f4fc80]1420        skype_printf(ic, "ALTER CHAT %s ADDMEMBERS %s\n", gc->title, nick);
[760319d]1421        g_free(nick);
[b01dc6c]1422}
1423
[09e2a69]1424void skype_chat_topic(struct groupchat *gc, char *message)
1425{
1426        struct im_connection *ic = gc->ic;
[a5f76a2]1427        struct skype_data *sd = ic->proto_data;
[1f4fc80]1428        skype_printf(ic, "ALTER CHAT %s SETTOPIC %s\n",
[8c09bb3]1429                gc->title, message);
[a5f76a2]1430        sd->topic_wait = 1;
[09e2a69]1431}
1432
[86278cd]1433struct groupchat *skype_chat_with(struct im_connection *ic, char *who)
1434{
1435        struct skype_data *sd = ic->proto_data;
[1f4fc80]1436        char *ptr, *nick;
[86278cd]1437        nick = g_strdup(who);
1438        ptr = strchr(nick, '@');
[5adcc65]1439        if (ptr)
[86278cd]1440                *ptr = '\0';
[1f4fc80]1441        skype_printf(ic, "CHAT CREATE %s\n", nick);
[86278cd]1442        sd->groupchat_with = g_strdup(nick);
1443        g_free(nick);
[5652d43]1444        /* We create a fake chat for now. We will replace it with a real one in
1445         * the real callback. */
[5adcc65]1446        return imcb_chat_new(ic, "");
[86278cd]1447}
1448
[67454bd]1449static void skype_get_info(struct im_connection *ic, char *who)
1450{
[1f4fc80]1451        char *ptr, *nick;
[67454bd]1452        nick = g_strdup(who);
1453        ptr = strchr(nick, '@');
[5adcc65]1454        if (ptr)
[67454bd]1455                *ptr = '\0';
[1f4fc80]1456        skype_printf(ic, "GET USER %s FULLNAME\n", nick);
1457        skype_printf(ic, "GET USER %s PHONE_HOME\n", nick);
1458        skype_printf(ic, "GET USER %s PHONE_OFFICE\n", nick);
1459        skype_printf(ic, "GET USER %s PHONE_MOBILE\n", nick);
1460        skype_printf(ic, "GET USER %s NROF_AUTHED_BUDDIES\n", nick);
1461        skype_printf(ic, "GET USER %s TIMEZONE\n", nick);
1462        skype_printf(ic, "GET USER %s LASTONLINETIMESTAMP\n", nick);
1463        skype_printf(ic, "GET USER %s BIRTHDAY\n", nick);
1464        skype_printf(ic, "GET USER %s SEX\n", nick);
1465        skype_printf(ic, "GET USER %s LANGUAGE\n", nick);
1466        skype_printf(ic, "GET USER %s COUNTRY\n", nick);
1467        skype_printf(ic, "GET USER %s PROVINCE\n", nick);
1468        skype_printf(ic, "GET USER %s CITY\n", nick);
1469        skype_printf(ic, "GET USER %s HOMEPAGE\n", nick);
1470        skype_printf(ic, "GET USER %s ABOUT\n", nick);
[67454bd]1471}
1472
[5adcc65]1473static void skype_set_my_name(struct im_connection *ic, char *info)
[93dffea]1474{
[5adcc65]1475        skype_set_display_name(set_find(&ic->acc->set, "display_name"), info);
[93dffea]1476}
1477
[5adcc65]1478static void skype_init(account_t *acc)
[93dffea]1479{
1480        set_t *s;
1481
[8c09bb3]1482        s = set_add(&acc->set, "server", SKYPE_DEFAULT_SERVER, set_eval_account,
1483                acc);
[93dffea]1484        s->flags |= ACC_SET_OFFLINE_ONLY;
1485
[5adcc65]1486        s = set_add(&acc->set, "port", SKYPE_DEFAULT_PORT, set_eval_int, acc);
[93dffea]1487        s->flags |= ACC_SET_OFFLINE_ONLY;
1488
[8c09bb3]1489        s = set_add(&acc->set, "display_name", NULL, skype_set_display_name,
1490                acc);
[93dffea]1491        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
[b68b023]1492
[5adcc65]1493        s = set_add(&acc->set, "call", NULL, skype_set_call, acc);
[b68b023]1494        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
[2af671a]1495
[5adcc65]1496        s = set_add(&acc->set, "balance", NULL, skype_set_balance, acc);
[2af671a]1497        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
[bd417a1]1498
[5adcc65]1499        s = set_add(&acc->set, "skypeout_offline", "true", set_eval_bool, acc);
[08a355b]1500
[5adcc65]1501        s = set_add(&acc->set, "skypeconsole", "false", set_eval_bool, acc);
[08a355b]1502        s->flags |= ACC_SET_OFFLINE_ONLY;
[5acf9ab]1503
[8c09bb3]1504        s = set_add(&acc->set, "skypeconsole_receive", "false", set_eval_bool,
1505                acc);
[b820226]1506        s->flags |= ACC_SET_OFFLINE_ONLY;
1507
[5adcc65]1508        s = set_add(&acc->set, "auto_join", "false", set_eval_bool, acc);
[5acf9ab]1509        s->flags |= ACC_SET_OFFLINE_ONLY;
[f4d37c6]1510
[49a3c02]1511        s = set_add(&acc->set, "test_join", "false", set_eval_bool, acc);
1512        s->flags |= ACC_SET_OFFLINE_ONLY;
1513
[304aa33]1514        s = set_add(&acc->set, "show_moods", "false", set_eval_bool, acc);
1515
[f4d37c6]1516        s = set_add(&acc->set, "edit_prefix", "EDIT:",
[1e3120f]1517                        NULL, acc);
[93dffea]1518}
1519
[c6e0218]1520#if BITLBEE_VERSION_CODE > BITLBEE_VER(3, 0, 1)
[fbb15f2]1521GList *skype_buddy_action_list(bee_user_t *bu)
[71c4bb6]1522{
[fbb15f2]1523        static GList *ret;
[71c4bb6]1524
[a5e6aa1]1525        /* Unused parameter */
1526        bu = bu;
1527
[fbb15f2]1528        if (ret == NULL) {
[71c4bb6]1529                static const struct buddy_action ba[3] = {
1530                        {"CALL", "Initiate a call" },
1531                        {"HANGUP", "Hang up a call" },
1532                };
1533
[fbb15f2]1534                ret = g_list_prepend(ret, (void *) ba + 0);
[71c4bb6]1535        }
1536
1537        return ret;
1538}
1539
[fbb15f2]1540void *skype_buddy_action(struct bee_user *bu, const char *action, char * const args[], void *data)
[71c4bb6]1541{
[a5e6aa1]1542        /* Unused parameters */
1543        args = args;
1544        data = data;
1545
[fbb15f2]1546        if (!g_strcasecmp(action, "CALL"))
[71c4bb6]1547                skype_call(bu->ic, bu->handle);
[fbb15f2]1548        else if (!g_strcasecmp(action, "HANGUP"))
[71c4bb6]1549                skype_hangup(bu->ic);
1550
1551        return NULL;
1552}
1553#endif
1554
[f06e3ac]1555void init_plugin(void)
1556{
[5adcc65]1557        struct prpl *ret = g_new0(struct prpl, 1);
[f06e3ac]1558
1559        ret->name = "skype";
1560        ret->login = skype_login;
1561        ret->init = skype_init;
1562        ret->logout = skype_logout;
[93ece66]1563        ret->buddy_msg = skype_buddy_msg;
[67454bd]1564        ret->get_info = skype_get_info;
[93dffea]1565        ret->set_my_name = skype_set_my_name;
[f06e3ac]1566        ret->away_states = skype_away_states;
[7daec06]1567        ret->set_away = skype_set_away;
[f06e3ac]1568        ret->add_buddy = skype_add_buddy;
1569        ret->remove_buddy = skype_remove_buddy;
[66c9558]1570        ret->chat_msg = skype_chat_msg;
[b01dc6c]1571        ret->chat_leave = skype_chat_leave;
[760319d]1572        ret->chat_invite = skype_chat_invite;
[86278cd]1573        ret->chat_with = skype_chat_with;
[f06e3ac]1574        ret->handle_cmp = g_strcasecmp;
[09e2a69]1575        ret->chat_topic = skype_chat_topic;
[c6e0218]1576#if BITLBEE_VERSION_CODE > BITLBEE_VER(3, 0, 1)
[71c4bb6]1577        ret->buddy_action_list = skype_buddy_action_list;
1578        ret->buddy_action = skype_buddy_action;
1579#endif
[5adcc65]1580        register_protocol(ret);
[f06e3ac]1581}
Note: See TracBrowser for help on using the repository browser.