source: skype/skype.c @ a92fb07

Last change on this file since a92fb07 was a92fb07, checked in by Miklos Vajna <vmiklos@…>, at 2008-02-29T01:10:01Z

detect when the call is refused

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