source: skype/skype.c @ 49a3c02

Last change on this file since 49a3c02 was 49a3c02, checked in by Miklos Vajna <vmiklos@…>, at 2010-04-05T22:55:07Z

add test_join setting

  • Property mode set to 100644
File size: 32.4 KB
Line 
1/*
2 *  skype.c - Skype plugin for BitlBee
3 *
4 *  Copyright (c) 2007, 2008, 2009 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 <bitlbee/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};
111
112struct skype_away_state {
113        char *code;
114        char *full_name;
115};
116
117struct skype_buddy_ask_data {
118        struct im_connection *ic;
119        /* This is also used for call IDs for simplicity */
120        char *handle;
121};
122
123/*
124 * Tables
125 */
126
127const struct skype_away_state skype_away_state_list[] = {
128        { "AWAY", "Away" },
129        { "NA", "Not available" },
130        { "DND", "Do Not Disturb" },
131        { "INVISIBLE", "Invisible" },
132        { "OFFLINE", "Offline" },
133        { "SKYPEME", "Skype Me" },
134        { "ONLINE", "Online" },
135        { NULL, NULL}
136};
137
138/*
139 * Functions
140 */
141
142int skype_write(struct im_connection *ic, char *buf, int len)
143{
144        struct skype_data *sd = ic->proto_data;
145        struct pollfd pfd[1];
146
147        pfd[0].fd = sd->fd;
148        pfd[0].events = POLLOUT;
149
150        /* This poll is necessary or we'll get a SIGPIPE when we write() to
151         * sd->fd. */
152        poll(pfd, 1, 1000);
153        if (pfd[0].revents & POLLHUP) {
154                imc_logout(ic, TRUE);
155                return FALSE;
156        }
157        ssl_write(sd->ssl, buf, len);
158
159        return TRUE;
160}
161
162int skype_printf(struct im_connection *ic, char *fmt, ...)
163{
164        va_list args;
165        char str[IRC_LINE_SIZE];
166
167        va_start(args, fmt);
168        vsnprintf(str, IRC_LINE_SIZE, fmt, args);
169        va_end(args);
170
171        return skype_write(ic, str, strlen(str));
172}
173
174static void skype_buddy_ask_yes(void *data)
175{
176        struct skype_buddy_ask_data *bla = data;
177        skype_printf(bla->ic, "SET USER %s ISAUTHORIZED TRUE",
178                bla->handle);
179        g_free(bla->handle);
180        g_free(bla);
181}
182
183static void skype_buddy_ask_no(void *data)
184{
185        struct skype_buddy_ask_data *bla = data;
186        skype_printf(bla->ic, "SET USER %s ISAUTHORIZED FALSE",
187                bla->handle);
188        g_free(bla->handle);
189        g_free(bla);
190}
191
192void skype_buddy_ask(struct im_connection *ic, char *handle, char *message)
193{
194        struct skype_buddy_ask_data *bla = g_new0(struct skype_buddy_ask_data,
195                1);
196        char *buf;
197
198        bla->ic = ic;
199        bla->handle = g_strdup(handle);
200
201        buf = g_strdup_printf("The user %s wants to add you to "
202                "his/her buddy list, saying: '%s'.", handle, message);
203        imcb_ask(ic, buf, bla, skype_buddy_ask_yes, skype_buddy_ask_no);
204        g_free(buf);
205}
206
207static void skype_call_ask_yes(void *data)
208{
209        struct skype_buddy_ask_data *bla = data;
210        skype_printf(bla->ic, "SET CALL %s STATUS INPROGRESS",
211                bla->handle);
212        g_free(bla->handle);
213        g_free(bla);
214}
215
216static void skype_call_ask_no(void *data)
217{
218        struct skype_buddy_ask_data *bla = data;
219        skype_printf(bla->ic, "SET CALL %s STATUS FINISHED",
220                bla->handle);
221        g_free(bla->handle);
222        g_free(bla);
223}
224
225void skype_call_ask(struct im_connection *ic, char *call_id, char *message)
226{
227        struct skype_buddy_ask_data *bla = g_new0(struct skype_buddy_ask_data,
228                1);
229
230        bla->ic = ic;
231        bla->handle = g_strdup(call_id);
232
233        imcb_ask(ic, message, bla, skype_call_ask_yes, skype_call_ask_no);
234}
235struct groupchat *skype_chat_by_name(struct im_connection *ic, char *name)
236{
237        struct groupchat *ret;
238
239        for (ret = ic->groupchats; ret; ret = ret->next)
240                if (strcmp(name, ret->title) == 0)
241                        break;
242
243        return ret;
244}
245
246static char *skype_call_strerror(int err)
247{
248        switch (err) {
249        case 1:
250                return "Miscellaneous error";
251        case 2:
252                return "User or phone number does not exist.";
253        case 3:
254                return "User is offline";
255        case 4:
256                return "No proxy found";
257        case 5:
258                return "Session terminated.";
259        case 6:
260                return "No common codec found.";
261        case 7:
262                return "Sound I/O error.";
263        case 8:
264                return "Problem with remote sound device.";
265        case 9:
266                return "Call blocked by recipient.";
267        case 10:
268                return "Recipient not a friend.";
269        case 11:
270                return "Current user not authorized by recipient.";
271        case 12:
272                return "Sound recording error.";
273        default:
274                return "Unknown error";
275        }
276}
277
278static void skype_parse_users(struct im_connection *ic, char *line)
279{
280        char **i, **nicks;
281
282        nicks = g_strsplit(line + 6, ", ", 0);
283        for (i = nicks; *i; i++)
284                skype_printf(ic, "GET USER %s ONLINESTATUS\n", *i);
285        g_strfreev(nicks);
286}
287
288static void skype_parse_user(struct im_connection *ic, char *line)
289{
290        int flags = 0;
291        char *ptr;
292        struct skype_data *sd = ic->proto_data;
293        char *user = strchr(line, ' ');
294        char *status = strrchr(line, ' ');
295
296        status++;
297        ptr = strchr(++user, ' ');
298        if (!ptr)
299                return;
300        *ptr = '\0';
301        ptr++;
302        if (!strncmp(ptr, "ONLINESTATUS ", 13)) {
303                        if (!strcmp(user, sd->username))
304                                return;
305                        if (!set_getbool(&ic->acc->set, "test_join")
306                                && !strcmp(user, "echo123"))
307                                return;
308                ptr = g_strdup_printf("%s@skype.com", user);
309                imcb_add_buddy(ic, ptr, NULL);
310                if (strcmp(status, "OFFLINE") && (strcmp(status, "SKYPEOUT") ||
311                        !set_getbool(&ic->acc->set, "skypeout_offline")))
312                        flags |= OPT_LOGGED_IN;
313                if (strcmp(status, "ONLINE") && strcmp(status, "SKYPEME"))
314                        flags |= OPT_AWAY;
315                imcb_buddy_status(ic, ptr, flags, NULL, NULL);
316                g_free(ptr);
317        } else if (!strncmp(ptr, "RECEIVEDAUTHREQUEST ", 20)) {
318                char *message = ptr + 20;
319                if (strlen(message))
320                        skype_buddy_ask(ic, user, message);
321        } else if (!strncmp(ptr, "BUDDYSTATUS ", 12)) {
322                char *st = ptr + 12;
323                if (!strcmp(st, "3")) {
324                        char *buf = g_strdup_printf("%s@skype.com", user);
325                        imcb_add_buddy(ic, buf, NULL);
326                        g_free(buf);
327                }
328        } else if (!strncmp(ptr, "FULLNAME ", 9))
329                sd->info_fullname = g_strdup(ptr + 9);
330        else if (!strncmp(ptr, "PHONE_HOME ", 11))
331                sd->info_phonehome = g_strdup(ptr + 11);
332        else if (!strncmp(ptr, "PHONE_OFFICE ", 13))
333                sd->info_phoneoffice = g_strdup(ptr + 13);
334        else if (!strncmp(ptr, "PHONE_MOBILE ", 13))
335                sd->info_phonemobile = g_strdup(ptr + 13);
336        else if (!strncmp(ptr, "NROF_AUTHED_BUDDIES ", 20))
337                sd->info_nrbuddies = g_strdup(ptr + 20);
338        else if (!strncmp(ptr, "TIMEZONE ", 9))
339                sd->info_tz = g_strdup(ptr + 9);
340        else if (!strncmp(ptr, "LASTONLINETIMESTAMP ", 20))
341                sd->info_seen = g_strdup(ptr + 20);
342        else if (!strncmp(ptr, "BIRTHDAY ", 9))
343                sd->info_birthday = g_strdup(ptr + 9);
344        else if (!strncmp(ptr, "SEX ", 4))
345                sd->info_sex = g_strdup(ptr + 4);
346        else if (!strncmp(ptr, "LANGUAGE ", 9))
347                sd->info_language = g_strdup(ptr + 9);
348        else if (!strncmp(ptr, "COUNTRY ", 8))
349                sd->info_country = g_strdup(ptr + 8);
350        else if (!strncmp(ptr, "PROVINCE ", 9))
351                sd->info_province = g_strdup(ptr + 9);
352        else if (!strncmp(ptr, "CITY ", 5))
353                sd->info_city = g_strdup(ptr + 5);
354        else if (!strncmp(ptr, "HOMEPAGE ", 9))
355                sd->info_homepage = g_strdup(ptr + 9);
356        else if (!strncmp(ptr, "ABOUT ", 6)) {
357                sd->info_about = g_strdup(ptr + 6);
358
359                GString *st = g_string_new("Contact Information\n");
360                g_string_append_printf(st, "Skype Name: %s\n", user);
361                if (sd->info_fullname) {
362                        if (strlen(sd->info_fullname))
363                                g_string_append_printf(st, "Full Name: %s\n",
364                                        sd->info_fullname);
365                        g_free(sd->info_fullname);
366                }
367                if (sd->info_phonehome) {
368                        if (strlen(sd->info_phonehome))
369                                g_string_append_printf(st, "Home Phone: %s\n",
370                                        sd->info_phonehome);
371                        g_free(sd->info_phonehome);
372                }
373                if (sd->info_phoneoffice) {
374                        if (strlen(sd->info_phoneoffice))
375                                g_string_append_printf(st, "Office Phone: %s\n",
376                                        sd->info_phoneoffice);
377                        g_free(sd->info_phoneoffice);
378                }
379                if (sd->info_phonemobile) {
380                        if (strlen(sd->info_phonemobile))
381                                g_string_append_printf(st, "Mobile Phone: %s\n",
382                                        sd->info_phonemobile);
383                        g_free(sd->info_phonemobile);
384                }
385                g_string_append_printf(st, "Personal Information\n");
386                if (sd->info_nrbuddies) {
387                        if (strlen(sd->info_nrbuddies))
388                                g_string_append_printf(st,
389                                        "Contacts: %s\n", sd->info_nrbuddies);
390                        g_free(sd->info_nrbuddies);
391                }
392                if (sd->info_tz) {
393                        if (strlen(sd->info_tz)) {
394                                char ib[256];
395                                time_t t = time(NULL);
396                                t += atoi(sd->info_tz)-(60*60*24);
397                                struct tm *gt = gmtime(&t);
398                                strftime(ib, 256, "%H:%M:%S", gt);
399                                g_string_append_printf(st,
400                                        "Local Time: %s\n", ib);
401                        }
402                        g_free(sd->info_tz);
403                }
404                if (sd->info_seen) {
405                        if (strlen(sd->info_seen)) {
406                                char ib[256];
407                                time_t it = atoi(sd->info_seen);
408                                struct tm *tm = localtime(&it);
409                                strftime(ib, 256, ("%Y. %m. %d. %H:%M"), tm);
410                                g_string_append_printf(st,
411                                        "Last Seen: %s\n", ib);
412                        }
413                        g_free(sd->info_seen);
414                }
415                if (sd->info_birthday) {
416                        if (strlen(sd->info_birthday) &&
417                                strcmp(sd->info_birthday, "0")) {
418                                char ib[256];
419                                struct tm tm;
420                                strptime(sd->info_birthday, "%Y%m%d", &tm);
421                                strftime(ib, 256, "%B %d, %Y", &tm);
422                                g_string_append_printf(st,
423                                        "Birthday: %s\n", ib);
424
425                                strftime(ib, 256, "%Y", &tm);
426                                int year = atoi(ib);
427                                time_t t = time(NULL);
428                                struct tm *lt = localtime(&t);
429                                g_string_append_printf(st,
430                                        "Age: %d\n", lt->tm_year+1900-year);
431                        }
432                        g_free(sd->info_birthday);
433                }
434                if (sd->info_sex) {
435                        if (strlen(sd->info_sex)) {
436                                char *iptr = sd->info_sex;
437                                while (*iptr++)
438                                        *iptr = tolower(*iptr);
439                                g_string_append_printf(st,
440                                        "Gender: %s\n", sd->info_sex);
441                        }
442                        g_free(sd->info_sex);
443                }
444                if (sd->info_language) {
445                        if (strlen(sd->info_language)) {
446                                char *iptr = strchr(sd->info_language, ' ');
447                                if (iptr)
448                                        iptr++;
449                                else
450                                        iptr = sd->info_language;
451                                g_string_append_printf(st,
452                                        "Language: %s\n", iptr);
453                        }
454                        g_free(sd->info_language);
455                }
456                if (sd->info_country) {
457                        if (strlen(sd->info_country)) {
458                                char *iptr = strchr(sd->info_country, ' ');
459                                if (iptr)
460                                        iptr++;
461                                else
462                                        iptr = sd->info_country;
463                                g_string_append_printf(st,
464                                        "Country: %s\n", iptr);
465                        }
466                        g_free(sd->info_country);
467                }
468                if (sd->info_province) {
469                        if (strlen(sd->info_province))
470                                g_string_append_printf(st,
471                                        "Region: %s\n", sd->info_province);
472                        g_free(sd->info_province);
473                }
474                if (sd->info_city) {
475                        if (strlen(sd->info_city))
476                                g_string_append_printf(st,
477                                        "City: %s\n", sd->info_city);
478                        g_free(sd->info_city);
479                }
480                if (sd->info_homepage) {
481                        if (strlen(sd->info_homepage))
482                                g_string_append_printf(st,
483                                        "Homepage: %s\n", sd->info_homepage);
484                        g_free(sd->info_homepage);
485                }
486                if (sd->info_about) {
487                        if (strlen(sd->info_about))
488                                g_string_append_printf(st, "%s\n",
489                                        sd->info_about);
490                        g_free(sd->info_about);
491                }
492                imcb_log(ic, "%s", st->str);
493                g_string_free(st, TRUE);
494        }
495}
496
497static void skype_parse_chatmessage(struct im_connection *ic, char *line)
498{
499        struct skype_data *sd = ic->proto_data;
500        char buf[IRC_LINE_SIZE];
501        char *id = strchr(line, ' ');
502
503        if (!++id)
504                return;
505        char *info = strchr(id, ' ');
506
507        if (!info)
508                return;
509        *info = '\0';
510        info++;
511        if (!strcmp(info, "STATUS RECEIVED") ||
512                !strncmp(info, "EDITED_TIMESTAMP", 16)) {
513                /* New message ID:
514                 * (1) Request its from field
515                 * (2) Request its body
516                 * (3) Request its type
517                 * (4) Query chatname
518                 */
519                skype_printf(ic, "GET CHATMESSAGE %s FROM_HANDLE\n", id);
520                if (!strcmp(info, "STATUS RECEIVED"))
521                        skype_printf(ic, "GET CHATMESSAGE %s BODY\n", id);
522                else
523                        sd->is_edit = 1;
524                skype_printf(ic, "GET CHATMESSAGE %s TYPE\n", id);
525                skype_printf(ic, "GET CHATMESSAGE %s CHATNAME\n", id);
526        } else if (!strncmp(info, "FROM_HANDLE ", 12)) {
527                info += 12;
528                /* New from field value. Store
529                 * it, then we can later use it
530                 * when we got the message's
531                 * body. */
532                g_free(sd->handle);
533                sd->handle = g_strdup_printf("%s@skype.com", info);
534        } else if (!strncmp(info, "EDITED_BY ", 10)) {
535                info += 10;
536                /* This is the same as
537                 * FROM_HANDLE, except that we
538                 * never request these lines
539                 * from Skype, we just get
540                 * them. */
541                g_free(sd->handle);
542                sd->handle = g_strdup_printf("%s@skype.com", info);
543        } else if (!strncmp(info, "BODY ", 5)) {
544                info += 5;
545                sd->body = g_list_append(sd->body, g_strdup(info));
546        }       else if (!strncmp(info, "TYPE ", 5)) {
547                info += 5;
548                g_free(sd->type);
549                sd->type = g_strdup(info);
550        } else if (strncmp(info, "CHATNAME ", 9))
551                return;
552        if (!sd->handle || !sd->body || !sd->type)
553                return;
554        info += 9;
555        struct groupchat *gc = skype_chat_by_name(ic, info);
556        int i;
557        for (i = 0; i < g_list_length(sd->body); i++) {
558                char *body = g_list_nth_data(sd->body, i);
559                if (!strcmp(sd->type, "SETTOPIC") && gc)
560                        imcb_chat_topic(gc,
561                                sd->handle, body, 0);
562                else if (!strcmp(sd->type, "LEFT") && gc)
563                        imcb_chat_remove_buddy(gc,
564                                sd->handle, NULL);
565                if (strcmp(sd->type, "SAID") &&
566                        strcmp(sd->type, "EMOTED"))
567                        continue;
568                if (!strcmp(sd->type, "SAID")) {
569                        if (!sd->is_edit)
570                                g_snprintf(buf, IRC_LINE_SIZE, "%s",
571                                        body);
572                        else {
573                                account_t *acc = ic->acc;
574                                g_snprintf(buf, IRC_LINE_SIZE, "%s %s",
575                                        set_getstr(&acc->set, "edit_prefix"),
576                                        body);
577                                sd->is_edit = 0;
578                        }
579                } else
580                        g_snprintf(buf, IRC_LINE_SIZE, "/me %s",
581                                body);
582                if (!gc)
583                        /* Private message */
584                        imcb_buddy_msg(ic,
585                                sd->handle, buf, 0, 0);
586                else
587                        /* Groupchat message */
588                        imcb_chat_msg(gc,
589                                sd->handle, buf, 0, 0);
590        }
591        g_list_free(sd->body);
592        sd->body = NULL;
593}
594
595static void skype_parse_call(struct im_connection *ic, char *line)
596{
597        struct skype_data *sd = ic->proto_data;
598        char *id = strchr(line, ' ');
599        char buf[IRC_LINE_SIZE];
600
601        if (!++id)
602                return;
603        char *info = strchr(id, ' ');
604
605        if (!info)
606                return;
607        *info = '\0';
608        info++;
609        if (!strncmp(info, "FAILUREREASON ", 14))
610                sd->failurereason = atoi(strchr(info, ' '));
611        else if (!strcmp(info, "STATUS RINGING")) {
612                if (sd->call_id)
613                        g_free(sd->call_id);
614                sd->call_id = g_strdup(id);
615                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
616                sd->call_status = SKYPE_CALL_RINGING;
617        } else if (!strcmp(info, "STATUS MISSED")) {
618                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
619                sd->call_status = SKYPE_CALL_MISSED;
620        } else if (!strcmp(info, "STATUS CANCELLED")) {
621                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
622                sd->call_status = SKYPE_CALL_CANCELLED;
623        } else if (!strcmp(info, "STATUS FINISHED")) {
624                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
625                sd->call_status = SKYPE_CALL_FINISHED;
626        } else if (!strcmp(info, "STATUS REFUSED")) {
627                skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
628                sd->call_status = SKYPE_CALL_REFUSED;
629        } else if (!strcmp(info, "STATUS UNPLACED")) {
630                if (sd->call_id)
631                        g_free(sd->call_id);
632                /* Save the ID for later usage (Cancel/Finish). */
633                sd->call_id = g_strdup(id);
634                sd->call_out = TRUE;
635        } else if (!strcmp(info, "STATUS FAILED")) {
636                imcb_error(ic, "Call failed: %s",
637                        skype_call_strerror(sd->failurereason));
638                sd->call_id = NULL;
639        } else if (!strncmp(info, "DURATION ", 9)) {
640                if (sd->call_duration)
641                        g_free(sd->call_duration);
642                sd->call_duration = g_strdup(info+9);
643        } else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
644                info += 15;
645                if (!sd->call_status)
646                        return;
647                switch (sd->call_status) {
648                case SKYPE_CALL_RINGING:
649                        if (sd->call_out)
650                                imcb_log(ic, "You are currently ringing "
651                                        "the user %s.", info);
652                        else {
653                                g_snprintf(buf, IRC_LINE_SIZE,
654                                        "The user %s is currently ringing you.",
655                                        info);
656                                skype_call_ask(ic, sd->call_id, buf);
657                        }
658                        break;
659                case SKYPE_CALL_MISSED:
660                        imcb_log(ic, "You have missed a call from user %s.",
661                                info);
662                        break;
663                case SKYPE_CALL_CANCELLED:
664                        imcb_log(ic, "You cancelled the call to the user %s.",
665                                info);
666                        sd->call_status = 0;
667                        sd->call_out = FALSE;
668                        break;
669                case SKYPE_CALL_REFUSED:
670                        if (sd->call_out)
671                                imcb_log(ic, "The user %s refused the call.",
672                                        info);
673                        else
674                                imcb_log(ic,
675                                        "You refused the call from user %s.",
676                                        info);
677                        sd->call_out = FALSE;
678                        break;
679                case SKYPE_CALL_FINISHED:
680                        if (sd->call_duration)
681                                imcb_log(ic,
682                                        "You finished the call to the user %s "
683                                        "(duration: %s seconds).",
684                                        info, sd->call_duration);
685                        else
686                                imcb_log(ic,
687                                        "You finished the call to the user %s.",
688                                        info);
689                        sd->call_out = FALSE;
690                        break;
691                default:
692                        /* Don't be noisy, ignore other statuses for now. */
693                        break;
694                }
695                sd->call_status = 0;
696        }
697}
698
699static void skype_parse_filetransfer(struct im_connection *ic, char *line)
700{
701        struct skype_data *sd = ic->proto_data;
702        char *id = strchr(line, ' ');
703
704        if (!++id)
705                return;
706        char *info = strchr(id, ' ');
707
708        if (!info)
709                return;
710        *info = '\0';
711        info++;
712        if (!strcmp(info, "STATUS NEW")) {
713                skype_printf(ic, "GET FILETRANSFER %s PARTNER_HANDLE\n",
714                        id);
715                sd->filetransfer_status = SKYPE_FILETRANSFER_NEW;
716        } else if (!strcmp(info, "STATUS FAILED")) {
717                skype_printf(ic, "GET FILETRANSFER %s PARTNER_HANDLE\n",
718                        id);
719                sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED;
720        } else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
721                info += 15;
722                if (!sd->filetransfer_status)
723                        return;
724                switch (sd->filetransfer_status) {
725                case SKYPE_FILETRANSFER_NEW:
726                        imcb_log(ic, "The user %s offered a new file for you.",
727                                info);
728                        break;
729                case SKYPE_FILETRANSFER_FAILED:
730                        imcb_log(ic, "Failed to transfer file from user %s.",
731                                info);
732                        break;
733                }
734                sd->filetransfer_status = 0;
735        }
736}
737
738static void skype_parse_chat(struct im_connection *ic, char *line)
739{
740        struct skype_data *sd = ic->proto_data;
741        char buf[IRC_LINE_SIZE];
742        char *id = strchr(line, ' ');
743
744        if (!++id)
745                return;
746        struct groupchat *gc;
747        char *info = strchr(id, ' ');
748
749        if (!info)
750                return;
751        *info = '\0';
752        info++;
753        /* Remove fake chat if we created one in skype_chat_with() */
754        gc = skype_chat_by_name(ic, "");
755        if (gc)
756                imcb_chat_free(gc);
757        if (!strcmp(info, "STATUS MULTI_SUBSCRIBED")) {
758                imcb_chat_new(ic, id);
759                skype_printf(ic, "GET CHAT %s ADDER\n", id);
760                skype_printf(ic, "GET CHAT %s TOPIC\n", id);
761        } else if (!strcmp(info, "STATUS DIALOG") && sd->groupchat_with) {
762                gc = imcb_chat_new(ic, id);
763                /* According to the docs this
764                 * is necessary. However it
765                 * does not seem the situation
766                 * and it would open an extra
767                 * window on our client, so
768                 * just leave it out. */
769                /*skype_printf(ic, "OPEN CHAT %s\n", id);*/
770                g_snprintf(buf, IRC_LINE_SIZE, "%s@skype.com",
771                                sd->groupchat_with);
772                imcb_chat_add_buddy(gc, buf);
773                imcb_chat_add_buddy(gc, sd->username);
774                g_free(sd->groupchat_with);
775                sd->groupchat_with = NULL;
776                skype_printf(ic, "GET CHAT %s ADDER\n", id);
777                skype_printf(ic, "GET CHAT %s TOPIC\n", id);
778        } else if (!strcmp(info, "STATUS UNSUBSCRIBED")) {
779                gc = skype_chat_by_name(ic, id);
780                if (gc)
781                        gc->data = (void *)FALSE;
782        } else if (!strncmp(info, "ADDER ", 6)) {
783                info += 6;
784                g_free(sd->adder);
785                sd->adder = g_strdup_printf("%s@skype.com", info);
786        } else if (!strncmp(info, "TOPIC ", 6)) {
787                info += 6;
788                gc = skype_chat_by_name(ic, id);
789                if (gc && (sd->adder || sd->topic_wait)) {
790                        if (sd->topic_wait) {
791                                sd->adder = g_strdup(sd->username);
792                                sd->topic_wait = 0;
793                        }
794                        imcb_chat_topic(gc, sd->adder, info, 0);
795                        g_free(sd->adder);
796                        sd->adder = NULL;
797                }
798        } else if (!strncmp(info, "ACTIVEMEMBERS ", 14)) {
799                info += 14;
800                gc = skype_chat_by_name(ic, id);
801                /* Hack! We set ->data to TRUE
802                 * while we're on the channel
803                 * so that we won't rejoin
804                 * after a /part. */
805                if (!gc || gc->data)
806                        return;
807                char **members = g_strsplit(info, " ", 0);
808                int i;
809                for (i = 0; members[i]; i++) {
810                        if (!strcmp(members[i], sd->username))
811                                continue;
812                        g_snprintf(buf, IRC_LINE_SIZE, "%s@skype.com",
813                                        members[i]);
814                        if (!g_list_find_custom(gc->in_room, buf,
815                                (GCompareFunc)strcmp))
816                                imcb_chat_add_buddy(gc, buf);
817                }
818                imcb_chat_add_buddy(gc, sd->username);
819                g_strfreev(members);
820        }
821}
822
823static void skype_parse_password(struct im_connection *ic, char *line)
824{
825        if (!strncmp(line+9, "OK", 2))
826                imcb_connected(ic);
827        else {
828                imcb_error(ic, "Authentication Failed");
829                imc_logout(ic, TRUE);
830        }
831}
832
833static void skype_parse_profile(struct im_connection *ic, char *line)
834{
835        imcb_log(ic, "SkypeOut balance value is '%s'.", line+21);
836}
837
838static void skype_parse_ping(struct im_connection *ic, char *line)
839{
840        skype_printf(ic, "PONG\n");
841}
842
843static void skype_parse_chats(struct im_connection *ic, char *line)
844{
845        char **i;
846        char **chats = g_strsplit(line + 6, ", ", 0);
847
848        i = chats;
849        while (*i) {
850                skype_printf(ic, "GET CHAT %s STATUS\n", *i);
851                skype_printf(ic, "GET CHAT %s ACTIVEMEMBERS\n", *i);
852                i++;
853        }
854        g_strfreev(chats);
855}
856
857typedef void (*skype_parser)(struct im_connection *ic, char *line);
858
859static gboolean skype_read_callback(gpointer data, gint fd,
860                                    b_input_condition cond)
861{
862        struct im_connection *ic = data;
863        struct skype_data *sd = ic->proto_data;
864        char buf[IRC_LINE_SIZE];
865        int st, i;
866        char **lines, **lineptr, *line;
867        static struct parse_map {
868                char *k;
869                skype_parser v;
870        } parsers[] = {
871                { "USERS ", skype_parse_users },
872                { "USER ", skype_parse_user },
873                { "CHATMESSAGE ", skype_parse_chatmessage },
874                { "CALL ", skype_parse_call },
875                { "FILETRANSFER ", skype_parse_filetransfer },
876                { "CHAT ", skype_parse_chat },
877                { "PASSWORD ", skype_parse_password },
878                { "PROFILE PSTN_BALANCE ", skype_parse_profile },
879                { "PING", skype_parse_ping },
880                { "CHATS ", skype_parse_chats },
881        };
882
883        if (!sd || sd->fd == -1)
884                return FALSE;
885        /* Read the whole data. */
886        st = ssl_read(sd->ssl, buf, sizeof(buf));
887        if (st > 0) {
888                buf[st] = '\0';
889                /* Then split it up to lines. */
890                lines = g_strsplit(buf, "\n", 0);
891                lineptr = lines;
892                while ((line = *lineptr)) {
893                        if (!strlen(line))
894                                break;
895                        if (set_getbool(&ic->acc->set, "skypeconsole_receive"))
896                                imcb_buddy_msg(ic, "skypeconsole", line, 0, 0);
897                        for (i = 0; i < ARRAY_SIZE(parsers); i++)
898                                if (!strncmp(line, parsers[i].k,
899                                        strlen(parsers[i].k))) {
900                                        parsers[i].v(ic, line);
901                                        break;
902                                }
903                        lineptr++;
904                }
905                g_strfreev(lines);
906        } else if (st == 0 || (st < 0 && !sockerr_again())) {
907                closesocket(sd->fd);
908                sd->fd = -1;
909
910                imcb_error(ic, "Error while reading from server");
911                imc_logout(ic, TRUE);
912                return FALSE;
913        }
914        return TRUE;
915}
916
917gboolean skype_start_stream(struct im_connection *ic)
918{
919        struct skype_data *sd = ic->proto_data;
920        int st;
921
922        if (!sd)
923                return FALSE;
924
925        if (sd->bfd <= 0)
926                sd->bfd = b_input_add(sd->fd, GAIM_INPUT_READ,
927                        skype_read_callback, ic);
928
929        /* Log in */
930        skype_printf(ic, "USERNAME %s\n", ic->acc->user);
931        skype_printf(ic, "PASSWORD %s\n", ic->acc->pass);
932
933        /* This will download all buddies. */
934        st = skype_printf(ic, "SEARCH FRIENDS\n");
935        skype_printf(ic, "SET USERSTATUS ONLINE\n");
936
937        /* Auto join to bookmarked chats if requested.*/
938        if (set_getbool(&ic->acc->set, "auto_join"))
939                skype_printf(ic, "SEARCH BOOKMARKEDCHATS\n");
940        return st;
941}
942
943gboolean skype_connected(gpointer data, void *source, b_input_condition cond)
944{
945        struct im_connection *ic = data;
946        struct skype_data *sd = ic->proto_data;
947        if (!source) {
948                sd->ssl = NULL;
949                imcb_error(ic, "Could not connect to server");
950                imc_logout(ic, TRUE);
951                return FALSE;
952        }
953        imcb_log(ic, "Connected to server, logging in");
954        return skype_start_stream(ic);
955}
956
957static void skype_login(account_t *acc)
958{
959        struct im_connection *ic = imcb_new(acc);
960        struct skype_data *sd = g_new0(struct skype_data, 1);
961
962        ic->proto_data = sd;
963
964        imcb_log(ic, "Connecting");
965        sd->ssl = ssl_connect(set_getstr(&acc->set, "server"),
966                set_getint(&acc->set, "port"), skype_connected, ic);
967        sd->fd = sd->ssl ? ssl_getfd(sd->ssl) : -1;
968        sd->username = g_strdup(acc->user);
969
970        sd->ic = ic;
971
972        if (set_getbool(&acc->set, "skypeconsole"))
973                imcb_add_buddy(ic, "skypeconsole", NULL);
974}
975
976static void skype_logout(struct im_connection *ic)
977{
978        struct skype_data *sd = ic->proto_data;
979
980        skype_printf(ic, "SET USERSTATUS OFFLINE\n");
981
982        g_free(sd->username);
983        g_free(sd->handle);
984        g_free(sd);
985        ic->proto_data = NULL;
986}
987
988static int skype_buddy_msg(struct im_connection *ic, char *who, char *message,
989                           int flags)
990{
991        char *ptr, *nick;
992        int st;
993
994        nick = g_strdup(who);
995        ptr = strchr(nick, '@');
996        if (ptr)
997                *ptr = '\0';
998
999        if (!strncmp(who, "skypeconsole", 12))
1000                st = skype_printf(ic, "%s\n", message);
1001        else
1002                st = skype_printf(ic, "MESSAGE %s %s\n", nick, message);
1003        g_free(nick);
1004
1005        return st;
1006}
1007
1008const struct skype_away_state *skype_away_state_by_name(char *name)
1009{
1010        int i;
1011
1012        for (i = 0; skype_away_state_list[i].full_name; i++)
1013                if (g_strcasecmp(skype_away_state_list[i].full_name, name) == 0)
1014                        return skype_away_state_list + i;
1015
1016        return NULL;
1017}
1018
1019static void skype_set_away(struct im_connection *ic, char *state_txt,
1020                           char *message)
1021{
1022        const struct skype_away_state *state;
1023
1024        if (state_txt == NULL)
1025                state = skype_away_state_by_name("Online");
1026        else
1027                state = skype_away_state_by_name(state_txt);
1028        skype_printf(ic, "SET USERSTATUS %s\n", state->code);
1029}
1030
1031static GList *skype_away_states(struct im_connection *ic)
1032{
1033        static GList *l;
1034        int i;
1035
1036        if (l == NULL)
1037                for (i = 0; skype_away_state_list[i].full_name; i++)
1038                        l = g_list_append(l,
1039                                (void *)skype_away_state_list[i].full_name);
1040
1041        return l;
1042}
1043
1044static char *skype_set_display_name(set_t *set, char *value)
1045{
1046        account_t *acc = set->data;
1047        struct im_connection *ic = acc->ic;
1048
1049        skype_printf(ic, "SET PROFILE FULLNAME %s", value);
1050        return value;
1051}
1052
1053static char *skype_set_balance(set_t *set, char *value)
1054{
1055        account_t *acc = set->data;
1056        struct im_connection *ic = acc->ic;
1057
1058        skype_printf(ic, "GET PROFILE PSTN_BALANCE");
1059        return value;
1060}
1061
1062static char *skype_set_call(set_t *set, char *value)
1063{
1064        account_t *acc = set->data;
1065        struct im_connection *ic = acc->ic;
1066        struct skype_data *sd = ic->proto_data;
1067        char *nick, *ptr;
1068
1069        if (value) {
1070                user_t *u = user_find(acc->irc, value);
1071                /* We are starting a call */
1072                if (!u)
1073                        nick = g_strdup(value);
1074                else
1075                        nick = g_strdup(u->handle);
1076                ptr = strchr(nick, '@');
1077                if (ptr)
1078                        *ptr = '\0';
1079
1080                skype_printf(ic, "CALL %s", nick);
1081                g_free(nick);
1082        } else {
1083                /* We are ending a call */
1084                if (sd->call_id) {
1085                        skype_printf(ic, "SET CALL %s STATUS FINISHED",
1086                                sd->call_id);
1087                        g_free(sd->call_id);
1088                        sd->call_id = NULL;
1089                } else
1090                        imcb_error(ic, "There are no active calls currently.");
1091        }
1092        return value;
1093}
1094
1095static void skype_add_buddy(struct im_connection *ic, char *who, char *group)
1096{
1097        char *nick, *ptr;
1098
1099        nick = g_strdup(who);
1100        ptr = strchr(nick, '@');
1101        if (ptr)
1102                *ptr = '\0';
1103        skype_printf(ic, "SET USER %s BUDDYSTATUS 2 Please authorize me\n",
1104                nick);
1105        g_free(nick);
1106}
1107
1108static void skype_remove_buddy(struct im_connection *ic, char *who, char *group)
1109{
1110        char *nick, *ptr;
1111
1112        nick = g_strdup(who);
1113        ptr = strchr(nick, '@');
1114        if (ptr)
1115                *ptr = '\0';
1116        skype_printf(ic, "SET USER %s BUDDYSTATUS 1\n", nick);
1117        g_free(nick);
1118}
1119
1120void skype_chat_msg(struct groupchat *gc, char *message, int flags)
1121{
1122        struct im_connection *ic = gc->ic;
1123        skype_printf(ic, "CHATMESSAGE %s %s\n", gc->title, message);
1124}
1125
1126void skype_chat_leave(struct groupchat *gc)
1127{
1128        struct im_connection *ic = gc->ic;
1129        skype_printf(ic, "ALTER CHAT %s LEAVE\n", gc->title);
1130        gc->data = (void *)TRUE;
1131}
1132
1133void skype_chat_invite(struct groupchat *gc, char *who, char *message)
1134{
1135        struct im_connection *ic = gc->ic;
1136        char *ptr, *nick;
1137        nick = g_strdup(message);
1138        ptr = strchr(nick, '@');
1139        if (ptr)
1140                *ptr = '\0';
1141        skype_printf(ic, "ALTER CHAT %s ADDMEMBERS %s\n", gc->title, nick);
1142        g_free(nick);
1143}
1144
1145void skype_chat_topic(struct groupchat *gc, char *message)
1146{
1147        struct im_connection *ic = gc->ic;
1148        struct skype_data *sd = ic->proto_data;
1149        skype_printf(ic, "ALTER CHAT %s SETTOPIC %s\n",
1150                gc->title, message);
1151        sd->topic_wait = 1;
1152}
1153
1154struct groupchat *skype_chat_with(struct im_connection *ic, char *who)
1155{
1156        struct skype_data *sd = ic->proto_data;
1157        char *ptr, *nick;
1158        nick = g_strdup(who);
1159        ptr = strchr(nick, '@');
1160        if (ptr)
1161                *ptr = '\0';
1162        skype_printf(ic, "CHAT CREATE %s\n", nick);
1163        sd->groupchat_with = g_strdup(nick);
1164        g_free(nick);
1165        /* We create a fake chat for now. We will replace it with a real one in
1166         * the real callback. */
1167        return imcb_chat_new(ic, "");
1168}
1169
1170static void skype_get_info(struct im_connection *ic, char *who)
1171{
1172        char *ptr, *nick;
1173        nick = g_strdup(who);
1174        ptr = strchr(nick, '@');
1175        if (ptr)
1176                *ptr = '\0';
1177        skype_printf(ic, "GET USER %s FULLNAME\n", nick);
1178        skype_printf(ic, "GET USER %s PHONE_HOME\n", nick);
1179        skype_printf(ic, "GET USER %s PHONE_OFFICE\n", nick);
1180        skype_printf(ic, "GET USER %s PHONE_MOBILE\n", nick);
1181        skype_printf(ic, "GET USER %s NROF_AUTHED_BUDDIES\n", nick);
1182        skype_printf(ic, "GET USER %s TIMEZONE\n", nick);
1183        skype_printf(ic, "GET USER %s LASTONLINETIMESTAMP\n", nick);
1184        skype_printf(ic, "GET USER %s BIRTHDAY\n", nick);
1185        skype_printf(ic, "GET USER %s SEX\n", nick);
1186        skype_printf(ic, "GET USER %s LANGUAGE\n", nick);
1187        skype_printf(ic, "GET USER %s COUNTRY\n", nick);
1188        skype_printf(ic, "GET USER %s PROVINCE\n", nick);
1189        skype_printf(ic, "GET USER %s CITY\n", nick);
1190        skype_printf(ic, "GET USER %s HOMEPAGE\n", nick);
1191        skype_printf(ic, "GET USER %s ABOUT\n", nick);
1192}
1193
1194static void skype_set_my_name(struct im_connection *ic, char *info)
1195{
1196        skype_set_display_name(set_find(&ic->acc->set, "display_name"), info);
1197}
1198
1199static void skype_init(account_t *acc)
1200{
1201        set_t *s;
1202
1203        s = set_add(&acc->set, "server", SKYPE_DEFAULT_SERVER, set_eval_account,
1204                acc);
1205        s->flags |= ACC_SET_OFFLINE_ONLY;
1206
1207        s = set_add(&acc->set, "port", SKYPE_DEFAULT_PORT, set_eval_int, acc);
1208        s->flags |= ACC_SET_OFFLINE_ONLY;
1209
1210        s = set_add(&acc->set, "display_name", NULL, skype_set_display_name,
1211                acc);
1212        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
1213
1214        s = set_add(&acc->set, "call", NULL, skype_set_call, acc);
1215        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
1216
1217        s = set_add(&acc->set, "balance", NULL, skype_set_balance, acc);
1218        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
1219
1220        s = set_add(&acc->set, "skypeout_offline", "true", set_eval_bool, acc);
1221
1222        s = set_add(&acc->set, "skypeconsole", "false", set_eval_bool, acc);
1223        s->flags |= ACC_SET_OFFLINE_ONLY;
1224
1225        s = set_add(&acc->set, "skypeconsole_receive", "false", set_eval_bool,
1226                acc);
1227        s->flags |= ACC_SET_OFFLINE_ONLY;
1228
1229        s = set_add(&acc->set, "auto_join", "false", set_eval_bool, acc);
1230        s->flags |= ACC_SET_OFFLINE_ONLY;
1231
1232        s = set_add(&acc->set, "test_join", "false", set_eval_bool, acc);
1233        s->flags |= ACC_SET_OFFLINE_ONLY;
1234
1235        s = set_add(&acc->set, "edit_prefix", "EDIT:",
1236                        NULL, acc);
1237}
1238
1239void init_plugin(void)
1240{
1241        struct prpl *ret = g_new0(struct prpl, 1);
1242
1243        ret->name = "skype";
1244        ret->login = skype_login;
1245        ret->init = skype_init;
1246        ret->logout = skype_logout;
1247        ret->buddy_msg = skype_buddy_msg;
1248        ret->get_info = skype_get_info;
1249        ret->set_my_name = skype_set_my_name;
1250        ret->away_states = skype_away_states;
1251        ret->set_away = skype_set_away;
1252        ret->add_buddy = skype_add_buddy;
1253        ret->remove_buddy = skype_remove_buddy;
1254        ret->chat_msg = skype_chat_msg;
1255        ret->chat_leave = skype_chat_leave;
1256        ret->chat_invite = skype_chat_invite;
1257        ret->chat_with = skype_chat_with;
1258        ret->handle_cmp = g_strcasecmp;
1259        ret->chat_topic = skype_chat_topic;
1260        register_protocol(ret);
1261}
Note: See TracBrowser for help on using the repository browser.