source: skype/skype.c @ 304aa33

Last change on this file since 304aa33 was 304aa33, checked in by Miklos Vajna <vmiklos@…>, at 2010-04-13T23:26:02Z

add new setting to show mood texts

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