source: skype/skype.c @ d87daf3

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

replace the fake SKYPE_CALL_RINGING_OUT with a proper call_out variable

  • 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_CANCELLED,
45        SKYPE_CALL_FINISHED,
46        SKYPE_CALL_REFUSED
47} skype_call_status;
48
49typedef enum
50{
51        SKYPE_FILETRANSFER_NEW = 1,
52        SKYPE_FILETRANSFER_FAILED
53} skype_filetransfer_status;
54
55/*
56 * Structures
57 */
58
59struct skype_data
60{
61        struct im_connection *ic;
62        char *username;
63        /* The effective file descriptor. We store it here so any function can
64         * write() to it. */
65        int fd;
66        /* File descriptor returned by bitlbee. we store it so we know when
67         * we're connected and when we aren't. */
68        int bfd;
69        /* ssl_getfd() uses this to get the file desciptor. */
70        void *ssl;
71        /* When we receive a new message id, we query the properties, finally
72         * the chatname. Store the properties here so that we can use
73         * imcb_buddy_msg() when we got the chatname. */
74        char *handle;
75        /* List, because of multiline messages. */
76        GList *body;
77        char *type;
78        /* This is necessary because we send a notification when we get the
79         * handle. So we store the state here and then we can send a
80         * notification about the handle is in a given status. */
81        skype_call_status call_status;
82        char *call_id;
83        char *call_duration;
84        /* If the call is outgoing or not */
85        int call_out;
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                                                sd->call_status = SKYPE_CALL_RINGING;
572                                        }
573                                        else if(!strcmp(info, "STATUS MISSED"))
574                                        {
575                                                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
576                                                skype_write( ic, buf, strlen( buf ) );
577                                                sd->call_status = SKYPE_CALL_MISSED;
578                                        }
579                                        else if(!strcmp(info, "STATUS CANCELLED"))
580                                        {
581                                                        g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
582                                                        skype_write( ic, buf, strlen( buf ) );
583                                                        sd->call_status = SKYPE_CALL_CANCELLED;
584                                        }
585                                        else if(!strcmp(info, "STATUS FINISHED"))
586                                        {
587                                                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
588                                                skype_write( ic, buf, strlen( buf ) );
589                                                sd->call_status = SKYPE_CALL_FINISHED;
590                                        }
591                                        else if(!strcmp(info, "STATUS REFUSED"))
592                                        {
593                                                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
594                                                skype_write( ic, buf, strlen( buf ) );
595                                                sd->call_status = SKYPE_CALL_REFUSED;
596                                        }
597                                        else if(!strcmp(info, "STATUS UNPLACED"))
598                                        {
599                                                if(sd->call_id)
600                                                        g_free(sd->call_id);
601                                                /* Save the ID for later usage (Cancel/Finish). */
602                                                sd->call_id = g_strdup(id);
603                                                sd->call_out = TRUE;
604                                        }
605                                        else if(!strncmp(info, "DURATION ", 9))
606                                        {
607                                                if(sd->call_duration)
608                                                        g_free(sd->call_duration);
609                                                sd->call_duration = g_strdup(info+9);
610                                        }
611                                        else if(!strncmp(info, "PARTNER_HANDLE ", 15))
612                                        {
613                                                info += 15;
614                                                if(sd->call_status) {
615                                                        switch(sd->call_status)
616                                                        {
617                                                                case SKYPE_CALL_RINGING:
618                                                                        if(sd->call_out)
619                                                                                imcb_log(ic, "You are currently ringing the user %s.", info);
620                                                                        else
621                                                                                imcb_log(ic, "The user %s is currently ringing you.", info);
622                                                                        break;
623                                                                case SKYPE_CALL_MISSED:
624                                                                        imcb_log(ic, "You have missed a call from user %s.", info);
625                                                                        break;
626                                                                case SKYPE_CALL_CANCELLED:
627                                                                        imcb_log(ic, "You cancelled the call to the user %s.", info);
628                                                                        sd->call_status = 0;
629                                                                        sd->call_out = FALSE;
630                                                                        break;
631                                                                case SKYPE_CALL_REFUSED:
632                                                                        if(sd->call_out)
633                                                                                imcb_log(ic, "The user %s refused the call.", info);
634                                                                        else
635                                                                                imcb_log(ic, "You refused the call from user %s.", info);
636                                                                        sd->call_out = FALSE;
637                                                                        break;
638                                                                case SKYPE_CALL_FINISHED:
639                                                                        if(sd->call_duration)
640                                                                                imcb_log(ic, "You finished the call to the user %s (duration: %s seconds).", info, sd->call_duration);
641                                                                        else
642                                                                                imcb_log(ic, "You finished the call to the user %s.", info);
643                                                                        sd->call_out = FALSE;
644                                                                        break;
645                                                                default:
646                                                                        /* Don't be noisy, ignore other statuses for now. */
647                                                                        break;
648                                                        }
649                                                        sd->call_status = 0;
650                                                }
651                                        }
652                                }
653                        }
654                        else if(!strncmp(line, "FILETRANSFER ", 13))
655                        {
656                                char *id = strchr(line, ' ');
657                                if(++id)
658                                {
659                                        char *info = strchr(id, ' ');
660                                        *info = '\0';
661                                        info++;
662                                        if(!strcmp(info, "STATUS NEW"))
663                                        {
664                                                g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id);
665                                                skype_write( ic, buf, strlen( buf ) );
666                                                sd->filetransfer_status = SKYPE_FILETRANSFER_NEW;
667                                        }
668                                        else if(!strcmp(info, "STATUS FAILED"))
669                                        {
670                                                g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id);
671                                                skype_write( ic, buf, strlen( buf ) );
672                                                sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED;
673                                        }
674                                        else if(!strncmp(info, "PARTNER_HANDLE ", 15))
675                                        {
676                                                info += 15;
677                                                if(sd->filetransfer_status) {
678                                                        switch(sd->filetransfer_status)
679                                                        {
680                                                                case SKYPE_FILETRANSFER_NEW:
681                                                                        imcb_log(ic, "The user %s offered a new file for you.", info);
682                                                                        break;
683                                                                case SKYPE_FILETRANSFER_FAILED:
684                                                                        imcb_log(ic, "Failed to transfer file from user %s.", info);
685                                                                        break;
686                                                        }
687                                                        sd->filetransfer_status = 0;
688                                                }
689                                        }
690                                }
691                        }
692                        else if(!strncmp(line, "CHAT ", 5))
693                        {
694                                char *id = strchr(line, ' ');
695                                if(++id)
696                                {
697                                        char *info = strchr(id, ' ');
698                                        if(info)
699                                                *info = '\0';
700                                        info++;
701                                        /* Remove fake chat if we created one in skype_chat_with() */
702                                        struct groupchat *gc = skype_chat_by_name(ic, "");
703                                        if(gc)
704                                                imcb_chat_free(gc);
705                                        if(!strcmp(info, "STATUS MULTI_SUBSCRIBED"))
706                                        {
707                                                imcb_chat_new( ic, id );
708                                                g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id);
709                                                skype_write(ic, buf, strlen(buf));
710                                                g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id);
711                                                skype_write(ic, buf, strlen(buf));
712                                        }
713                                        else if(!strcmp(info, "STATUS DIALOG") && sd->groupchat_with)
714                                        {
715                                                gc = imcb_chat_new( ic, id );
716                                                /* According to the docs this
717                                                 * is necessary. However it
718                                                 * does not seem the situation
719                                                 * and it would open an extra
720                                                 * window on our client, so
721                                                 * just leave it out. */
722                                                /*g_snprintf(buf, 1024, "OPEN CHAT %s\n", id);
723                                                skype_write(ic, buf, strlen(buf));*/
724                                                g_snprintf(buf, 1024, "%s@skype.com", sd->groupchat_with);
725                                                imcb_chat_add_buddy(gc, buf);
726                                                imcb_chat_add_buddy(gc, sd->username);
727                                                g_free(sd->groupchat_with);
728                                                sd->groupchat_with = NULL;
729                                                g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id);
730                                                skype_write(ic, buf, strlen(buf));
731                                                g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id);
732                                                skype_write(ic, buf, strlen(buf));
733                                        }
734                                        else if(!strcmp(info, "STATUS UNSUBSCRIBED"))
735                                        {
736                                                gc = skype_chat_by_name(ic, id);
737                                                if(gc)
738                                                        gc->data = (void*)FALSE;
739                                        }
740                                        else if(!strncmp(info, "ADDER ", 6))
741                                        {
742                                                info += 6;
743                                                g_free(sd->adder);
744                                                sd->adder = g_strdup_printf("%s@skype.com", info);
745                                        }
746                                        else if(!strncmp(info, "TOPIC ", 6))
747                                        {
748                                                info += 6;
749                                                gc = skype_chat_by_name(ic, id);
750                                                if(gc && (sd->adder || sd->topic_wait))
751                                                {
752                                                        if(sd->topic_wait)
753                                                        {
754                                                                sd->adder = g_strdup(sd->username);
755                                                                sd->topic_wait = 0;
756                                                        }
757                                                        imcb_chat_topic(gc, sd->adder, info, 0);
758                                                        g_free(sd->adder);
759                                                        sd->adder = NULL;
760                                                }
761                                        }
762                                        else if(!strncmp(info, "ACTIVEMEMBERS ", 14))
763                                        {
764                                                info += 14;
765                                                gc = skype_chat_by_name(ic, id);
766                                                /* Hack! We set ->data to TRUE
767                                                 * while we're on the channel
768                                                 * so that we won't rejoin
769                                                 * after a /part. */
770                                                if(gc && !gc->data)
771                                                {
772                                                        char **members = g_strsplit(info, " ", 0);
773                                                        int i;
774                                                        for(i=0;members[i];i++)
775                                                        {
776                                                                if(!strcmp(members[i], sd->username))
777                                                                        continue;
778                                                                g_snprintf(buf, 1024, "%s@skype.com", members[i]);
779                                                                if(!g_list_find_custom(gc->in_room, buf, (GCompareFunc)strcmp))
780                                                                        imcb_chat_add_buddy(gc, buf);
781                                                        }
782                                                        imcb_chat_add_buddy(gc, sd->username);
783                                                        g_strfreev(members);
784                                                }
785                                        }
786                                }
787                        }
788                        else if(!strncmp(line, "PASSWORD ", 9))
789                        {
790                                if(!strncmp(line+9, "OK", 2))
791                                        imcb_connected(ic);
792                                else
793                                {
794                                        imcb_error(ic, "Authentication Failed");
795                                        imc_logout( ic, TRUE );
796                                }
797                        }
798                        lineptr++;
799                }
800                g_strfreev(lines);
801        }
802        else if( st == 0 || ( st < 0 && !sockerr_again() ) )
803        {
804                closesocket( sd->fd );
805                sd->fd = -1;
806
807                imcb_error( ic, "Error while reading from server" );
808                imc_logout( ic, TRUE );
809                return FALSE;
810        }
811        return TRUE;
812}
813
814gboolean skype_start_stream( struct im_connection *ic )
815{
816        struct skype_data *sd = ic->proto_data;
817        char *buf;
818        int st;
819
820        if(!sd)
821                return FALSE;
822
823        if( sd->bfd <= 0 )
824                sd->bfd = b_input_add( sd->fd, GAIM_INPUT_READ, skype_read_callback, ic );
825
826        /* Log in */
827        buf = g_strdup_printf("USERNAME %s\n", ic->acc->user);
828        st = skype_write( ic, buf, strlen( buf ) );
829        g_free(buf);
830        buf = g_strdup_printf("PASSWORD %s\n", ic->acc->pass);
831        st = skype_write( ic, buf, strlen( buf ) );
832        g_free(buf);
833
834        /* This will download all buddies. */
835        buf = g_strdup_printf("SEARCH FRIENDS\n");
836        st = skype_write( ic, buf, strlen( buf ) );
837        g_free(buf);
838        buf = g_strdup_printf("SET USERSTATUS ONLINE\n");
839        skype_write( ic, buf, strlen( buf ) );
840        g_free(buf);
841        return st;
842}
843
844gboolean skype_connected( gpointer data, void *source, b_input_condition cond )
845{
846        struct im_connection *ic = data;
847        struct skype_data *sd = ic->proto_data;
848        if(!source)
849        {
850                sd->ssl = NULL;
851                imcb_error( ic, "Could not connect to server" );
852                imc_logout( ic, TRUE );
853                return FALSE;
854        }
855        imcb_log( ic, "Connected to server, logging in" );
856        return skype_start_stream(ic);
857}
858
859static void skype_login( account_t *acc )
860{
861        struct im_connection *ic = imcb_new( acc );
862        struct skype_data *sd = g_new0( struct skype_data, 1 );
863
864        ic->proto_data = sd;
865
866        imcb_log( ic, "Connecting" );
867        sd->ssl = ssl_connect(set_getstr( &acc->set, "server" ), set_getint( &acc->set, "port" ), skype_connected, ic );
868        sd->fd = sd->ssl ? ssl_getfd( sd->ssl ) : -1;
869        sd->username = g_strdup( acc->user );
870
871        sd->ic = ic;
872}
873
874static void skype_logout( struct im_connection *ic )
875{
876        struct skype_data *sd = ic->proto_data;
877        char *buf;
878
879        buf = g_strdup_printf("SET USERSTATUS OFFLINE\n");
880        skype_write( ic, buf, strlen( buf ) );
881        g_free(buf);
882
883        g_free(sd->username);
884        g_free(sd->handle);
885        g_free(sd);
886        ic->proto_data = NULL;
887}
888
889static int skype_buddy_msg( struct im_connection *ic, char *who, char *message, int flags )
890{
891        char *buf, *ptr, *nick;
892        int st;
893
894        nick = g_strdup(who);
895        ptr = strchr(nick, '@');
896        if(ptr)
897                *ptr = '\0';
898
899        buf = g_strdup_printf("MESSAGE %s %s\n", nick, message);
900        g_free(nick);
901        st = skype_write( ic, buf, strlen( buf ) );
902        g_free(buf);
903
904        return st;
905}
906
907const struct skype_away_state *skype_away_state_by_name( char *name )
908{
909        int i;
910
911        for( i = 0; skype_away_state_list[i].full_name; i ++ )
912                if( g_strcasecmp( skype_away_state_list[i].full_name, name ) == 0 )
913                        return( skype_away_state_list + i );
914
915        return NULL;
916}
917
918static void skype_set_away( struct im_connection *ic, char *state_txt, char *message )
919{
920        const struct skype_away_state *state;
921        char *buf;
922
923        if( strcmp( state_txt, GAIM_AWAY_CUSTOM ) == 0 )
924                state = skype_away_state_by_name( "Away" );
925        else
926                state = skype_away_state_by_name( state_txt );
927        buf = g_strdup_printf("SET USERSTATUS %s\n", state->code);
928        skype_write( ic, buf, strlen( buf ) );
929        g_free(buf);
930}
931
932static GList *skype_away_states( struct im_connection *ic )
933{
934        static GList *l = NULL;
935        int i;
936       
937        if( l == NULL )
938                for( i = 0; skype_away_state_list[i].full_name; i ++ )
939                        l = g_list_append( l, (void*) skype_away_state_list[i].full_name );
940       
941        return l;
942}
943
944static char *skype_set_display_name( set_t *set, char *value )
945{
946        account_t *acc = set->data;
947        struct im_connection *ic = acc->ic;
948        char *buf;
949
950        buf = g_strdup_printf("SET PROFILE FULLNAME %s", value);
951        skype_write( ic, buf, strlen( buf ) );
952        g_free(buf);
953        return(value);
954}
955
956static char *skype_set_call( set_t *set, char *value )
957{
958        account_t *acc = set->data;
959        struct im_connection *ic = acc->ic;
960        struct skype_data *sd = ic->proto_data;
961        char *nick, *ptr, *buf;
962
963        if(value)
964        {
965                user_t *u = user_find(acc->irc, value);
966                /* We are starting a call */
967                if(!u)
968                {
969                        imcb_error(ic, "%s - no such nick", value);
970                        return(value);
971                }
972                nick = g_strdup(u->handle);
973                ptr = strchr(nick, '@');
974                if(ptr)
975                        *ptr = '\0';
976
977                buf = g_strdup_printf("CALL %s", nick);
978                skype_write( ic, buf, strlen( buf ) );
979                g_free(buf);
980                g_free(nick);
981        }
982        else
983        {
984                /* We are ending a call */
985                if(sd->call_id)
986                {
987                        buf = g_strdup_printf("SET CALL %s STATUS FINISHED", sd->call_id);
988                        skype_write( ic, buf, strlen( buf ) );
989                        g_free(buf);
990                        g_free(sd->call_id);
991                        sd->call_id = NULL;
992                }
993                else
994                {
995                        imcb_error(ic, "There are no active calls currently.");
996                }
997        }
998        return(value);
999}
1000
1001static void skype_add_buddy( struct im_connection *ic, char *who, char *group )
1002{
1003        char *buf, *nick, *ptr;
1004
1005        nick = g_strdup(who);
1006        ptr = strchr(nick, '@');
1007        if(ptr)
1008                *ptr = '\0';
1009        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 2 Please authorize me\n", nick);
1010        skype_write( ic, buf, strlen( buf ) );
1011        g_free(nick);
1012}
1013
1014static void skype_remove_buddy( struct im_connection *ic, char *who, char *group )
1015{
1016        char *buf, *nick, *ptr;
1017
1018        nick = g_strdup(who);
1019        ptr = strchr(nick, '@');
1020        if(ptr)
1021                *ptr = '\0';
1022        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 1\n", nick);
1023        skype_write( ic, buf, strlen( buf ) );
1024        g_free(nick);
1025}
1026
1027void skype_chat_msg( struct groupchat *gc, char *message, int flags )
1028{
1029        struct im_connection *ic = gc->ic;
1030        char *buf;
1031        buf = g_strdup_printf("CHATMESSAGE %s %s\n", gc->title, message);
1032        skype_write( ic, buf, strlen( buf ) );
1033        g_free(buf);
1034}
1035
1036void skype_chat_leave( struct groupchat *gc )
1037{
1038        struct im_connection *ic = gc->ic;
1039        char *buf;
1040        buf = g_strdup_printf("ALTER CHAT %s LEAVE\n", gc->title);
1041        skype_write( ic, buf, strlen( buf ) );
1042        g_free(buf);
1043        gc->data = (void*)TRUE;
1044}
1045
1046void skype_chat_invite(struct groupchat *gc, char *who, char *message)
1047{
1048        struct im_connection *ic = gc->ic;
1049        char *buf, *ptr, *nick;
1050        nick = g_strdup(message);
1051        ptr = strchr(nick, '@');
1052        if(ptr)
1053                *ptr = '\0';
1054        buf = g_strdup_printf("ALTER CHAT %s ADDMEMBERS %s\n", gc->title, nick);
1055        skype_write( ic, buf, strlen( buf ) );
1056        g_free(buf);
1057        g_free(nick);
1058}
1059
1060void skype_chat_topic(struct groupchat *gc, char *message)
1061{
1062        struct im_connection *ic = gc->ic;
1063        struct skype_data *sd = ic->proto_data;
1064        char *buf;
1065        buf = g_strdup_printf("ALTER CHAT %s SETTOPIC %s\n", gc->title, message);
1066        skype_write( ic, buf, strlen( buf ) );
1067        g_free(buf);
1068        sd->topic_wait = 1;
1069}
1070
1071struct groupchat *skype_chat_with(struct im_connection *ic, char *who)
1072{
1073        struct skype_data *sd = ic->proto_data;
1074        char *ptr, *nick, *buf;
1075        nick = g_strdup(who);
1076        ptr = strchr(nick, '@');
1077        if(ptr)
1078                *ptr = '\0';
1079        buf = g_strdup_printf("CHAT CREATE %s\n", nick);
1080        skype_write(ic, buf, strlen(buf));
1081        g_free(buf);
1082        sd->groupchat_with = g_strdup(nick);
1083        g_free(nick);
1084        /* We create a fake chat for now. We will replace it with a real one in
1085         * the real callback. */
1086        return(imcb_chat_new( ic, "" ));
1087}
1088
1089static void skype_get_info(struct im_connection *ic, char *who)
1090{
1091        char *ptr, *nick, *buf;
1092        nick = g_strdup(who);
1093        ptr = strchr(nick, '@');
1094        if(ptr)
1095                *ptr = '\0';
1096        buf = g_strdup_printf("GET USER %s FULLNAME\n", nick);
1097        skype_write(ic, buf, strlen(buf));
1098        g_free(buf);
1099        buf = g_strdup_printf("GET USER %s PHONE_HOME\n", nick);
1100        skype_write(ic, buf, strlen(buf));
1101        g_free(buf);
1102        buf = g_strdup_printf("GET USER %s PHONE_OFFICE\n", nick);
1103        skype_write(ic, buf, strlen(buf));
1104        g_free(buf);
1105        buf = g_strdup_printf("GET USER %s PHONE_MOBILE\n", nick);
1106        skype_write(ic, buf, strlen(buf));
1107        g_free(buf);
1108        buf = g_strdup_printf("GET USER %s NROF_AUTHED_BUDDIES\n", nick);
1109        skype_write(ic, buf, strlen(buf));
1110        g_free(buf);
1111        buf = g_strdup_printf("GET USER %s TIMEZONE\n", nick);
1112        skype_write(ic, buf, strlen(buf));
1113        g_free(buf);
1114        buf = g_strdup_printf("GET USER %s LASTONLINETIMESTAMP\n", nick);
1115        skype_write(ic, buf, strlen(buf));
1116        g_free(buf);
1117        buf = g_strdup_printf("GET USER %s BIRTHDAY\n", nick);
1118        skype_write(ic, buf, strlen(buf));
1119        g_free(buf);
1120        buf = g_strdup_printf("GET USER %s SEX\n", nick);
1121        skype_write(ic, buf, strlen(buf));
1122        g_free(buf);
1123        buf = g_strdup_printf("GET USER %s LANGUAGE\n", nick);
1124        skype_write(ic, buf, strlen(buf));
1125        g_free(buf);
1126        buf = g_strdup_printf("GET USER %s COUNTRY\n", nick);
1127        skype_write(ic, buf, strlen(buf));
1128        g_free(buf);
1129        buf = g_strdup_printf("GET USER %s PROVINCE\n", nick);
1130        skype_write(ic, buf, strlen(buf));
1131        g_free(buf);
1132        buf = g_strdup_printf("GET USER %s CITY\n", nick);
1133        skype_write(ic, buf, strlen(buf));
1134        g_free(buf);
1135        buf = g_strdup_printf("GET USER %s HOMEPAGE\n", nick);
1136        skype_write(ic, buf, strlen(buf));
1137        g_free(buf);
1138        buf = g_strdup_printf("GET USER %s ABOUT\n", nick);
1139        skype_write(ic, buf, strlen(buf));
1140        g_free(buf);
1141}
1142
1143static void skype_set_my_name( struct im_connection *ic, char *info )
1144{
1145        skype_set_display_name( set_find( &ic->acc->set, "display_name" ), info );
1146}
1147
1148static void skype_init( account_t *acc )
1149{
1150        set_t *s;
1151
1152        s = set_add( &acc->set, "server", SKYPE_DEFAULT_SERVER, set_eval_account, acc );
1153        s->flags |= ACC_SET_OFFLINE_ONLY;
1154
1155        s = set_add( &acc->set, "port", SKYPE_DEFAULT_PORT, set_eval_int, acc );
1156        s->flags |= ACC_SET_OFFLINE_ONLY;
1157
1158        s = set_add( &acc->set, "display_name", NULL, skype_set_display_name, acc );
1159        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
1160
1161        s = set_add( &acc->set, "call", NULL, skype_set_call, acc );
1162        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
1163}
1164
1165void init_plugin(void)
1166{
1167        struct prpl *ret = g_new0( struct prpl, 1 );
1168
1169        ret->name = "skype";
1170        ret->login = skype_login;
1171        ret->init = skype_init;
1172        ret->logout = skype_logout;
1173        ret->buddy_msg = skype_buddy_msg;
1174        ret->get_info = skype_get_info;
1175        ret->set_my_name = skype_set_my_name;
1176        ret->away_states = skype_away_states;
1177        ret->set_away = skype_set_away;
1178        ret->add_buddy = skype_add_buddy;
1179        ret->remove_buddy = skype_remove_buddy;
1180        ret->chat_msg = skype_chat_msg;
1181        ret->chat_leave = skype_chat_leave;
1182        ret->chat_invite = skype_chat_invite;
1183        ret->chat_with = skype_chat_with;
1184        ret->handle_cmp = g_strcasecmp;
1185        ret->chat_topic = skype_chat_topic;
1186        register_protocol( ret );
1187}
1188
1189/* vim: set ts=2 sw=2 noet: */
Note: See TracBrowser for help on using the repository browser.