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