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