]> andersk Git - libfaim.git/blob - aim_im.c
Forgot this file.
[libfaim.git] / aim_im.c
1 /*
2  *  aim_im.c
3  *
4  *  The routines for sending/receiving Instant Messages.
5  *
6  */
7
8 #include <faim/aim.h>
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  *
19  */
20 u_long aim_send_im(struct aim_session_t *sess,
21                    struct aim_conn_t *conn, 
22                    char *destsn, u_int flags, char *msg)
23 {   
24
25   int curbyte,i;
26   struct command_tx_struct *newpacket;
27   
28   if (!(newpacket = aim_tx_new(0x0002, conn, 1152)))
29     return -1;
30
31   newpacket->lock = 1; /* lock struct */
32
33   curbyte  = 0;
34   curbyte += aim_putsnac(newpacket->data+curbyte, 
35                          0x0004, 0x0006, 0x0000, sess->snac_nextid);
36
37   /* 
38    * Generate a random message cookie 
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    *
45    */
46   for (i=0;i<8;i++)
47     curbyte += aimutil_put8(newpacket->data+curbyte, (u_char) random());
48
49   /*
50    * Channel ID
51    */
52   curbyte += aimutil_put16(newpacket->data+curbyte,0x0001);
53
54   /* 
55    * Destination SN (prepended with byte length)
56    */
57   curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn));
58   curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn));
59
60   /*
61    * metaTLV start.
62    */
63   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
64   curbyte += aimutil_put16(newpacket->data+curbyte, strlen(msg) + 0x0d);
65
66   /*
67    * Flag data?
68    */
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);
73
74   /* 
75    * Message block length.
76    */
77   curbyte += aimutil_put16(newpacket->data+curbyte, strlen(msg) + 0x04);
78
79   /*
80    * Character set data? 
81    */
82   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
83   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
84
85   /*
86    * Message.  Not terminated.
87    */
88   curbyte += aimutil_putstr(newpacket->data+curbyte,msg, strlen(msg));
89
90   /*
91    * Set the Request Acknowledge flag.  
92    */
93   if (flags & AIM_IMFLAGS_ACK) {
94     curbyte += aimutil_put16(newpacket->data+curbyte,0x0003);
95     curbyte += aimutil_put16(newpacket->data+curbyte,0x0000);
96   }
97   
98   /*
99    * Set the Autoresponse flag.
100    */
101   if (flags & AIM_IMFLAGS_AWAY) {
102     curbyte += aimutil_put16(newpacket->data+curbyte,0x0004);
103     curbyte += aimutil_put16(newpacket->data+curbyte,0x0000);
104   }
105   
106   newpacket->commandlen = curbyte;
107   newpacket->lock = 0;
108
109   aim_tx_enqueue(sess, newpacket);
110
111 #ifdef USE_SNAC_FOR_IMS
112  {
113     struct aim_snac_t snac;
114
115     snac.id = sess->snac_nextid;
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
123     aim_newsnac(sess, &snac);
124   }
125
126  aim_cleansnacs(sess, 60); /* clean out all SNACs over 60sec old */
127 #endif
128
129   return (sess->snac_nextid++);
130 }
131
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  */
147 int aim_parse_incoming_im_middle(struct aim_session_t *sess,
148                                  struct command_rx_struct *command)
149 {
150   u_int i = 0,z;
151   rxcallback_t userfunc = NULL;
152   u_char cookie[8];
153   int channel;
154   struct aim_tlvlist_t *tlvlist;
155   struct aim_userinfo_s userinfo;
156   u_short wastebits;
157
158   memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
159  
160   i = 10; /* Skip SNAC header */
161
162   /*
163    * Read ICBM Cookie.  And throw away.
164    */
165   for (z=0; z<8; z++,i++)
166     cookie[z] = command->data[i];
167   
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
174    * features of AIM2/3/3.5. 
175    *
176    * Channel 0x0002 is the Rendevous channel, which
177    * is where Chat Invitiations and various client-client
178    * connection negotiations come from.
179    * 
180    */
181   channel = aimutil_get16(command->data+i);
182   i += 2;
183   
184   /*
185    *
186    */
187   if ((channel != 0x01) && (channel != 0x02))
188     {
189       printf("faim: icbm: ICBM received on an unsupported channel.  Ignoring.\n (chan = %04x)", channel);
190       return 1;
191     }
192
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];
199
200   /*
201    * Warning Level
202    */
203   userinfo.warnlevel = aimutil_get16(command->data+i); /* guess */
204   i += 2;
205
206   /*
207    * Number of TLVs that follow.  Not needed.
208    */
209   wastebits = aimutil_get16(command->data+i);
210   i += 2;
211   
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);
217
218   /*
219    * From here on, its depends on what channel we're on.
220    */
221   if (channel == 1)
222     {
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;
229            
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);
279           return 1;
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);
347     }
348   else if (channel == 0x0002)
349     {   
350       int rendtype;
351       struct aim_tlv_t *block1;
352       struct aim_tlvlist_t *list2;
353       struct aim_tlv_t *tmptlv;
354       int a;
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
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)
388         {
389           aim_freetlvchain(&tlvlist);
390           return 1;
391         }
392
393       list2 = aim_readtlvchain(block1->value+a, block1->length-a);
394
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);
435       
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;
452       
453           free(roominfo.name);
454           free(msg);
455           free(encoding);
456           free(lang);
457         }
458       aim_freetlvchain(&list2);
459     }
460
461   /*
462    * Free up the TLV chain.
463    */
464   aim_freetlvchain(&tlvlist);
465   
466
467   return i;
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  */
477 u_long aim_seticbmparam(struct aim_session_t *sess,
478                         struct aim_conn_t *conn)
479 {
480   struct command_tx_struct *newpacket;
481   int curbyte;
482
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);
502
503   return (sess->snac_nextid++);
504 }
505
506 int 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);
543
544   return ret;
545 }
546
547
This page took 0.128847 seconds and 5 git commands to generate.