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