source: skype/skype.c @ 3ef1910

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

don't mark messages as seen

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