source: skype/skype.c @ 46e9822

Last change on this file since 46e9822 was 46e9822, checked in by Miklos Vajna <vmiklos@…>, at 2011-01-03T02:47:40Z

Make /invite work for existing groups

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