]> andersk Git - libfaim.git/blame - aim_im.c
Last few changes left in my inbox.
[libfaim.git] / aim_im.c
CommitLineData
9de3ca7e 1/*
2 * aim_im.c
3 *
4 * The routines for sending/receiving Instant Messages.
5 *
6 */
7
24286d93 8#include <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 */
24286d93 20u_long aim_send_im(struct aim_conn_t *conn, char *destsn, u_int flags, char *msg)
9de3ca7e 21{
22
49c8a2fa 23 int curbyte,i;
9de3ca7e 24 struct command_tx_struct newpacket;
25
26 newpacket.lock = 1; /* lock struct */
27 newpacket.type = 0x02; /* IMs are always family 0x02 */
28 if (conn)
29 newpacket.conn = conn;
30 else
31 newpacket.conn = aim_getconn_type(AIM_CONN_TYPE_BOS);
32
49c8a2fa 33 /*
34 * Its simplest to set this arbitrarily large and waste
35 * space. Precalculating is costly here.
36 */
37 newpacket.commandlen = 1152;
9de3ca7e 38
39 newpacket.data = (char *) calloc(1, newpacket.commandlen);
40
41 curbyte = 0;
49c8a2fa 42 curbyte += aim_putsnac(newpacket.data+curbyte,
43 0x0004, 0x0006, 0x0000, aim_snac_nextid);
9de3ca7e 44
49c8a2fa 45 /*
46 * Generate a random message cookie
47 */
48 for (i=0;i<8;i++)
49 curbyte += aimutil_put8(newpacket.data+curbyte, (u_char) random());
9de3ca7e 50
49c8a2fa 51 /*
52 * Channel ID
53 */
9de3ca7e 54 curbyte += aimutil_put16(newpacket.data+curbyte,0x0001);
9de3ca7e 55
49c8a2fa 56 /*
57 * Destination SN (prepended with byte length)
58 */
59 curbyte += aimutil_put8(newpacket.data+curbyte,strlen(destsn));
60 curbyte += aimutil_putstr(newpacket.data+curbyte, destsn, strlen(destsn));
9de3ca7e 61
49c8a2fa 62 /*
63 * metaTLV start.
64 */
65 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0002);
66 curbyte += aimutil_put16(newpacket.data+curbyte, strlen(msg) + 0x0d);
9de3ca7e 67
49c8a2fa 68 /*
69 * Flag data?
70 */
71 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0501);
72 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0001);
73 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0101);
74 curbyte += aimutil_put8 (newpacket.data+curbyte, 0x01);
9de3ca7e 75
49c8a2fa 76 /*
77 * Message block length.
78 */
79 curbyte += aimutil_put16(newpacket.data+curbyte, strlen(msg) + 0x04);
9de3ca7e 80
49c8a2fa 81 /*
82 * Character set data?
83 */
84 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
85 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
9de3ca7e 86
49c8a2fa 87 /*
88 * Message. Not terminated.
89 */
90 curbyte += aimutil_putstr(newpacket.data+curbyte,msg, strlen(msg));
9de3ca7e 91
49c8a2fa 92 /*
93 * Set the Request Acknowledge flag.
94 */
9de3ca7e 95 if (flags & AIM_IMFLAGS_ACK)
96 {
49c8a2fa 97 curbyte += aimutil_put16(newpacket.data+curbyte,0x0003);
98 curbyte += aimutil_put16(newpacket.data+curbyte,0x0000);
9de3ca7e 99 }
49c8a2fa 100
101 /*
102 * Set the Autoresponse flag.
103 */
9de3ca7e 104 if (flags & AIM_IMFLAGS_AWAY)
105 {
49c8a2fa 106 curbyte += aimutil_put16(newpacket.data+curbyte,0x0004);
107 curbyte += aimutil_put16(newpacket.data+curbyte,0x0000);
9de3ca7e 108 }
49c8a2fa 109
110 newpacket.commandlen = curbyte;
9de3ca7e 111
112 aim_tx_enqueue(&newpacket);
49c8a2fa 113
9de3ca7e 114#ifdef USE_SNAC_FOR_IMS
115 {
116 struct aim_snac_t snac;
117
118 snac.id = aim_snac_nextid;
119 snac.family = 0x0004;
120 snac.type = 0x0006;
121 snac.flags = 0x0000;
122
123 snac.data = malloc(strlen(destsn)+1);
124 memcpy(snac.data, destsn, strlen(destsn)+1);
125
126 aim_newsnac(&snac);
127 }
128
129 aim_cleansnacs(60); /* clean out all SNACs over 60sec old */
130#endif
131
132 return (aim_snac_nextid++);
133}
134
49c8a2fa 135/*
136 * It can easily be said that parsing ICBMs is THE single
137 * most difficult thing to do in the in AIM protocol. In
138 * fact, I think I just did say that.
139 *
140 * Below is the best damned solution I've come up with
141 * over the past sixteen months of battling with it. This
142 * can parse both away and normal messages from every client
143 * I have access to. Its not fast, its not clean. But it works.
144 *
145 * We should also support at least minimal parsing of
146 * Channel 2, so that we can at least know the name of the
147 * room we're invited to, but obviously can't attend...
148 *
149 */
9de3ca7e 150int aim_parse_incoming_im_middle(struct command_rx_struct *command)
151{
49c8a2fa 152 struct aim_userinfo_s userinfo;
153 u_int i = 0, j = 0, y = 0, z = 0;
9de3ca7e 154 char *msg = NULL;
24286d93 155 u_int icbmflags = 0;
9de3ca7e 156 rxcallback_t userfunc = NULL;
49c8a2fa 157 u_char cookie[8];
158 int channel;
159 struct aim_tlvlist_t *tlvlist;
160 struct aim_tlv_t *msgblocktlv, *tmptlv;
161 u_char *msgblock;
162 u_short wastebits;
163 u_short flag1,flag2;
164
165 memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
9de3ca7e 166
49c8a2fa 167 i = 10; /* Skip SNAC header */
168
9de3ca7e 169 /*
49c8a2fa 170 * Read ICBM Cookie. And throw away.
9de3ca7e 171 */
49c8a2fa 172 for (z=0; z<8; z++,i++)
173 cookie[z] = command->data[i];
9de3ca7e 174
49c8a2fa 175 /*
176 * Channel ID.
177 *
178 * Channel 0x0001 is the message channel. There are
179 * other channels for things called "rendevous"
180 * which represent chat and some of the other new
181 * features of AIM2/3/3.5. We only support
182 * standard messages; those on channel 0x0001.
183 */
184 channel = aimutil_get16(command->data+i);
9de3ca7e 185 i += 2;
49c8a2fa 186 if (channel != 0x0001)
9de3ca7e 187 {
49c8a2fa 188 printf("faim: icbm: ICBM received on an unsupported channel. Ignoring.\n (chan = %04x)", channel);
189 return 1;
9de3ca7e 190 }
191
49c8a2fa 192 /*
193 * Source screen name.
194 */
195 memcpy(userinfo.sn, command->data+i+1, (int)command->data[i]);
196 userinfo.sn[(int)command->data[i]] = '\0';
197 i += 1 + (int)command->data[i];
9de3ca7e 198
49c8a2fa 199 /*
200 * Unknown bits.
201 */
202 wastebits = aimutil_get16(command->data+i);
203 i += 2;
204 wastebits = aimutil_get16(command->data+i);
9de3ca7e 205 i += 2;
206
49c8a2fa 207 /*
208 * Read block of TLVs. All further data is derived
209 * from what is parsed here.
210 */
211 tlvlist = aim_readtlvchain(command->data+i, command->commandlen-i);
9de3ca7e 212
49c8a2fa 213 /*
214 * Check Autoresponse status. If it is an autoresponse,
215 * it will contain a second type 0x0004 TLV, with zero length.
216 */
217 if (aim_gettlv(tlvlist, 0x0004, 2))
24286d93 218 icbmflags |= AIM_IMFLAGS_AWAY;
219
220 /*
221 * Check Ack Request status.
222 */
223 if (aim_gettlv(tlvlist, 0x0003, 2))
224 icbmflags |= AIM_IMFLAGS_ACK;
9de3ca7e 225
49c8a2fa 226 /*
227 * Extract the various pieces of the userinfo struct.
228 */
229 /* Class. */
230 if ((tmptlv = aim_gettlv(tlvlist, 0x0001, 1)))
231 userinfo.class = aimutil_get16(tmptlv->value);
232 /* Member-since date. */
233 if ((tmptlv = aim_gettlv(tlvlist, 0x0002, 1)))
234 {
235 /* If this is larger than 4, its probably the message block, skip */
236 if (tmptlv->length <= 4)
237 userinfo.membersince = aimutil_get32(tmptlv->value);
238 }
239 /* On-since date */
240 if ((tmptlv = aim_gettlv(tlvlist, 0x0003, 1)))
241 userinfo.onlinesince = aimutil_get32(tmptlv->value);
242 /* Idle-time */
243 if ((tmptlv = aim_gettlv(tlvlist, 0x0004, 1)))
244 userinfo.idletime = aimutil_get16(tmptlv->value);
245 /* Session Length (AIM) */
246 if ((tmptlv = aim_gettlv(tlvlist, 0x000f, 1)))
247 userinfo.sessionlen = aimutil_get16(tmptlv->value);
248 /* Session Length (AOL) */
249 if ((tmptlv = aim_gettlv(tlvlist, 0x0010, 1)))
250 userinfo.sessionlen = aimutil_get16(tmptlv->value);
9de3ca7e 251
49c8a2fa 252 /*
253 * Message block.
254 *
255 * XXX: Will the msgblock always be the second 0x0002?
256 */
257 msgblocktlv = aim_gettlv(tlvlist, 0x0002, 1);
258 if (!msgblocktlv)
9de3ca7e 259 {
49c8a2fa 260 printf("faim: icbm: major error! no message block TLV found!\n");
261 aim_freetlvchain(&tlvlist);
9de3ca7e 262 }
263
49c8a2fa 264 /*
265 * Extracting the message from the unknown cruft.
266 *
267 * This is a bit messy, and I'm not really qualified,
268 * even as the author, to comment on it. At least
269 * its not as bad as a while loop shooting into infinity.
270 *
271 * "Do you believe in magic?"
272 *
273 */
274 msgblock = msgblocktlv->value;
275 j = 0;
276
277 wastebits = aimutil_get8(msgblock+j++);
278 wastebits = aimutil_get8(msgblock+j++);
9de3ca7e 279
49c8a2fa 280 y = aimutil_get16(msgblock+j);
281 j += 2;
282 for (z = 0; z < y; z++)
283 wastebits = aimutil_get8(msgblock+j++);
284 wastebits = aimutil_get8(msgblock+j++);
285 wastebits = aimutil_get8(msgblock+j++);
286
287 /*
288 * Message string length, including flag words.
289 */
290 i = aimutil_get16(msgblock+j);
291 j += 2;
9de3ca7e 292
49c8a2fa 293 /*
294 * Flag words.
295 *
296 * Its rumored that these can kick in some funky
297 * 16bit-wide char stuff that used to really kill
298 * libfaim. Hopefully the latter is no longer true.
299 *
300 * Though someone should investiagte the former.
301 *
302 */
303 flag1 = aimutil_get16(msgblock+j);
304 j += 2;
305 flag2 = aimutil_get16(msgblock+j);
306 j += 2;
9de3ca7e 307
49c8a2fa 308 if (flag1 || flag2)
309 printf("faim: icbm: **warning: encoding flags are being used! {%04x, %04x}\n", flag1, flag2);
9de3ca7e 310
49c8a2fa 311 /*
312 * Message string.
313 */
314 i -= 4;
315 msg = (char *)malloc(i+1);
316 memcpy(msg, msgblock+j, i);
317 msg[i] = '\0';
9de3ca7e 318
49c8a2fa 319 /*
320 * Free up the TLV chain.
321 */
322 aim_freetlvchain(&tlvlist);
323
324 /*
325 * Call client.
326 */
327 userfunc = aim_callhandler(command->conn, 0x0004, 0x0007);
9de3ca7e 328 if (userfunc)
24286d93 329 i = userfunc(command, &userinfo, msg, icbmflags, flag1, flag2);
9de3ca7e 330 else
331 i = 0;
332
333 free(msg);
334
49c8a2fa 335 return 1;
336}
337
338/*
339 * Not real sure what this does, nor does anyone I've talk to.
340 *
341 * Didn't use to send it. But now I think it might be a good
342 * idea.
343 *
344 */
345u_long aim_seticbmparam(struct aim_conn_t *conn)
346{
347 struct command_tx_struct newpacket;
348 int curbyte;
349
350 newpacket.lock = 1;
351 if (conn)
352 newpacket.conn = conn;
353 else
354 newpacket.conn = aim_getconn_type(AIM_CONN_TYPE_BOS);
355 newpacket.type = 0x02;
356
357 newpacket.commandlen = 10 + 16;
358 newpacket.data = (u_char *) malloc (newpacket.commandlen);
359
360 curbyte = aim_putsnac(newpacket.data, 0x0004, 0x0002, 0x0000, aim_snac_nextid);
361 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
362 curbyte += aimutil_put32(newpacket.data+curbyte, 0x00000003);
363 curbyte += aimutil_put8(newpacket.data+curbyte, 0x1f);
364 curbyte += aimutil_put8(newpacket.data+curbyte, 0x40);
365 curbyte += aimutil_put8(newpacket.data+curbyte, 0x03);
366 curbyte += aimutil_put8(newpacket.data+curbyte, 0xe7);
367 curbyte += aimutil_put8(newpacket.data+curbyte, 0x03);
368 curbyte += aimutil_put8(newpacket.data+curbyte, 0xe7);
369 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
370 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
371
372 aim_tx_enqueue(&newpacket);
373
374 return (aim_snac_nextid++);
9de3ca7e 375}
This page took 0.0999139999999999 seconds and 5 git commands to generate.