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