4 * The routines for sending/receiving Instant Messages.
11 * Send an ICBM (instant message).
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)
20 u_long aim_send_im(struct aim_conn_t *conn, char *destsn, int flags, char *msg)
24 struct command_tx_struct newpacket;
26 newpacket.lock = 1; /* lock struct */
27 newpacket.type = 0x02; /* IMs are always family 0x02 */
29 newpacket.conn = conn;
31 newpacket.conn = aim_getconn_type(AIM_CONN_TYPE_BOS);
34 * Its simplest to set this arbitrarily large and waste
35 * space. Precalculating is costly here.
37 newpacket.commandlen = 1152;
39 newpacket.data = (char *) calloc(1, newpacket.commandlen);
42 curbyte += aim_putsnac(newpacket.data+curbyte,
43 0x0004, 0x0006, 0x0000, aim_snac_nextid);
46 * Generate a random message cookie
49 curbyte += aimutil_put8(newpacket.data+curbyte, (u_char) random());
54 curbyte += aimutil_put16(newpacket.data+curbyte,0x0001);
57 * Destination SN (prepended with byte length)
59 curbyte += aimutil_put8(newpacket.data+curbyte,strlen(destsn));
60 curbyte += aimutil_putstr(newpacket.data+curbyte, destsn, strlen(destsn));
65 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0002);
66 curbyte += aimutil_put16(newpacket.data+curbyte, strlen(msg) + 0x0d);
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);
77 * Message block length.
79 curbyte += aimutil_put16(newpacket.data+curbyte, strlen(msg) + 0x04);
84 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
85 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
88 * Message. Not terminated.
90 curbyte += aimutil_putstr(newpacket.data+curbyte,msg, strlen(msg));
93 * Set the Request Acknowledge flag.
95 if (flags & AIM_IMFLAGS_ACK)
97 curbyte += aimutil_put16(newpacket.data+curbyte,0x0003);
98 curbyte += aimutil_put16(newpacket.data+curbyte,0x0000);
102 * Set the Autoresponse flag.
104 if (flags & AIM_IMFLAGS_AWAY)
106 curbyte += aimutil_put16(newpacket.data+curbyte,0x0004);
107 curbyte += aimutil_put16(newpacket.data+curbyte,0x0000);
110 newpacket.commandlen = curbyte;
112 aim_tx_enqueue(&newpacket);
114 #ifdef USE_SNAC_FOR_IMS
116 struct aim_snac_t snac;
118 snac.id = aim_snac_nextid;
119 snac.family = 0x0004;
123 snac.data = malloc(strlen(destsn)+1);
124 memcpy(snac.data, destsn, strlen(destsn)+1);
129 aim_cleansnacs(60); /* clean out all SNACs over 60sec old */
132 return (aim_snac_nextid++);
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.
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.
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...
150 int aim_parse_incoming_im_middle(struct command_rx_struct *command)
152 struct aim_userinfo_s userinfo;
153 u_int i = 0, j = 0, y = 0, z = 0;
155 int isautoresponse = 0;
156 rxcallback_t userfunc = NULL;
159 struct aim_tlvlist_t *tlvlist;
160 struct aim_tlv_t *msgblocktlv, *tmptlv;
165 memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
167 i = 10; /* Skip SNAC header */
170 * Read ICBM Cookie. And throw away.
172 for (z=0; z<8; z++,i++)
173 cookie[z] = command->data[i];
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.
184 channel = aimutil_get16(command->data+i);
186 if (channel != 0x0001)
188 printf("faim: icbm: ICBM received on an unsupported channel. Ignoring.\n (chan = %04x)", channel);
193 * Source screen name.
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];
202 wastebits = aimutil_get16(command->data+i);
204 wastebits = aimutil_get16(command->data+i);
208 * Read block of TLVs. All further data is derived
209 * from what is parsed here.
211 tlvlist = aim_readtlvchain(command->data+i, command->commandlen-i);
214 * Check Autoresponse status. If it is an autoresponse,
215 * it will contain a second type 0x0004 TLV, with zero length.
217 if (aim_gettlv(tlvlist, 0x0004, 2))
221 * Extract the various pieces of the userinfo struct.
224 if ((tmptlv = aim_gettlv(tlvlist, 0x0001, 1)))
225 userinfo.class = aimutil_get16(tmptlv->value);
226 /* Member-since date. */
227 if ((tmptlv = aim_gettlv(tlvlist, 0x0002, 1)))
229 /* If this is larger than 4, its probably the message block, skip */
230 if (tmptlv->length <= 4)
231 userinfo.membersince = aimutil_get32(tmptlv->value);
234 if ((tmptlv = aim_gettlv(tlvlist, 0x0003, 1)))
235 userinfo.onlinesince = aimutil_get32(tmptlv->value);
237 if ((tmptlv = aim_gettlv(tlvlist, 0x0004, 1)))
238 userinfo.idletime = aimutil_get16(tmptlv->value);
239 /* Session Length (AIM) */
240 if ((tmptlv = aim_gettlv(tlvlist, 0x000f, 1)))
241 userinfo.sessionlen = aimutil_get16(tmptlv->value);
242 /* Session Length (AOL) */
243 if ((tmptlv = aim_gettlv(tlvlist, 0x0010, 1)))
244 userinfo.sessionlen = aimutil_get16(tmptlv->value);
249 * XXX: Will the msgblock always be the second 0x0002?
251 msgblocktlv = aim_gettlv(tlvlist, 0x0002, 1);
254 printf("faim: icbm: major error! no message block TLV found!\n");
255 aim_freetlvchain(&tlvlist);
259 * Extracting the message from the unknown cruft.
261 * This is a bit messy, and I'm not really qualified,
262 * even as the author, to comment on it. At least
263 * its not as bad as a while loop shooting into infinity.
265 * "Do you believe in magic?"
268 msgblock = msgblocktlv->value;
271 wastebits = aimutil_get8(msgblock+j++);
272 wastebits = aimutil_get8(msgblock+j++);
274 y = aimutil_get16(msgblock+j);
276 for (z = 0; z < y; z++)
277 wastebits = aimutil_get8(msgblock+j++);
278 wastebits = aimutil_get8(msgblock+j++);
279 wastebits = aimutil_get8(msgblock+j++);
282 * Message string length, including flag words.
284 i = aimutil_get16(msgblock+j);
290 * Its rumored that these can kick in some funky
291 * 16bit-wide char stuff that used to really kill
292 * libfaim. Hopefully the latter is no longer true.
294 * Though someone should investiagte the former.
297 flag1 = aimutil_get16(msgblock+j);
299 flag2 = aimutil_get16(msgblock+j);
303 printf("faim: icbm: **warning: encoding flags are being used! {%04x, %04x}\n", flag1, flag2);
309 msg = (char *)malloc(i+1);
310 memcpy(msg, msgblock+j, i);
314 * Free up the TLV chain.
316 aim_freetlvchain(&tlvlist);
321 userfunc = aim_callhandler(command->conn, 0x0004, 0x0007);
323 i = userfunc(command, &userinfo, msg, isautoresponse, flag1, flag2);
333 * Not real sure what this does, nor does anyone I've talk to.
335 * Didn't use to send it. But now I think it might be a good
339 u_long aim_seticbmparam(struct aim_conn_t *conn)
341 struct command_tx_struct newpacket;
346 newpacket.conn = conn;
348 newpacket.conn = aim_getconn_type(AIM_CONN_TYPE_BOS);
349 newpacket.type = 0x02;
351 newpacket.commandlen = 10 + 16;
352 newpacket.data = (u_char *) malloc (newpacket.commandlen);
354 curbyte = aim_putsnac(newpacket.data, 0x0004, 0x0002, 0x0000, aim_snac_nextid);
355 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
356 curbyte += aimutil_put32(newpacket.data+curbyte, 0x00000003);
357 curbyte += aimutil_put8(newpacket.data+curbyte, 0x1f);
358 curbyte += aimutil_put8(newpacket.data+curbyte, 0x40);
359 curbyte += aimutil_put8(newpacket.data+curbyte, 0x03);
360 curbyte += aimutil_put8(newpacket.data+curbyte, 0xe7);
361 curbyte += aimutil_put8(newpacket.data+curbyte, 0x03);
362 curbyte += aimutil_put8(newpacket.data+curbyte, 0xe7);
363 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
364 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
366 aim_tx_enqueue(&newpacket);
368 return (aim_snac_nextid++);