source: protocols/skype/skype.c @ a72dc2b

Last change on this file since a72dc2b was a72dc2b, checked in by Wilmer van der Gaast <wilmer@…>, at 2011-12-19T17:57:20Z

Add verify argument to ssl_connect() so HTTPS-based stuff is also secure.
(Think of Twitter, but also MSN/Yahoo! authentication.)

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