source: protocols/skype/skype.c @ db0d979

Last change on this file since db0d979 was 9ec6b36, checked in by Miklos Vajna <vmiklos@…>, at 2013-01-13T11:41:21Z

Update my email

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