source: skype/skype.c @ c35bf7a

Last change on this file since c35bf7a was c35bf7a, checked in by Miklos Vajna <vmiklos@…>, at 2009-01-07T01:29:22Z

introduce skype_parse_chat()

  • 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 void skype_parse_filetransfer(struct im_connection *ic, char *line)
658{
659        struct skype_data *sd = ic->proto_data;
660        char buf[1024];
661        char *id = strchr(line, ' ');
662
663        if (++id) {
664                char *info = strchr(id, ' ');
665
666                if (!info)
667                        return;
668                *info = '\0';
669                info++;
670                if (!strcmp(info, "STATUS NEW")) {
671                        g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id);
672                        skype_write(ic, buf);
673                        sd->filetransfer_status = SKYPE_FILETRANSFER_NEW;
674                } else if (!strcmp(info, "STATUS FAILED")) {
675                        g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id);
676                        skype_write(ic, buf);
677                        sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED;
678                } else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
679                        info += 15;
680                        if (sd->filetransfer_status) {
681                                switch (sd->filetransfer_status) {
682                                        case SKYPE_FILETRANSFER_NEW:
683                                                imcb_log(ic, "The user %s offered a new file for you.", info);
684                                                break;
685                                        case SKYPE_FILETRANSFER_FAILED:
686                                                imcb_log(ic, "Failed to transfer file from user %s.", info);
687                                                break;
688                                }
689                                sd->filetransfer_status = 0;
690                        }
691                }
692        }
693}
694
695static void skype_parse_chat(struct im_connection *ic, char *line)
696{
697        struct skype_data *sd = ic->proto_data;
698        char buf[1024];
699        char *id = strchr(line, ' ');
700
701        if (++id) {
702                struct groupchat *gc;
703                char *info = strchr(id, ' ');
704
705                if (!info)
706                        return;
707                *info = '\0';
708                info++;
709                /* Remove fake chat if we created one in skype_chat_with() */
710                gc = skype_chat_by_name(ic, "");
711                if (gc)
712                        imcb_chat_free(gc);
713                if (!strcmp(info, "STATUS MULTI_SUBSCRIBED")) {
714                        imcb_chat_new(ic, id);
715                        g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id);
716                        skype_write(ic, buf);
717                        g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id);
718                        skype_write(ic, buf);
719                } else if (!strcmp(info, "STATUS DIALOG") && sd->groupchat_with) {
720                        gc = imcb_chat_new(ic, id);
721                        /* According to the docs this
722                         * is necessary. However it
723                         * does not seem the situation
724                         * and it would open an extra
725                         * window on our client, so
726                         * just leave it out. */
727                        /*g_snprintf(buf, 1024, "OPEN CHAT %s\n", id);
728                          skype_write(ic, buf);*/
729                        g_snprintf(buf, 1024, "%s@skype.com", sd->groupchat_with);
730                        imcb_chat_add_buddy(gc, buf);
731                        imcb_chat_add_buddy(gc, sd->username);
732                        g_free(sd->groupchat_with);
733                        sd->groupchat_with = NULL;
734                        g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id);
735                        skype_write(ic, buf);
736                        g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id);
737                        skype_write(ic, buf);
738                } else if (!strcmp(info, "STATUS UNSUBSCRIBED")) {
739                        gc = skype_chat_by_name(ic, id);
740                        if (gc)
741                                gc->data = (void *)FALSE;
742                } else if (!strncmp(info, "ADDER ", 6)) {
743                        info += 6;
744                        g_free(sd->adder);
745                        sd->adder = g_strdup_printf("%s@skype.com", info);
746                } else if (!strncmp(info, "TOPIC ", 6)) {
747                        info += 6;
748                        gc = skype_chat_by_name(ic, id);
749                        if (gc && (sd->adder || sd->topic_wait)) {
750                                if (sd->topic_wait) {
751                                        sd->adder = g_strdup(sd->username);
752                                        sd->topic_wait = 0;
753                                }
754                                imcb_chat_topic(gc, sd->adder, info, 0);
755                                g_free(sd->adder);
756                                sd->adder = NULL;
757                        }
758                } else if (!strncmp(info, "ACTIVEMEMBERS ", 14)) {
759                        info += 14;
760                        gc = skype_chat_by_name(ic, id);
761                        /* Hack! We set ->data to TRUE
762                         * while we're on the channel
763                         * so that we won't rejoin
764                         * after a /part. */
765                        if (gc && !gc->data) {
766                                char **members = g_strsplit(info, " ", 0);
767                                int i;
768                                for (i = 0; members[i]; i++) {
769                                        if (!strcmp(members[i], sd->username))
770                                                continue;
771                                        g_snprintf(buf, 1024, "%s@skype.com", members[i]);
772                                        if (!g_list_find_custom(gc->in_room, buf, (GCompareFunc)strcmp))
773                                                imcb_chat_add_buddy(gc, buf);
774                                }
775                                imcb_chat_add_buddy(gc, sd->username);
776                                g_strfreev(members);
777                        }
778                }
779        }
780}
781
782static gboolean skype_read_callback(gpointer data, gint fd,
783                                    b_input_condition cond)
784{
785        struct im_connection *ic = data;
786        struct skype_data *sd = ic->proto_data;
787        char buf[1024];
788        int st;
789        char **lines, **lineptr, *line;
790
791        if (!sd || sd->fd == -1)
792                return FALSE;
793        /* Read the whole data. */
794        st = ssl_read(sd->ssl, buf, sizeof(buf));
795        if (st > 0) {
796                buf[st] = '\0';
797                /* Then split it up to lines. */
798                lines = g_strsplit(buf, "\n", 0);
799                lineptr = lines;
800                while ((line = *lineptr)) {
801                        if (!strlen(line))
802                                break;
803                        if (set_getbool(&ic->acc->set, "skypeconsole_receive"))
804                                imcb_buddy_msg(ic, "skypeconsole", line, 0, 0);
805                        if (!strncmp(line, "USERS ", 6))
806                                skype_parse_users(ic, line);
807                        else if (!strncmp(line, "USER ", 5))
808                                skype_parse_user(ic, line);
809                        else if (!strncmp(line, "CHATMESSAGE ", 12))
810                                skype_parse_chatmessage(ic, line);
811                        else if (!strncmp(line, "CALL ", 5))
812                                skype_parse_call(ic, line);
813                        else if (!strncmp(line, "FILETRANSFER ", 13))
814                                skype_parse_filetransfer(ic, line);
815                        else if (!strncmp(line, "CHAT ", 5))
816                                skype_parse_chat(ic, line);
817                        else if (!strncmp(line, "PASSWORD ", 9)) {
818                                if (!strncmp(line+9, "OK", 2))
819                                        imcb_connected(ic);
820                                else {
821                                        imcb_error(ic, "Authentication Failed");
822                                        imc_logout(ic, TRUE);
823                                }
824                        } else if (!strncmp(line, "PROFILE PSTN_BALANCE ", 21))
825                                imcb_log(ic, "SkypeOut balance value is '%s'.", line+21);
826                        else if (!strncmp(line, "PING", 4)) {
827                                g_snprintf(buf, 1024, "PONG\n");
828                                skype_write(ic, buf);
829                        } else if (!strncmp(line, "CHATS ", 6)) {
830                                char **i;
831                                char **chats = g_strsplit(line + 6, ", ", 0);
832
833                                i = chats;
834                                while (*i) {
835                                        g_snprintf(buf, 1024, "GET CHAT %s STATUS\n", *i);
836                                        skype_write(ic, buf);
837                                        g_snprintf(buf, 1024, "GET CHAT %s ACTIVEMEMBERS\n", *i);
838                                        skype_write(ic, buf);
839                                        i++;
840                                }
841                                g_strfreev(chats);
842                        }
843                        lineptr++;
844                }
845                g_strfreev(lines);
846        } else if (st == 0 || (st < 0 && !sockerr_again())) {
847                closesocket(sd->fd);
848                sd->fd = -1;
849
850                imcb_error(ic, "Error while reading from server");
851                imc_logout(ic, TRUE);
852                return FALSE;
853        }
854        return TRUE;
855}
856
857gboolean skype_start_stream(struct im_connection *ic)
858{
859        struct skype_data *sd = ic->proto_data;
860        char *buf;
861        int st;
862
863        if (!sd)
864                return FALSE;
865
866        if (sd->bfd <= 0)
867                sd->bfd = b_input_add(sd->fd, GAIM_INPUT_READ,
868                        skype_read_callback, ic);
869
870        /* Log in */
871        buf = g_strdup_printf("USERNAME %s\n", ic->acc->user);
872        st = skype_write(ic, buf);
873        g_free(buf);
874        buf = g_strdup_printf("PASSWORD %s\n", ic->acc->pass);
875        st = skype_write(ic, buf);
876        g_free(buf);
877
878        /* This will download all buddies. */
879        buf = g_strdup_printf("SEARCH FRIENDS\n");
880        st = skype_write(ic, buf);
881        g_free(buf);
882        buf = g_strdup_printf("SET USERSTATUS ONLINE\n");
883        skype_write(ic, buf);
884        g_free(buf);
885
886        /* Auto join to bookmarked chats if requested.*/
887        if (set_getbool(&ic->acc->set, "auto_join")) {
888                buf = g_strdup_printf("SEARCH BOOKMARKEDCHATS\n");
889                skype_write(ic, buf);
890                g_free(buf);
891        }
892        return st;
893}
894
895gboolean skype_connected(gpointer data, void *source, b_input_condition cond)
896{
897        struct im_connection *ic = data;
898        struct skype_data *sd = ic->proto_data;
899        if (!source) {
900                sd->ssl = NULL;
901                imcb_error(ic, "Could not connect to server");
902                imc_logout(ic, TRUE);
903                return FALSE;
904        }
905        imcb_log(ic, "Connected to server, logging in");
906        return skype_start_stream(ic);
907}
908
909static void skype_login(account_t *acc)
910{
911        struct im_connection *ic = imcb_new(acc);
912        struct skype_data *sd = g_new0(struct skype_data, 1);
913
914        ic->proto_data = sd;
915
916        imcb_log(ic, "Connecting");
917        sd->ssl = ssl_connect(set_getstr(&acc->set, "server"),
918                set_getint(&acc->set, "port"), skype_connected, ic);
919        sd->fd = sd->ssl ? ssl_getfd(sd->ssl) : -1;
920        sd->username = g_strdup(acc->user);
921
922        sd->ic = ic;
923
924        if (set_getbool(&acc->set, "skypeconsole"))
925                imcb_add_buddy(ic, "skypeconsole", NULL);
926}
927
928static void skype_logout(struct im_connection *ic)
929{
930        struct skype_data *sd = ic->proto_data;
931        char *buf;
932
933        buf = g_strdup_printf("SET USERSTATUS OFFLINE\n");
934        skype_write(ic, buf);
935        g_free(buf);
936
937        g_free(sd->username);
938        g_free(sd->handle);
939        g_free(sd);
940        ic->proto_data = NULL;
941}
942
943static int skype_buddy_msg(struct im_connection *ic, char *who, char *message,
944                           int flags)
945{
946        char *buf, *ptr, *nick;
947        int st;
948
949        nick = g_strdup(who);
950        ptr = strchr(nick, '@');
951        if (ptr)
952                *ptr = '\0';
953
954        if (!strncmp(who, "skypeconsole", 12))
955                buf = g_strdup_printf("%s\n", message);
956        else
957                buf = g_strdup_printf("MESSAGE %s %s\n", nick, message);
958        g_free(nick);
959        st = skype_write(ic, buf);
960        g_free(buf);
961
962        return st;
963}
964
965const struct skype_away_state *skype_away_state_by_name(char *name)
966{
967        int i;
968
969        for (i = 0; skype_away_state_list[i].full_name; i++)
970                if (g_strcasecmp(skype_away_state_list[i].full_name, name) == 0)
971                        return skype_away_state_list + i;
972
973        return NULL;
974}
975
976static void skype_set_away(struct im_connection *ic, char *state_txt,
977                           char *message)
978{
979        const struct skype_away_state *state;
980        char *buf;
981
982        if (strcmp(state_txt, GAIM_AWAY_CUSTOM) == 0)
983                state = skype_away_state_by_name("Away");
984        else
985                state = skype_away_state_by_name(state_txt);
986        buf = g_strdup_printf("SET USERSTATUS %s\n", state->code);
987        skype_write(ic, buf);
988        g_free(buf);
989}
990
991static GList *skype_away_states(struct im_connection *ic)
992{
993        static GList *l;
994        int i;
995
996        if (l == NULL)
997                for (i = 0; skype_away_state_list[i].full_name; i++)
998                        l = g_list_append(l,
999                                (void *)skype_away_state_list[i].full_name);
1000
1001        return l;
1002}
1003
1004static char *skype_set_display_name(set_t *set, char *value)
1005{
1006        account_t *acc = set->data;
1007        struct im_connection *ic = acc->ic;
1008        char *buf;
1009
1010        buf = g_strdup_printf("SET PROFILE FULLNAME %s", value);
1011        skype_write(ic, buf);
1012        g_free(buf);
1013        return value;
1014}
1015
1016static char *skype_set_balance(set_t *set, char *value)
1017{
1018        account_t *acc = set->data;
1019        struct im_connection *ic = acc->ic;
1020        char *buf;
1021
1022        buf = g_strdup_printf("GET PROFILE PSTN_BALANCE");
1023        skype_write(ic, buf);
1024        g_free(buf);
1025        return value;
1026}
1027
1028static char *skype_set_call(set_t *set, char *value)
1029{
1030        account_t *acc = set->data;
1031        struct im_connection *ic = acc->ic;
1032        struct skype_data *sd = ic->proto_data;
1033        char *nick, *ptr, *buf;
1034
1035        if (value) {
1036                user_t *u = user_find(acc->irc, value);
1037                /* We are starting a call */
1038                if (!u)
1039                        nick = g_strdup(value);
1040                else
1041                        nick = g_strdup(u->handle);
1042                ptr = strchr(nick, '@');
1043                if (ptr)
1044                        *ptr = '\0';
1045
1046                buf = g_strdup_printf("CALL %s", nick);
1047                skype_write(ic, buf);
1048                g_free(buf);
1049                g_free(nick);
1050        } else {
1051                /* We are ending a call */
1052                if (sd->call_id) {
1053                        buf = g_strdup_printf("SET CALL %s STATUS FINISHED",
1054                                sd->call_id);
1055                        skype_write(ic, buf);
1056                        g_free(buf);
1057                        g_free(sd->call_id);
1058                        sd->call_id = NULL;
1059                } else
1060                        imcb_error(ic, "There are no active calls currently.");
1061        }
1062        return value;
1063}
1064
1065static void skype_add_buddy(struct im_connection *ic, char *who, char *group)
1066{
1067        char *buf, *nick, *ptr;
1068
1069        nick = g_strdup(who);
1070        ptr = strchr(nick, '@');
1071        if (ptr)
1072                *ptr = '\0';
1073        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 2 Please authorize me\n",
1074                nick);
1075        skype_write(ic, buf);
1076        g_free(nick);
1077}
1078
1079static void skype_remove_buddy(struct im_connection *ic, char *who, char *group)
1080{
1081        char *buf, *nick, *ptr;
1082
1083        nick = g_strdup(who);
1084        ptr = strchr(nick, '@');
1085        if (ptr)
1086                *ptr = '\0';
1087        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 1\n", nick);
1088        skype_write(ic, buf);
1089        g_free(nick);
1090}
1091
1092void skype_chat_msg(struct groupchat *gc, char *message, int flags)
1093{
1094        struct im_connection *ic = gc->ic;
1095        char *buf;
1096        buf = g_strdup_printf("CHATMESSAGE %s %s\n", gc->title, message);
1097        skype_write(ic, buf);
1098        g_free(buf);
1099}
1100
1101void skype_chat_leave(struct groupchat *gc)
1102{
1103        struct im_connection *ic = gc->ic;
1104        char *buf;
1105        buf = g_strdup_printf("ALTER CHAT %s LEAVE\n", gc->title);
1106        skype_write(ic, buf);
1107        g_free(buf);
1108        gc->data = (void *)TRUE;
1109}
1110
1111void skype_chat_invite(struct groupchat *gc, char *who, char *message)
1112{
1113        struct im_connection *ic = gc->ic;
1114        char *buf, *ptr, *nick;
1115        nick = g_strdup(message);
1116        ptr = strchr(nick, '@');
1117        if (ptr)
1118                *ptr = '\0';
1119        buf = g_strdup_printf("ALTER CHAT %s ADDMEMBERS %s\n", gc->title, nick);
1120        skype_write(ic, buf);
1121        g_free(buf);
1122        g_free(nick);
1123}
1124
1125void skype_chat_topic(struct groupchat *gc, char *message)
1126{
1127        struct im_connection *ic = gc->ic;
1128        struct skype_data *sd = ic->proto_data;
1129        char *buf;
1130        buf = g_strdup_printf("ALTER CHAT %s SETTOPIC %s\n",
1131                gc->title, message);
1132        skype_write(ic, buf);
1133        g_free(buf);
1134        sd->topic_wait = 1;
1135}
1136
1137struct groupchat *skype_chat_with(struct im_connection *ic, char *who)
1138{
1139        struct skype_data *sd = ic->proto_data;
1140        char *ptr, *nick, *buf;
1141        nick = g_strdup(who);
1142        ptr = strchr(nick, '@');
1143        if (ptr)
1144                *ptr = '\0';
1145        buf = g_strdup_printf("CHAT CREATE %s\n", nick);
1146        skype_write(ic, buf);
1147        g_free(buf);
1148        sd->groupchat_with = g_strdup(nick);
1149        g_free(nick);
1150        /* We create a fake chat for now. We will replace it with a real one in
1151         * the real callback. */
1152        return imcb_chat_new(ic, "");
1153}
1154
1155static void skype_get_info(struct im_connection *ic, char *who)
1156{
1157        char *ptr, *nick, *buf;
1158        nick = g_strdup(who);
1159        ptr = strchr(nick, '@');
1160        if (ptr)
1161                *ptr = '\0';
1162        buf = g_strdup_printf("GET USER %s FULLNAME\n", nick);
1163        skype_write(ic, buf);
1164        g_free(buf);
1165        buf = g_strdup_printf("GET USER %s PHONE_HOME\n", nick);
1166        skype_write(ic, buf);
1167        g_free(buf);
1168        buf = g_strdup_printf("GET USER %s PHONE_OFFICE\n", nick);
1169        skype_write(ic, buf);
1170        g_free(buf);
1171        buf = g_strdup_printf("GET USER %s PHONE_MOBILE\n", nick);
1172        skype_write(ic, buf);
1173        g_free(buf);
1174        buf = g_strdup_printf("GET USER %s NROF_AUTHED_BUDDIES\n", nick);
1175        skype_write(ic, buf);
1176        g_free(buf);
1177        buf = g_strdup_printf("GET USER %s TIMEZONE\n", nick);
1178        skype_write(ic, buf);
1179        g_free(buf);
1180        buf = g_strdup_printf("GET USER %s LASTONLINETIMESTAMP\n", nick);
1181        skype_write(ic, buf);
1182        g_free(buf);
1183        buf = g_strdup_printf("GET USER %s BIRTHDAY\n", nick);
1184        skype_write(ic, buf);
1185        g_free(buf);
1186        buf = g_strdup_printf("GET USER %s SEX\n", nick);
1187        skype_write(ic, buf);
1188        g_free(buf);
1189        buf = g_strdup_printf("GET USER %s LANGUAGE\n", nick);
1190        skype_write(ic, buf);
1191        g_free(buf);
1192        buf = g_strdup_printf("GET USER %s COUNTRY\n", nick);
1193        skype_write(ic, buf);
1194        g_free(buf);
1195        buf = g_strdup_printf("GET USER %s PROVINCE\n", nick);
1196        skype_write(ic, buf);
1197        g_free(buf);
1198        buf = g_strdup_printf("GET USER %s CITY\n", nick);
1199        skype_write(ic, buf);
1200        g_free(buf);
1201        buf = g_strdup_printf("GET USER %s HOMEPAGE\n", nick);
1202        skype_write(ic, buf);
1203        g_free(buf);
1204        buf = g_strdup_printf("GET USER %s ABOUT\n", nick);
1205        skype_write(ic, buf);
1206        g_free(buf);
1207}
1208
1209static void skype_set_my_name(struct im_connection *ic, char *info)
1210{
1211        skype_set_display_name(set_find(&ic->acc->set, "display_name"), info);
1212}
1213
1214static void skype_init(account_t *acc)
1215{
1216        set_t *s;
1217
1218        s = set_add(&acc->set, "server", SKYPE_DEFAULT_SERVER, set_eval_account,
1219                acc);
1220        s->flags |= ACC_SET_OFFLINE_ONLY;
1221
1222        s = set_add(&acc->set, "port", SKYPE_DEFAULT_PORT, set_eval_int, acc);
1223        s->flags |= ACC_SET_OFFLINE_ONLY;
1224
1225        s = set_add(&acc->set, "display_name", NULL, skype_set_display_name,
1226                acc);
1227        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
1228
1229        s = set_add(&acc->set, "call", NULL, skype_set_call, acc);
1230        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
1231
1232        s = set_add(&acc->set, "balance", NULL, skype_set_balance, acc);
1233        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
1234
1235        s = set_add(&acc->set, "skypeout_offline", "true", set_eval_bool, acc);
1236
1237        s = set_add(&acc->set, "skypeconsole", "false", set_eval_bool, acc);
1238        s->flags |= ACC_SET_OFFLINE_ONLY;
1239
1240        s = set_add(&acc->set, "skypeconsole_receive", "false", set_eval_bool,
1241                acc);
1242        s->flags |= ACC_SET_OFFLINE_ONLY;
1243
1244        s = set_add(&acc->set, "auto_join", "false", set_eval_bool, acc);
1245        s->flags |= ACC_SET_OFFLINE_ONLY;
1246}
1247
1248void init_plugin(void)
1249{
1250        struct prpl *ret = g_new0(struct prpl, 1);
1251
1252        ret->name = "skype";
1253        ret->login = skype_login;
1254        ret->init = skype_init;
1255        ret->logout = skype_logout;
1256        ret->buddy_msg = skype_buddy_msg;
1257        ret->get_info = skype_get_info;
1258        ret->set_my_name = skype_set_my_name;
1259        ret->away_states = skype_away_states;
1260        ret->set_away = skype_set_away;
1261        ret->add_buddy = skype_add_buddy;
1262        ret->remove_buddy = skype_remove_buddy;
1263        ret->chat_msg = skype_chat_msg;
1264        ret->chat_leave = skype_chat_leave;
1265        ret->chat_invite = skype_chat_invite;
1266        ret->chat_with = skype_chat_with;
1267        ret->handle_cmp = g_strcasecmp;
1268        ret->chat_topic = skype_chat_topic;
1269        register_protocol(ret);
1270}
Note: See TracBrowser for help on using the repository browser.