source: skype/skype.c @ 1f4fc80

Last change on this file since 1f4fc80 was 1f4fc80, checked in by Miklos Vajna <vmiklos@…>, at 2009-12-12T01:04:43Z

introduce skype_printf

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