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