]> andersk Git - libfaim.git/blame - aim_im.c
Added stubs for those capability TLVs in userinfo.
[libfaim.git] / aim_im.c
CommitLineData
9de3ca7e 1/*
2 * aim_im.c
3 *
4 * The routines for sending/receiving Instant Messages.
5 *
6 */
7
a25832e6 8#include <faim/aim.h>
9de3ca7e 9
10/*
11 * Send an ICBM (instant message).
12 *
13 *
14 * Possible flags:
15 * AIM_IMFLAGS_AWAY -- Marks the message as an autoresponse
16 * AIM_IMFLAGS_ACK -- Requests that the server send an ack
17 * when the message is received (of type 0x0004/0x000c)
18 *
9de3ca7e 19 */
a25832e6 20u_long aim_send_im(struct aim_session_t *sess,
21 struct aim_conn_t *conn,
22 char *destsn, u_int flags, char *msg)
9de3ca7e 23{
24
49c8a2fa 25 int curbyte,i;
9de3ca7e 26 struct command_tx_struct newpacket;
27
28 newpacket.lock = 1; /* lock struct */
29 newpacket.type = 0x02; /* IMs are always family 0x02 */
30 if (conn)
31 newpacket.conn = conn;
32 else
a25832e6 33 newpacket.conn = aim_getconn_type(sess, AIM_CONN_TYPE_BOS);
9de3ca7e 34
49c8a2fa 35 /*
36 * Its simplest to set this arbitrarily large and waste
37 * space. Precalculating is costly here.
38 */
39 newpacket.commandlen = 1152;
9de3ca7e 40
a25832e6 41 newpacket.data = (u_char *) calloc(1, newpacket.commandlen);
9de3ca7e 42
43 curbyte = 0;
49c8a2fa 44 curbyte += aim_putsnac(newpacket.data+curbyte,
a25832e6 45 0x0004, 0x0006, 0x0000, sess->snac_nextid);
9de3ca7e 46
49c8a2fa 47 /*
48 * Generate a random message cookie
a25832e6 49 *
50 * We could cache these like we do SNAC IDs. (In fact, it
51 * might be a good idea.) In the message error functions,
52 * the 8byte message cookie is returned as well as the
53 * SNAC ID.
54 *
49c8a2fa 55 */
56 for (i=0;i<8;i++)
57 curbyte += aimutil_put8(newpacket.data+curbyte, (u_char) random());
9de3ca7e 58
49c8a2fa 59 /*
60 * Channel ID
61 */
9de3ca7e 62 curbyte += aimutil_put16(newpacket.data+curbyte,0x0001);
9de3ca7e 63
49c8a2fa 64 /*
65 * Destination SN (prepended with byte length)
66 */
67 curbyte += aimutil_put8(newpacket.data+curbyte,strlen(destsn));
68 curbyte += aimutil_putstr(newpacket.data+curbyte, destsn, strlen(destsn));
9de3ca7e 69
49c8a2fa 70 /*
71 * metaTLV start.
72 */
73 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0002);
74 curbyte += aimutil_put16(newpacket.data+curbyte, strlen(msg) + 0x0d);
9de3ca7e 75
49c8a2fa 76 /*
77 * Flag data?
78 */
79 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0501);
80 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0001);
81 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0101);
82 curbyte += aimutil_put8 (newpacket.data+curbyte, 0x01);
9de3ca7e 83
49c8a2fa 84 /*
85 * Message block length.
86 */
87 curbyte += aimutil_put16(newpacket.data+curbyte, strlen(msg) + 0x04);
9de3ca7e 88
49c8a2fa 89 /*
90 * Character set data?
91 */
92 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
93 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
9de3ca7e 94
49c8a2fa 95 /*
96 * Message. Not terminated.
97 */
98 curbyte += aimutil_putstr(newpacket.data+curbyte,msg, strlen(msg));
9de3ca7e 99
49c8a2fa 100 /*
101 * Set the Request Acknowledge flag.
102 */
9de3ca7e 103 if (flags & AIM_IMFLAGS_ACK)
104 {
49c8a2fa 105 curbyte += aimutil_put16(newpacket.data+curbyte,0x0003);
106 curbyte += aimutil_put16(newpacket.data+curbyte,0x0000);
9de3ca7e 107 }
49c8a2fa 108
109 /*
110 * Set the Autoresponse flag.
111 */
9de3ca7e 112 if (flags & AIM_IMFLAGS_AWAY)
113 {
49c8a2fa 114 curbyte += aimutil_put16(newpacket.data+curbyte,0x0004);
115 curbyte += aimutil_put16(newpacket.data+curbyte,0x0000);
9de3ca7e 116 }
49c8a2fa 117
118 newpacket.commandlen = curbyte;
9de3ca7e 119
a25832e6 120 aim_tx_enqueue(sess, &newpacket);
49c8a2fa 121
9de3ca7e 122#ifdef USE_SNAC_FOR_IMS
123 {
124 struct aim_snac_t snac;
125
a25832e6 126 snac.id = sess->snac_nextid;
9de3ca7e 127 snac.family = 0x0004;
128 snac.type = 0x0006;
129 snac.flags = 0x0000;
130
131 snac.data = malloc(strlen(destsn)+1);
132 memcpy(snac.data, destsn, strlen(destsn)+1);
133
a25832e6 134 aim_newsnac(sess, &snac);
9de3ca7e 135 }
136
a25832e6 137 aim_cleansnacs(sess, 60); /* clean out all SNACs over 60sec old */
9de3ca7e 138#endif
139
a25832e6 140 return (sess->snac_nextid++);
9de3ca7e 141}
142
49c8a2fa 143/*
144 * It can easily be said that parsing ICBMs is THE single
145 * most difficult thing to do in the in AIM protocol. In
146 * fact, I think I just did say that.
147 *
148 * Below is the best damned solution I've come up with
149 * over the past sixteen months of battling with it. This
150 * can parse both away and normal messages from every client
151 * I have access to. Its not fast, its not clean. But it works.
152 *
153 * We should also support at least minimal parsing of
154 * Channel 2, so that we can at least know the name of the
155 * room we're invited to, but obviously can't attend...
156 *
157 */
a25832e6 158int aim_parse_incoming_im_middle(struct aim_session_t *sess,
159 struct command_rx_struct *command)
9de3ca7e 160{
26af6789 161 u_int i = 0,z;
9de3ca7e 162 rxcallback_t userfunc = NULL;
49c8a2fa 163 u_char cookie[8];
164 int channel;
165 struct aim_tlvlist_t *tlvlist;
26af6789 166 struct aim_userinfo_s userinfo;
49c8a2fa 167 u_short wastebits;
49c8a2fa 168
95d7332a 169 memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
170
49c8a2fa 171 i = 10; /* Skip SNAC header */
172
9de3ca7e 173 /*
49c8a2fa 174 * Read ICBM Cookie. And throw away.
9de3ca7e 175 */
49c8a2fa 176 for (z=0; z<8; z++,i++)
177 cookie[z] = command->data[i];
9de3ca7e 178
49c8a2fa 179 /*
180 * Channel ID.
181 *
182 * Channel 0x0001 is the message channel. There are
183 * other channels for things called "rendevous"
184 * which represent chat and some of the other new
26af6789 185 * features of AIM2/3/3.5.
186 *
187 * Channel 0x0002 is the Rendevous channel, which
188 * is where Chat Invitiations come from.
189 *
49c8a2fa 190 */
191 channel = aimutil_get16(command->data+i);
9de3ca7e 192 i += 2;
26af6789 193
194 /*
195 *
196 */
197 if ((channel != 0x01) && (channel != 0x02))
9de3ca7e 198 {
49c8a2fa 199 printf("faim: icbm: ICBM received on an unsupported channel. Ignoring.\n (chan = %04x)", channel);
200 return 1;
9de3ca7e 201 }
202
49c8a2fa 203 /*
204 * Source screen name.
205 */
206 memcpy(userinfo.sn, command->data+i+1, (int)command->data[i]);
207 userinfo.sn[(int)command->data[i]] = '\0';
208 i += 1 + (int)command->data[i];
95d7332a 209
49c8a2fa 210 /*
26af6789 211 * Warning Level
49c8a2fa 212 */
26af6789 213 userinfo.warnlevel = aimutil_get16(command->data+i); /* guess */
49c8a2fa 214 i += 2;
26af6789 215
216 /*
217 * Number of TLVs that follow. Not needed.
218 */
49c8a2fa 219 wastebits = aimutil_get16(command->data+i);
9de3ca7e 220 i += 2;
26af6789 221
49c8a2fa 222 /*
223 * Read block of TLVs. All further data is derived
224 * from what is parsed here.
225 */
226 tlvlist = aim_readtlvchain(command->data+i, command->commandlen-i);
9de3ca7e 227
49c8a2fa 228 /*
26af6789 229 * From here on, its depends on what channel we're on.
49c8a2fa 230 */
26af6789 231 if (channel == 1)
49c8a2fa 232 {
26af6789 233 u_int j = 0, y = 0, z = 0;
234 char *msg = NULL;
235 u_int icbmflags = 0;
236 struct aim_tlv_t *msgblocktlv, *tmptlv;
237 u_char *msgblock;
238 u_short flag1,flag2;
95d7332a 239
26af6789 240 /*
241 * Check Autoresponse status. If it is an autoresponse,
242 * it will contain a second type 0x0004 TLV, with zero length.
243 */
244 if (aim_gettlv(tlvlist, 0x0004, 2))
245 icbmflags |= AIM_IMFLAGS_AWAY;
246
247 /*
248 * Check Ack Request status.
249 */
250 if (aim_gettlv(tlvlist, 0x0003, 2))
251 icbmflags |= AIM_IMFLAGS_ACK;
252
253 /*
254 * Extract the various pieces of the userinfo struct.
255 */
256 /* Class. */
257 if ((tmptlv = aim_gettlv(tlvlist, 0x0001, 1)))
258 userinfo.class = aimutil_get16(tmptlv->value);
259 /* Member-since date. */
260 if ((tmptlv = aim_gettlv(tlvlist, 0x0002, 1)))
261 {
262 /* If this is larger than 4, its probably the message block, skip */
263 if (tmptlv->length <= 4)
264 userinfo.membersince = aimutil_get32(tmptlv->value);
265 }
266 /* On-since date */
267 if ((tmptlv = aim_gettlv(tlvlist, 0x0003, 1)))
268 userinfo.onlinesince = aimutil_get32(tmptlv->value);
269 /* Idle-time */
270 if ((tmptlv = aim_gettlv(tlvlist, 0x0004, 1)))
271 userinfo.idletime = aimutil_get16(tmptlv->value);
272 /* Session Length (AIM) */
273 if ((tmptlv = aim_gettlv(tlvlist, 0x000f, 1)))
274 userinfo.sessionlen = aimutil_get16(tmptlv->value);
275 /* Session Length (AOL) */
276 if ((tmptlv = aim_gettlv(tlvlist, 0x0010, 1)))
277 userinfo.sessionlen = aimutil_get16(tmptlv->value);
278
279 /*
280 * Message block.
281 *
282 * XXX: Will the msgblock always be the second 0x0002?
283 */
284 msgblocktlv = aim_gettlv(tlvlist, 0x0002, 1);
285 if (!msgblocktlv)
286 {
287 printf("faim: icbm: major error! no message block TLV found!\n");
288 aim_freetlvchain(&tlvlist);
289 }
290
291 /*
292 * Extracting the message from the unknown cruft.
293 *
294 * This is a bit messy, and I'm not really qualified,
295 * even as the author, to comment on it. At least
296 * its not as bad as a while loop shooting into infinity.
297 *
298 * "Do you believe in magic?"
299 *
300 */
301 msgblock = msgblocktlv->value;
302 j = 0;
303
304 wastebits = aimutil_get8(msgblock+j++);
305 wastebits = aimutil_get8(msgblock+j++);
306
307 y = aimutil_get16(msgblock+j);
308 j += 2;
309 for (z = 0; z < y; z++)
310 wastebits = aimutil_get8(msgblock+j++);
311 wastebits = aimutil_get8(msgblock+j++);
312 wastebits = aimutil_get8(msgblock+j++);
313
314 /*
315 * Message string length, including flag words.
316 */
317 i = aimutil_get16(msgblock+j);
318 j += 2;
319
320 /*
321 * Flag words.
322 *
323 * Its rumored that these can kick in some funky
324 * 16bit-wide char stuff that used to really kill
325 * libfaim. Hopefully the latter is no longer true.
326 *
327 * Though someone should investiagte the former.
328 *
329 */
330 flag1 = aimutil_get16(msgblock+j);
331 j += 2;
332 flag2 = aimutil_get16(msgblock+j);
333 j += 2;
334
335 if (flag1 || flag2)
336 printf("faim: icbm: **warning: encoding flags are being used! {%04x, %04x}\n", flag1, flag2);
337
338 /*
339 * Message string.
340 */
341 i -= 4;
342 msg = (char *)malloc(i+1);
343 memcpy(msg, msgblock+j, i);
344 msg[i] = '\0';
345
346 /*
347 * Call client.
348 */
349 userfunc = aim_callhandler(command->conn, 0x0004, 0x0007);
350 if (userfunc)
351 i = userfunc(sess, command, channel, &userinfo, msg, icbmflags, flag1, flag2);
352 else
353 i = 0;
354
355 free(msg);
49c8a2fa 356 }
26af6789 357 else if (channel == 0x0002)
9de3ca7e 358 {
26af6789 359 struct aim_tlv_t *block1;
360 struct aim_tlvlist_t *list2;
361 struct aim_tlv_t *tmptlv;
362 int a;
363 u_short exchange,instance;
364 char *roomname,*msg,*encoding,*lang;
365
366 /* Class. */
367 if ((tmptlv = aim_gettlv(tlvlist, 0x0001, 1)))
368 userinfo.class = aimutil_get16(tmptlv->value);
369 /* On-since date */
370 if ((tmptlv = aim_gettlv(tlvlist, 0x0003, 1)))
371 userinfo.onlinesince = aimutil_get32(tmptlv->value);
372 /* Idle-time */
373 if ((tmptlv = aim_gettlv(tlvlist, 0x0004, 1)))
374 userinfo.idletime = aimutil_get16(tmptlv->value);
375 /* Session Length (AIM) */
376 if ((tmptlv = aim_gettlv(tlvlist, 0x000f, 1)))
377 userinfo.sessionlen = aimutil_get16(tmptlv->value);
378 /* Session Length (AOL) */
379 if ((tmptlv = aim_gettlv(tlvlist, 0x0010, 1)))
380 userinfo.sessionlen = aimutil_get16(tmptlv->value);
381
382 /*
383 * There's another block of TLVs embedded in the type 5 here.
384 */
385 block1 = aim_gettlv(tlvlist, 0x0005, 1);
386 if (!block1)
387 return 1; /* major problem */
388
389 a = 0x1a; /* skip -- not sure what this information is! */
390
391 list2 = aim_readtlvchain(block1->value+a, block1->length-a);
392 if (aim_gettlv(list2, 0x2711, 1))
393 {
394 struct aim_tlv_t *name;
395 int len;
396
397 name = aim_gettlv(list2, 0x2711, 1);
398
399 exchange = aimutil_get16(name->value+0);
400
401 len = aimutil_get16(name->value+2);
402 roomname = (char *)malloc(len+1);
403 memcpy(roomname, name->value+3, len);
404 roomname[len] = '\0';
405
406 instance = aimutil_get16(name->value+3+len);
407 }
408
409 if (aim_gettlv(list2, 0x000c, 1))
410 msg = aim_gettlv_str(list2, 0x000c, 1);
411
412 if (aim_gettlv(list2, 0x000d, 1))
413 encoding = aim_gettlv_str(list2, 0x000d, 1);
49c8a2fa 414
26af6789 415 if (aim_gettlv(list2, 0x000e, 1))
416 lang = aim_gettlv_str(list2, 0x000e, 1);
417
418 /*
419 * Call client.
420 */
421 userfunc = aim_callhandler(command->conn, 0x0004, 0x0007);
422 if (userfunc)
423 i = userfunc(sess, command, channel, &userinfo, roomname, msg, encoding+1, lang+1, exchange, instance);
424 else
425 i = 0;
426
427 free(roomname);
428 free(msg);
429 free(encoding);
430 free(lang);
431 aim_freetlvchain(&list2);
432 }
9de3ca7e 433
49c8a2fa 434 /*
435 * Free up the TLV chain.
436 */
437 aim_freetlvchain(&tlvlist);
26af6789 438
49c8a2fa 439
26af6789 440 return i;
49c8a2fa 441}
442
443/*
444 * Not real sure what this does, nor does anyone I've talk to.
445 *
446 * Didn't use to send it. But now I think it might be a good
447 * idea.
448 *
449 */
a25832e6 450u_long aim_seticbmparam(struct aim_session_t *sess,
451 struct aim_conn_t *conn)
49c8a2fa 452{
453 struct command_tx_struct newpacket;
454 int curbyte;
455
456 newpacket.lock = 1;
457 if (conn)
458 newpacket.conn = conn;
459 else
a25832e6 460 newpacket.conn = aim_getconn_type(sess, AIM_CONN_TYPE_BOS);
49c8a2fa 461 newpacket.type = 0x02;
462
463 newpacket.commandlen = 10 + 16;
464 newpacket.data = (u_char *) malloc (newpacket.commandlen);
465
a25832e6 466 curbyte = aim_putsnac(newpacket.data, 0x0004, 0x0002, 0x0000, sess->snac_nextid);
49c8a2fa 467 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
468 curbyte += aimutil_put32(newpacket.data+curbyte, 0x00000003);
469 curbyte += aimutil_put8(newpacket.data+curbyte, 0x1f);
470 curbyte += aimutil_put8(newpacket.data+curbyte, 0x40);
471 curbyte += aimutil_put8(newpacket.data+curbyte, 0x03);
472 curbyte += aimutil_put8(newpacket.data+curbyte, 0xe7);
473 curbyte += aimutil_put8(newpacket.data+curbyte, 0x03);
474 curbyte += aimutil_put8(newpacket.data+curbyte, 0xe7);
475 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
476 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
477
a25832e6 478 aim_tx_enqueue(sess, &newpacket);
479
480 return (sess->snac_nextid++);
481}
482
483int aim_parse_msgerror_middle(struct aim_session_t *sess,
484 struct command_rx_struct *command)
485{
486 u_long snacid = 0x000000000;
487 struct aim_snac_t *snac = NULL;
488 int ret = 0;
489 rxcallback_t userfunc = NULL;
490
491 /*
492 * Get SNAC from packet and look it up
493 * the list of unrepliedto/outstanding
494 * SNACs.
495 *
496 * After its looked up, the SN that the
497 * message should've gone to will be
498 * in the ->data element of the snac struct.
499 *
500 */
501 snacid = aimutil_get32(command->data+6);
502 snac = aim_remsnac(sess, snacid);
503
504 if (!snac)
505 {
506 printf("faim: msgerr: got an ICBM-failed error on an unknown SNAC ID! (%08lx)\n", snacid);
507 }
508
509 /*
510 * Call client.
511 */
512 userfunc = aim_callhandler(command->conn, 0x0004, 0x0001);
513 if (userfunc)
514 ret = userfunc(sess, command, (snac)?snac->data:"(UNKNOWN)");
515 else
516 ret = 0;
517
518 free(snac->data);
519 free(snac);
49c8a2fa 520
a25832e6 521 return ret;
9de3ca7e 522}
This page took 2.687225 seconds and 5 git commands to generate.