source: skype/skype.c @ a349932

Last change on this file since a349932 was 5adcc65, checked in by Miklos Vajna <vmiklos@…>, at 2008-12-21T20:51:12Z

whitespace cleanup

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