source: protocols/skype/skype.c @ 1146e21

Last change on this file since 1146e21 was b2dc873, checked in by Miklos Vajna <vmiklos@…>, at 2013-01-19T19:25:19Z

skype: provide more info about file transfers

Till now only a single line was printed, when a new offer arrived. Now
two more lines are printed:

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