source: skype/skype.c @ 5d9db76

Last change on this file since 5d9db76 was 5d9db76, checked in by Miklos Vajna <vmiklos@…>, at 2010-03-07T23:34:45Z

checkpatch fixes

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