source: skype/skype.c @ 9f2f25f

Last change on this file since 9f2f25f was 9f2f25f, checked in by Miklos Vajna <vmiklos@…>, at 2009-01-07T01:23:38Z

introduce skype_parse_call()

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