source: skype/skype.c @ 9fd7202

Last change on this file since 9fd7202 was 9fd7202, checked in by Miklos Vajna <vmiklos@…>, at 2008-02-29T00:59:51Z

error out on cancelling a non-existing call

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