]> andersk Git - libfaim.git/blob - aim_snac.c
Stupid offgoing buddy middle handler bug.
[libfaim.git] / aim_snac.c
1
2 /*
3  *
4  * Various SNAC-related dodads... 
5  *
6  * outstanding_snacs is a list of aim_snac_t structs.  A SNAC should be added
7  * whenever a new SNAC is sent and it should remain in the list until the
8  * response for it has been receieved.
9  *
10  * First edition badly written by Adam Fritzler (afritz@delphid.ml.org)
11  * Current edition nicely rewritten (it even works) by n (n@ml.org)
12  *
13  */
14
15 #include <faim/aim.h>
16
17 u_long aim_newsnac(struct aim_session_t *sess,
18                    struct aim_snac_t *newsnac) 
19 {
20   struct aim_snac_t *snac = NULL, *cur = NULL;
21   
22   if (!newsnac)
23     return 0;
24
25   cur = sess->outstanding_snacs;
26
27   snac = calloc(1, sizeof(struct aim_snac_t));
28   if (!snac)
29     return 0;
30   memcpy(snac, newsnac, sizeof(struct aim_snac_t));
31   snac->issuetime = time(&snac->issuetime);
32   snac->next = NULL;
33   
34   if (cur == NULL) {
35     sess->outstanding_snacs = snac;
36     return(snac->id);
37   }
38   while (cur->next != NULL)
39     cur = cur->next;
40   cur->next = snac;
41   return(snac->id);
42 }
43
44 struct aim_snac_t *aim_remsnac(struct aim_session_t *sess, 
45                                u_long id) 
46 {
47   struct aim_snac_t *cur;
48   
49   if (cur == NULL)
50     return(NULL);
51
52   cur = sess->outstanding_snacs;
53   
54   if (cur->id == id) {
55     sess->outstanding_snacs = cur->next;
56     return(cur);
57   }
58   while (cur->next != NULL) {
59     if (cur->next->id == id) {
60       struct aim_snac_t *tmp = NULL;
61       
62       tmp = cur->next;
63       cur->next = cur->next->next;
64       return(tmp);
65     }
66     cur = cur->next;
67   }
68   return(NULL);
69 }
70
71 /*
72  * This is for cleaning up old SNACs that either don't get replies or
73  * a reply was never received for.  Garabage collection. Plain and simple.
74  *
75  * maxage is the _minimum_ age in seconds to keep SNACs (though I don't know
76  * why its called _max_age).
77  *
78  */
79 int aim_cleansnacs(struct aim_session_t *sess,
80                    int maxage)
81 {
82   struct aim_snac_t *cur;
83   struct aim_snac_t *remed = NULL;
84   time_t curtime;
85  
86   cur = sess->outstanding_snacs;
87   
88   curtime = time(&curtime);
89  
90   while (cur)
91     {
92       if ( (cur) && (((cur->issuetime) + maxage) < curtime))
93         {
94 #if DEBUG > 1
95           printf("aimsnac: WARNING purged obsolete snac %ul\n", cur->id);
96 #endif
97           remed = aim_remsnac(sess, cur->id);
98           if (remed)
99             {
100               if (remed->data)
101                 free(remed->data);
102               free(remed);
103             }
104         }
105       cur = cur->next;
106     }
107   
108   return 0;
109 }
110
111 int aim_putsnac(u_char *buf, int family, int subtype, int flags, u_long snacid)
112 {
113   int curbyte = 0;
114   curbyte += aimutil_put16(buf+curbyte, (u_short)(family&0xffff));
115   curbyte += aimutil_put16(buf+curbyte, (u_short)(subtype&0xffff));
116   curbyte += aimutil_put16(buf+curbyte, (u_short)(flags&0xffff));
117   curbyte += aimutil_put32(buf+curbyte, snacid);
118   return curbyte;
119 }
This page took 0.044675 seconds and 5 git commands to generate.