source: skype/skype.c @ 7c300bb

Last change on this file since 7c300bb was 7c300bb, checked in by Miklos Vajna <vmiklos@…>, at 2009-12-12T01:08:22Z

make skype_write() parameters mimic write()'s one

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