source: protocols/skype/skype.c @ 6b13103

Last change on this file since 6b13103 was 6b13103, checked in by dequis <dx@…>, at 2015-01-16T19:50:23Z

Replace isdigit/isalpha/.../tolower/toupper with glib variants

This fixes warnings about passing signed chars to them (apparently they
are implemented as macros that do array lookups without checks in some
platforms, yay)

Specifically:

functions=isalnum|isalpha|isdigit|isspace|isxdigit|tolower|toupper
sed -ir "s/$functions/g_ascii_&/g" /*.c

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