source: protocols/skype/skype.c @ c7336ba

Last change on this file since c7336ba was c7336ba, checked in by Miklos Vajna <vmiklos@…>, at 2013-02-16T14:03:56Z

skype_buddy_action_list: fix missing hangup in help output

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