source: skype/skype.c @ c24ae8e

Last change on this file since c24ae8e was 877b6f4, checked in by VMiklos <vmiklos@…>, at 2007-08-21T17:46:40Z

cleanup in struct skype_data

  • Property mode set to 100644
File size: 10.3 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
30#define SKYPE_PORT_DEFAULT "2727"
31
32/*
33 * Structures
34 */
35
36struct skype_data
37{
38        struct im_connection *ic;
39        char *username;
40        int fd;
41        int r_inpa;
42        /* When we receive a new message id, we query the handle, then the
43         * body. Store the handle here so that we imcb_buddy_msg() when we got
44         * the body. */
45        char *handle;
46};
47
48struct skype_away_state
49{
50        char *code;
51        char *full_name;
52};
53
54struct skype_buddy_ask_data
55{
56        struct im_connection *ic;
57        char *handle;
58};
59
60/*
61 * Tables
62 */
63
64const struct skype_away_state skype_away_state_list[] =
65{
66        { "ONLINE",  "Online" },
67        { "SKYPEME",  "Skype Me" },
68        { "AWAY",   "Away" },
69        { "NA",    "Not available" },
70        { "DND",      "Do Not Disturb" },
71        { "INVISIBLE",      "Invisible" },
72        { "OFFLINE",      "Offline" },
73        { NULL, NULL}
74};
75
76/*
77 * Functions
78 */
79
80static void skype_init( account_t *acc )
81{
82        set_t *s;
83
84        s = set_add( &acc->set, "port", SKYPE_PORT_DEFAULT, set_eval_int, acc );
85        s->flags |= ACC_SET_OFFLINE_ONLY;
86
87        s = set_add( &acc->set, "server", NULL, set_eval_account, acc );
88        s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY;
89}
90
91int skype_write( struct im_connection *ic, char *buf, int len )
92{
93        struct skype_data *sd = ic->proto_data;
94        struct pollfd pfd[1];
95
96        pfd[0].fd = sd->fd;
97        pfd[0].events = POLLOUT;
98
99        /* This poll is necessary or we'll get a SIGPIPE when we write() to
100         * sd->fd. */
101        poll(pfd, 1, 1000);
102        if(pfd[0].revents & POLLHUP)
103        {
104                imcb_error( ic, "Could not connect to server" );
105                imc_logout( ic, TRUE );
106                return FALSE;
107        }
108        write( sd->fd, buf, len );
109
110        return TRUE;
111}
112
113static void skype_buddy_ask_yes( gpointer w, struct skype_buddy_ask_data *bla )
114{
115        char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED TRUE", bla->handle);
116        skype_write( bla->ic, buf, strlen( buf ) );
117        g_free(buf);
118        g_free(bla->handle);
119        g_free(bla);
120}
121
122static void skype_buddy_ask_no( gpointer w, struct skype_buddy_ask_data *bla )
123{
124        char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED FALSE", bla->handle);
125        skype_write( bla->ic, buf, strlen( buf ) );
126        g_free(buf);
127        g_free(bla->handle);
128        g_free(bla);
129}
130
131void skype_buddy_ask( struct im_connection *ic, char *handle, char *message)
132{
133        struct skype_buddy_ask_data *bla = g_new0( struct skype_buddy_ask_data, 1 );
134        char *buf;
135
136        bla->ic = ic;
137        bla->handle = g_strdup(handle);
138
139        buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list, saying: '%s'.", handle, message);
140        imcb_ask( ic, buf, bla, skype_buddy_ask_yes, skype_buddy_ask_no );
141        g_free( buf );
142}
143
144static gboolean skype_read_callback( gpointer data, gint fd, b_input_condition cond )
145{
146        struct im_connection *ic = data;
147        struct skype_data *sd = ic->proto_data;
148        char buf[1024];
149        int st;
150        char **lines, **lineptr, *line, *ptr;
151
152        if( !sd || sd->fd == -1 )
153                return FALSE;
154        /* Read the whole data. */
155        st = read( sd->fd, buf, sizeof( buf ) );
156        if( st > 0 )
157        {
158                buf[st] = '\0';
159                /* Then split it up to lines. */
160                lines = g_strsplit(buf, "\n", 0);
161                lineptr = lines;
162                while((line = *lineptr))
163                {
164                        if(!strlen(line))
165                                break;
166                        if(!strncmp(line, "USERS ", 6))
167                        {
168                                char **i;
169                                char **nicks;
170
171                                nicks = g_strsplit(line + 6, ", ", 0);
172                                i = nicks;
173                                while(*i)
174                                {
175                                        g_snprintf(buf, 1024, "GET USER %s ONLINESTATUS\n", *i);
176                                        skype_write( ic, buf, strlen( buf ) );
177                                        i++;
178                                }
179                                g_strfreev(nicks);
180                        }
181                        else if(!strncmp(line, "USER ", 5))
182                        {
183                                int flags = 0;
184                                char *status = strrchr(line, ' ');
185                                char *user = strchr(line, ' ');
186                                status++;
187                                ptr = strchr(++user, ' ');
188                                *ptr = '\0';
189                                ptr++;
190                                if(!strncmp(ptr, "ONLINESTATUS ", 13) &&
191                                                strcmp(user, sd->username) != 0
192                                                && strcmp(user, "echo123") != 0)
193                                {
194                                        ptr = g_strdup_printf("%s@skype.com", user);
195                                        imcb_add_buddy(ic, ptr, NULL);
196                                        if(strcmp(status, "OFFLINE") != 0)
197                                                flags |= OPT_LOGGED_IN;
198                                        if(strcmp(status, "ONLINE") != 0 && strcmp(status, "SKYPEME") != 0)
199                                                flags |= OPT_AWAY;
200                                        imcb_buddy_status(ic, ptr, flags, NULL, NULL);
201                                        g_free(ptr);
202                                }
203                                else if(!strncmp(ptr, "RECEIVEDAUTHREQUEST ", 20))
204                                {
205                                        char *message = ptr + 20;
206                                        if(strlen(message))
207                                                skype_buddy_ask(ic, user, message);
208                                }
209                                else if(!strncmp(ptr, "BUDDYSTATUS ", 12))
210                                {
211                                        char *st = ptr + 12;
212                                        if(!strcmp(st, "3"))
213                                        {
214                                                char *buf = g_strdup_printf("%s@skype.com", user);
215                                                imcb_add_buddy(ic, buf, NULL);
216                                                g_free(buf);
217                                        }
218                                }
219                        }
220                        else if(!strncmp(line, "CHATMESSAGE ", 12))
221                        {
222                                char *id = strchr(line, ' ');
223                                if(++id)
224                                {
225                                        char *info = strchr(id, ' ');
226                                        *info = '\0';
227                                        info++;
228                                        if(!strcmp(info, "STATUS RECEIVED"))
229                                        {
230                                                /* New message ID:
231                                                 * (1) Request its from field
232                                                 * (2) Request its body
233                                                 * (3) Mark it as seen
234                                                 */
235                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s FROM_HANDLE\n", id);
236                                                skype_write( ic, buf, strlen( buf ) );
237                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s BODY\n", id);
238                                                skype_write( ic, buf, strlen( buf ) );
239                                                g_snprintf(buf, 1024, "SET CHATMESSAGE %s SEEN\n", id);
240                                                skype_write( ic, buf, strlen( buf ) );
241                                        }
242                                        else if(!strncmp(info, "FROM_HANDLE ", 12))
243                                        {
244                                                info += 12;
245                                                /* New from field value. Store
246                                                 * it, then we can later use it
247                                                 * when we got the message's
248                                                 * body. */
249                                                sd->handle = g_strdup_printf("%s@skype.com", info);
250                                        }
251                                        else if(!strncmp(info, "BODY ", 5))
252                                        {
253                                                info += 5;
254                                                if(sd->handle)
255                                                {
256                                                        /* New body, we have everything to use imcb_buddy_msg() now! */
257                                                        imcb_buddy_msg(ic, sd->handle, info, 0, 0);
258                                                        g_free(sd->handle);
259                                                        sd->handle = NULL;
260                                                }
261                                        }
262                                }
263                        }
264                        lineptr++;
265                }
266                g_strfreev(lines);
267        }
268        else if( st == 0 || ( st < 0 && !sockerr_again() ) )
269        {
270                closesocket( sd->fd );
271                sd->fd = -1;
272
273                imcb_error( ic, "Error while reading from server" );
274                imc_logout( ic, TRUE );
275                return FALSE;
276        }
277        return TRUE;
278}
279
280gboolean skype_start_stream( struct im_connection *ic )
281{
282        struct skype_data *sd = ic->proto_data;
283        char *buf;
284        int st;
285
286        if(!sd)
287                return FALSE;
288
289        if( sd->r_inpa <= 0 )
290                sd->r_inpa = b_input_add( sd->fd, GAIM_INPUT_READ, skype_read_callback, ic );
291
292        /* This will download all buddies. */
293        buf = g_strdup_printf("SEARCH FRIENDS\n");
294        st = skype_write( ic, buf, strlen( buf ) );
295        g_free(buf);
296        buf = g_strdup_printf("SET USERSTATUS ONLINE\n");
297        skype_write( ic, buf, strlen( buf ) );
298        g_free(buf);
299        return st;
300}
301
302gboolean skype_connected( gpointer data, gint source, b_input_condition cond )
303{
304        struct im_connection *ic = data;
305        imcb_connected(ic);
306        return skype_start_stream(ic);
307}
308
309static void skype_login( account_t *acc )
310{
311        struct im_connection *ic = imcb_new( acc );
312        struct skype_data *sd = g_new0( struct skype_data, 1 );
313
314        ic->proto_data = sd;
315
316        imcb_log( ic, "Connecting" );
317        sd->fd = proxy_connect(acc->server, set_getint( &acc->set, "port" ), skype_connected, ic );
318        sd->username = g_strdup( acc->user );
319
320        sd->ic = ic;
321}
322
323static void skype_logout( struct im_connection *ic )
324{
325        struct skype_data *sd = ic->proto_data;
326        char *buf;
327
328        buf = g_strdup_printf("SET USERSTATUS OFFLINE\n");
329        skype_write( ic, buf, strlen( buf ) );
330        g_free(buf);
331
332        g_free(sd->username);
333        g_free(sd);
334        ic->proto_data = NULL;
335}
336
337static int skype_buddy_msg( struct im_connection *ic, char *who, char *message, int flags )
338{
339        char *buf, *ptr, *nick;
340        int st;
341
342        nick = g_strdup(who);
343        ptr = strchr(nick, '@');
344        if(ptr)
345                *ptr = '\0';
346
347        buf = g_strdup_printf("MESSAGE %s %s\n", nick, message);
348        g_free(nick);
349        st = skype_write( ic, buf, strlen( buf ) );
350        g_free(buf);
351
352        return st;
353}
354
355const struct skype_away_state *skype_away_state_by_name( char *name )
356{
357        int i;
358
359        for( i = 0; skype_away_state_list[i].full_name; i ++ )
360                if( g_strcasecmp( skype_away_state_list[i].full_name, name ) == 0 )
361                        return( skype_away_state_list + i );
362
363        return NULL;
364}
365
366static void skype_set_away( struct im_connection *ic, char *state_txt, char *message )
367{
368        const struct skype_away_state *state;
369        char *buf;
370
371        if( strcmp( state_txt, GAIM_AWAY_CUSTOM ) == 0 )
372                state = skype_away_state_by_name( "Away" );
373        else
374                state = skype_away_state_by_name( state_txt );
375        buf = g_strdup_printf("SET USERSTATUS %s\n", state->code);
376        skype_write( ic, buf, strlen( buf ) );
377        g_free(buf);
378}
379
380static GList *skype_away_states( struct im_connection *ic )
381{
382        GList *l = NULL;
383        int i;
384       
385        for( i = 0; skype_away_state_list[i].full_name; i ++ )
386                l = g_list_append( l, (void*) skype_away_state_list[i].full_name );
387       
388        return l;
389}
390
391static void skype_add_buddy( struct im_connection *ic, char *who, char *group )
392{
393        char *buf, *nick, *ptr;
394
395        nick = g_strdup(who);
396        ptr = strchr(nick, '@');
397        if(ptr)
398                *ptr = '\0';
399        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 2 Please authorize me\n", nick);
400        skype_write( ic, buf, strlen( buf ) );
401        g_free(nick);
402}
403
404static void skype_remove_buddy( struct im_connection *ic, char *who, char *group )
405{
406        char *buf, *nick, *ptr;
407
408        nick = g_strdup(who);
409        ptr = strchr(nick, '@');
410        if(ptr)
411                *ptr = '\0';
412        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 1\n", nick);
413        skype_write( ic, buf, strlen( buf ) );
414        g_free(nick);
415}
416
417void init_plugin(void)
418{
419        struct prpl *ret = g_new0( struct prpl, 1 );
420
421        ret->name = "skype";
422        ret->login = skype_login;
423        ret->init = skype_init;
424        ret->logout = skype_logout;
425        ret->buddy_msg = skype_buddy_msg;
426        ret->away_states = skype_away_states;
427        ret->set_away = skype_set_away;
428        ret->add_buddy = skype_add_buddy;
429        ret->remove_buddy = skype_remove_buddy;
430        ret->handle_cmp = g_strcasecmp;
431        register_protocol( ret );
432}
Note: See TracBrowser for help on using the repository browser.