source: protocols/skype/skype.c @ 486ddb5

Last change on this file since 486ddb5 was 486ddb5, checked in by Wilmer van der Gaast <wilmer@…>, at 2011-12-19T14:50:58Z

Initial merge of tls_verify patch from AopicieR.

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