1 | /* |
---|
2 | * skype.c - Skype plugin for BitlBee |
---|
3 | * |
---|
4 | * Copyright (c) 2007, 2008 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 | #define _XOPEN_SOURCE |
---|
27 | #include <stdio.h> |
---|
28 | #include <poll.h> |
---|
29 | #include <bitlbee.h> |
---|
30 | #include <bitlbee/ssl_client.h> |
---|
31 | #include <glib.h> |
---|
32 | |
---|
33 | #define SKYPE_DEFAULT_SERVER "localhost" |
---|
34 | #define SKYPE_DEFAULT_PORT "2727" |
---|
35 | |
---|
36 | /* |
---|
37 | * Enumerations |
---|
38 | */ |
---|
39 | |
---|
40 | typedef enum |
---|
41 | { |
---|
42 | SKYPE_CALL_RINGING = 1, |
---|
43 | SKYPE_CALL_MISSED, |
---|
44 | SKYPE_CALL_CANCELLED, |
---|
45 | SKYPE_CALL_FINISHED, |
---|
46 | SKYPE_CALL_REFUSED |
---|
47 | } skype_call_status; |
---|
48 | |
---|
49 | typedef enum |
---|
50 | { |
---|
51 | SKYPE_FILETRANSFER_NEW = 1, |
---|
52 | SKYPE_FILETRANSFER_FAILED |
---|
53 | } skype_filetransfer_status; |
---|
54 | |
---|
55 | /* |
---|
56 | * Structures |
---|
57 | */ |
---|
58 | |
---|
59 | struct skype_data |
---|
60 | { |
---|
61 | struct im_connection *ic; |
---|
62 | char *username; |
---|
63 | /* The effective file descriptor. We store it here so any function can |
---|
64 | * write() to it. */ |
---|
65 | int fd; |
---|
66 | /* File descriptor returned by bitlbee. we store it so we know when |
---|
67 | * we're connected and when we aren't. */ |
---|
68 | int bfd; |
---|
69 | /* ssl_getfd() uses this to get the file desciptor. */ |
---|
70 | void *ssl; |
---|
71 | /* When we receive a new message id, we query the properties, finally |
---|
72 | * the chatname. Store the properties here so that we can use |
---|
73 | * imcb_buddy_msg() when we got the chatname. */ |
---|
74 | char *handle; |
---|
75 | /* List, because of multiline messages. */ |
---|
76 | GList *body; |
---|
77 | char *type; |
---|
78 | /* This is necessary because we send a notification when we get the |
---|
79 | * handle. So we store the state here and then we can send a |
---|
80 | * notification about the handle is in a given status. */ |
---|
81 | skype_call_status call_status; |
---|
82 | char *call_id; |
---|
83 | char *call_duration; |
---|
84 | /* If the call is outgoing or not */ |
---|
85 | int call_out; |
---|
86 | /* Same for file transfers. */ |
---|
87 | skype_filetransfer_status filetransfer_status; |
---|
88 | /* Using /j #nick we want to have a groupchat with two people. Usually |
---|
89 | * not (default). */ |
---|
90 | char* groupchat_with; |
---|
91 | /* The user who invited us to the chat. */ |
---|
92 | char* adder; |
---|
93 | /* If we are waiting for a confirmation about we changed the topic. */ |
---|
94 | int topic_wait; |
---|
95 | /* These are used by the info command. */ |
---|
96 | char *info_fullname; |
---|
97 | char *info_phonehome; |
---|
98 | char *info_phoneoffice; |
---|
99 | char *info_phonemobile; |
---|
100 | char *info_nrbuddies; |
---|
101 | char *info_tz; |
---|
102 | char *info_seen; |
---|
103 | char *info_birthday; |
---|
104 | char *info_sex; |
---|
105 | char *info_language; |
---|
106 | char *info_country; |
---|
107 | char *info_province; |
---|
108 | char *info_city; |
---|
109 | char *info_homepage; |
---|
110 | char *info_about; |
---|
111 | /* When a call fails, we get the reason and later we get the failure |
---|
112 | * event, so store the failure code here till then */ |
---|
113 | int failurereason; |
---|
114 | }; |
---|
115 | |
---|
116 | struct skype_away_state |
---|
117 | { |
---|
118 | char *code; |
---|
119 | char *full_name; |
---|
120 | }; |
---|
121 | |
---|
122 | struct skype_buddy_ask_data |
---|
123 | { |
---|
124 | struct im_connection *ic; |
---|
125 | /* This is also used for call IDs for simplicity */ |
---|
126 | char *handle; |
---|
127 | }; |
---|
128 | |
---|
129 | /* |
---|
130 | * Tables |
---|
131 | */ |
---|
132 | |
---|
133 | const struct skype_away_state skype_away_state_list[] = |
---|
134 | { |
---|
135 | { "ONLINE", "Online" }, |
---|
136 | { "SKYPEME", "Skype Me" }, |
---|
137 | { "AWAY", "Away" }, |
---|
138 | { "NA", "Not available" }, |
---|
139 | { "DND", "Do Not Disturb" }, |
---|
140 | { "INVISIBLE", "Invisible" }, |
---|
141 | { "OFFLINE", "Offline" }, |
---|
142 | { NULL, NULL} |
---|
143 | }; |
---|
144 | |
---|
145 | /* |
---|
146 | * Functions |
---|
147 | */ |
---|
148 | |
---|
149 | int skype_write( struct im_connection *ic, char *buf, int len ) |
---|
150 | { |
---|
151 | struct skype_data *sd = ic->proto_data; |
---|
152 | struct pollfd pfd[1]; |
---|
153 | |
---|
154 | pfd[0].fd = sd->fd; |
---|
155 | pfd[0].events = POLLOUT; |
---|
156 | |
---|
157 | /* This poll is necessary or we'll get a SIGPIPE when we write() to |
---|
158 | * sd->fd. */ |
---|
159 | poll(pfd, 1, 1000); |
---|
160 | if(pfd[0].revents & POLLHUP) |
---|
161 | { |
---|
162 | imc_logout( ic, TRUE ); |
---|
163 | return FALSE; |
---|
164 | } |
---|
165 | ssl_write( sd->ssl, buf, len ); |
---|
166 | |
---|
167 | return TRUE; |
---|
168 | } |
---|
169 | |
---|
170 | static void skype_buddy_ask_yes( void *data ) |
---|
171 | { |
---|
172 | struct skype_buddy_ask_data *bla = data; |
---|
173 | char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED TRUE", bla->handle); |
---|
174 | skype_write( bla->ic, buf, strlen( buf ) ); |
---|
175 | g_free(buf); |
---|
176 | g_free(bla->handle); |
---|
177 | g_free(bla); |
---|
178 | } |
---|
179 | |
---|
180 | static void skype_buddy_ask_no( void *data ) |
---|
181 | { |
---|
182 | struct skype_buddy_ask_data *bla = data; |
---|
183 | char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED FALSE", bla->handle); |
---|
184 | skype_write( bla->ic, buf, strlen( buf ) ); |
---|
185 | g_free(buf); |
---|
186 | g_free(bla->handle); |
---|
187 | g_free(bla); |
---|
188 | } |
---|
189 | |
---|
190 | void skype_buddy_ask( struct im_connection *ic, char *handle, char *message) |
---|
191 | { |
---|
192 | struct skype_buddy_ask_data *bla = g_new0( struct skype_buddy_ask_data, 1 ); |
---|
193 | char *buf; |
---|
194 | |
---|
195 | bla->ic = ic; |
---|
196 | bla->handle = g_strdup(handle); |
---|
197 | |
---|
198 | buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list, saying: '%s'.", handle, message); |
---|
199 | imcb_ask( ic, buf, bla, skype_buddy_ask_yes, skype_buddy_ask_no ); |
---|
200 | g_free( buf ); |
---|
201 | } |
---|
202 | |
---|
203 | static void skype_call_ask_yes( void *data ) |
---|
204 | { |
---|
205 | struct skype_buddy_ask_data *bla = data; |
---|
206 | char *buf = g_strdup_printf("SET CALL %s STATUS INPROGRESS", bla->handle); |
---|
207 | skype_write( bla->ic, buf, strlen( buf ) ); |
---|
208 | g_free(buf); |
---|
209 | g_free(bla->handle); |
---|
210 | g_free(bla); |
---|
211 | } |
---|
212 | |
---|
213 | static void skype_call_ask_no( void *data ) |
---|
214 | { |
---|
215 | struct skype_buddy_ask_data *bla = data; |
---|
216 | char *buf = g_strdup_printf("SET CALL %s STATUS FINISHED", bla->handle); |
---|
217 | skype_write( bla->ic, buf, strlen( buf ) ); |
---|
218 | g_free(buf); |
---|
219 | g_free(bla->handle); |
---|
220 | g_free(bla); |
---|
221 | } |
---|
222 | |
---|
223 | void skype_call_ask( struct im_connection *ic, char *call_id, char *message) |
---|
224 | { |
---|
225 | struct skype_buddy_ask_data *bla = g_new0( struct skype_buddy_ask_data, 1 ); |
---|
226 | |
---|
227 | bla->ic = ic; |
---|
228 | bla->handle = g_strdup(call_id); |
---|
229 | |
---|
230 | imcb_ask( ic, message, bla, skype_call_ask_yes, skype_call_ask_no ); |
---|
231 | } |
---|
232 | struct groupchat *skype_chat_by_name( struct im_connection *ic, char *name ) |
---|
233 | { |
---|
234 | struct groupchat *ret; |
---|
235 | |
---|
236 | for( ret = ic->groupchats; ret; ret = ret->next ) |
---|
237 | { |
---|
238 | if(strcmp(name, ret->title ) == 0 ) |
---|
239 | break; |
---|
240 | } |
---|
241 | |
---|
242 | return ret; |
---|
243 | } |
---|
244 | |
---|
245 | static char *skype_call_strerror(int err) |
---|
246 | { |
---|
247 | switch(err) { |
---|
248 | case 1: |
---|
249 | return "Miscellaneous error"; |
---|
250 | case 2: |
---|
251 | return "User or phone number does not exist."; |
---|
252 | case 3: |
---|
253 | return "User is offline"; |
---|
254 | case 4: |
---|
255 | return "No proxy found"; |
---|
256 | case 5: |
---|
257 | return "Session terminated."; |
---|
258 | case 6: |
---|
259 | return "No common codec found."; |
---|
260 | case 7: |
---|
261 | return "Sound I/O error."; |
---|
262 | case 8: |
---|
263 | return "Problem with remote sound device."; |
---|
264 | case 9: |
---|
265 | return "Call blocked by recipient."; |
---|
266 | case 10: |
---|
267 | return "Recipient not a friend."; |
---|
268 | case 11: |
---|
269 | return "Current user not authorized by recipient."; |
---|
270 | case 12: |
---|
271 | return "Sound recording error."; |
---|
272 | default: |
---|
273 | return "Unknown error"; |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | static gboolean skype_read_callback( gpointer data, gint fd, b_input_condition cond ) |
---|
278 | { |
---|
279 | struct im_connection *ic = data; |
---|
280 | struct skype_data *sd = ic->proto_data; |
---|
281 | char buf[1024]; |
---|
282 | int st; |
---|
283 | char **lines, **lineptr, *line, *ptr; |
---|
284 | |
---|
285 | if( !sd || sd->fd == -1 ) |
---|
286 | return FALSE; |
---|
287 | /* Read the whole data. */ |
---|
288 | st = ssl_read( sd->ssl, buf, sizeof( buf ) ); |
---|
289 | if( st > 0 ) |
---|
290 | { |
---|
291 | buf[st] = '\0'; |
---|
292 | /* Then split it up to lines. */ |
---|
293 | lines = g_strsplit(buf, "\n", 0); |
---|
294 | lineptr = lines; |
---|
295 | while((line = *lineptr)) |
---|
296 | { |
---|
297 | if(!strlen(line)) |
---|
298 | break; |
---|
299 | if(!strncmp(line, "USERS ", 6)) |
---|
300 | { |
---|
301 | char **i; |
---|
302 | char **nicks; |
---|
303 | |
---|
304 | nicks = g_strsplit(line + 6, ", ", 0); |
---|
305 | i = nicks; |
---|
306 | while(*i) |
---|
307 | { |
---|
308 | g_snprintf(buf, 1024, "GET USER %s ONLINESTATUS\n", *i); |
---|
309 | skype_write( ic, buf, strlen( buf ) ); |
---|
310 | i++; |
---|
311 | } |
---|
312 | g_strfreev(nicks); |
---|
313 | } |
---|
314 | else if(!strncmp(line, "USER ", 5)) |
---|
315 | { |
---|
316 | int flags = 0; |
---|
317 | char *status = strrchr(line, ' '); |
---|
318 | char *user = strchr(line, ' '); |
---|
319 | status++; |
---|
320 | ptr = strchr(++user, ' '); |
---|
321 | *ptr = '\0'; |
---|
322 | ptr++; |
---|
323 | if(!strncmp(ptr, "ONLINESTATUS ", 13) && |
---|
324 | strcmp(user, sd->username) != 0 |
---|
325 | && strcmp(user, "echo123") != 0) |
---|
326 | { |
---|
327 | ptr = g_strdup_printf("%s@skype.com", user); |
---|
328 | imcb_add_buddy(ic, ptr, NULL); |
---|
329 | if(strcmp(status, "OFFLINE") && (strcmp(status, "SKYPEOUT") || !set_getbool(&ic->acc->set, "skypeout_offline"))) |
---|
330 | flags |= OPT_LOGGED_IN; |
---|
331 | if(strcmp(status, "ONLINE") != 0 && strcmp(status, "SKYPEME") != 0) |
---|
332 | flags |= OPT_AWAY; |
---|
333 | imcb_buddy_status(ic, ptr, flags, NULL, NULL); |
---|
334 | g_free(ptr); |
---|
335 | } |
---|
336 | else if(!strncmp(ptr, "RECEIVEDAUTHREQUEST ", 20)) |
---|
337 | { |
---|
338 | char *message = ptr + 20; |
---|
339 | if(strlen(message)) |
---|
340 | skype_buddy_ask(ic, user, message); |
---|
341 | } |
---|
342 | else if(!strncmp(ptr, "BUDDYSTATUS ", 12)) |
---|
343 | { |
---|
344 | char *st = ptr + 12; |
---|
345 | if(!strcmp(st, "3")) |
---|
346 | { |
---|
347 | char *buf = g_strdup_printf("%s@skype.com", user); |
---|
348 | imcb_add_buddy(ic, buf, NULL); |
---|
349 | g_free(buf); |
---|
350 | } |
---|
351 | } |
---|
352 | else if(!strncmp(ptr, "FULLNAME ", 9)) |
---|
353 | sd->info_fullname = g_strdup_printf("%s", ptr + 9); |
---|
354 | else if(!strncmp(ptr, "PHONE_HOME ", 11)) |
---|
355 | sd->info_phonehome = g_strdup_printf("%s", ptr + 11); |
---|
356 | else if(!strncmp(ptr, "PHONE_OFFICE ", 13)) |
---|
357 | sd->info_phoneoffice = g_strdup_printf("%s", ptr + 13); |
---|
358 | else if(!strncmp(ptr, "PHONE_MOBILE ", 13)) |
---|
359 | sd->info_phonemobile = g_strdup_printf("%s", ptr + 13); |
---|
360 | else if(!strncmp(ptr, "NROF_AUTHED_BUDDIES ", 20)) |
---|
361 | sd->info_nrbuddies = g_strdup_printf("%s", ptr + 20); |
---|
362 | else if(!strncmp(ptr, "TIMEZONE ", 9)) |
---|
363 | sd->info_tz = g_strdup_printf("%s", ptr + 9); |
---|
364 | else if(!strncmp(ptr, "LASTONLINETIMESTAMP ", 20)) |
---|
365 | sd->info_seen = g_strdup_printf("%s", ptr + 20); |
---|
366 | else if(!strncmp(ptr, "BIRTHDAY ", 9)) |
---|
367 | sd->info_birthday = g_strdup_printf("%s", ptr + 9); |
---|
368 | else if(!strncmp(ptr, "SEX ", 4)) |
---|
369 | sd->info_sex = g_strdup_printf("%s", ptr + 4); |
---|
370 | else if(!strncmp(ptr, "LANGUAGE ", 9)) |
---|
371 | sd->info_language = g_strdup_printf("%s", ptr + 9); |
---|
372 | else if(!strncmp(ptr, "COUNTRY ", 8)) |
---|
373 | sd->info_country = g_strdup_printf("%s", ptr + 8); |
---|
374 | else if(!strncmp(ptr, "PROVINCE ", 9)) |
---|
375 | sd->info_province = g_strdup_printf("%s", ptr + 9); |
---|
376 | else if(!strncmp(ptr, "CITY ", 5)) |
---|
377 | sd->info_city = g_strdup_printf("%s", ptr + 5); |
---|
378 | else if(!strncmp(ptr, "HOMEPAGE ", 9)) |
---|
379 | sd->info_homepage = g_strdup_printf("%s", ptr + 9); |
---|
380 | else if(!strncmp(ptr, "ABOUT ", 6)) |
---|
381 | { |
---|
382 | sd->info_about = g_strdup_printf("%s", ptr + 6); |
---|
383 | |
---|
384 | GString *st = g_string_new("Contact Information\n"); |
---|
385 | g_string_append_printf(st, "Skype Name: %s\n", user); |
---|
386 | if(sd->info_fullname) |
---|
387 | { |
---|
388 | if(strlen(sd->info_fullname)) |
---|
389 | g_string_append_printf(st, "Full Name: %s\n", sd->info_fullname); |
---|
390 | g_free(sd->info_fullname); |
---|
391 | } |
---|
392 | if(sd->info_phonehome) |
---|
393 | { |
---|
394 | if(strlen(sd->info_phonehome)) |
---|
395 | g_string_append_printf(st, "Home Phone: %s\n", sd->info_phonehome); |
---|
396 | g_free(sd->info_phonehome); |
---|
397 | } |
---|
398 | if(sd->info_phoneoffice) |
---|
399 | { |
---|
400 | if(strlen(sd->info_phoneoffice)) |
---|
401 | g_string_append_printf(st, "Office Phone: %s\n", sd->info_phoneoffice); |
---|
402 | g_free(sd->info_phoneoffice); |
---|
403 | } |
---|
404 | if(sd->info_phonemobile) |
---|
405 | { |
---|
406 | if(strlen(sd->info_phonemobile)) |
---|
407 | g_string_append_printf(st, "Mobile Phone: %s\n", sd->info_phonemobile); |
---|
408 | g_free(sd->info_phonemobile); |
---|
409 | } |
---|
410 | g_string_append_printf(st, "Personal Information\n"); |
---|
411 | if(sd->info_nrbuddies) |
---|
412 | { |
---|
413 | if(strlen(sd->info_nrbuddies)) |
---|
414 | g_string_append_printf(st, "Contacts: %s\n", sd->info_nrbuddies); |
---|
415 | g_free(sd->info_nrbuddies); |
---|
416 | } |
---|
417 | if(sd->info_tz) |
---|
418 | { |
---|
419 | if(strlen(sd->info_tz)) |
---|
420 | { |
---|
421 | char ib[256]; |
---|
422 | time_t t = time(NULL); |
---|
423 | t += atoi(sd->info_tz)-(60*60*24); |
---|
424 | struct tm *gt = gmtime(&t); |
---|
425 | strftime(ib, 256, "%H:%M:%S", gt); |
---|
426 | g_string_append_printf(st, "Local Time: %s\n", ib); |
---|
427 | } |
---|
428 | g_free(sd->info_tz); |
---|
429 | } |
---|
430 | if(sd->info_seen) |
---|
431 | { |
---|
432 | if(strlen(sd->info_seen)) |
---|
433 | { |
---|
434 | char ib[256]; |
---|
435 | time_t it = atoi(sd->info_seen); |
---|
436 | struct tm *tm = localtime(&it); |
---|
437 | strftime(ib, 256, ("%Y. %m. %d. %H:%M"), tm); |
---|
438 | g_string_append_printf(st, "Last Seen: %s\n", ib); |
---|
439 | } |
---|
440 | g_free(sd->info_seen); |
---|
441 | } |
---|
442 | if(sd->info_birthday) |
---|
443 | { |
---|
444 | if(strlen(sd->info_birthday) && strcmp(sd->info_birthday, "0")) |
---|
445 | { |
---|
446 | char ib[256]; |
---|
447 | struct tm tm; |
---|
448 | strptime(sd->info_birthday, "%Y%m%d", &tm); |
---|
449 | strftime(ib, 256, "%B %d, %Y", &tm); |
---|
450 | g_string_append_printf(st, "Birthday: %s\n", ib); |
---|
451 | |
---|
452 | strftime(ib, 256, "%Y", &tm); |
---|
453 | int year = atoi(ib); |
---|
454 | time_t t = time(NULL); |
---|
455 | struct tm *lt = localtime(&t); |
---|
456 | g_string_append_printf(st, "Age: %d\n", lt->tm_year+1900-year); |
---|
457 | } |
---|
458 | g_free(sd->info_birthday); |
---|
459 | } |
---|
460 | if(sd->info_sex) |
---|
461 | { |
---|
462 | if(strlen(sd->info_sex)) |
---|
463 | { |
---|
464 | char *iptr = sd->info_sex; |
---|
465 | while(*iptr++) |
---|
466 | *iptr = tolower(*iptr); |
---|
467 | g_string_append_printf(st, "Gender: %s\n", sd->info_sex); |
---|
468 | } |
---|
469 | g_free(sd->info_sex); |
---|
470 | } |
---|
471 | if(sd->info_language) |
---|
472 | { |
---|
473 | if(strlen(sd->info_language)) |
---|
474 | { |
---|
475 | char *iptr = strchr(sd->info_language, ' '); |
---|
476 | if(iptr) |
---|
477 | iptr++; |
---|
478 | else |
---|
479 | iptr = sd->info_language; |
---|
480 | g_string_append_printf(st, "Language: %s\n", iptr); |
---|
481 | } |
---|
482 | g_free(sd->info_language); |
---|
483 | } |
---|
484 | if(sd->info_country) |
---|
485 | { |
---|
486 | if(strlen(sd->info_country)) |
---|
487 | { |
---|
488 | char *iptr = strchr(sd->info_country, ' '); |
---|
489 | if(iptr) |
---|
490 | iptr++; |
---|
491 | else |
---|
492 | iptr = sd->info_country; |
---|
493 | g_string_append_printf(st, "Country: %s\n", iptr); |
---|
494 | } |
---|
495 | g_free(sd->info_country); |
---|
496 | } |
---|
497 | if(sd->info_province) |
---|
498 | { |
---|
499 | if(strlen(sd->info_province)) |
---|
500 | g_string_append_printf(st, "Region: %s\n", sd->info_province); |
---|
501 | g_free(sd->info_province); |
---|
502 | } |
---|
503 | if(sd->info_city) |
---|
504 | { |
---|
505 | if(strlen(sd->info_city)) |
---|
506 | g_string_append_printf(st, "City: %s\n", sd->info_city); |
---|
507 | g_free(sd->info_city); |
---|
508 | } |
---|
509 | if(sd->info_homepage) |
---|
510 | { |
---|
511 | if(strlen(sd->info_homepage)) |
---|
512 | g_string_append_printf(st, "Homepage: %s\n", sd->info_homepage); |
---|
513 | g_free(sd->info_homepage); |
---|
514 | } |
---|
515 | if(sd->info_about) |
---|
516 | { |
---|
517 | if(strlen(sd->info_about)) |
---|
518 | g_string_append_printf(st, "%s\n", sd->info_about); |
---|
519 | g_free(sd->info_about); |
---|
520 | } |
---|
521 | imcb_log(ic, "%s", st->str); |
---|
522 | g_string_free(st, TRUE); |
---|
523 | } |
---|
524 | } |
---|
525 | else if(!strncmp(line, "CHATMESSAGE ", 12)) |
---|
526 | { |
---|
527 | char *id = strchr(line, ' '); |
---|
528 | if(++id) |
---|
529 | { |
---|
530 | char *info = strchr(id, ' '); |
---|
531 | *info = '\0'; |
---|
532 | info++; |
---|
533 | if(!strcmp(info, "STATUS RECEIVED")) |
---|
534 | { |
---|
535 | /* New message ID: |
---|
536 | * (1) Request its from field |
---|
537 | * (2) Request its body |
---|
538 | * (3) Request its type |
---|
539 | * (4) Query chatname |
---|
540 | */ |
---|
541 | g_snprintf(buf, 1024, "GET CHATMESSAGE %s FROM_HANDLE\n", id); |
---|
542 | skype_write( ic, buf, strlen( buf ) ); |
---|
543 | g_snprintf(buf, 1024, "GET CHATMESSAGE %s BODY\n", id); |
---|
544 | skype_write( ic, buf, strlen( buf ) ); |
---|
545 | g_snprintf(buf, 1024, "GET CHATMESSAGE %s TYPE\n", id); |
---|
546 | skype_write( ic, buf, strlen( buf ) ); |
---|
547 | g_snprintf(buf, 1024, "GET CHATMESSAGE %s CHATNAME\n", id); |
---|
548 | skype_write( ic, buf, strlen( buf ) ); |
---|
549 | } |
---|
550 | else if(!strncmp(info, "FROM_HANDLE ", 12)) |
---|
551 | { |
---|
552 | info += 12; |
---|
553 | /* New from field value. Store |
---|
554 | * it, then we can later use it |
---|
555 | * when we got the message's |
---|
556 | * body. */ |
---|
557 | g_free(sd->handle); |
---|
558 | sd->handle = g_strdup_printf("%s@skype.com", info); |
---|
559 | } |
---|
560 | else if(!strncmp(info, "EDITED_BY ", 10)) |
---|
561 | { |
---|
562 | info += 10; |
---|
563 | /* This is the same as |
---|
564 | * FROM_HANDLE, except that we |
---|
565 | * never request these lines |
---|
566 | * from Skype, we just get |
---|
567 | * them. */ |
---|
568 | g_free(sd->handle); |
---|
569 | sd->handle = g_strdup_printf("%s@skype.com", info); |
---|
570 | } |
---|
571 | else if(!strncmp(info, "BODY ", 5)) |
---|
572 | { |
---|
573 | info += 5; |
---|
574 | sd->body = g_list_append(sd->body, g_strdup(info)); |
---|
575 | } |
---|
576 | else if(!strncmp(info, "TYPE ", 5)) |
---|
577 | { |
---|
578 | info += 5; |
---|
579 | g_free(sd->type); |
---|
580 | sd->type = g_strdup(info); |
---|
581 | } |
---|
582 | else if(!strncmp(info, "CHATNAME ", 9)) |
---|
583 | { |
---|
584 | info += 9; |
---|
585 | if(sd->handle && sd->body && sd->type) |
---|
586 | { |
---|
587 | struct groupchat *gc = skype_chat_by_name(ic, info); |
---|
588 | int i; |
---|
589 | for(i=0;i<g_list_length(sd->body);i++) |
---|
590 | { |
---|
591 | char *body = g_list_nth_data(sd->body, i); |
---|
592 | if(!strcmp(sd->type, "SAID") || !strcmp(sd->type, "EMOTED")) |
---|
593 | { |
---|
594 | char *st; |
---|
595 | if(!strcmp(sd->type, "SAID")) |
---|
596 | st = g_strdup(body); |
---|
597 | else |
---|
598 | { |
---|
599 | st = g_strdup_printf("/me %s", body); |
---|
600 | } |
---|
601 | if(!gc) |
---|
602 | /* Private message */ |
---|
603 | imcb_buddy_msg(ic, sd->handle, st, 0, 0); |
---|
604 | else |
---|
605 | /* Groupchat message */ |
---|
606 | imcb_chat_msg(gc, sd->handle, st, 0, 0); |
---|
607 | g_free(st); |
---|
608 | } |
---|
609 | else if(!strcmp(sd->type, "SETTOPIC")) |
---|
610 | { |
---|
611 | if(gc) |
---|
612 | imcb_chat_topic(gc, sd->handle, body, 0); |
---|
613 | } |
---|
614 | else if(!strcmp(sd->type, "LEFT")) |
---|
615 | { |
---|
616 | if(gc) |
---|
617 | imcb_chat_remove_buddy(gc, sd->handle, NULL); |
---|
618 | } |
---|
619 | } |
---|
620 | g_list_free(sd->body); |
---|
621 | sd->body = NULL; |
---|
622 | } |
---|
623 | } |
---|
624 | } |
---|
625 | } |
---|
626 | else if(!strncmp(line, "CALL ", 5)) |
---|
627 | { |
---|
628 | char *id = strchr(line, ' '); |
---|
629 | if(++id) |
---|
630 | { |
---|
631 | char *info = strchr(id, ' '); |
---|
632 | *info = '\0'; |
---|
633 | info++; |
---|
634 | if(!strncmp(info, "FAILUREREASON ", 14)) |
---|
635 | sd->failurereason = atoi(strchr(info, ' ')); |
---|
636 | else if(!strcmp(info, "STATUS RINGING")) |
---|
637 | { |
---|
638 | if(sd->call_id) |
---|
639 | g_free(sd->call_id); |
---|
640 | sd->call_id = g_strdup(id); |
---|
641 | g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id); |
---|
642 | skype_write( ic, buf, strlen( buf ) ); |
---|
643 | sd->call_status = SKYPE_CALL_RINGING; |
---|
644 | } |
---|
645 | else if(!strcmp(info, "STATUS MISSED")) |
---|
646 | { |
---|
647 | g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id); |
---|
648 | skype_write( ic, buf, strlen( buf ) ); |
---|
649 | sd->call_status = SKYPE_CALL_MISSED; |
---|
650 | } |
---|
651 | else if(!strcmp(info, "STATUS CANCELLED")) |
---|
652 | { |
---|
653 | g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id); |
---|
654 | skype_write( ic, buf, strlen( buf ) ); |
---|
655 | sd->call_status = SKYPE_CALL_CANCELLED; |
---|
656 | } |
---|
657 | else if(!strcmp(info, "STATUS FINISHED")) |
---|
658 | { |
---|
659 | g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id); |
---|
660 | skype_write( ic, buf, strlen( buf ) ); |
---|
661 | sd->call_status = SKYPE_CALL_FINISHED; |
---|
662 | } |
---|
663 | else if(!strcmp(info, "STATUS REFUSED")) |
---|
664 | { |
---|
665 | g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id); |
---|
666 | skype_write( ic, buf, strlen( buf ) ); |
---|
667 | sd->call_status = SKYPE_CALL_REFUSED; |
---|
668 | } |
---|
669 | else if(!strcmp(info, "STATUS UNPLACED")) |
---|
670 | { |
---|
671 | if(sd->call_id) |
---|
672 | g_free(sd->call_id); |
---|
673 | /* Save the ID for later usage (Cancel/Finish). */ |
---|
674 | sd->call_id = g_strdup(id); |
---|
675 | sd->call_out = TRUE; |
---|
676 | } |
---|
677 | else if(!strcmp(info, "STATUS FAILED")) |
---|
678 | { |
---|
679 | imcb_error(ic, "Call failed: %s", skype_call_strerror(sd->failurereason)); |
---|
680 | sd->call_id = NULL; |
---|
681 | } |
---|
682 | else if(!strncmp(info, "DURATION ", 9)) |
---|
683 | { |
---|
684 | if(sd->call_duration) |
---|
685 | g_free(sd->call_duration); |
---|
686 | sd->call_duration = g_strdup(info+9); |
---|
687 | } |
---|
688 | else if(!strncmp(info, "PARTNER_HANDLE ", 15)) |
---|
689 | { |
---|
690 | info += 15; |
---|
691 | if(sd->call_status) { |
---|
692 | switch(sd->call_status) |
---|
693 | { |
---|
694 | case SKYPE_CALL_RINGING: |
---|
695 | if(sd->call_out) |
---|
696 | imcb_log(ic, "You are currently ringing the user %s.", info); |
---|
697 | else |
---|
698 | { |
---|
699 | g_snprintf(buf, 1024, "The user %s is currently ringing you.", info); |
---|
700 | skype_call_ask(ic, sd->call_id, buf); |
---|
701 | } |
---|
702 | break; |
---|
703 | case SKYPE_CALL_MISSED: |
---|
704 | imcb_log(ic, "You have missed a call from user %s.", info); |
---|
705 | break; |
---|
706 | case SKYPE_CALL_CANCELLED: |
---|
707 | imcb_log(ic, "You cancelled the call to the user %s.", info); |
---|
708 | sd->call_status = 0; |
---|
709 | sd->call_out = FALSE; |
---|
710 | break; |
---|
711 | case SKYPE_CALL_REFUSED: |
---|
712 | if(sd->call_out) |
---|
713 | imcb_log(ic, "The user %s refused the call.", info); |
---|
714 | else |
---|
715 | imcb_log(ic, "You refused the call from user %s.", info); |
---|
716 | sd->call_out = FALSE; |
---|
717 | break; |
---|
718 | case SKYPE_CALL_FINISHED: |
---|
719 | if(sd->call_duration) |
---|
720 | imcb_log(ic, "You finished the call to the user %s (duration: %s seconds).", info, sd->call_duration); |
---|
721 | else |
---|
722 | imcb_log(ic, "You finished the call to the user %s.", info); |
---|
723 | sd->call_out = FALSE; |
---|
724 | break; |
---|
725 | default: |
---|
726 | /* Don't be noisy, ignore other statuses for now. */ |
---|
727 | break; |
---|
728 | } |
---|
729 | sd->call_status = 0; |
---|
730 | } |
---|
731 | } |
---|
732 | } |
---|
733 | } |
---|
734 | else if(!strncmp(line, "FILETRANSFER ", 13)) |
---|
735 | { |
---|
736 | char *id = strchr(line, ' '); |
---|
737 | if(++id) |
---|
738 | { |
---|
739 | char *info = strchr(id, ' '); |
---|
740 | *info = '\0'; |
---|
741 | info++; |
---|
742 | if(!strcmp(info, "STATUS NEW")) |
---|
743 | { |
---|
744 | g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id); |
---|
745 | skype_write( ic, buf, strlen( buf ) ); |
---|
746 | sd->filetransfer_status = SKYPE_FILETRANSFER_NEW; |
---|
747 | } |
---|
748 | else if(!strcmp(info, "STATUS FAILED")) |
---|
749 | { |
---|
750 | g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id); |
---|
751 | skype_write( ic, buf, strlen( buf ) ); |
---|
752 | sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED; |
---|
753 | } |
---|
754 | else if(!strncmp(info, "PARTNER_HANDLE ", 15)) |
---|
755 | { |
---|
756 | info += 15; |
---|
757 | if(sd->filetransfer_status) { |
---|
758 | switch(sd->filetransfer_status) |
---|
759 | { |
---|
760 | case SKYPE_FILETRANSFER_NEW: |
---|
761 | imcb_log(ic, "The user %s offered a new file for you.", info); |
---|
762 | break; |
---|
763 | case SKYPE_FILETRANSFER_FAILED: |
---|
764 | imcb_log(ic, "Failed to transfer file from user %s.", info); |
---|
765 | break; |
---|
766 | } |
---|
767 | sd->filetransfer_status = 0; |
---|
768 | } |
---|
769 | } |
---|
770 | } |
---|
771 | } |
---|
772 | else if(!strncmp(line, "CHAT ", 5)) |
---|
773 | { |
---|
774 | char *id = strchr(line, ' '); |
---|
775 | if(++id) |
---|
776 | { |
---|
777 | char *info = strchr(id, ' '); |
---|
778 | if(info) |
---|
779 | *info = '\0'; |
---|
780 | info++; |
---|
781 | /* Remove fake chat if we created one in skype_chat_with() */ |
---|
782 | struct groupchat *gc = skype_chat_by_name(ic, ""); |
---|
783 | if(gc) |
---|
784 | imcb_chat_free(gc); |
---|
785 | if(!strcmp(info, "STATUS MULTI_SUBSCRIBED")) |
---|
786 | { |
---|
787 | imcb_chat_new( ic, id ); |
---|
788 | g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id); |
---|
789 | skype_write(ic, buf, strlen(buf)); |
---|
790 | g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id); |
---|
791 | skype_write(ic, buf, strlen(buf)); |
---|
792 | } |
---|
793 | else if(!strcmp(info, "STATUS DIALOG") && sd->groupchat_with) |
---|
794 | { |
---|
795 | gc = imcb_chat_new( ic, id ); |
---|
796 | /* According to the docs this |
---|
797 | * is necessary. However it |
---|
798 | * does not seem the situation |
---|
799 | * and it would open an extra |
---|
800 | * window on our client, so |
---|
801 | * just leave it out. */ |
---|
802 | /*g_snprintf(buf, 1024, "OPEN CHAT %s\n", id); |
---|
803 | skype_write(ic, buf, strlen(buf));*/ |
---|
804 | g_snprintf(buf, 1024, "%s@skype.com", sd->groupchat_with); |
---|
805 | imcb_chat_add_buddy(gc, buf); |
---|
806 | imcb_chat_add_buddy(gc, sd->username); |
---|
807 | g_free(sd->groupchat_with); |
---|
808 | sd->groupchat_with = NULL; |
---|
809 | g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id); |
---|
810 | skype_write(ic, buf, strlen(buf)); |
---|
811 | g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id); |
---|
812 | skype_write(ic, buf, strlen(buf)); |
---|
813 | } |
---|
814 | else if(!strcmp(info, "STATUS UNSUBSCRIBED")) |
---|
815 | { |
---|
816 | gc = skype_chat_by_name(ic, id); |
---|
817 | if(gc) |
---|
818 | gc->data = (void*)FALSE; |
---|
819 | } |
---|
820 | else if(!strncmp(info, "ADDER ", 6)) |
---|
821 | { |
---|
822 | info += 6; |
---|
823 | g_free(sd->adder); |
---|
824 | sd->adder = g_strdup_printf("%s@skype.com", info); |
---|
825 | } |
---|
826 | else if(!strncmp(info, "TOPIC ", 6)) |
---|
827 | { |
---|
828 | info += 6; |
---|
829 | gc = skype_chat_by_name(ic, id); |
---|
830 | if(gc && (sd->adder || sd->topic_wait)) |
---|
831 | { |
---|
832 | if(sd->topic_wait) |
---|
833 | { |
---|
834 | sd->adder = g_strdup(sd->username); |
---|
835 | sd->topic_wait = 0; |
---|
836 | } |
---|
837 | imcb_chat_topic(gc, sd->adder, info, 0); |
---|
838 | g_free(sd->adder); |
---|
839 | sd->adder = NULL; |
---|
840 | } |
---|
841 | } |
---|
842 | else if(!strncmp(info, "ACTIVEMEMBERS ", 14)) |
---|
843 | { |
---|
844 | info += 14; |
---|
845 | gc = skype_chat_by_name(ic, id); |
---|
846 | /* Hack! We set ->data to TRUE |
---|
847 | * while we're on the channel |
---|
848 | * so that we won't rejoin |
---|
849 | * after a /part. */ |
---|
850 | if(gc && !gc->data) |
---|
851 | { |
---|
852 | char **members = g_strsplit(info, " ", 0); |
---|
853 | int i; |
---|
854 | for(i=0;members[i];i++) |
---|
855 | { |
---|
856 | if(!strcmp(members[i], sd->username)) |
---|
857 | continue; |
---|
858 | g_snprintf(buf, 1024, "%s@skype.com", members[i]); |
---|
859 | if(!g_list_find_custom(gc->in_room, buf, (GCompareFunc)strcmp)) |
---|
860 | imcb_chat_add_buddy(gc, buf); |
---|
861 | } |
---|
862 | imcb_chat_add_buddy(gc, sd->username); |
---|
863 | g_strfreev(members); |
---|
864 | } |
---|
865 | } |
---|
866 | } |
---|
867 | } |
---|
868 | else if(!strncmp(line, "PASSWORD ", 9)) |
---|
869 | { |
---|
870 | if(!strncmp(line+9, "OK", 2)) |
---|
871 | imcb_connected(ic); |
---|
872 | else |
---|
873 | { |
---|
874 | imcb_error(ic, "Authentication Failed"); |
---|
875 | imc_logout( ic, TRUE ); |
---|
876 | } |
---|
877 | } |
---|
878 | else if(!strncmp(line, "PROFILE PSTN_BALANCE ", 21)) |
---|
879 | { |
---|
880 | imcb_log(ic, "SkypeOut balance value is '%s'.", line+21); |
---|
881 | } |
---|
882 | else if(!strncmp(line, "PING", 4)) |
---|
883 | { |
---|
884 | g_snprintf(buf, 1024, "PONG\n"); |
---|
885 | skype_write(ic, buf, strlen(buf)); |
---|
886 | } |
---|
887 | else if(!strncmp(line, "CHATS ", 6)) |
---|
888 | { |
---|
889 | char **i; |
---|
890 | char **chats = g_strsplit(line + 6, ", ", 0); |
---|
891 | |
---|
892 | i = chats; |
---|
893 | while (*i) |
---|
894 | { |
---|
895 | g_snprintf(buf, 1024, "GET CHAT %s STATUS\n", *i); |
---|
896 | skype_write( ic, buf, strlen( buf ) ); |
---|
897 | g_snprintf(buf, 1024, "GET CHAT %s ACTIVEMEMBERS\n", *i); |
---|
898 | skype_write( ic, buf, strlen( buf ) ); |
---|
899 | i++; |
---|
900 | } |
---|
901 | g_strfreev(chats); |
---|
902 | } |
---|
903 | lineptr++; |
---|
904 | } |
---|
905 | g_strfreev(lines); |
---|
906 | } |
---|
907 | else if( st == 0 || ( st < 0 && !sockerr_again() ) ) |
---|
908 | { |
---|
909 | closesocket( sd->fd ); |
---|
910 | sd->fd = -1; |
---|
911 | |
---|
912 | imcb_error( ic, "Error while reading from server" ); |
---|
913 | imc_logout( ic, TRUE ); |
---|
914 | return FALSE; |
---|
915 | } |
---|
916 | return TRUE; |
---|
917 | } |
---|
918 | |
---|
919 | gboolean skype_start_stream( struct im_connection *ic ) |
---|
920 | { |
---|
921 | struct skype_data *sd = ic->proto_data; |
---|
922 | char *buf; |
---|
923 | int st; |
---|
924 | |
---|
925 | if(!sd) |
---|
926 | return FALSE; |
---|
927 | |
---|
928 | if( sd->bfd <= 0 ) |
---|
929 | sd->bfd = b_input_add( sd->fd, GAIM_INPUT_READ, skype_read_callback, ic ); |
---|
930 | |
---|
931 | /* Log in */ |
---|
932 | buf = g_strdup_printf("USERNAME %s\n", ic->acc->user); |
---|
933 | st = skype_write( ic, buf, strlen( buf ) ); |
---|
934 | g_free(buf); |
---|
935 | buf = g_strdup_printf("PASSWORD %s\n", ic->acc->pass); |
---|
936 | st = skype_write( ic, buf, strlen( buf ) ); |
---|
937 | g_free(buf); |
---|
938 | |
---|
939 | /* This will download all buddies. */ |
---|
940 | buf = g_strdup_printf("SEARCH FRIENDS\n"); |
---|
941 | st = skype_write( ic, buf, strlen( buf ) ); |
---|
942 | g_free(buf); |
---|
943 | buf = g_strdup_printf("SET USERSTATUS ONLINE\n"); |
---|
944 | skype_write( ic, buf, strlen( buf ) ); |
---|
945 | g_free(buf); |
---|
946 | |
---|
947 | /* Auto join to bookmarked chats if requested.*/ |
---|
948 | if (set_getbool(&ic->acc->set, "auto_join")) { |
---|
949 | buf = g_strdup_printf("SEARCH BOOKMARKEDCHATS\n"); |
---|
950 | skype_write( ic, buf, strlen( buf ) ); |
---|
951 | g_free(buf); |
---|
952 | } |
---|
953 | return st; |
---|
954 | } |
---|
955 | |
---|
956 | gboolean skype_connected( gpointer data, void *source, b_input_condition cond ) |
---|
957 | { |
---|
958 | struct im_connection *ic = data; |
---|
959 | struct skype_data *sd = ic->proto_data; |
---|
960 | if(!source) |
---|
961 | { |
---|
962 | sd->ssl = NULL; |
---|
963 | imcb_error( ic, "Could not connect to server" ); |
---|
964 | imc_logout( ic, TRUE ); |
---|
965 | return FALSE; |
---|
966 | } |
---|
967 | imcb_log( ic, "Connected to server, logging in" ); |
---|
968 | return skype_start_stream(ic); |
---|
969 | } |
---|
970 | |
---|
971 | static void skype_login( account_t *acc ) |
---|
972 | { |
---|
973 | struct im_connection *ic = imcb_new( acc ); |
---|
974 | struct skype_data *sd = g_new0( struct skype_data, 1 ); |
---|
975 | |
---|
976 | ic->proto_data = sd; |
---|
977 | |
---|
978 | imcb_log( ic, "Connecting" ); |
---|
979 | sd->ssl = ssl_connect(set_getstr( &acc->set, "server" ), set_getint( &acc->set, "port" ), skype_connected, ic ); |
---|
980 | sd->fd = sd->ssl ? ssl_getfd( sd->ssl ) : -1; |
---|
981 | sd->username = g_strdup( acc->user ); |
---|
982 | |
---|
983 | sd->ic = ic; |
---|
984 | |
---|
985 | if (set_getbool(&acc->set, "skypeconsole")) |
---|
986 | imcb_add_buddy(ic, "skypeconsole", NULL); |
---|
987 | } |
---|
988 | |
---|
989 | static void skype_logout( struct im_connection *ic ) |
---|
990 | { |
---|
991 | struct skype_data *sd = ic->proto_data; |
---|
992 | char *buf; |
---|
993 | |
---|
994 | buf = g_strdup_printf("SET USERSTATUS OFFLINE\n"); |
---|
995 | skype_write( ic, buf, strlen( buf ) ); |
---|
996 | g_free(buf); |
---|
997 | |
---|
998 | g_free(sd->username); |
---|
999 | g_free(sd->handle); |
---|
1000 | g_free(sd); |
---|
1001 | ic->proto_data = NULL; |
---|
1002 | } |
---|
1003 | |
---|
1004 | static int skype_buddy_msg( struct im_connection *ic, char *who, char *message, int flags ) |
---|
1005 | { |
---|
1006 | char *buf, *ptr, *nick; |
---|
1007 | int st; |
---|
1008 | |
---|
1009 | nick = g_strdup(who); |
---|
1010 | ptr = strchr(nick, '@'); |
---|
1011 | if(ptr) |
---|
1012 | *ptr = '\0'; |
---|
1013 | |
---|
1014 | if (!strncmp(who, "skypeconsole", 12)) |
---|
1015 | buf = g_strdup_printf("%s\n", message); |
---|
1016 | else |
---|
1017 | buf = g_strdup_printf("MESSAGE %s %s\n", nick, message); |
---|
1018 | g_free(nick); |
---|
1019 | st = skype_write( ic, buf, strlen( buf ) ); |
---|
1020 | g_free(buf); |
---|
1021 | |
---|
1022 | return st; |
---|
1023 | } |
---|
1024 | |
---|
1025 | const struct skype_away_state *skype_away_state_by_name( char *name ) |
---|
1026 | { |
---|
1027 | int i; |
---|
1028 | |
---|
1029 | for( i = 0; skype_away_state_list[i].full_name; i ++ ) |
---|
1030 | if( g_strcasecmp( skype_away_state_list[i].full_name, name ) == 0 ) |
---|
1031 | return( skype_away_state_list + i ); |
---|
1032 | |
---|
1033 | return NULL; |
---|
1034 | } |
---|
1035 | |
---|
1036 | static void skype_set_away( struct im_connection *ic, char *state_txt, char *message ) |
---|
1037 | { |
---|
1038 | const struct skype_away_state *state; |
---|
1039 | char *buf; |
---|
1040 | |
---|
1041 | if( strcmp( state_txt, GAIM_AWAY_CUSTOM ) == 0 ) |
---|
1042 | state = skype_away_state_by_name( "Away" ); |
---|
1043 | else |
---|
1044 | state = skype_away_state_by_name( state_txt ); |
---|
1045 | buf = g_strdup_printf("SET USERSTATUS %s\n", state->code); |
---|
1046 | skype_write( ic, buf, strlen( buf ) ); |
---|
1047 | g_free(buf); |
---|
1048 | } |
---|
1049 | |
---|
1050 | static GList *skype_away_states( struct im_connection *ic ) |
---|
1051 | { |
---|
1052 | static GList *l = NULL; |
---|
1053 | int i; |
---|
1054 | |
---|
1055 | if( l == NULL ) |
---|
1056 | for( i = 0; skype_away_state_list[i].full_name; i ++ ) |
---|
1057 | l = g_list_append( l, (void*) skype_away_state_list[i].full_name ); |
---|
1058 | |
---|
1059 | return l; |
---|
1060 | } |
---|
1061 | |
---|
1062 | static char *skype_set_display_name( set_t *set, char *value ) |
---|
1063 | { |
---|
1064 | account_t *acc = set->data; |
---|
1065 | struct im_connection *ic = acc->ic; |
---|
1066 | char *buf; |
---|
1067 | |
---|
1068 | buf = g_strdup_printf("SET PROFILE FULLNAME %s", value); |
---|
1069 | skype_write( ic, buf, strlen( buf ) ); |
---|
1070 | g_free(buf); |
---|
1071 | return(value); |
---|
1072 | } |
---|
1073 | |
---|
1074 | static char *skype_set_balance( set_t *set, char *value ) |
---|
1075 | { |
---|
1076 | account_t *acc = set->data; |
---|
1077 | struct im_connection *ic = acc->ic; |
---|
1078 | char *buf; |
---|
1079 | |
---|
1080 | buf = g_strdup_printf("GET PROFILE PSTN_BALANCE"); |
---|
1081 | skype_write( ic, buf, strlen( buf ) ); |
---|
1082 | g_free(buf); |
---|
1083 | return(value); |
---|
1084 | } |
---|
1085 | |
---|
1086 | static char *skype_set_call( set_t *set, char *value ) |
---|
1087 | { |
---|
1088 | account_t *acc = set->data; |
---|
1089 | struct im_connection *ic = acc->ic; |
---|
1090 | struct skype_data *sd = ic->proto_data; |
---|
1091 | char *nick, *ptr, *buf; |
---|
1092 | |
---|
1093 | if(value) |
---|
1094 | { |
---|
1095 | user_t *u = user_find(acc->irc, value); |
---|
1096 | /* We are starting a call */ |
---|
1097 | if(!u) |
---|
1098 | nick = g_strdup(value); |
---|
1099 | else |
---|
1100 | nick = g_strdup(u->handle); |
---|
1101 | ptr = strchr(nick, '@'); |
---|
1102 | if(ptr) |
---|
1103 | *ptr = '\0'; |
---|
1104 | |
---|
1105 | buf = g_strdup_printf("CALL %s", nick); |
---|
1106 | skype_write( ic, buf, strlen( buf ) ); |
---|
1107 | g_free(buf); |
---|
1108 | g_free(nick); |
---|
1109 | } |
---|
1110 | else |
---|
1111 | { |
---|
1112 | /* We are ending a call */ |
---|
1113 | if(sd->call_id) |
---|
1114 | { |
---|
1115 | buf = g_strdup_printf("SET CALL %s STATUS FINISHED", sd->call_id); |
---|
1116 | skype_write( ic, buf, strlen( buf ) ); |
---|
1117 | g_free(buf); |
---|
1118 | g_free(sd->call_id); |
---|
1119 | sd->call_id = NULL; |
---|
1120 | } |
---|
1121 | else |
---|
1122 | { |
---|
1123 | imcb_error(ic, "There are no active calls currently."); |
---|
1124 | } |
---|
1125 | } |
---|
1126 | return(value); |
---|
1127 | } |
---|
1128 | |
---|
1129 | static void skype_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
1130 | { |
---|
1131 | char *buf, *nick, *ptr; |
---|
1132 | |
---|
1133 | nick = g_strdup(who); |
---|
1134 | ptr = strchr(nick, '@'); |
---|
1135 | if(ptr) |
---|
1136 | *ptr = '\0'; |
---|
1137 | buf = g_strdup_printf("SET USER %s BUDDYSTATUS 2 Please authorize me\n", nick); |
---|
1138 | skype_write( ic, buf, strlen( buf ) ); |
---|
1139 | g_free(nick); |
---|
1140 | } |
---|
1141 | |
---|
1142 | static void skype_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
1143 | { |
---|
1144 | char *buf, *nick, *ptr; |
---|
1145 | |
---|
1146 | nick = g_strdup(who); |
---|
1147 | ptr = strchr(nick, '@'); |
---|
1148 | if(ptr) |
---|
1149 | *ptr = '\0'; |
---|
1150 | buf = g_strdup_printf("SET USER %s BUDDYSTATUS 1\n", nick); |
---|
1151 | skype_write( ic, buf, strlen( buf ) ); |
---|
1152 | g_free(nick); |
---|
1153 | } |
---|
1154 | |
---|
1155 | void skype_chat_msg( struct groupchat *gc, char *message, int flags ) |
---|
1156 | { |
---|
1157 | struct im_connection *ic = gc->ic; |
---|
1158 | char *buf; |
---|
1159 | buf = g_strdup_printf("CHATMESSAGE %s %s\n", gc->title, message); |
---|
1160 | skype_write( ic, buf, strlen( buf ) ); |
---|
1161 | g_free(buf); |
---|
1162 | } |
---|
1163 | |
---|
1164 | void skype_chat_leave( struct groupchat *gc ) |
---|
1165 | { |
---|
1166 | struct im_connection *ic = gc->ic; |
---|
1167 | char *buf; |
---|
1168 | buf = g_strdup_printf("ALTER CHAT %s LEAVE\n", gc->title); |
---|
1169 | skype_write( ic, buf, strlen( buf ) ); |
---|
1170 | g_free(buf); |
---|
1171 | gc->data = (void*)TRUE; |
---|
1172 | } |
---|
1173 | |
---|
1174 | void skype_chat_invite(struct groupchat *gc, char *who, char *message) |
---|
1175 | { |
---|
1176 | struct im_connection *ic = gc->ic; |
---|
1177 | char *buf, *ptr, *nick; |
---|
1178 | nick = g_strdup(message); |
---|
1179 | ptr = strchr(nick, '@'); |
---|
1180 | if(ptr) |
---|
1181 | *ptr = '\0'; |
---|
1182 | buf = g_strdup_printf("ALTER CHAT %s ADDMEMBERS %s\n", gc->title, nick); |
---|
1183 | skype_write( ic, buf, strlen( buf ) ); |
---|
1184 | g_free(buf); |
---|
1185 | g_free(nick); |
---|
1186 | } |
---|
1187 | |
---|
1188 | void skype_chat_topic(struct groupchat *gc, char *message) |
---|
1189 | { |
---|
1190 | struct im_connection *ic = gc->ic; |
---|
1191 | struct skype_data *sd = ic->proto_data; |
---|
1192 | char *buf; |
---|
1193 | buf = g_strdup_printf("ALTER CHAT %s SETTOPIC %s\n", gc->title, message); |
---|
1194 | skype_write( ic, buf, strlen( buf ) ); |
---|
1195 | g_free(buf); |
---|
1196 | sd->topic_wait = 1; |
---|
1197 | } |
---|
1198 | |
---|
1199 | struct groupchat *skype_chat_with(struct im_connection *ic, char *who) |
---|
1200 | { |
---|
1201 | struct skype_data *sd = ic->proto_data; |
---|
1202 | char *ptr, *nick, *buf; |
---|
1203 | nick = g_strdup(who); |
---|
1204 | ptr = strchr(nick, '@'); |
---|
1205 | if(ptr) |
---|
1206 | *ptr = '\0'; |
---|
1207 | buf = g_strdup_printf("CHAT CREATE %s\n", nick); |
---|
1208 | skype_write(ic, buf, strlen(buf)); |
---|
1209 | g_free(buf); |
---|
1210 | sd->groupchat_with = g_strdup(nick); |
---|
1211 | g_free(nick); |
---|
1212 | /* We create a fake chat for now. We will replace it with a real one in |
---|
1213 | * the real callback. */ |
---|
1214 | return(imcb_chat_new( ic, "" )); |
---|
1215 | } |
---|
1216 | |
---|
1217 | static void skype_get_info(struct im_connection *ic, char *who) |
---|
1218 | { |
---|
1219 | char *ptr, *nick, *buf; |
---|
1220 | nick = g_strdup(who); |
---|
1221 | ptr = strchr(nick, '@'); |
---|
1222 | if(ptr) |
---|
1223 | *ptr = '\0'; |
---|
1224 | buf = g_strdup_printf("GET USER %s FULLNAME\n", nick); |
---|
1225 | skype_write(ic, buf, strlen(buf)); |
---|
1226 | g_free(buf); |
---|
1227 | buf = g_strdup_printf("GET USER %s PHONE_HOME\n", nick); |
---|
1228 | skype_write(ic, buf, strlen(buf)); |
---|
1229 | g_free(buf); |
---|
1230 | buf = g_strdup_printf("GET USER %s PHONE_OFFICE\n", nick); |
---|
1231 | skype_write(ic, buf, strlen(buf)); |
---|
1232 | g_free(buf); |
---|
1233 | buf = g_strdup_printf("GET USER %s PHONE_MOBILE\n", nick); |
---|
1234 | skype_write(ic, buf, strlen(buf)); |
---|
1235 | g_free(buf); |
---|
1236 | buf = g_strdup_printf("GET USER %s NROF_AUTHED_BUDDIES\n", nick); |
---|
1237 | skype_write(ic, buf, strlen(buf)); |
---|
1238 | g_free(buf); |
---|
1239 | buf = g_strdup_printf("GET USER %s TIMEZONE\n", nick); |
---|
1240 | skype_write(ic, buf, strlen(buf)); |
---|
1241 | g_free(buf); |
---|
1242 | buf = g_strdup_printf("GET USER %s LASTONLINETIMESTAMP\n", nick); |
---|
1243 | skype_write(ic, buf, strlen(buf)); |
---|
1244 | g_free(buf); |
---|
1245 | buf = g_strdup_printf("GET USER %s BIRTHDAY\n", nick); |
---|
1246 | skype_write(ic, buf, strlen(buf)); |
---|
1247 | g_free(buf); |
---|
1248 | buf = g_strdup_printf("GET USER %s SEX\n", nick); |
---|
1249 | skype_write(ic, buf, strlen(buf)); |
---|
1250 | g_free(buf); |
---|
1251 | buf = g_strdup_printf("GET USER %s LANGUAGE\n", nick); |
---|
1252 | skype_write(ic, buf, strlen(buf)); |
---|
1253 | g_free(buf); |
---|
1254 | buf = g_strdup_printf("GET USER %s COUNTRY\n", nick); |
---|
1255 | skype_write(ic, buf, strlen(buf)); |
---|
1256 | g_free(buf); |
---|
1257 | buf = g_strdup_printf("GET USER %s PROVINCE\n", nick); |
---|
1258 | skype_write(ic, buf, strlen(buf)); |
---|
1259 | g_free(buf); |
---|
1260 | buf = g_strdup_printf("GET USER %s CITY\n", nick); |
---|
1261 | skype_write(ic, buf, strlen(buf)); |
---|
1262 | g_free(buf); |
---|
1263 | buf = g_strdup_printf("GET USER %s HOMEPAGE\n", nick); |
---|
1264 | skype_write(ic, buf, strlen(buf)); |
---|
1265 | g_free(buf); |
---|
1266 | buf = g_strdup_printf("GET USER %s ABOUT\n", nick); |
---|
1267 | skype_write(ic, buf, strlen(buf)); |
---|
1268 | g_free(buf); |
---|
1269 | } |
---|
1270 | |
---|
1271 | static void skype_set_my_name( struct im_connection *ic, char *info ) |
---|
1272 | { |
---|
1273 | skype_set_display_name( set_find( &ic->acc->set, "display_name" ), info ); |
---|
1274 | } |
---|
1275 | |
---|
1276 | static void skype_init( account_t *acc ) |
---|
1277 | { |
---|
1278 | set_t *s; |
---|
1279 | |
---|
1280 | s = set_add( &acc->set, "server", SKYPE_DEFAULT_SERVER, set_eval_account, acc ); |
---|
1281 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
1282 | |
---|
1283 | s = set_add( &acc->set, "port", SKYPE_DEFAULT_PORT, set_eval_int, acc ); |
---|
1284 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
1285 | |
---|
1286 | s = set_add( &acc->set, "display_name", NULL, skype_set_display_name, acc ); |
---|
1287 | s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY; |
---|
1288 | |
---|
1289 | s = set_add( &acc->set, "call", NULL, skype_set_call, acc ); |
---|
1290 | s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY; |
---|
1291 | |
---|
1292 | s = set_add( &acc->set, "balance", NULL, skype_set_balance, acc ); |
---|
1293 | s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY; |
---|
1294 | |
---|
1295 | s = set_add( &acc->set, "skypeout_offline", "true", set_eval_bool, acc ); |
---|
1296 | |
---|
1297 | s = set_add( &acc->set, "skypeconsole", "false", set_eval_bool, acc ); |
---|
1298 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
1299 | |
---|
1300 | s = set_add( &acc->set, "auto_join", "false", set_eval_bool, acc ); |
---|
1301 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
1302 | } |
---|
1303 | |
---|
1304 | void init_plugin(void) |
---|
1305 | { |
---|
1306 | struct prpl *ret = g_new0( struct prpl, 1 ); |
---|
1307 | |
---|
1308 | ret->name = "skype"; |
---|
1309 | ret->login = skype_login; |
---|
1310 | ret->init = skype_init; |
---|
1311 | ret->logout = skype_logout; |
---|
1312 | ret->buddy_msg = skype_buddy_msg; |
---|
1313 | ret->get_info = skype_get_info; |
---|
1314 | ret->set_my_name = skype_set_my_name; |
---|
1315 | ret->away_states = skype_away_states; |
---|
1316 | ret->set_away = skype_set_away; |
---|
1317 | ret->add_buddy = skype_add_buddy; |
---|
1318 | ret->remove_buddy = skype_remove_buddy; |
---|
1319 | ret->chat_msg = skype_chat_msg; |
---|
1320 | ret->chat_leave = skype_chat_leave; |
---|
1321 | ret->chat_invite = skype_chat_invite; |
---|
1322 | ret->chat_with = skype_chat_with; |
---|
1323 | ret->handle_cmp = g_strcasecmp; |
---|
1324 | ret->chat_topic = skype_chat_topic; |
---|
1325 | register_protocol( ret ); |
---|
1326 | } |
---|
1327 | |
---|
1328 | /* vim: set ts=2 sw=2 noet: */ |
---|