source: skype/skype.c @ 46641bf

Last change on this file since 46641bf was 46641bf, checked in by Miklos Vajna <vmiklos@…>, at 2011-01-03T03:13:00Z

skype_add_buddy: handle the case when /invite is used on a new group

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