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