source: skype/skype.c @ 6e14204

Last change on this file since 6e14204 was 6e14204, checked in by Miklos Vajna <vmiklos@…>, at 2009-01-07T01:20:57Z

introduce skype_parse_user() and skype_parse_chatmessage()

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