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_free(void *data) |
---|
124 | { |
---|
125 | struct msn_buddy_ask_data *bla = data; |
---|
126 | |
---|
127 | g_free(bla->handle); |
---|
128 | g_free(bla->realname); |
---|
129 | g_free(bla); |
---|
130 | } |
---|
131 | |
---|
132 | static void msn_buddy_ask_yes(void *data) |
---|
133 | { |
---|
134 | struct msn_buddy_ask_data *bla = data; |
---|
135 | |
---|
136 | msn_buddy_list_add(bla->ic, MSN_BUDDY_AL, bla->handle, bla->realname, NULL); |
---|
137 | |
---|
138 | imcb_ask_add(bla->ic, bla->handle, NULL); |
---|
139 | |
---|
140 | msn_buddy_ask_free(bla); |
---|
141 | } |
---|
142 | |
---|
143 | static void msn_buddy_ask_no(void *data) |
---|
144 | { |
---|
145 | struct msn_buddy_ask_data *bla = data; |
---|
146 | |
---|
147 | msn_buddy_list_add(bla->ic, MSN_BUDDY_BL, bla->handle, bla->realname, NULL); |
---|
148 | |
---|
149 | msn_buddy_ask_free(bla); |
---|
150 | } |
---|
151 | |
---|
152 | void msn_buddy_ask(bee_user_t *bu) |
---|
153 | { |
---|
154 | struct msn_buddy_ask_data *bla; |
---|
155 | struct msn_buddy_data *bd = bu->data; |
---|
156 | char buf[1024]; |
---|
157 | |
---|
158 | if (!(bd->flags & MSN_BUDDY_PL)) { |
---|
159 | return; |
---|
160 | } |
---|
161 | |
---|
162 | bla = g_new0(struct msn_buddy_ask_data, 1); |
---|
163 | bla->ic = bu->ic; |
---|
164 | bla->handle = g_strdup(bu->handle); |
---|
165 | bla->realname = g_strdup(bu->fullname); |
---|
166 | |
---|
167 | g_snprintf(buf, sizeof(buf), |
---|
168 | "The user %s (%s) wants to add you to his/her buddy list.", |
---|
169 | bu->handle, bu->fullname); |
---|
170 | |
---|
171 | imcb_ask_with_free(bu->ic, buf, bla, msn_buddy_ask_yes, msn_buddy_ask_no, msn_buddy_ask_free); |
---|
172 | } |
---|
173 | |
---|
174 | void msn_queue_feed(struct msn_data *h, char *bytes, int st) |
---|
175 | { |
---|
176 | h->rxq = g_renew(char, h->rxq, h->rxlen + st); |
---|
177 | memcpy(h->rxq + h->rxlen, bytes, st); |
---|
178 | h->rxlen += st; |
---|
179 | |
---|
180 | if (getenv("BITLBEE_DEBUG")) { |
---|
181 | fprintf(stderr, "\n\x1b[92m<<< "); |
---|
182 | write(2, bytes , st); |
---|
183 | fprintf(stderr, "\x1b[97m"); |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | /* This one handles input from a MSN Messenger server. Both the NS and SB servers usually give |
---|
188 | commands, but sometimes they give additional data (payload). This function tries to handle |
---|
189 | this all in a nice way and send all data to the right places. */ |
---|
190 | |
---|
191 | /* Return values: -1: Read error, abort connection. |
---|
192 | 0: Command reported error; Abort *immediately*. (The connection does not exist anymore) |
---|
193 | 1: OK */ |
---|
194 | |
---|
195 | int msn_handler(struct msn_data *h) |
---|
196 | { |
---|
197 | int st = 1; |
---|
198 | |
---|
199 | while (st) { |
---|
200 | int i; |
---|
201 | |
---|
202 | if (h->msglen == 0) { |
---|
203 | for (i = 0; i < h->rxlen; i++) { |
---|
204 | if (h->rxq[i] == '\r' || h->rxq[i] == '\n') { |
---|
205 | char *cmd_text, **cmd; |
---|
206 | int count; |
---|
207 | |
---|
208 | cmd_text = g_strndup(h->rxq, i); |
---|
209 | cmd = g_strsplit_set(cmd_text, " ", -1); |
---|
210 | count = g_strv_length(cmd); |
---|
211 | |
---|
212 | st = msn_ns_command(h, cmd, count); |
---|
213 | |
---|
214 | g_strfreev(cmd); |
---|
215 | g_free(cmd_text); |
---|
216 | |
---|
217 | /* If the connection broke, don't continue. We don't even exist anymore. */ |
---|
218 | if (!st) { |
---|
219 | return(0); |
---|
220 | } |
---|
221 | |
---|
222 | if (h->msglen) { |
---|
223 | h->cmd_text = g_strndup(h->rxq, i); |
---|
224 | } |
---|
225 | |
---|
226 | /* Skip to the next non-emptyline */ |
---|
227 | while (i < h->rxlen && (h->rxq[i] == '\r' || h->rxq[i] == '\n')) { |
---|
228 | i++; |
---|
229 | } |
---|
230 | |
---|
231 | break; |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | /* If we reached the end of the buffer, there's still an incomplete command there. |
---|
236 | Return and wait for more data. */ |
---|
237 | if (i && i == h->rxlen && h->rxq[i - 1] != '\r' && h->rxq[i - 1] != '\n') { |
---|
238 | break; |
---|
239 | } |
---|
240 | } else { |
---|
241 | char *msg, **cmd; |
---|
242 | int count; |
---|
243 | |
---|
244 | /* Do we have the complete message already? */ |
---|
245 | if (h->msglen > h->rxlen) { |
---|
246 | break; |
---|
247 | } |
---|
248 | |
---|
249 | msg = g_strndup(h->rxq, h->msglen); |
---|
250 | |
---|
251 | cmd = g_strsplit_set(h->cmd_text, " ", -1); |
---|
252 | count = g_strv_length(cmd); |
---|
253 | |
---|
254 | st = msn_ns_message(h, msg, h->msglen, cmd, count); |
---|
255 | |
---|
256 | g_strfreev(cmd); |
---|
257 | g_free(msg); |
---|
258 | g_free(h->cmd_text); |
---|
259 | h->cmd_text = NULL; |
---|
260 | |
---|
261 | if (!st) { |
---|
262 | return(0); |
---|
263 | } |
---|
264 | |
---|
265 | i = h->msglen; |
---|
266 | h->msglen = 0; |
---|
267 | } |
---|
268 | |
---|
269 | /* More data after this block? */ |
---|
270 | if (i < h->rxlen) { |
---|
271 | char *tmp; |
---|
272 | |
---|
273 | tmp = g_memdup(h->rxq + i, h->rxlen - i); |
---|
274 | g_free(h->rxq); |
---|
275 | h->rxq = tmp; |
---|
276 | h->rxlen -= i; |
---|
277 | i = 0; |
---|
278 | } else { |
---|
279 | /* If not, reset the rx queue and get lost. */ |
---|
280 | g_free(h->rxq); |
---|
281 | h->rxq = g_new0(char, 1); |
---|
282 | h->rxlen = 0; |
---|
283 | return(1); |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
287 | return(1); |
---|
288 | } |
---|
289 | |
---|
290 | /* Copied and heavily modified from http://tmsnc.sourceforge.net/chl.c */ |
---|
291 | char *msn_p11_challenge(char *challenge) |
---|
292 | { |
---|
293 | char *output, buf[256]; |
---|
294 | md5_state_t md5c; |
---|
295 | unsigned char md5Hash[16], *newHash; |
---|
296 | unsigned int *md5Parts, *chlStringParts, newHashParts[5]; |
---|
297 | long long nHigh = 0, nLow = 0; |
---|
298 | int i, n; |
---|
299 | |
---|
300 | /* Create the MD5 hash */ |
---|
301 | md5_init(&md5c); |
---|
302 | md5_append(&md5c, (unsigned char *) challenge, strlen(challenge)); |
---|
303 | md5_append(&md5c, (unsigned char *) MSNP11_PROD_KEY, strlen(MSNP11_PROD_KEY)); |
---|
304 | md5_finish(&md5c, md5Hash); |
---|
305 | |
---|
306 | /* Split it into four integers */ |
---|
307 | md5Parts = (unsigned int *) md5Hash; |
---|
308 | for (i = 0; i < 4; i++) { |
---|
309 | md5Parts[i] = GUINT32_TO_LE(md5Parts[i]); |
---|
310 | |
---|
311 | /* & each integer with 0x7FFFFFFF */ |
---|
312 | /* and save one unmodified array for later */ |
---|
313 | newHashParts[i] = md5Parts[i]; |
---|
314 | md5Parts[i] &= 0x7FFFFFFF; |
---|
315 | } |
---|
316 | |
---|
317 | /* make a new string and pad with '0' */ |
---|
318 | n = g_snprintf(buf, sizeof(buf) - 5, "%s%s00000000", challenge, MSNP11_PROD_ID); |
---|
319 | /* truncate at an 8-byte boundary */ |
---|
320 | buf[n &= ~7] = '\0'; |
---|
321 | |
---|
322 | /* split into integers */ |
---|
323 | chlStringParts = (unsigned int *) buf; |
---|
324 | |
---|
325 | /* this is magic */ |
---|
326 | for (i = 0; i < (n / 4) - 1; i += 2) { |
---|
327 | long long temp; |
---|
328 | |
---|
329 | chlStringParts[i] = GUINT32_TO_LE(chlStringParts[i]); |
---|
330 | chlStringParts[i + 1] = GUINT32_TO_LE(chlStringParts[i + 1]); |
---|
331 | |
---|
332 | temp = |
---|
333 | (md5Parts[0] * |
---|
334 | (((0x0E79A9C1 * |
---|
335 | (long long) chlStringParts[i]) % 0x7FFFFFFF) + nHigh) + md5Parts[1]) % 0x7FFFFFFF; |
---|
336 | nHigh = |
---|
337 | (md5Parts[2] * |
---|
338 | (((long long) chlStringParts[i + 1] + temp) % 0x7FFFFFFF) + md5Parts[3]) % 0x7FFFFFFF; |
---|
339 | nLow = nLow + nHigh + temp; |
---|
340 | } |
---|
341 | nHigh = (nHigh + md5Parts[1]) % 0x7FFFFFFF; |
---|
342 | nLow = (nLow + md5Parts[3]) % 0x7FFFFFFF; |
---|
343 | |
---|
344 | newHashParts[0] ^= nHigh; |
---|
345 | newHashParts[1] ^= nLow; |
---|
346 | newHashParts[2] ^= nHigh; |
---|
347 | newHashParts[3] ^= nLow; |
---|
348 | |
---|
349 | /* swap more bytes if big endian */ |
---|
350 | for (i = 0; i < 4; i++) { |
---|
351 | newHashParts[i] = GUINT32_TO_LE(newHashParts[i]); |
---|
352 | } |
---|
353 | |
---|
354 | /* make a string of the parts */ |
---|
355 | newHash = (unsigned char *) newHashParts; |
---|
356 | |
---|
357 | /* convert to hexadecimal */ |
---|
358 | output = g_new(char, 33); |
---|
359 | for (i = 0; i < 16; i++) { |
---|
360 | sprintf(output + i * 2, "%02x", newHash[i]); |
---|
361 | } |
---|
362 | |
---|
363 | return output; |
---|
364 | } |
---|
365 | |
---|
366 | gint msn_domaintree_cmp(gconstpointer a_, gconstpointer b_) |
---|
367 | { |
---|
368 | const char *a = a_, *b = b_; |
---|
369 | gint ret; |
---|
370 | |
---|
371 | if (!(a = strchr(a, '@')) || !(b = strchr(b, '@')) || |
---|
372 | (ret = strcmp(a, b)) == 0) { |
---|
373 | ret = strcmp(a_, b_); |
---|
374 | } |
---|
375 | |
---|
376 | return ret; |
---|
377 | } |
---|
378 | |
---|
379 | struct msn_group *msn_group_by_name(struct im_connection *ic, const char *name) |
---|
380 | { |
---|
381 | struct msn_data *md = ic->proto_data; |
---|
382 | GSList *l; |
---|
383 | |
---|
384 | for (l = md->groups; l; l = l->next) { |
---|
385 | struct msn_group *mg = l->data; |
---|
386 | |
---|
387 | if (g_strcasecmp(mg->name, name) == 0) { |
---|
388 | return mg; |
---|
389 | } |
---|
390 | } |
---|
391 | |
---|
392 | return NULL; |
---|
393 | } |
---|
394 | |
---|
395 | struct msn_group *msn_group_by_id(struct im_connection *ic, const char *id) |
---|
396 | { |
---|
397 | struct msn_data *md = ic->proto_data; |
---|
398 | GSList *l; |
---|
399 | |
---|
400 | for (l = md->groups; l; l = l->next) { |
---|
401 | struct msn_group *mg = l->data; |
---|
402 | |
---|
403 | if (g_strcasecmp(mg->id, id) == 0) { |
---|
404 | return mg; |
---|
405 | } |
---|
406 | } |
---|
407 | |
---|
408 | return NULL; |
---|
409 | } |
---|
410 | |
---|
411 | int msn_ns_set_display_name(struct im_connection *ic, const char *value) |
---|
412 | { |
---|
413 | // TODO, implement this through msn_set_away's method |
---|
414 | return 1; |
---|
415 | } |
---|
416 | |
---|
417 | const char *msn_normalize_handle(const char *handle) |
---|
418 | { |
---|
419 | if (strncmp(handle, "1:", 2) == 0) { |
---|
420 | return handle + 2; |
---|
421 | } else { |
---|
422 | return handle; |
---|
423 | } |
---|
424 | } |
---|