source: skype/skype.c @ c81d0ef

Last change on this file since c81d0ef was c81d0ef, checked in by VMiklos <vmiklos@…>, at 2007-10-06T20:28:44Z

receiving group chat messages now works
it's a bit agressive. dialogs are not group chats, but i'll fix it later

  • Property mode set to 100644
File size: 15.0 KB
Line 
1/*
2 *  skype.c - Skype plugin for BitlBee
3 *
4 *  Copyright (c) 2007 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#include <stdio.h>
27#include <poll.h>
28#include <bitlbee.h>
29#include <glib.h>
30
31#define SKYPE_PORT_DEFAULT "2727"
32
33/*
34 * Enumerations
35 */
36
37typedef enum
38{
39        SKYPE_CALL_RINGING = 1,
40        SKYPE_CALL_MISSED
41} skype_call_status;
42
43typedef enum
44{
45        SKYPE_FILETRANSFER_NEW = 1,
46        SKYPE_FILETRANSFER_FAILED
47} skype_filetransfer_status;
48
49/*
50 * Structures
51 */
52
53struct skype_data
54{
55        struct im_connection *ic;
56        char *username;
57        /* The effective file descriptor. We store it here so any function can
58         * write() to it. */
59        int fd;
60        /* File descriptor returned by bitlbee. we store it so we know when
61         * we're connected and when we aren't. */
62        int bfd;
63        /* When we receive a new message id, we query the handle, the body and
64         * the chatname. Store the handle and the body here so that we
65         * imcb_buddy_msg() when we got the chatname. */
66        char *handle;
67        char *body;
68        /* This is necessary because we send a notification when we get the
69         * handle. So we store the state here and then we can send a
70         * notification about the handle is in a given status. */
71        skype_call_status call_status;
72        /* Same for file transfers. */
73        skype_filetransfer_status filetransfer_status;
74};
75
76struct skype_away_state
77{
78        char *code;
79        char *full_name;
80};
81
82struct skype_buddy_ask_data
83{
84        struct im_connection *ic;
85        char *handle;
86};
87
88/*
89 * Tables
90 */
91
92const struct skype_away_state skype_away_state_list[] =
93{
94        { "ONLINE",  "Online" },
95        { "SKYPEME",  "Skype Me" },
96        { "AWAY",   "Away" },
97        { "NA",    "Not available" },
98        { "DND",      "Do Not Disturb" },
99        { "INVISIBLE",      "Invisible" },
100        { "OFFLINE",      "Offline" },
101        { NULL, NULL}
102};
103
104/*
105 * Functions
106 */
107
108static void skype_init( account_t *acc )
109{
110        set_t *s;
111
112        s = set_add( &acc->set, "port", SKYPE_PORT_DEFAULT, set_eval_int, acc );
113        s->flags |= ACC_SET_OFFLINE_ONLY;
114
115        s = set_add( &acc->set, "server", NULL, set_eval_account, acc );
116        s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY;
117}
118
119int skype_write( struct im_connection *ic, char *buf, int len )
120{
121        struct skype_data *sd = ic->proto_data;
122        struct pollfd pfd[1];
123
124        pfd[0].fd = sd->fd;
125        pfd[0].events = POLLOUT;
126
127        /* This poll is necessary or we'll get a SIGPIPE when we write() to
128         * sd->fd. */
129        poll(pfd, 1, 1000);
130        if(pfd[0].revents & POLLHUP)
131        {
132                imcb_error( ic, "Could not connect to server" );
133                imc_logout( ic, TRUE );
134                return FALSE;
135        }
136        write( sd->fd, buf, len );
137
138        return TRUE;
139}
140
141static void skype_buddy_ask_yes( gpointer w, struct skype_buddy_ask_data *bla )
142{
143        char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED TRUE", bla->handle);
144        skype_write( bla->ic, buf, strlen( buf ) );
145        g_free(buf);
146        g_free(bla->handle);
147        g_free(bla);
148}
149
150static void skype_buddy_ask_no( gpointer w, struct skype_buddy_ask_data *bla )
151{
152        char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED FALSE", bla->handle);
153        skype_write( bla->ic, buf, strlen( buf ) );
154        g_free(buf);
155        g_free(bla->handle);
156        g_free(bla);
157}
158
159void skype_buddy_ask( struct im_connection *ic, char *handle, char *message)
160{
161        struct skype_buddy_ask_data *bla = g_new0( struct skype_buddy_ask_data, 1 );
162        char *buf;
163
164        bla->ic = ic;
165        bla->handle = g_strdup(handle);
166
167        buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list, saying: '%s'.", handle, message);
168        imcb_ask( ic, buf, bla, skype_buddy_ask_yes, skype_buddy_ask_no );
169        g_free( buf );
170}
171
172struct groupchat *skype_chat_by_name( struct im_connection *ic, char *name )
173{
174        struct groupchat *ret;
175
176        for( ret = ic->conversations; ret; ret = ret->next )
177        {
178                if(strcmp(name, ret->title ) == 0 )
179                        break;
180        }
181
182        return ret;
183}
184
185static gboolean skype_read_callback( gpointer data, gint fd, b_input_condition cond )
186{
187        struct im_connection *ic = data;
188        struct skype_data *sd = ic->proto_data;
189        char buf[1024];
190        int st;
191        char **lines, **lineptr, *line, *ptr;
192
193        if( !sd || sd->fd == -1 )
194                return FALSE;
195        /* Read the whole data. */
196        st = read( sd->fd, buf, sizeof( buf ) );
197        if( st > 0 )
198        {
199                buf[st] = '\0';
200                /* Then split it up to lines. */
201                lines = g_strsplit(buf, "\n", 0);
202                lineptr = lines;
203                while((line = *lineptr))
204                {
205                        if(!strlen(line))
206                                break;
207                        if(!strncmp(line, "USERS ", 6))
208                        {
209                                char **i;
210                                char **nicks;
211
212                                nicks = g_strsplit(line + 6, ", ", 0);
213                                i = nicks;
214                                while(*i)
215                                {
216                                        g_snprintf(buf, 1024, "GET USER %s ONLINESTATUS\n", *i);
217                                        skype_write( ic, buf, strlen( buf ) );
218                                        i++;
219                                }
220                                g_strfreev(nicks);
221                        }
222                        else if(!strncmp(line, "USER ", 5))
223                        {
224                                int flags = 0;
225                                char *status = strrchr(line, ' ');
226                                char *user = strchr(line, ' ');
227                                status++;
228                                ptr = strchr(++user, ' ');
229                                *ptr = '\0';
230                                ptr++;
231                                if(!strncmp(ptr, "ONLINESTATUS ", 13) &&
232                                                strcmp(user, sd->username) != 0
233                                                && strcmp(user, "echo123") != 0)
234                                {
235                                        ptr = g_strdup_printf("%s@skype.com", user);
236                                        imcb_add_buddy(ic, ptr, NULL);
237                                        if(strcmp(status, "OFFLINE") != 0)
238                                                flags |= OPT_LOGGED_IN;
239                                        if(strcmp(status, "ONLINE") != 0 && strcmp(status, "SKYPEME") != 0)
240                                                flags |= OPT_AWAY;
241                                        imcb_buddy_status(ic, ptr, flags, NULL, NULL);
242                                        g_free(ptr);
243                                }
244                                else if(!strncmp(ptr, "RECEIVEDAUTHREQUEST ", 20))
245                                {
246                                        char *message = ptr + 20;
247                                        if(strlen(message))
248                                                skype_buddy_ask(ic, user, message);
249                                }
250                                else if(!strncmp(ptr, "BUDDYSTATUS ", 12))
251                                {
252                                        char *st = ptr + 12;
253                                        if(!strcmp(st, "3"))
254                                        {
255                                                char *buf = g_strdup_printf("%s@skype.com", user);
256                                                imcb_add_buddy(ic, buf, NULL);
257                                                g_free(buf);
258                                        }
259                                }
260                        }
261                        else if(!strncmp(line, "CHATMESSAGE ", 12))
262                        {
263                                char *id = strchr(line, ' ');
264                                if(++id)
265                                {
266                                        char *info = strchr(id, ' ');
267                                        *info = '\0';
268                                        info++;
269                                        if(!strcmp(info, "STATUS RECEIVED"))
270                                        {
271                                                /* New message ID:
272                                                 * (1) Request its from field
273                                                 * (2) Request its body
274                                                 * (3) Query chatname
275                                                 */
276                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s FROM_HANDLE\n", id);
277                                                skype_write( ic, buf, strlen( buf ) );
278                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s BODY\n", id);
279                                                skype_write( ic, buf, strlen( buf ) );
280                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s CHATNAME\n", id);
281                                                skype_write( ic, buf, strlen( buf ) );
282                                        }
283                                        else if(!strncmp(info, "FROM_HANDLE ", 12))
284                                        {
285                                                info += 12;
286                                                /* New from field value. Store
287                                                 * it, then we can later use it
288                                                 * when we got the message's
289                                                 * body. */
290                                                g_free(sd->handle);
291                                                sd->handle = g_strdup_printf("%s@skype.com", info);
292                                        }
293                                        else if(!strncmp(info, "EDITED_BY ", 10))
294                                        {
295                                                info += 10;
296                                                /* This is the same as
297                                                 * FROM_HANDLE, except that we
298                                                 * never request these lines
299                                                 * from Skype, we just get
300                                                 * them. */
301                                                g_free(sd->handle);
302                                                sd->handle = g_strdup_printf("%s@skype.com", info);
303                                        }
304                                        else if(!strncmp(info, "BODY ", 5))
305                                        {
306                                                info += 5;
307                                                g_free(sd->body);
308                                                sd->body = g_strdup(info);
309                                        }
310                                        else if(!strncmp(info, "CHATNAME ", 9))
311                                        {
312                                                info += 9;
313                                                if(sd->handle && sd->body && strlen(info))
314                                                {
315                                                        struct groupchat *gc = skype_chat_by_name(ic, info);
316                                                        if(!gc)
317                                                        {
318                                                                printf("gc is null, id is '%s'\n", info);
319                                                                /* Private message */
320                                                                imcb_buddy_msg(ic, sd->handle, sd->body, 0, 0);
321                                                        }
322                                                        else
323                                                        {
324                                                                printf("gc is not null, id is '%s'\n", info);
325                                                                imcb_chat_msg(gc, sd->handle, sd->body, 0, 0);
326                                                        }
327                                                }
328                                        }
329                                }
330                        }
331                        else if(!strncmp(line, "CALL ", 5))
332                        {
333                                char *id = strchr(line, ' ');
334                                if(++id)
335                                {
336                                        char *info = strchr(id, ' ');
337                                        *info = '\0';
338                                        info++;
339                                        if(!strcmp(info, "STATUS RINGING"))
340                                        {
341                                                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
342                                                skype_write( ic, buf, strlen( buf ) );
343                                                sd->call_status = SKYPE_CALL_RINGING;
344                                        }
345                                        else if(!strcmp(info, "STATUS MISSED"))
346                                        {
347                                                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
348                                                skype_write( ic, buf, strlen( buf ) );
349                                                sd->call_status = SKYPE_CALL_MISSED;
350                                        }
351                                        else if(!strncmp(info, "PARTNER_HANDLE ", 15))
352                                        {
353                                                info += 15;
354                                                if(sd->call_status) {
355                                                        switch(sd->call_status)
356                                                        {
357                                                                case SKYPE_CALL_RINGING:
358                                                                        imcb_log(ic, "The user %s is currently ringing you.", info);
359                                                                        break;
360                                                                case SKYPE_CALL_MISSED:
361                                                                        imcb_log(ic, "You have missed a call from user %s.", info);
362                                                                        break;
363                                                        }
364                                                        sd->call_status = 0;
365                                                }
366                                        }
367                                }
368                        }
369                        else if(!strncmp(line, "FILETRANSFER ", 13))
370                        {
371                                char *id = strchr(line, ' ');
372                                if(++id)
373                                {
374                                        char *info = strchr(id, ' ');
375                                        *info = '\0';
376                                        info++;
377                                        if(!strcmp(info, "STATUS NEW"))
378                                        {
379                                                g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id);
380                                                skype_write( ic, buf, strlen( buf ) );
381                                                sd->filetransfer_status = SKYPE_FILETRANSFER_NEW;
382                                        }
383                                        else if(!strcmp(info, "STATUS FAILED"))
384                                        {
385                                                g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id);
386                                                skype_write( ic, buf, strlen( buf ) );
387                                                sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED;
388                                        }
389                                        else if(!strncmp(info, "PARTNER_HANDLE ", 15))
390                                        {
391                                                info += 15;
392                                                if(sd->filetransfer_status) {
393                                                        switch(sd->filetransfer_status)
394                                                        {
395                                                                case SKYPE_FILETRANSFER_NEW:
396                                                                        imcb_log(ic, "The user %s offered a new file for you.", info);
397                                                                        break;
398                                                                case SKYPE_FILETRANSFER_FAILED:
399                                                                        imcb_log(ic, "Failed to transfer file from user %s.", info);
400                                                                        break;
401                                                        }
402                                                        sd->filetransfer_status = 0;
403                                                }
404                                        }
405                                }
406                        }
407                        else if(!strncmp(line, "CHAT ", 5))
408                        {
409                                char *id = strchr(line, ' ');
410                                if(++id)
411                                {
412                                        char *info = strchr(id, ' ');
413                                        *info = '\0';
414                                        info++;
415                                        if(!strcmp(info, "STATUS MULTI_SUBSCRIBED"))
416                                        {
417                                                struct groupchat *gc;
418                                                gc = imcb_chat_new( ic, id );
419                                        }
420                                        else if(!strncmp(info, "ACTIVEMEMBERS ", 14))
421                                        {
422                                                info += 14;
423                                                struct groupchat *gc = skype_chat_by_name(ic, id);
424                                                if(!gc)
425                                                        gc = imcb_chat_new( ic, id );
426                                                char **members = g_strsplit(info, " ", 0);
427                                                int i;
428                                                for(i=0;members[i];i++)
429                                                {
430                                                        if(!strcmp(members[i], sd->username))
431                                                                continue;
432                                                        g_snprintf(buf, 1024, "%s@skype.com", members[i]);
433                                                        imcb_chat_add_buddy(gc, buf);
434                                                }
435                                                imcb_chat_add_buddy(gc, sd->username);
436                                                g_strfreev(members);
437                                        }
438                                }
439                        }
440                        lineptr++;
441                }
442                g_strfreev(lines);
443        }
444        else if( st == 0 || ( st < 0 && !sockerr_again() ) )
445        {
446                closesocket( sd->fd );
447                sd->fd = -1;
448
449                imcb_error( ic, "Error while reading from server" );
450                imc_logout( ic, TRUE );
451                return FALSE;
452        }
453        return TRUE;
454}
455
456gboolean skype_start_stream( struct im_connection *ic )
457{
458        struct skype_data *sd = ic->proto_data;
459        char *buf;
460        int st;
461
462        if(!sd)
463                return FALSE;
464
465        if( sd->bfd <= 0 )
466                sd->bfd = b_input_add( sd->fd, GAIM_INPUT_READ, skype_read_callback, ic );
467
468        /* This will download all buddies. */
469        buf = g_strdup_printf("SEARCH FRIENDS\n");
470        st = skype_write( ic, buf, strlen( buf ) );
471        g_free(buf);
472        buf = g_strdup_printf("SET USERSTATUS ONLINE\n");
473        skype_write( ic, buf, strlen( buf ) );
474        g_free(buf);
475        return st;
476}
477
478gboolean skype_connected( gpointer data, gint source, b_input_condition cond )
479{
480        struct im_connection *ic = data;
481        imcb_connected(ic);
482        return skype_start_stream(ic);
483}
484
485static void skype_login( account_t *acc )
486{
487        struct im_connection *ic = imcb_new( acc );
488        struct skype_data *sd = g_new0( struct skype_data, 1 );
489
490        ic->proto_data = sd;
491
492        imcb_log( ic, "Connecting" );
493        sd->fd = proxy_connect(acc->server, set_getint( &acc->set, "port" ), skype_connected, ic );
494        sd->username = g_strdup( acc->user );
495
496        sd->ic = ic;
497}
498
499static void skype_logout( struct im_connection *ic )
500{
501        struct skype_data *sd = ic->proto_data;
502        char *buf;
503
504        buf = g_strdup_printf("SET USERSTATUS OFFLINE\n");
505        skype_write( ic, buf, strlen( buf ) );
506        g_free(buf);
507
508        g_free(sd->username);
509        g_free(sd->handle);
510        g_free(sd);
511        ic->proto_data = NULL;
512}
513
514static int skype_buddy_msg( struct im_connection *ic, char *who, char *message, int flags )
515{
516        char *buf, *ptr, *nick;
517        int st;
518
519        nick = g_strdup(who);
520        ptr = strchr(nick, '@');
521        if(ptr)
522                *ptr = '\0';
523
524        buf = g_strdup_printf("MESSAGE %s %s\n", nick, message);
525        g_free(nick);
526        st = skype_write( ic, buf, strlen( buf ) );
527        g_free(buf);
528
529        return st;
530}
531
532const struct skype_away_state *skype_away_state_by_name( char *name )
533{
534        int i;
535
536        for( i = 0; skype_away_state_list[i].full_name; i ++ )
537                if( g_strcasecmp( skype_away_state_list[i].full_name, name ) == 0 )
538                        return( skype_away_state_list + i );
539
540        return NULL;
541}
542
543static void skype_set_away( struct im_connection *ic, char *state_txt, char *message )
544{
545        const struct skype_away_state *state;
546        char *buf;
547
548        if( strcmp( state_txt, GAIM_AWAY_CUSTOM ) == 0 )
549                state = skype_away_state_by_name( "Away" );
550        else
551                state = skype_away_state_by_name( state_txt );
552        buf = g_strdup_printf("SET USERSTATUS %s\n", state->code);
553        skype_write( ic, buf, strlen( buf ) );
554        g_free(buf);
555}
556
557static GList *skype_away_states( struct im_connection *ic )
558{
559        GList *l = NULL;
560        int i;
561       
562        for( i = 0; skype_away_state_list[i].full_name; i ++ )
563                l = g_list_append( l, (void*) skype_away_state_list[i].full_name );
564       
565        return l;
566}
567
568static void skype_add_buddy( struct im_connection *ic, char *who, char *group )
569{
570        char *buf, *nick, *ptr;
571
572        nick = g_strdup(who);
573        ptr = strchr(nick, '@');
574        if(ptr)
575                *ptr = '\0';
576        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 2 Please authorize me\n", nick);
577        skype_write( ic, buf, strlen( buf ) );
578        g_free(nick);
579}
580
581static void skype_remove_buddy( struct im_connection *ic, char *who, char *group )
582{
583        char *buf, *nick, *ptr;
584
585        nick = g_strdup(who);
586        ptr = strchr(nick, '@');
587        if(ptr)
588                *ptr = '\0';
589        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 1\n", nick);
590        skype_write( ic, buf, strlen( buf ) );
591        g_free(nick);
592}
593
594void skype_chat_msg( struct groupchat *c, char *message, int flags )
595{
596        // TODO: this is just here atm to prevent a segfault
597}
598
599void init_plugin(void)
600{
601        struct prpl *ret = g_new0( struct prpl, 1 );
602
603        ret->name = "skype";
604        ret->login = skype_login;
605        ret->init = skype_init;
606        ret->logout = skype_logout;
607        ret->buddy_msg = skype_buddy_msg;
608        ret->away_states = skype_away_states;
609        ret->set_away = skype_set_away;
610        ret->add_buddy = skype_add_buddy;
611        ret->remove_buddy = skype_remove_buddy;
612        ret->chat_msg = skype_chat_msg;
613        ret->handle_cmp = g_strcasecmp;
614        register_protocol( ret );
615}
Note: See TracBrowser for help on using the repository browser.