source: protocols/skype/skype.c @ 43b1cd7

Last change on this file since 43b1cd7 was 43b1cd7, checked in by Miklos Vajna <vmiklos@…>, at 2011-04-12T00:10:57Z

Merge bitlbee-skype into bitlbee

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