source: skype/skype.c @ 46e9822

Last change on this file since 46e9822 was 46e9822, checked in by Miklos Vajna <vmiklos@…>, at 2011-01-03T02:47:40Z

Make /invite work for existing groups

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