1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* MSN module - Miscellaneous utilities */ |
---|
8 | |
---|
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 with |
---|
21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
22 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
23 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #include "nogaim.h" |
---|
27 | #include "msn.h" |
---|
28 | #include "md5.h" |
---|
29 | #include "soap.h" |
---|
30 | #include <ctype.h> |
---|
31 | |
---|
32 | static char *adlrml_entry(const char *handle_, msn_buddy_flags_t list) |
---|
33 | { |
---|
34 | char *domain, handle[strlen(handle_) + 1]; |
---|
35 | |
---|
36 | strcpy(handle, handle_); |
---|
37 | if ((domain = strchr(handle, '@'))) { |
---|
38 | *(domain++) = '\0'; |
---|
39 | } else { |
---|
40 | return NULL; |
---|
41 | } |
---|
42 | |
---|
43 | return g_markup_printf_escaped("<ml><d n=\"%s\"><c n=\"%s\" l=\"%d\" t=\"1\"/></d></ml>", |
---|
44 | domain, handle, list); |
---|
45 | } |
---|
46 | |
---|
47 | int msn_buddy_list_add(struct im_connection *ic, msn_buddy_flags_t list, const char *who, const char *realname, |
---|
48 | const char *group) |
---|
49 | { |
---|
50 | struct msn_data *md = ic->proto_data; |
---|
51 | char groupid[8]; |
---|
52 | bee_user_t *bu; |
---|
53 | struct msn_buddy_data *bd; |
---|
54 | char *adl; |
---|
55 | |
---|
56 | *groupid = '\0'; |
---|
57 | |
---|
58 | if (!((bu = bee_user_by_handle(ic->bee, ic, who)) || |
---|
59 | (bu = bee_user_new(ic->bee, ic, who, 0))) || |
---|
60 | !(bd = bu->data) || bd->flags & list) { |
---|
61 | return 1; |
---|
62 | } |
---|
63 | |
---|
64 | bd->flags |= list; |
---|
65 | |
---|
66 | if (list == MSN_BUDDY_FL) { |
---|
67 | msn_soap_ab_contact_add(ic, bu); |
---|
68 | } else { |
---|
69 | msn_soap_memlist_edit(ic, who, TRUE, list); |
---|
70 | } |
---|
71 | |
---|
72 | if ((adl = adlrml_entry(who, list))) { |
---|
73 | int st = msn_ns_write(ic, -1, "ADL %d %zd\r\n%s", |
---|
74 | ++md->trId, strlen(adl), adl); |
---|
75 | g_free(adl); |
---|
76 | |
---|
77 | return st; |
---|
78 | } |
---|
79 | |
---|
80 | return 1; |
---|
81 | } |
---|
82 | |
---|
83 | int msn_buddy_list_remove(struct im_connection *ic, msn_buddy_flags_t list, const char *who, const char *group) |
---|
84 | { |
---|
85 | struct msn_data *md = ic->proto_data; |
---|
86 | char groupid[8]; |
---|
87 | bee_user_t *bu; |
---|
88 | struct msn_buddy_data *bd; |
---|
89 | char *adl; |
---|
90 | |
---|
91 | *groupid = '\0'; |
---|
92 | |
---|
93 | if (!(bu = bee_user_by_handle(ic->bee, ic, who)) || |
---|
94 | !(bd = bu->data) || !(bd->flags & list)) { |
---|
95 | return 1; |
---|
96 | } |
---|
97 | |
---|
98 | bd->flags &= ~list; |
---|
99 | |
---|
100 | if (list == MSN_BUDDY_FL) { |
---|
101 | msn_soap_ab_contact_del(ic, bu); |
---|
102 | } else { |
---|
103 | msn_soap_memlist_edit(ic, who, FALSE, list); |
---|
104 | } |
---|
105 | |
---|
106 | if ((adl = adlrml_entry(who, list))) { |
---|
107 | int st = msn_ns_write(ic, -1, "RML %d %zd\r\n%s", |
---|
108 | ++md->trId, strlen(adl), adl); |
---|
109 | g_free(adl); |
---|
110 | |
---|
111 | return st; |
---|
112 | } |
---|
113 | |
---|
114 | return 1; |
---|
115 | } |
---|
116 | |
---|
117 | struct msn_buddy_ask_data { |
---|
118 | struct im_connection *ic; |
---|
119 | char *handle; |
---|
120 | char *realname; |
---|
121 | }; |
---|
122 | |
---|
123 | static void msn_buddy_ask_yes(void *data) |
---|
124 | { |
---|
125 | struct msn_buddy_ask_data *bla = data; |
---|
126 | |
---|
127 | msn_buddy_list_add(bla->ic, MSN_BUDDY_AL, bla->handle, bla->realname, NULL); |
---|
128 | |
---|
129 | imcb_ask_add(bla->ic, bla->handle, NULL); |
---|
130 | |
---|
131 | g_free(bla->handle); |
---|
132 | g_free(bla->realname); |
---|
133 | g_free(bla); |
---|
134 | } |
---|
135 | |
---|
136 | static void msn_buddy_ask_no(void *data) |
---|
137 | { |
---|
138 | struct msn_buddy_ask_data *bla = data; |
---|
139 | |
---|
140 | msn_buddy_list_add(bla->ic, MSN_BUDDY_BL, bla->handle, bla->realname, NULL); |
---|
141 | |
---|
142 | g_free(bla->handle); |
---|
143 | g_free(bla->realname); |
---|
144 | g_free(bla); |
---|
145 | } |
---|
146 | |
---|
147 | void msn_buddy_ask(bee_user_t *bu) |
---|
148 | { |
---|
149 | struct msn_buddy_ask_data *bla; |
---|
150 | struct msn_buddy_data *bd = bu->data; |
---|
151 | char buf[1024]; |
---|
152 | |
---|
153 | if (!(bd->flags & MSN_BUDDY_PL)) { |
---|
154 | return; |
---|
155 | } |
---|
156 | |
---|
157 | bla = g_new0(struct msn_buddy_ask_data, 1); |
---|
158 | bla->ic = bu->ic; |
---|
159 | bla->handle = g_strdup(bu->handle); |
---|
160 | bla->realname = g_strdup(bu->fullname); |
---|
161 | |
---|
162 | g_snprintf(buf, sizeof(buf), |
---|
163 | "The user %s (%s) wants to add you to his/her buddy list.", |
---|
164 | bu->handle, bu->fullname); |
---|
165 | imcb_ask(bu->ic, buf, bla, msn_buddy_ask_yes, msn_buddy_ask_no); |
---|
166 | } |
---|
167 | |
---|
168 | /* *NOT* thread-safe, but that's not a problem for now... */ |
---|
169 | char **msn_linesplit(char *line) |
---|
170 | { |
---|
171 | static char **ret = NULL; |
---|
172 | static int size = 3; |
---|
173 | int i, n = 0; |
---|
174 | |
---|
175 | if (ret == NULL) { |
---|
176 | ret = g_new0(char*, size); |
---|
177 | } |
---|
178 | |
---|
179 | for (i = 0; line[i] && line[i] == ' '; i++) { |
---|
180 | ; |
---|
181 | } |
---|
182 | if (line[i]) { |
---|
183 | ret[n++] = line + i; |
---|
184 | for (i++; line[i]; i++) { |
---|
185 | if (line[i] == ' ') { |
---|
186 | line[i] = 0; |
---|
187 | } else if (line[i] != ' ' && !line[i - 1]) { |
---|
188 | ret[n++] = line + i; |
---|
189 | } |
---|
190 | |
---|
191 | if (n >= size) { |
---|
192 | ret = g_renew(char*, ret, size += 2); |
---|
193 | } |
---|
194 | } |
---|
195 | } |
---|
196 | ret[n] = NULL; |
---|
197 | |
---|
198 | return(ret); |
---|
199 | } |
---|
200 | |
---|
201 | /* This one handles input from a MSN Messenger server. Both the NS and SB servers usually give |
---|
202 | commands, but sometimes they give additional data (payload). This function tries to handle |
---|
203 | this all in a nice way and send all data to the right places. */ |
---|
204 | |
---|
205 | /* Return values: -1: Read error, abort connection. |
---|
206 | 0: Command reported error; Abort *immediately*. (The connection does not exist anymore) |
---|
207 | 1: OK */ |
---|
208 | |
---|
209 | int msn_handler(struct msn_data *h) |
---|
210 | { |
---|
211 | struct im_connection *ic = h->ic; |
---|
212 | int st; |
---|
213 | |
---|
214 | h->rxq = g_renew(char, h->rxq, h->rxlen + 1024); |
---|
215 | st = read(h->fd, h->rxq + h->rxlen, 1024); |
---|
216 | h->rxlen += st; |
---|
217 | |
---|
218 | if (st <= 0) { |
---|
219 | fprintf(stderr, "\n\x1b[92m<<< [closed]\x1b[97m "); |
---|
220 | return(-1); |
---|
221 | } |
---|
222 | |
---|
223 | if (getenv("BITLBEE_DEBUG")) { |
---|
224 | fprintf(stderr, "\n\x1b[92m<<< "); |
---|
225 | write(2, h->rxq + h->rxlen - st, st); |
---|
226 | fprintf(stderr, "\x1b[97m"); |
---|
227 | } |
---|
228 | |
---|
229 | while (st) { |
---|
230 | int i; |
---|
231 | |
---|
232 | if (h->msglen == 0) { |
---|
233 | for (i = 0; i < h->rxlen; i++) { |
---|
234 | if (h->rxq[i] == '\r' || h->rxq[i] == '\n') { |
---|
235 | char *cmd_text, **cmd; |
---|
236 | int count; |
---|
237 | |
---|
238 | cmd_text = g_strndup(h->rxq, i); |
---|
239 | cmd = msn_linesplit(cmd_text); |
---|
240 | for (count = 0; cmd[count]; count++) { |
---|
241 | ; |
---|
242 | } |
---|
243 | st = msn_ns_command(h, cmd, count); |
---|
244 | g_free(cmd_text); |
---|
245 | |
---|
246 | /* If the connection broke, don't continue. We don't even exist anymore. */ |
---|
247 | if (!st) { |
---|
248 | return(0); |
---|
249 | } |
---|
250 | |
---|
251 | if (h->msglen) { |
---|
252 | h->cmd_text = g_strndup(h->rxq, i); |
---|
253 | } |
---|
254 | |
---|
255 | /* Skip to the next non-emptyline */ |
---|
256 | while (i < h->rxlen && (h->rxq[i] == '\r' || h->rxq[i] == '\n')) { |
---|
257 | i++; |
---|
258 | } |
---|
259 | |
---|
260 | break; |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | /* If we reached the end of the buffer, there's still an incomplete command there. |
---|
265 | Return and wait for more data. */ |
---|
266 | if (i == h->rxlen && h->rxq[i - 1] != '\r' && h->rxq[i - 1] != '\n') { |
---|
267 | break; |
---|
268 | } |
---|
269 | } else { |
---|
270 | char *msg, **cmd; |
---|
271 | int count; |
---|
272 | |
---|
273 | /* Do we have the complete message already? */ |
---|
274 | if (h->msglen > h->rxlen) { |
---|
275 | break; |
---|
276 | } |
---|
277 | |
---|
278 | msg = g_strndup(h->rxq, h->msglen); |
---|
279 | cmd = msn_linesplit(h->cmd_text); |
---|
280 | for (count = 0; cmd[count]; count++) { |
---|
281 | ; |
---|
282 | } |
---|
283 | |
---|
284 | st = msn_ns_message(h, msg, h->msglen, cmd, count); |
---|
285 | g_free(msg); |
---|
286 | g_free(h->cmd_text); |
---|
287 | h->cmd_text = NULL; |
---|
288 | |
---|
289 | if (!st) { |
---|
290 | return(0); |
---|
291 | } |
---|
292 | |
---|
293 | i = h->msglen; |
---|
294 | h->msglen = 0; |
---|
295 | } |
---|
296 | |
---|
297 | /* More data after this block? */ |
---|
298 | if (i < h->rxlen) { |
---|
299 | char *tmp; |
---|
300 | |
---|
301 | tmp = g_memdup(h->rxq + i, h->rxlen - i); |
---|
302 | g_free(h->rxq); |
---|
303 | h->rxq = tmp; |
---|
304 | h->rxlen -= i; |
---|
305 | i = 0; |
---|
306 | } else { |
---|
307 | /* If not, reset the rx queue and get lost. */ |
---|
308 | g_free(h->rxq); |
---|
309 | h->rxq = g_new0(char, 1); |
---|
310 | h->rxlen = 0; |
---|
311 | return(1); |
---|
312 | } |
---|
313 | } |
---|
314 | |
---|
315 | return(1); |
---|
316 | } |
---|
317 | |
---|
318 | /* Copied and heavily modified from http://tmsnc.sourceforge.net/chl.c */ |
---|
319 | char *msn_p11_challenge(char *challenge) |
---|
320 | { |
---|
321 | char *output, buf[256]; |
---|
322 | md5_state_t md5c; |
---|
323 | unsigned char md5Hash[16], *newHash; |
---|
324 | unsigned int *md5Parts, *chlStringParts, newHashParts[5]; |
---|
325 | long long nHigh = 0, nLow = 0; |
---|
326 | int i, n; |
---|
327 | |
---|
328 | /* Create the MD5 hash */ |
---|
329 | md5_init(&md5c); |
---|
330 | md5_append(&md5c, (unsigned char *) challenge, strlen(challenge)); |
---|
331 | md5_append(&md5c, (unsigned char *) MSNP11_PROD_KEY, strlen(MSNP11_PROD_KEY)); |
---|
332 | md5_finish(&md5c, md5Hash); |
---|
333 | |
---|
334 | /* Split it into four integers */ |
---|
335 | md5Parts = (unsigned int *) md5Hash; |
---|
336 | for (i = 0; i < 4; i++) { |
---|
337 | md5Parts[i] = GUINT32_TO_LE(md5Parts[i]); |
---|
338 | |
---|
339 | /* & each integer with 0x7FFFFFFF */ |
---|
340 | /* and save one unmodified array for later */ |
---|
341 | newHashParts[i] = md5Parts[i]; |
---|
342 | md5Parts[i] &= 0x7FFFFFFF; |
---|
343 | } |
---|
344 | |
---|
345 | /* make a new string and pad with '0' */ |
---|
346 | n = g_snprintf(buf, sizeof(buf) - 5, "%s%s00000000", challenge, MSNP11_PROD_ID); |
---|
347 | /* truncate at an 8-byte boundary */ |
---|
348 | buf[n &= ~7] = '\0'; |
---|
349 | |
---|
350 | /* split into integers */ |
---|
351 | chlStringParts = (unsigned int *) buf; |
---|
352 | |
---|
353 | /* this is magic */ |
---|
354 | for (i = 0; i < (n / 4) - 1; i += 2) { |
---|
355 | long long temp; |
---|
356 | |
---|
357 | chlStringParts[i] = GUINT32_TO_LE(chlStringParts[i]); |
---|
358 | chlStringParts[i + 1] = GUINT32_TO_LE(chlStringParts[i + 1]); |
---|
359 | |
---|
360 | temp = |
---|
361 | (md5Parts[0] * |
---|
362 | (((0x0E79A9C1 * |
---|
363 | (long long) chlStringParts[i]) % 0x7FFFFFFF) + nHigh) + md5Parts[1]) % 0x7FFFFFFF; |
---|
364 | nHigh = |
---|
365 | (md5Parts[2] * |
---|
366 | (((long long) chlStringParts[i + 1] + temp) % 0x7FFFFFFF) + md5Parts[3]) % 0x7FFFFFFF; |
---|
367 | nLow = nLow + nHigh + temp; |
---|
368 | } |
---|
369 | nHigh = (nHigh + md5Parts[1]) % 0x7FFFFFFF; |
---|
370 | nLow = (nLow + md5Parts[3]) % 0x7FFFFFFF; |
---|
371 | |
---|
372 | newHashParts[0] ^= nHigh; |
---|
373 | newHashParts[1] ^= nLow; |
---|
374 | newHashParts[2] ^= nHigh; |
---|
375 | newHashParts[3] ^= nLow; |
---|
376 | |
---|
377 | /* swap more bytes if big endian */ |
---|
378 | for (i = 0; i < 4; i++) { |
---|
379 | newHashParts[i] = GUINT32_TO_LE(newHashParts[i]); |
---|
380 | } |
---|
381 | |
---|
382 | /* make a string of the parts */ |
---|
383 | newHash = (unsigned char *) newHashParts; |
---|
384 | |
---|
385 | /* convert to hexadecimal */ |
---|
386 | output = g_new(char, 33); |
---|
387 | for (i = 0; i < 16; i++) { |
---|
388 | sprintf(output + i * 2, "%02x", newHash[i]); |
---|
389 | } |
---|
390 | |
---|
391 | return output; |
---|
392 | } |
---|
393 | |
---|
394 | gint msn_domaintree_cmp(gconstpointer a_, gconstpointer b_) |
---|
395 | { |
---|
396 | const char *a = a_, *b = b_; |
---|
397 | gint ret; |
---|
398 | |
---|
399 | if (!(a = strchr(a, '@')) || !(b = strchr(b, '@')) || |
---|
400 | (ret = strcmp(a, b)) == 0) { |
---|
401 | ret = strcmp(a_, b_); |
---|
402 | } |
---|
403 | |
---|
404 | return ret; |
---|
405 | } |
---|
406 | |
---|
407 | struct msn_group *msn_group_by_name(struct im_connection *ic, const char *name) |
---|
408 | { |
---|
409 | struct msn_data *md = ic->proto_data; |
---|
410 | GSList *l; |
---|
411 | |
---|
412 | for (l = md->groups; l; l = l->next) { |
---|
413 | struct msn_group *mg = l->data; |
---|
414 | |
---|
415 | if (g_strcasecmp(mg->name, name) == 0) { |
---|
416 | return mg; |
---|
417 | } |
---|
418 | } |
---|
419 | |
---|
420 | return NULL; |
---|
421 | } |
---|
422 | |
---|
423 | struct msn_group *msn_group_by_id(struct im_connection *ic, const char *id) |
---|
424 | { |
---|
425 | struct msn_data *md = ic->proto_data; |
---|
426 | GSList *l; |
---|
427 | |
---|
428 | for (l = md->groups; l; l = l->next) { |
---|
429 | struct msn_group *mg = l->data; |
---|
430 | |
---|
431 | if (g_strcasecmp(mg->id, id) == 0) { |
---|
432 | return mg; |
---|
433 | } |
---|
434 | } |
---|
435 | |
---|
436 | return NULL; |
---|
437 | } |
---|
438 | |
---|
439 | int msn_ns_set_display_name(struct im_connection *ic, const char *value) |
---|
440 | { |
---|
441 | // TODO, implement this through msn_set_away's method |
---|
442 | return 1; |
---|
443 | } |
---|
444 | |
---|
445 | const char *msn_normalize_handle(const char *handle) |
---|
446 | { |
---|
447 | if (strncmp(handle, "1:", 2) == 0) { |
---|
448 | return handle + 2; |
---|
449 | } else { |
---|
450 | return handle; |
---|
451 | } |
---|
452 | } |
---|