source: protocols/skype/skype.c @ 7c2daf5f

Last change on this file since 7c2daf5f was 7c2daf5f, checked in by Miklos Vajna <vmiklos@…>, at 2012-01-27T23:16:15Z

skype: don't crash in skype_parse_user() if the user has multiple about lines

  • Property mode set to 100644
File size: 40.0 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                        sd->info_fullname = NULL;
413                }
414                if (sd->info_phonehome) {
415                        if (strlen(sd->info_phonehome))
416                                g_string_append_printf(st, "Home Phone: %s\n",
417                                        sd->info_phonehome);
418                        g_free(sd->info_phonehome);
419                        sd->info_phonehome = NULL;
420                }
421                if (sd->info_phoneoffice) {
422                        if (strlen(sd->info_phoneoffice))
423                                g_string_append_printf(st, "Office Phone: %s\n",
424                                        sd->info_phoneoffice);
425                        g_free(sd->info_phoneoffice);
426                        sd->info_phoneoffice = NULL;
427                }
428                if (sd->info_phonemobile) {
429                        if (strlen(sd->info_phonemobile))
430                                g_string_append_printf(st, "Mobile Phone: %s\n",
431                                        sd->info_phonemobile);
432                        g_free(sd->info_phonemobile);
433                        sd->info_phonemobile = NULL;
434                }
435                g_string_append_printf(st, "Personal Information\n");
436                if (sd->info_nrbuddies) {
437                        if (strlen(sd->info_nrbuddies))
438                                g_string_append_printf(st,
439                                        "Contacts: %s\n", sd->info_nrbuddies);
440                        g_free(sd->info_nrbuddies);
441                        sd->info_nrbuddies = NULL;
442                }
443                if (sd->info_tz) {
444                        if (strlen(sd->info_tz)) {
445                                char ib[256];
446                                time_t t = time(NULL);
447                                t += atoi(sd->info_tz)-(60*60*24);
448                                struct tm *gt = gmtime(&t);
449                                strftime(ib, 256, "%H:%M:%S", gt);
450                                g_string_append_printf(st,
451                                        "Local Time: %s\n", ib);
452                        }
453                        g_free(sd->info_tz);
454                        sd->info_tz = NULL;
455                }
456                if (sd->info_seen) {
457                        if (strlen(sd->info_seen)) {
458                                char ib[256];
459                                time_t it = atoi(sd->info_seen);
460                                struct tm *tm = localtime(&it);
461                                strftime(ib, 256, ("%Y. %m. %d. %H:%M"), tm);
462                                g_string_append_printf(st,
463                                        "Last Seen: %s\n", ib);
464                        }
465                        g_free(sd->info_seen);
466                        sd->info_seen = NULL;
467                }
468                if (sd->info_birthday) {
469                        if (strlen(sd->info_birthday) &&
470                                strcmp(sd->info_birthday, "0")) {
471                                char ib[256];
472                                struct tm tm;
473                                strptime(sd->info_birthday, "%Y%m%d", &tm);
474                                strftime(ib, 256, "%B %d, %Y", &tm);
475                                g_string_append_printf(st,
476                                        "Birthday: %s\n", ib);
477
478                                strftime(ib, 256, "%Y", &tm);
479                                int year = atoi(ib);
480                                time_t t = time(NULL);
481                                struct tm *lt = localtime(&t);
482                                g_string_append_printf(st,
483                                        "Age: %d\n", lt->tm_year+1900-year);
484                        }
485                        g_free(sd->info_birthday);
486                        sd->info_birthday = NULL;
487                }
488                if (sd->info_sex) {
489                        if (strlen(sd->info_sex)) {
490                                char *iptr = sd->info_sex;
491                                while (*iptr++)
492                                        *iptr = tolower(*iptr);
493                                g_string_append_printf(st,
494                                        "Gender: %s\n", sd->info_sex);
495                        }
496                        g_free(sd->info_sex);
497                        sd->info_sex = NULL;
498                }
499                if (sd->info_language) {
500                        if (strlen(sd->info_language)) {
501                                char *iptr = strchr(sd->info_language, ' ');
502                                if (iptr)
503                                        iptr++;
504                                else
505                                        iptr = sd->info_language;
506                                g_string_append_printf(st,
507                                        "Language: %s\n", iptr);
508                        }
509                        g_free(sd->info_language);
510                        sd->info_language = NULL;
511                }
512                if (sd->info_country) {
513                        if (strlen(sd->info_country)) {
514                                char *iptr = strchr(sd->info_country, ' ');
515                                if (iptr)
516                                        iptr++;
517                                else
518                                        iptr = sd->info_country;
519                                g_string_append_printf(st,
520                                        "Country: %s\n", iptr);
521                        }
522                        g_free(sd->info_country);
523                        sd->info_country = NULL;
524                }
525                if (sd->info_province) {
526                        if (strlen(sd->info_province))
527                                g_string_append_printf(st,
528                                        "Region: %s\n", sd->info_province);
529                        g_free(sd->info_province);
530                        sd->info_province = NULL;
531                }
532                if (sd->info_city) {
533                        if (strlen(sd->info_city))
534                                g_string_append_printf(st,
535                                        "City: %s\n", sd->info_city);
536                        g_free(sd->info_city);
537                        sd->info_city = NULL;
538                }
539                if (sd->info_homepage) {
540                        if (strlen(sd->info_homepage))
541                                g_string_append_printf(st,
542                                        "Homepage: %s\n", sd->info_homepage);
543                        g_free(sd->info_homepage);
544                        sd->info_homepage = NULL;
545                }
546                if (sd->info_about) {
547                        if (strlen(sd->info_about))
548                                g_string_append_printf(st, "%s\n",
549                                        sd->info_about);
550                        g_free(sd->info_about);
551                        sd->info_about = NULL;
552                }
553                imcb_log(ic, "%s", st->str);
554                g_string_free(st, TRUE);
555        }
556}
557
558static void skype_parse_chatmessage(struct im_connection *ic, char *line)
559{
560        struct skype_data *sd = ic->proto_data;
561        char buf[IRC_LINE_SIZE];
562        char *id = strchr(line, ' ');
563
564        if (!++id)
565                return;
566        char *info = strchr(id, ' ');
567
568        if (!info)
569                return;
570        *info = '\0';
571        info++;
572        if (!strcmp(info, "STATUS RECEIVED") || !strncmp(info, "EDITED_TIMESTAMP", 16)) {
573                /* New message ID:
574                 * (1) Request its from field
575                 * (2) Request its body
576                 * (3) Request its type
577                 * (4) Query chatname
578                 */
579                skype_printf(ic, "GET CHATMESSAGE %s FROM_HANDLE\n", id);
580                if (!strcmp(info, "STATUS RECEIVED"))
581                        skype_printf(ic, "GET CHATMESSAGE %s BODY\n", id);
582                else
583                        sd->is_edit = 1;
584                skype_printf(ic, "GET CHATMESSAGE %s TYPE\n", id);
585                skype_printf(ic, "GET CHATMESSAGE %s CHATNAME\n", id);
586        } else if (!strncmp(info, "FROM_HANDLE ", 12)) {
587                info += 12;
588                /* New from field value. Store
589                 * it, then we can later use it
590                 * when we got the message's
591                 * body. */
592                g_free(sd->handle);
593                sd->handle = g_strdup_printf("%s@skype.com", info);
594        } else if (!strncmp(info, "EDITED_BY ", 10)) {
595                info += 10;
596                /* This is the same as
597                 * FROM_HANDLE, except that we
598                 * never request these lines
599                 * from Skype, we just get
600                 * them. */
601                g_free(sd->handle);
602                sd->handle = g_strdup_printf("%s@skype.com", info);
603        } else if (!strncmp(info, "BODY ", 5)) {
604                info += 5;
605                sd->body = g_list_append(sd->body, g_strdup(info));
606        }       else if (!strncmp(info, "TYPE ", 5)) {
607                info += 5;
608                g_free(sd->type);
609                sd->type = g_strdup(info);
610        } else if (!strncmp(info, "CHATNAME ", 9)) {
611                info += 9;
612                if (sd->handle && sd->body && sd->type) {
613                        struct groupchat *gc = bee_chat_by_title(ic->bee, ic, info);
614                        int i;
615                        for (i = 0; i < g_list_length(sd->body); i++) {
616                                char *body = g_list_nth_data(sd->body, i);
617                                if (!strcmp(sd->type, "SAID") ||
618                                        !strcmp(sd->type, "EMOTED")) {
619                                        if (!strcmp(sd->type, "SAID")) {
620                                                if (!sd->is_edit)
621                                                        g_snprintf(buf, IRC_LINE_SIZE, "%s",
622                                                                body);
623                                                else {
624                                                        g_snprintf(buf, IRC_LINE_SIZE, "%s %s",
625                                                                set_getstr(&ic->acc->set, "edit_prefix"),
626                                                                body);
627                                                        sd->is_edit = 0;
628                                                }
629                                        } else
630                                                g_snprintf(buf, IRC_LINE_SIZE, "/me %s",
631                                                        body);
632                                        if (!gc)
633                                                /* Private message */
634                                                imcb_buddy_msg(ic,
635                                                        sd->handle, buf, 0, 0);
636                                        else
637                                                /* Groupchat message */
638                                                imcb_chat_msg(gc,
639                                                        sd->handle, buf, 0, 0);
640                                } else if (!strcmp(sd->type, "SETTOPIC") && gc)
641                                        imcb_chat_topic(gc,
642                                                sd->handle, body, 0);
643                                else if (!strcmp(sd->type, "LEFT") && gc)
644                                        imcb_chat_remove_buddy(gc,
645                                                sd->handle, NULL);
646                        }
647                        g_list_free(sd->body);
648                        sd->body = NULL;
649                }
650        }
651}
652
653static void skype_parse_call(struct im_connection *ic, char *line)
654{
655        struct skype_data *sd = ic->proto_data;
656        char *id = strchr(line, ' ');
657        char buf[IRC_LINE_SIZE];
658
659        if (!++id)
660                return;
661        char *info = strchr(id, ' ');
662
663        if (!info)
664                return;
665        *info = '\0';
666        info++;
667        if (!strncmp(info, "FAILUREREASON ", 14))
668                sd->failurereason = atoi(strchr(info, ' '));
669        else if (!strcmp(info, "STATUS RINGING")) {
670                if (sd->call_id)
671                        g_free(sd->call_id);
672                sd->call_id = g_strdup(id);
673                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
674                sd->call_status = SKYPE_CALL_RINGING;
675        } else if (!strcmp(info, "STATUS MISSED")) {
676                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
677                sd->call_status = SKYPE_CALL_MISSED;
678        } else if (!strcmp(info, "STATUS CANCELLED")) {
679                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
680                sd->call_status = SKYPE_CALL_CANCELLED;
681        } else if (!strcmp(info, "STATUS FINISHED")) {
682                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
683                sd->call_status = SKYPE_CALL_FINISHED;
684        } else if (!strcmp(info, "STATUS REFUSED")) {
685                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
686                sd->call_status = SKYPE_CALL_REFUSED;
687        } else if (!strcmp(info, "STATUS UNPLACED")) {
688                if (sd->call_id)
689                        g_free(sd->call_id);
690                /* Save the ID for later usage (Cancel/Finish). */
691                sd->call_id = g_strdup(id);
692                sd->call_out = TRUE;
693        } else if (!strcmp(info, "STATUS FAILED")) {
694                imcb_error(ic, "Call failed: %s",
695                        skype_call_strerror(sd->failurereason));
696                sd->call_id = NULL;
697        } else if (!strncmp(info, "DURATION ", 9)) {
698                if (sd->call_duration)
699                        g_free(sd->call_duration);
700                sd->call_duration = g_strdup(info+9);
701        } else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
702                info += 15;
703                if (!sd->call_status)
704                        return;
705                switch (sd->call_status) {
706                case SKYPE_CALL_RINGING:
707                        if (sd->call_out)
708                                imcb_log(ic, "You are currently ringing "
709                                        "the user %s.", info);
710                        else {
711                                g_snprintf(buf, IRC_LINE_SIZE,
712                                        "The user %s is currently ringing you.",
713                                        info);
714                                skype_call_ask(ic, sd->call_id, buf);
715                        }
716                        break;
717                case SKYPE_CALL_MISSED:
718                        imcb_log(ic, "You have missed a call from user %s.",
719                                info);
720                        break;
721                case SKYPE_CALL_CANCELLED:
722                        imcb_log(ic, "You cancelled the call to the user %s.",
723                                info);
724                        sd->call_status = 0;
725                        sd->call_out = FALSE;
726                        break;
727                case SKYPE_CALL_REFUSED:
728                        if (sd->call_out)
729                                imcb_log(ic, "The user %s refused the call.",
730                                        info);
731                        else
732                                imcb_log(ic,
733                                        "You refused the call from user %s.",
734                                        info);
735                        sd->call_out = FALSE;
736                        break;
737                case SKYPE_CALL_FINISHED:
738                        if (sd->call_duration)
739                                imcb_log(ic,
740                                        "You finished the call to the user %s "
741                                        "(duration: %s seconds).",
742                                        info, sd->call_duration);
743                        else
744                                imcb_log(ic,
745                                        "You finished the call to the user %s.",
746                                        info);
747                        sd->call_out = FALSE;
748                        break;
749                default:
750                        /* Don't be noisy, ignore other statuses for now. */
751                        break;
752                }
753                sd->call_status = 0;
754        }
755}
756
757static void skype_parse_filetransfer(struct im_connection *ic, char *line)
758{
759        struct skype_data *sd = ic->proto_data;
760        char *id = strchr(line, ' ');
761
762        if (!++id)
763                return;
764        char *info = strchr(id, ' ');
765
766        if (!info)
767                return;
768        *info = '\0';
769        info++;
770        if (!strcmp(info, "STATUS NEW")) {
771                skype_printf(ic, "GET FILETRANSFER %s PARTNER_HANDLE\n",
772                        id);
773                sd->filetransfer_status = SKYPE_FILETRANSFER_NEW;
774        } else if (!strcmp(info, "STATUS FAILED")) {
775                skype_printf(ic, "GET FILETRANSFER %s PARTNER_HANDLE\n",
776                        id);
777                sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED;
778        } else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
779                info += 15;
780                if (!sd->filetransfer_status)
781                        return;
782                switch (sd->filetransfer_status) {
783                case SKYPE_FILETRANSFER_NEW:
784                        imcb_log(ic, "The user %s offered a new file for you.",
785                                info);
786                        break;
787                case SKYPE_FILETRANSFER_FAILED:
788                        imcb_log(ic, "Failed to transfer file from user %s.",
789                                info);
790                        break;
791                }
792                sd->filetransfer_status = 0;
793        }
794}
795
796static struct skype_group *skype_group_by_id(struct im_connection *ic, int id)
797{
798        struct skype_data *sd = ic->proto_data;
799        int i;
800
801        for (i = 0; i < g_list_length(sd->groups); i++) {
802                struct skype_group *sg = (struct skype_group *)g_list_nth_data(sd->groups, i);
803
804                if (sg->id == id)
805                        return sg;
806        }
807        return NULL;
808}
809
810static void skype_group_free(struct skype_group *sg, gboolean usersonly)
811{
812        int i;
813
814        for (i = 0; i < g_list_length(sg->users); i++) {
815                char *user = g_list_nth_data(sg->users, i);
816                g_free(user);
817        }
818        sg->users = NULL;
819        if (usersonly)
820                return;
821        g_free(sg->name);
822        g_free(sg);
823}
824
825/* Update the group of each user in this group */
826static void skype_group_users(struct im_connection *ic, struct skype_group *sg)
827{
828        int i;
829
830        for (i = 0; i < g_list_length(sg->users); i++) {
831                char *user = g_list_nth_data(sg->users, i);
832                char *buf = g_strdup_printf("%s@skype.com", user);
833                imcb_add_buddy(ic, buf, sg->name);
834                g_free(buf);
835        }
836}
837
838static void skype_parse_group(struct im_connection *ic, char *line)
839{
840        struct skype_data *sd = ic->proto_data;
841        char *id = strchr(line, ' ');
842
843        if (!++id)
844                return;
845
846        char *info = strchr(id, ' ');
847
848        if (!info)
849                return;
850        *info = '\0';
851        info++;
852
853        if (!strncmp(info, "DISPLAYNAME ", 12)) {
854                info += 12;
855
856                /* Name given for a group ID: try to update it or insert a new
857                 * one if not found */
858                struct skype_group *sg = skype_group_by_id(ic, atoi(id));
859                if (sg) {
860                        g_free(sg->name);
861                        sg->name = g_strdup(info);
862                } else {
863                        sg = g_new0(struct skype_group, 1);
864                        sg->id = atoi(id);
865                        sg->name = g_strdup(info);
866                        sd->groups = g_list_append(sd->groups, sg);
867                }
868        } else if (!strncmp(info, "USERS ", 6)) {
869                struct skype_group *sg = skype_group_by_id(ic, atoi(id));
870
871                if (sg) {
872                        char **i;
873                        char **users = g_strsplit(info + 6, ", ", 0);
874
875                        skype_group_free(sg, TRUE);
876                        i = users;
877                        while (*i) {
878                                sg->users = g_list_append(sg->users, g_strdup(*i));
879                                i++;
880                        }
881                        g_strfreev(users);
882                        skype_group_users(ic, sg);
883                } else
884                        log_message(LOGLVL_ERROR,
885                                "No skype group with id %s. That's probably a bug.", id);
886        } else if (!strncmp(info, "NROFUSERS ", 10)) {
887                if (!sd->pending_user) {
888                        /* Number of users changed in this group, query its type to see
889                         * if it's a custom one we should care about. */
890                        skype_printf(ic, "GET GROUP %s TYPE", id);
891                        return;
892                }
893
894                /* This is a newly created group, we have a single user
895                 * to add. */
896                struct skype_group *sg = skype_group_by_id(ic, atoi(id));
897
898                if (sg) {
899                        skype_printf(ic, "ALTER GROUP %d ADDUSER %s", sg->id, sd->pending_user);
900                        g_free(sd->pending_user);
901                        sd->pending_user = NULL;
902                } else
903                        log_message(LOGLVL_ERROR,
904                                        "No skype group with id %s. That's probably a bug.", id);
905        } else if (!strcmp(info, "TYPE CUSTOM_GROUP"))
906                /* This one is interesting, query its users. */
907                skype_printf(ic, "GET GROUP %s USERS", id);
908}
909
910static void skype_parse_chat(struct im_connection *ic, char *line)
911{
912        struct skype_data *sd = ic->proto_data;
913        char buf[IRC_LINE_SIZE];
914        char *id = strchr(line, ' ');
915
916        if (!++id)
917                return;
918        struct groupchat *gc;
919        char *info = strchr(id, ' ');
920
921        if (!info)
922                return;
923        *info = '\0';
924        info++;
925        /* Remove fake chat if we created one in skype_chat_with() */
926        gc = bee_chat_by_title(ic->bee, ic, "");
927        if (gc)
928                imcb_chat_free(gc);
929        if (!strcmp(info, "STATUS MULTI_SUBSCRIBED")) {
930                gc = bee_chat_by_title(ic->bee, ic, id);
931                if (!gc) {
932                        gc = imcb_chat_new(ic, id);
933                        imcb_chat_name_hint(gc, id);
934                }
935                skype_printf(ic, "GET CHAT %s ADDER\n", id);
936                skype_printf(ic, "GET CHAT %s TOPIC\n", id);
937        } else if (!strcmp(info, "STATUS DIALOG") && sd->groupchat_with) {
938                gc = imcb_chat_new(ic, id);
939                imcb_chat_name_hint(gc, id);
940                /* According to the docs this
941                 * is necessary. However it
942                 * does not seem the situation
943                 * and it would open an extra
944                 * window on our client, so
945                 * just leave it out. */
946                /*skype_printf(ic, "OPEN CHAT %s\n", id);*/
947                g_snprintf(buf, IRC_LINE_SIZE, "%s@skype.com",
948                                sd->groupchat_with);
949                imcb_chat_add_buddy(gc, buf);
950                imcb_chat_add_buddy(gc, sd->username);
951                g_free(sd->groupchat_with);
952                sd->groupchat_with = NULL;
953                skype_printf(ic, "GET CHAT %s ADDER\n", id);
954                skype_printf(ic, "GET CHAT %s TOPIC\n", id);
955        } else if (!strcmp(info, "STATUS UNSUBSCRIBED")) {
956                gc = bee_chat_by_title(ic->bee, ic, id);
957                if (gc)
958                        gc->data = (void *)FALSE;
959        } else if (!strncmp(info, "ADDER ", 6)) {
960                info += 6;
961                g_free(sd->adder);
962                sd->adder = g_strdup_printf("%s@skype.com", info);
963        } else if (!strncmp(info, "TOPIC ", 6)) {
964                info += 6;
965                gc = bee_chat_by_title(ic->bee, ic, id);
966                if (gc && (sd->adder || sd->topic_wait)) {
967                        if (sd->topic_wait) {
968                                sd->adder = g_strdup(sd->username);
969                                sd->topic_wait = 0;
970                        }
971                        imcb_chat_topic(gc, sd->adder, info, 0);
972                        g_free(sd->adder);
973                        sd->adder = NULL;
974                }
975        } else if (!strncmp(info, "ACTIVEMEMBERS ", 14)) {
976                info += 14;
977                gc = bee_chat_by_title(ic->bee, ic, id);
978                /* Hack! We set ->data to TRUE
979                 * while we're on the channel
980                 * so that we won't rejoin
981                 * after a /part. */
982                if (!gc || gc->data)
983                        return;
984                char **members = g_strsplit(info, " ", 0);
985                int i;
986                for (i = 0; members[i]; i++) {
987                        if (!strcmp(members[i], sd->username))
988                                continue;
989                        g_snprintf(buf, IRC_LINE_SIZE, "%s@skype.com",
990                                        members[i]);
991                        if (!g_list_find_custom(gc->in_room, buf,
992                                (GCompareFunc)strcmp))
993                                imcb_chat_add_buddy(gc, buf);
994                }
995                imcb_chat_add_buddy(gc, sd->username);
996                g_strfreev(members);
997        }
998}
999
1000static void skype_parse_password(struct im_connection *ic, char *line)
1001{
1002        if (!strncmp(line+9, "OK", 2))
1003                imcb_connected(ic);
1004        else {
1005                imcb_error(ic, "Authentication Failed");
1006                imc_logout(ic, TRUE);
1007        }
1008}
1009
1010static void skype_parse_profile(struct im_connection *ic, char *line)
1011{
1012        imcb_log(ic, "SkypeOut balance value is '%s'.", line+21);
1013}
1014
1015static void skype_parse_ping(struct im_connection *ic, char *line)
1016{
1017        /* Unused parameter */
1018        line = line;
1019        skype_printf(ic, "PONG\n");
1020}
1021
1022static void skype_parse_chats(struct im_connection *ic, char *line)
1023{
1024        char **i;
1025        char **chats = g_strsplit(line + 6, ", ", 0);
1026
1027        i = chats;
1028        while (*i) {
1029                skype_printf(ic, "GET CHAT %s STATUS\n", *i);
1030                skype_printf(ic, "GET CHAT %s ACTIVEMEMBERS\n", *i);
1031                i++;
1032        }
1033        g_strfreev(chats);
1034}
1035
1036static void skype_parse_groups(struct im_connection *ic, char *line)
1037{
1038        char **i;
1039        char **groups = g_strsplit(line + 7, ", ", 0);
1040
1041        i = groups;
1042        while (*i) {
1043                skype_printf(ic, "GET GROUP %s DISPLAYNAME\n", *i);
1044                skype_printf(ic, "GET GROUP %s USERS\n", *i);
1045                i++;
1046        }
1047        g_strfreev(groups);
1048}
1049
1050static void skype_parse_alter_group(struct im_connection *ic, char *line)
1051{
1052        char *id = line + strlen("ALTER GROUP");
1053
1054        if (!++id)
1055                return;
1056
1057        char *info = strchr(id, ' ');
1058
1059        if (!info)
1060                return;
1061        *info = '\0';
1062        info++;
1063
1064        if (!strncmp(info, "ADDUSER ", 8)) {
1065                struct skype_group *sg = skype_group_by_id(ic, atoi(id));
1066
1067                info += 8;
1068                if (sg) {
1069                        char *buf = g_strdup_printf("%s@skype.com", info);
1070                        sg->users = g_list_append(sg->users, g_strdup(info));
1071                        imcb_add_buddy(ic, buf, sg->name);
1072                        g_free(buf);
1073                } else
1074                        log_message(LOGLVL_ERROR,
1075                                "No skype group with id %s. That's probably a bug.", id);
1076        }
1077}
1078
1079typedef void (*skype_parser)(struct im_connection *ic, char *line);
1080
1081static gboolean skype_read_callback(gpointer data, gint fd,
1082                                    b_input_condition cond)
1083{
1084        struct im_connection *ic = data;
1085        struct skype_data *sd = ic->proto_data;
1086        char buf[IRC_LINE_SIZE];
1087        int st, i;
1088        char **lines, **lineptr, *line;
1089        static struct parse_map {
1090                char *k;
1091                skype_parser v;
1092        } parsers[] = {
1093                { "USERS ", skype_parse_users },
1094                { "USER ", skype_parse_user },
1095                { "CHATMESSAGE ", skype_parse_chatmessage },
1096                { "CALL ", skype_parse_call },
1097                { "FILETRANSFER ", skype_parse_filetransfer },
1098                { "CHAT ", skype_parse_chat },
1099                { "GROUP ", skype_parse_group },
1100                { "PASSWORD ", skype_parse_password },
1101                { "PROFILE PSTN_BALANCE ", skype_parse_profile },
1102                { "PING", skype_parse_ping },
1103                { "CHATS ", skype_parse_chats },
1104                { "GROUPS ", skype_parse_groups },
1105                { "ALTER GROUP ", skype_parse_alter_group },
1106        };
1107
1108        /* Unused parameters */
1109        fd = fd;
1110        cond = cond;
1111
1112        if (!sd || sd->fd == -1)
1113                return FALSE;
1114        /* Read the whole data. */
1115        st = ssl_read(sd->ssl, buf, sizeof(buf));
1116        if (st > 0) {
1117                buf[st] = '\0';
1118                /* Then split it up to lines. */
1119                lines = g_strsplit(buf, "\n", 0);
1120                lineptr = lines;
1121                while ((line = *lineptr)) {
1122                        if (!strlen(line))
1123                                break;
1124                        if (set_getbool(&ic->acc->set, "skypeconsole_receive"))
1125                                imcb_buddy_msg(ic, "skypeconsole", line, 0, 0);
1126                        for (i = 0; i < ARRAY_SIZE(parsers); i++)
1127                                if (!strncmp(line, parsers[i].k,
1128                                        strlen(parsers[i].k))) {
1129                                        parsers[i].v(ic, line);
1130                                        break;
1131                                }
1132                        lineptr++;
1133                }
1134                g_strfreev(lines);
1135        } else if (st == 0 || (st < 0 && !sockerr_again())) {
1136                closesocket(sd->fd);
1137                sd->fd = -1;
1138
1139                imcb_error(ic, "Error while reading from server");
1140                imc_logout(ic, TRUE);
1141                return FALSE;
1142        }
1143        return TRUE;
1144}
1145
1146gboolean skype_start_stream(struct im_connection *ic)
1147{
1148        struct skype_data *sd = ic->proto_data;
1149        int st;
1150
1151        if (!sd)
1152                return FALSE;
1153
1154        if (sd->bfd <= 0)
1155                sd->bfd = b_input_add(sd->fd, B_EV_IO_READ,
1156                        skype_read_callback, ic);
1157
1158        /* Log in */
1159        skype_printf(ic, "USERNAME %s\n", ic->acc->user);
1160        skype_printf(ic, "PASSWORD %s\n", ic->acc->pass);
1161
1162        /* This will download all buddies and groups. */
1163        st = skype_printf(ic, "SEARCH GROUPS CUSTOM\n");
1164        skype_printf(ic, "SEARCH FRIENDS\n");
1165
1166        skype_printf(ic, "SET USERSTATUS ONLINE\n");
1167
1168        /* Auto join to bookmarked chats if requested.*/
1169        if (set_getbool(&ic->acc->set, "auto_join"))
1170                skype_printf(ic, "SEARCH BOOKMARKEDCHATS\n");
1171        return st;
1172}
1173
1174gboolean skype_connected(gpointer data, int returncode, void *source, b_input_condition cond)
1175{
1176        struct im_connection *ic = data;
1177        struct skype_data *sd = ic->proto_data;
1178
1179        /* Unused parameter */
1180        cond = cond;
1181
1182        if (!source) {
1183                sd->ssl = NULL;
1184                imcb_error(ic, "Could not connect to server");
1185                imc_logout(ic, TRUE);
1186                return FALSE;
1187        }
1188        imcb_log(ic, "Connected to server, logging in");
1189
1190        return skype_start_stream(ic);
1191}
1192
1193static void skype_login(account_t *acc)
1194{
1195        struct im_connection *ic = imcb_new(acc);
1196        struct skype_data *sd = g_new0(struct skype_data, 1);
1197
1198        ic->proto_data = sd;
1199
1200        imcb_log(ic, "Connecting");
1201        sd->ssl = ssl_connect(set_getstr(&acc->set, "server"),
1202                set_getint(&acc->set, "port"), FALSE, skype_connected, ic);
1203        sd->fd = sd->ssl ? ssl_getfd(sd->ssl) : -1;
1204        sd->username = g_strdup(acc->user);
1205
1206        sd->ic = ic;
1207
1208        if (set_getbool(&acc->set, "skypeconsole"))
1209                imcb_add_buddy(ic, "skypeconsole", NULL);
1210}
1211
1212static void skype_logout(struct im_connection *ic)
1213{
1214        struct skype_data *sd = ic->proto_data;
1215        int i;
1216
1217        skype_printf(ic, "SET USERSTATUS OFFLINE\n");
1218
1219        while( ic->groupchats )
1220                imcb_chat_free(ic->groupchats->data);
1221
1222        for (i = 0; i < g_list_length(sd->groups); i++) {
1223                struct skype_group *sg = (struct skype_group *)g_list_nth_data(sd->groups, i);
1224                skype_group_free(sg, FALSE);
1225        }
1226        g_free(sd->username);
1227        g_free(sd->handle);
1228        g_free(sd);
1229        ic->proto_data = NULL;
1230}
1231
1232static int skype_buddy_msg(struct im_connection *ic, char *who, char *message,
1233                           int flags)
1234{
1235        char *ptr, *nick;
1236        int st;
1237
1238        /* Unused parameter */
1239        flags = flags;
1240
1241        nick = g_strdup(who);
1242        ptr = strchr(nick, '@');
1243        if (ptr)
1244                *ptr = '\0';
1245
1246        if (!strncmp(who, "skypeconsole", 12))
1247                st = skype_printf(ic, "%s\n", message);
1248        else
1249                st = skype_printf(ic, "MESSAGE %s %s\n", nick, message);
1250        g_free(nick);
1251
1252        return st;
1253}
1254
1255const struct skype_away_state *skype_away_state_by_name(char *name)
1256{
1257        int i;
1258
1259        for (i = 0; skype_away_state_list[i].full_name; i++)
1260                if (g_strcasecmp(skype_away_state_list[i].full_name, name) == 0)
1261                        return skype_away_state_list + i;
1262
1263        return NULL;
1264}
1265
1266static void skype_set_away(struct im_connection *ic, char *state_txt,
1267                           char *message)
1268{
1269        const struct skype_away_state *state;
1270
1271        /* Unused parameter */
1272        message = message;
1273
1274        if (state_txt == NULL)
1275                state = skype_away_state_by_name("Online");
1276        else
1277                state = skype_away_state_by_name(state_txt);
1278        skype_printf(ic, "SET USERSTATUS %s\n", state->code);
1279}
1280
1281static GList *skype_away_states(struct im_connection *ic)
1282{
1283        static GList *l;
1284        int i;
1285
1286        /* Unused parameter */
1287        ic = ic;
1288
1289        if (l == NULL)
1290                for (i = 0; skype_away_state_list[i].full_name; i++)
1291                        l = g_list_append(l,
1292                                (void *)skype_away_state_list[i].full_name);
1293
1294        return l;
1295}
1296
1297static char *skype_set_display_name(set_t *set, char *value)
1298{
1299        account_t *acc = set->data;
1300        struct im_connection *ic = acc->ic;
1301
1302        skype_printf(ic, "SET PROFILE FULLNAME %s", value);
1303        return value;
1304}
1305
1306static char *skype_set_balance(set_t *set, char *value)
1307{
1308        account_t *acc = set->data;
1309        struct im_connection *ic = acc->ic;
1310
1311        skype_printf(ic, "GET PROFILE PSTN_BALANCE");
1312        return value;
1313}
1314
1315static void skype_call(struct im_connection *ic, char *value)
1316{
1317        char *nick = g_strdup(value);
1318        char *ptr = strchr(nick, '@');
1319
1320        if (ptr)
1321                *ptr = '\0';
1322        skype_printf(ic, "CALL %s", nick);
1323        g_free(nick);
1324}
1325
1326static void skype_hangup(struct im_connection *ic)
1327{
1328        struct skype_data *sd = ic->proto_data;
1329
1330        if (sd->call_id) {
1331                skype_printf(ic, "SET CALL %s STATUS FINISHED",
1332                                sd->call_id);
1333                g_free(sd->call_id);
1334                sd->call_id = 0;
1335        } else
1336                imcb_error(ic, "There are no active calls currently.");
1337}
1338
1339static char *skype_set_call(set_t *set, char *value)
1340{
1341        account_t *acc = set->data;
1342        struct im_connection *ic = acc->ic;
1343
1344        if (value)
1345                skype_call(ic, value);
1346        else
1347                skype_hangup(ic);
1348        return value;
1349}
1350
1351static void skype_add_buddy(struct im_connection *ic, char *who, char *group)
1352{
1353        struct skype_data *sd = ic->proto_data;
1354        char *nick, *ptr;
1355
1356        nick = g_strdup(who);
1357        ptr = strchr(nick, '@');
1358        if (ptr)
1359                *ptr = '\0';
1360
1361        if (!group) {
1362                skype_printf(ic, "SET USER %s BUDDYSTATUS 2 Please authorize me\n",
1363                                nick);
1364                g_free(nick);
1365        } else {
1366                struct skype_group *sg = skype_group_by_name(ic, group);
1367
1368                if (!sg) {
1369                        /* No such group, we need to create it, then have to
1370                         * add the user once it's created. */
1371                        skype_printf(ic, "CREATE GROUP %s", group);
1372                        sd->pending_user = g_strdup(nick);
1373                } else {
1374                        skype_printf(ic, "ALTER GROUP %d ADDUSER %s", sg->id, nick);
1375                }
1376        }
1377}
1378
1379static void skype_remove_buddy(struct im_connection *ic, char *who, char *group)
1380{
1381        char *nick, *ptr;
1382
1383        /* Unused parameter */
1384        group = group;
1385
1386        nick = g_strdup(who);
1387        ptr = strchr(nick, '@');
1388        if (ptr)
1389                *ptr = '\0';
1390        skype_printf(ic, "SET USER %s BUDDYSTATUS 1\n", nick);
1391        g_free(nick);
1392}
1393
1394void skype_chat_msg(struct groupchat *gc, char *message, int flags)
1395{
1396        struct im_connection *ic = gc->ic;
1397
1398        /* Unused parameter */
1399        flags = flags;
1400
1401        skype_printf(ic, "CHATMESSAGE %s %s\n", gc->title, message);
1402}
1403
1404void skype_chat_leave(struct groupchat *gc)
1405{
1406        struct im_connection *ic = gc->ic;
1407        skype_printf(ic, "ALTER CHAT %s LEAVE\n", gc->title);
1408        gc->data = (void *)TRUE;
1409}
1410
1411void skype_chat_invite(struct groupchat *gc, char *who, char *message)
1412{
1413        struct im_connection *ic = gc->ic;
1414        char *ptr, *nick;
1415
1416        nick = g_strdup(who);
1417        ptr = strchr(nick, '@');
1418        if (ptr)
1419                *ptr = '\0';
1420        skype_printf(ic, "ALTER CHAT %s ADDMEMBERS %s\n", gc->title, nick);
1421        g_free(nick);
1422}
1423
1424void skype_chat_topic(struct groupchat *gc, char *message)
1425{
1426        struct im_connection *ic = gc->ic;
1427        struct skype_data *sd = ic->proto_data;
1428        skype_printf(ic, "ALTER CHAT %s SETTOPIC %s\n",
1429                gc->title, message);
1430        sd->topic_wait = 1;
1431}
1432
1433struct groupchat *skype_chat_with(struct im_connection *ic, char *who)
1434{
1435        struct skype_data *sd = ic->proto_data;
1436        char *ptr, *nick;
1437        nick = g_strdup(who);
1438        ptr = strchr(nick, '@');
1439        if (ptr)
1440                *ptr = '\0';
1441        skype_printf(ic, "CHAT CREATE %s\n", nick);
1442        sd->groupchat_with = g_strdup(nick);
1443        g_free(nick);
1444        /* We create a fake chat for now. We will replace it with a real one in
1445         * the real callback. */
1446        return imcb_chat_new(ic, "");
1447}
1448
1449static void skype_get_info(struct im_connection *ic, char *who)
1450{
1451        char *ptr, *nick;
1452        nick = g_strdup(who);
1453        ptr = strchr(nick, '@');
1454        if (ptr)
1455                *ptr = '\0';
1456        skype_printf(ic, "GET USER %s FULLNAME\n", nick);
1457        skype_printf(ic, "GET USER %s PHONE_HOME\n", nick);
1458        skype_printf(ic, "GET USER %s PHONE_OFFICE\n", nick);
1459        skype_printf(ic, "GET USER %s PHONE_MOBILE\n", nick);
1460        skype_printf(ic, "GET USER %s NROF_AUTHED_BUDDIES\n", nick);
1461        skype_printf(ic, "GET USER %s TIMEZONE\n", nick);
1462        skype_printf(ic, "GET USER %s LASTONLINETIMESTAMP\n", nick);
1463        skype_printf(ic, "GET USER %s BIRTHDAY\n", nick);
1464        skype_printf(ic, "GET USER %s SEX\n", nick);
1465        skype_printf(ic, "GET USER %s LANGUAGE\n", nick);
1466        skype_printf(ic, "GET USER %s COUNTRY\n", nick);
1467        skype_printf(ic, "GET USER %s PROVINCE\n", nick);
1468        skype_printf(ic, "GET USER %s CITY\n", nick);
1469        skype_printf(ic, "GET USER %s HOMEPAGE\n", nick);
1470        skype_printf(ic, "GET USER %s ABOUT\n", nick);
1471}
1472
1473static void skype_set_my_name(struct im_connection *ic, char *info)
1474{
1475        skype_set_display_name(set_find(&ic->acc->set, "display_name"), info);
1476}
1477
1478static void skype_init(account_t *acc)
1479{
1480        set_t *s;
1481
1482        s = set_add(&acc->set, "server", SKYPE_DEFAULT_SERVER, set_eval_account,
1483                acc);
1484        s->flags |= ACC_SET_OFFLINE_ONLY;
1485
1486        s = set_add(&acc->set, "port", SKYPE_DEFAULT_PORT, set_eval_int, acc);
1487        s->flags |= ACC_SET_OFFLINE_ONLY;
1488
1489        s = set_add(&acc->set, "display_name", NULL, skype_set_display_name,
1490                acc);
1491        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
1492
1493        s = set_add(&acc->set, "call", NULL, skype_set_call, acc);
1494        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
1495
1496        s = set_add(&acc->set, "balance", NULL, skype_set_balance, acc);
1497        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
1498
1499        s = set_add(&acc->set, "skypeout_offline", "true", set_eval_bool, acc);
1500
1501        s = set_add(&acc->set, "skypeconsole", "false", set_eval_bool, acc);
1502        s->flags |= ACC_SET_OFFLINE_ONLY;
1503
1504        s = set_add(&acc->set, "skypeconsole_receive", "false", set_eval_bool,
1505                acc);
1506        s->flags |= ACC_SET_OFFLINE_ONLY;
1507
1508        s = set_add(&acc->set, "auto_join", "false", set_eval_bool, acc);
1509        s->flags |= ACC_SET_OFFLINE_ONLY;
1510
1511        s = set_add(&acc->set, "test_join", "false", set_eval_bool, acc);
1512        s->flags |= ACC_SET_OFFLINE_ONLY;
1513
1514        s = set_add(&acc->set, "show_moods", "false", set_eval_bool, acc);
1515
1516        s = set_add(&acc->set, "edit_prefix", "EDIT:",
1517                        NULL, acc);
1518}
1519
1520#if BITLBEE_VERSION_CODE > BITLBEE_VER(3, 0, 1)
1521GList *skype_buddy_action_list(bee_user_t *bu)
1522{
1523        static GList *ret;
1524
1525        /* Unused parameter */
1526        bu = bu;
1527
1528        if (ret == NULL) {
1529                static const struct buddy_action ba[3] = {
1530                        {"CALL", "Initiate a call" },
1531                        {"HANGUP", "Hang up a call" },
1532                };
1533
1534                ret = g_list_prepend(ret, (void *) ba + 0);
1535        }
1536
1537        return ret;
1538}
1539
1540void *skype_buddy_action(struct bee_user *bu, const char *action, char * const args[], void *data)
1541{
1542        /* Unused parameters */
1543        args = args;
1544        data = data;
1545
1546        if (!g_strcasecmp(action, "CALL"))
1547                skype_call(bu->ic, bu->handle);
1548        else if (!g_strcasecmp(action, "HANGUP"))
1549                skype_hangup(bu->ic);
1550
1551        return NULL;
1552}
1553#endif
1554
1555void init_plugin(void)
1556{
1557        struct prpl *ret = g_new0(struct prpl, 1);
1558
1559        ret->name = "skype";
1560        ret->login = skype_login;
1561        ret->init = skype_init;
1562        ret->logout = skype_logout;
1563        ret->buddy_msg = skype_buddy_msg;
1564        ret->get_info = skype_get_info;
1565        ret->set_my_name = skype_set_my_name;
1566        ret->away_states = skype_away_states;
1567        ret->set_away = skype_set_away;
1568        ret->add_buddy = skype_add_buddy;
1569        ret->remove_buddy = skype_remove_buddy;
1570        ret->chat_msg = skype_chat_msg;
1571        ret->chat_leave = skype_chat_leave;
1572        ret->chat_invite = skype_chat_invite;
1573        ret->chat_with = skype_chat_with;
1574        ret->handle_cmp = g_strcasecmp;
1575        ret->chat_topic = skype_chat_topic;
1576#if BITLBEE_VERSION_CODE > BITLBEE_VER(3, 0, 1)
1577        ret->buddy_action_list = skype_buddy_action_list;
1578        ret->buddy_action = skype_buddy_action;
1579#endif
1580        register_protocol(ret);
1581}
Note: See TracBrowser for help on using the repository browser.