source: skype/skype.c @ 2eb4b1f

Last change on this file since 2eb4b1f was 2eb4b1f, checked in by Miklos Vajna <vmiklos@…>, at 2008-02-29T00:34:31Z

add support for account set -del skype/call
to finish an outgoing call

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