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