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