source: skype/skype.c @ dbbf34a

Last change on this file since dbbf34a was 3518933, checked in by Miklos Vajna <vmiklos@…>, at 2010-04-19T22:40:58Z

cosmetics

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