source: protocols/skype/skype.c @ ce70e88

Last change on this file since ce70e88 was 5ebff60, checked in by dequis <dx@…>, at 2015-02-20T22:50:54Z

Reindent everything to K&R style with tabs

Used uncrustify, with the configuration file in ./doc/uncrustify.cfg

Commit author set to "Indent <please@…>" so that it's easier to
skip while doing git blame.

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