]> andersk Git - libfaim.git/blob - faim/aim.h
- Tue May 30 22:32:31 UTC 2000
[libfaim.git] / faim / aim.h
1 /* 
2  * Main libfaim header.  Must be included in client for prototypes/macros.
3  *
4  */
5
6 #ifndef __AIM_H__
7 #define __AIM_H__
8
9 #define FAIM_VERSION_MAJOR 0
10 #define FAIM_VERSION_MINOR 99
11 #define FAIM_VERSION_MINORMINOR 0
12
13 #include <faim/faimconfig.h>
14 #include <faim/aim_cbtypes.h>
15
16 #ifndef FAIM_USEPTHREADS
17 #error pthreads are currently required.
18 #endif
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <fcntl.h>
23 #include <sys/types.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <errno.h>
27
28 #ifdef FAIM_USEPTHREADS
29 #include <pthread.h>
30 #define faim_mutex_t pthread_mutex_t 
31 #define faim_mutex_init pthread_mutex_init
32 #define faim_mutex_lock pthread_mutex_lock
33 #define faim_mutex_unlock pthread_mutex_unlock
34 #endif
35
36 #ifdef _WIN32
37 #include <windows.h>
38 #include <time.h>
39 #include <io.h>
40 #else
41 #include <netdb.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <sys/time.h>
45 #include <unistd.h>
46 #endif
47
48 /* Portability stuff (DMP) */
49
50 #ifdef _WIN32
51 #define sleep Sleep
52 #define strlen(x) (int)strlen(x)  /* win32 has a unsigned size_t */
53 #endif
54
55 #if defined(_WIN32) || (defined(mach) && defined(__APPLE__)) 
56 #define gethostbyname2(x,y) gethostbyname(x) /* revert to IPv4 */
57 #endif 
58
59 /* 
60  * Current Maximum Length for Screen Names (not including NULL) 
61  */
62 #define MAXSNLEN 16
63
64 /*
65  * Standard size of an AIM authorization cookie
66  */
67 #define AIM_COOKIELEN            0x100
68
69 #if debug > 0
70 #define faimdprintf(l, x...) {if (l >= debug) printf(x); }
71 #else
72 #define faimdprintf(l, x...)
73 #endif
74
75 /*
76  * Login info.  Passes information from the Authorization
77  * stage of login to the service (BOS, etc) connection
78  * phase.
79  *
80  */
81 struct aim_login_struct {
82   char screen_name[MAXSNLEN+1];
83   char *BOSIP;
84   char cookie[AIM_COOKIELEN];
85   char *email;
86   u_short regstatus;
87   char *errorurl;
88   u_short errorcode;
89 };
90
91 /*
92  * Client info.  Filled in by the client and passed
93  * in to aim_login().  The information ends up
94  * getting passed to OSCAR through the initial
95  * login command.
96  *
97  * XXX: Should this be per-session? -mid
98  *
99  */
100 struct client_info_s {
101   char clientstring[100]; /* arbitrary size */
102   int major;
103   int minor;
104   int build;
105   char country[3];
106   char lang[3];
107 };
108
109 #ifndef TRUE
110 #define TRUE 1
111 #define FALSE 0
112 #endif
113
114 /* 
115  * These could be arbitrary, but its easier to use the actual AIM values 
116  */
117 #define AIM_CONN_TYPE_AUTH          0x0007
118 #define AIM_CONN_TYPE_ADS           0x0005
119 #define AIM_CONN_TYPE_BOS           0x0002
120 #define AIM_CONN_TYPE_CHAT          0x000e
121 #define AIM_CONN_TYPE_CHATNAV       0x000d
122
123 /*
124  * Status values returned from aim_conn_new().  ORed together.
125  */
126 #define AIM_CONN_STATUS_READY       0x0001
127 #define AIM_CONN_STATUS_INTERNALERR 0x0002
128 #define AIM_CONN_STATUS_RESOLVERR   0x0080
129 #define AIM_CONN_STATUS_CONNERR     0x0040
130
131 struct aim_conn_t {
132   int fd;
133   int type;
134   int seqnum;
135   int status;
136   void *priv; /* misc data the client may want to store */
137   time_t lastactivity; /* time of last transmit */
138   int forcedlatency; 
139   struct aim_rxcblist_t *handlerlist;
140   faim_mutex_t active; /* lock around read/writes */
141   faim_mutex_t seqnum_lock; /* lock around ->seqnum changes */
142 };
143
144 /* struct for incoming commands */
145 struct command_rx_struct {
146                             /* byte 1 assumed to always be 0x2a */
147   char type;                /* type code (byte 2) */
148   u_int seqnum;             /* sequence number (bytes 3 and 4) */
149   u_int commandlen;         /* total packet len - 6 (bytes 5 and 6) */
150   u_char *data;             /* packet data (from 7 byte on) */
151   u_int lock;               /* 0 = open, !0 = locked */
152   u_int handled;            /* 0 = new, !0 = been handled */
153   u_int nofree;             /* 0 = free data on purge, 1 = only unlink */
154   struct aim_conn_t *conn;  /* the connection it came in on... */
155   struct command_rx_struct *next; /* ptr to next struct in list */
156 };
157
158 /* struct for outgoing commands */
159 struct command_tx_struct {
160                             /* byte 1 assumed to be 0x2a */
161   char type;                /* type/family code */
162   u_int seqnum;             /* seqnum dynamically assigned on tx */
163   u_int commandlen;         /* SNAC length */
164   u_char *data;             /* packet data */
165   u_int lock;               /* 0 = open, !0 = locked */
166   u_int sent;               /* 0 = pending, !0 = has been sent */
167   struct aim_conn_t *conn; 
168   struct command_tx_struct *next; /* ptr to next struct in list */
169 };
170
171
172 /*
173  * AIM Session: The main client-data interface.  
174  *
175  */
176 struct aim_session_t {
177
178   /* ---- Client Accessible ------------------------ */
179   /* 
180    * Login information.  See definition above.
181    *
182    */
183   struct aim_login_struct logininfo;
184   
185   /*
186    * Pointer to anything the client wants to 
187    * explicitly associate with this session.
188    */
189   void *aux_data;
190
191
192   /* ---- Internal Use Only ------------------------ */
193   /* 
194    * Connection information
195    */
196   struct aim_conn_t conns[AIM_CONN_MAX];
197   
198   /* 
199    * TX/RX queues 
200    */
201   struct command_tx_struct *queue_outgoing;   
202   struct command_rx_struct *queue_incoming; 
203   
204   /*
205    * Tx Enqueuing function
206    */
207   int (*tx_enqueue)(struct aim_session_t *, struct command_tx_struct *);
208
209   /*
210    * This is a dreadful solution to the what-room-are-we-joining
211    * problem.  (There's no connection between the service
212    * request and the resulting redirect.)
213    */ 
214   char *pendingjoin;
215
216   /*
217    * Outstanding snac handling 
218    *
219    * XXX: Should these be per-connection? -mid
220    **/
221   struct aim_snac_t *outstanding_snacs;
222   u_long snac_nextid;
223 };
224
225
226 /*
227  * AIM User Info, Standard Form.
228  */
229 struct aim_userinfo_s {
230   char sn[MAXSNLEN+1];
231   u_short warnlevel;
232   u_short idletime;
233   u_short class;
234   u_long membersince;
235   u_long onlinesince;
236   u_long sessionlen;  
237   u_short capabilities;
238 };
239
240 #define AIM_CLASS_TRIAL         0x0001
241 #define AIM_CLASS_UNKNOWN2      0x0002
242 #define AIM_CLASS_AOL           0x0004
243 #define AIM_CLASS_UNKNOWN4      0x0008
244 #define AIM_CLASS_FREE          0x0010
245 #define AIM_CLASS_AWAY          0x0020
246 #define AIM_CLASS_UNKNOWN40     0x0040
247 #define AIM_CLASS_UNKNOWN80     0x0080
248
249 /*
250  * TLV handling
251  */
252
253 /* Generic TLV structure. */
254 struct aim_tlv_t {
255   u_short type;
256   u_short length;
257   u_char *value;
258 };
259
260 /* List of above. */
261 struct aim_tlvlist_t {
262   struct aim_tlv_t *tlv;
263   struct aim_tlvlist_t *next;
264 };
265
266 /* TLV-handling functions */
267 struct aim_tlvlist_t *aim_readtlvchain(u_char *buf, int maxlen);
268 void aim_freetlvchain(struct aim_tlvlist_t **list);
269 struct aim_tlv_t *aim_grabtlv(u_char *src);
270 struct aim_tlv_t *aim_grabtlvstr(u_char *src);
271 struct aim_tlv_t *aim_gettlv(struct aim_tlvlist_t *, u_short, int);
272 char *aim_gettlv_str(struct aim_tlvlist_t *, u_short, int);
273 int aim_puttlv (u_char *dest, struct aim_tlv_t *newtlv);
274 struct aim_tlv_t *aim_createtlv(void);
275 int aim_freetlv(struct aim_tlv_t **oldtlv);
276 int aim_puttlv_16(u_char *, u_short, u_short);
277 int aim_puttlv_32(u_char *, u_short, u_long);
278 int aim_puttlv_str(u_char *buf, u_short t, u_short l, u_char *v);
279 int aim_writetlvchain(u_char *buf, int buflen, struct aim_tlvlist_t **list);
280 int aim_addtlvtochain16(struct aim_tlvlist_t **list, unsigned short type, unsigned short val);
281 int aim_addtlvtochain32(struct aim_tlvlist_t **list, unsigned short type, unsigned long val);
282 int aim_addtlvtochain_str(struct aim_tlvlist_t **list, unsigned short type, char *str, int len);
283 int aim_counttlvchain(struct aim_tlvlist_t **list);
284
285 /*
286  * Get command from connections / Dispatch commands
287  * already in queue.
288  */
289 int aim_get_command(struct aim_session_t *, struct aim_conn_t *);
290 int aim_rxdispatch(struct aim_session_t *);
291
292 int aim_logoff(struct aim_session_t *);
293
294
295 typedef int (*rxcallback_t)(struct aim_session_t *, struct command_rx_struct *, ...);
296 int aim_register_callbacks(rxcallback_t *);
297
298 u_long aim_genericreq_n(struct aim_session_t *, struct aim_conn_t *conn, u_short family, u_short subtype);
299 u_long aim_genericreq_l(struct aim_session_t *, struct aim_conn_t *conn, u_short family, u_short subtype, u_long *);
300 u_long aim_genericreq_s(struct aim_session_t *, struct aim_conn_t *conn, u_short family, u_short subtype, u_short *);
301
302 /* aim_login.c */
303 int aim_sendconnack(struct aim_session_t *sess, struct aim_conn_t *conn);
304 int aim_request_login (struct aim_session_t *sess, struct aim_conn_t *conn, char *sn);
305 int aim_send_login (struct aim_session_t *, struct aim_conn_t *, char *, char *, struct client_info_s *);
306 int aim_encode_password(const char *, u_char *);
307 unsigned long aim_sendauthresp(struct aim_session_t *sess, 
308                                struct aim_conn_t *conn, 
309                                char *sn, char *bosip, 
310                                char *cookie, char *email, 
311                                int regstatus);
312 int aim_gencookie(unsigned char *buf);
313 int aim_sendserverready(struct aim_session_t *sess, struct aim_conn_t *conn);
314 unsigned long aim_sendredirect(struct aim_session_t *sess, 
315                                struct aim_conn_t *conn, 
316                                unsigned short servid, 
317                                char *ip,
318                                char *cookie);
319 void aim_purge_rxqueue(struct aim_session_t *);
320
321
322 int aim_parse_unknown(struct aim_session_t *, struct command_rx_struct *command, ...);
323 int aim_parse_missed_im(struct aim_session_t *, struct command_rx_struct *, ...);
324 int aim_parse_last_bad(struct aim_session_t *, struct command_rx_struct *, ...);
325
326 struct command_tx_struct *aim_tx_new(int, struct aim_conn_t *, int);
327 int aim_tx_enqueue__queuebased(struct aim_session_t *, struct command_tx_struct *);
328 int aim_tx_enqueue__immediate(struct aim_session_t *, struct command_tx_struct *);
329 #define aim_tx_enqueue(x, y) ((*(x->tx_enqueue))(x, y))
330 int aim_tx_sendframe(struct command_tx_struct *cur);
331 u_int aim_get_next_txseqnum(struct aim_conn_t *);
332 int aim_tx_flushqueue(struct aim_session_t *);
333 int aim_tx_printqueue(struct aim_session_t *);
334 void aim_tx_purgequeue(struct aim_session_t *);
335
336 struct aim_rxcblist_t {
337   u_short family;
338   u_short type;
339   rxcallback_t handler;
340   u_short flags;
341   struct aim_rxcblist_t *next;
342 };
343
344 int aim_conn_setlatency(struct aim_conn_t *conn, int newval);
345
346 int aim_conn_addhandler(struct aim_session_t *, struct aim_conn_t *conn, u_short family, u_short type, rxcallback_t newhandler, u_short flags);
347 rxcallback_t aim_callhandler(struct aim_conn_t *conn, u_short family, u_short type);
348 int aim_clearhandlers(struct aim_conn_t *conn);
349
350 /*
351  * Generic SNAC structure.  Rarely if ever used.
352  */
353 struct aim_snac_t {
354   u_long id;
355   u_short family;
356   u_short type;
357   u_short flags;
358   void *data;
359   time_t issuetime;
360   struct aim_snac_t *next;
361 };
362 u_long aim_newsnac(struct aim_session_t *, struct aim_snac_t *newsnac);
363 struct aim_snac_t *aim_remsnac(struct aim_session_t *, u_long id);
364 int aim_cleansnacs(struct aim_session_t *, int maxage);
365 int aim_putsnac(u_char *, int, int, int, u_long);
366
367
368 void aim_connrst(struct aim_session_t *);
369 struct aim_conn_t *aim_conn_getnext(struct aim_session_t *);
370 void aim_conn_close(struct aim_conn_t *deadconn);
371 struct aim_conn_t *aim_getconn_type(struct aim_session_t *, int type);
372 struct aim_conn_t *aim_newconn(struct aim_session_t *, int type, char *dest);
373 int aim_conngetmaxfd(struct aim_session_t *);
374 struct aim_conn_t *aim_select(struct aim_session_t *, struct timeval *, int *);
375 int aim_conn_isready(struct aim_conn_t *);
376 int aim_conn_setstatus(struct aim_conn_t *, int);
377 void aim_session_init(struct aim_session_t *);
378
379 /* aim_misc.c */
380
381 #define AIM_VISIBILITYCHANGE_PERMITADD    0x05
382 #define AIM_VISIBILITYCHANGE_PERMITREMOVE 0x06
383 #define AIM_VISIBILITYCHANGE_DENYADD      0x07
384 #define AIM_VISIBILITYCHANGE_DENYREMOVE   0x08
385
386 u_long aim_bos_setidle(struct aim_session_t *, struct aim_conn_t *, u_long);
387 u_long aim_bos_changevisibility(struct aim_session_t *, struct aim_conn_t *, int, char *);
388 u_long aim_bos_setbuddylist(struct aim_session_t *, struct aim_conn_t *, char *);
389 u_long aim_bos_setprofile(struct aim_session_t *, struct aim_conn_t *, char *, char *, unsigned int);
390 u_long aim_bos_setgroupperm(struct aim_session_t *, struct aim_conn_t *, u_long);
391 u_long aim_bos_clientready(struct aim_session_t *, struct aim_conn_t *);
392 u_long aim_bos_reqrate(struct aim_session_t *, struct aim_conn_t *);
393 u_long aim_bos_ackrateresp(struct aim_session_t *, struct aim_conn_t *);
394 u_long aim_bos_setprivacyflags(struct aim_session_t *, struct aim_conn_t *, u_long);
395 u_long aim_bos_reqpersonalinfo(struct aim_session_t *, struct aim_conn_t *);
396 u_long aim_bos_reqservice(struct aim_session_t *, struct aim_conn_t *, u_short);
397 u_long aim_bos_reqrights(struct aim_session_t *, struct aim_conn_t *);
398 u_long aim_bos_reqbuddyrights(struct aim_session_t *, struct aim_conn_t *);
399 u_long aim_bos_reqlocaterights(struct aim_session_t *, struct aim_conn_t *);
400 u_long aim_bos_reqicbmparaminfo(struct aim_session_t *, struct aim_conn_t *);
401 u_long aim_setversions(struct aim_session_t *sess, struct aim_conn_t *conn);
402
403 /* aim_rxhandlers.c */
404 int aim_rxdispatch(struct aim_session_t *);
405 int aim_authparse(struct aim_session_t *, struct command_rx_struct *);
406 int aim_handleredirect_middle(struct aim_session_t *, struct command_rx_struct *, ...);
407 int aim_parse_unknown(struct aim_session_t *, struct command_rx_struct *, ...);
408 int aim_parse_last_bad(struct aim_session_t *, struct command_rx_struct *, ...);
409 int aim_parse_generalerrs(struct aim_session_t *, struct command_rx_struct *command, ...);
410 int aim_parsemotd_middle(struct aim_session_t *sess, struct command_rx_struct *command, ...);
411
412 /* aim_im.c */
413 #define AIM_IMFLAGS_AWAY 0x01 /* mark as an autoreply */
414 #define AIM_IMFLAGS_ACK  0x02 /* request a receipt notice */
415
416 u_long aim_send_im(struct aim_session_t *, struct aim_conn_t *, char *, u_int, char *);
417 int aim_parse_incoming_im_middle(struct aim_session_t *, struct command_rx_struct *);
418 u_long aim_seticbmparam(struct aim_session_t *, struct aim_conn_t *conn);
419 int aim_parse_msgerror_middle(struct aim_session_t *, struct command_rx_struct *);
420
421 /* aim_info.c */
422 #define AIM_CAPS_BUDDYICON 0x01
423 #define AIM_CAPS_VOICE 0x02
424 #define AIM_CAPS_IMIMAGE 0x04
425 #define AIM_CAPS_CHAT 0x08
426 #define AIM_CAPS_GETFILE 0x10
427 #define AIM_CAPS_SENDFILE 0x20
428 extern u_char aim_caps[6][16];
429
430 #define AIM_GETINFO_GENERALINFO 0x00001
431 #define AIM_GETINFO_AWAYMESSAGE 0x00003
432
433 u_long aim_getinfo(struct aim_session_t *, struct aim_conn_t *, const char *, unsigned short);
434 int aim_extractuserinfo(u_char *, struct aim_userinfo_s *);
435 int aim_parse_userinfo_middle(struct aim_session_t *, struct command_rx_struct *);
436 int aim_parse_oncoming_middle(struct aim_session_t *, struct command_rx_struct *);
437 int aim_parse_offgoing_middle(struct aim_session_t *, struct command_rx_struct *);
438 int aim_putuserinfo(u_char *buf, int buflen, struct aim_userinfo_s *info);
439 int aim_sendbuddyoncoming(struct aim_session_t *sess, struct aim_conn_t *conn, struct aim_userinfo_s *info);
440 int aim_sendbuddyoffgoing(struct aim_session_t *sess, struct aim_conn_t *conn, char *sn);
441
442
443 /* aim_auth.c */
444 int aim_auth_sendcookie(struct aim_session_t *, struct aim_conn_t *, u_char *);
445 u_long aim_auth_clientready(struct aim_session_t *, struct aim_conn_t *);
446 u_long aim_auth_changepasswd(struct aim_session_t *, struct aim_conn_t *, char *, char *);
447
448 /* aim_buddylist.c */
449 u_long aim_add_buddy(struct aim_session_t *, struct aim_conn_t *, char *);
450 u_long aim_remove_buddy(struct aim_session_t *, struct aim_conn_t *, char *);
451
452 /* aim_search.c */
453 u_long aim_usersearch_address(struct aim_session_t *, struct aim_conn_t *, char *);
454 /* u_long aim_usersearch_name(struct aim_session_t *, struct aim_conn_t *, char *); */
455
456
457 struct aim_chat_roominfo {
458   u_short exchange;
459   char *name;
460   u_short instance;
461 };
462 int aim_chat_readroominfo(u_char *buf, struct aim_chat_roominfo *outinfo);
463 int aim_chat_parse_infoupdate(struct aim_session_t *sess, struct command_rx_struct *command);
464 int aim_chat_parse_joined(struct aim_session_t *sess, struct command_rx_struct *command);
465 int aim_chat_parse_leave(struct aim_session_t *sess, struct command_rx_struct *command);
466 int aim_chat_parse_incoming(struct aim_session_t *sess, struct command_rx_struct *command);
467 u_long aim_chat_send_im(struct aim_session_t *sess, struct aim_conn_t *conn, char *msg);
468 u_long aim_chat_join(struct aim_session_t *sess, struct aim_conn_t *conn, u_short exchange, const char *roomname);
469 u_long aim_chat_clientready(struct aim_session_t *sess, struct aim_conn_t *conn);
470 int aim_chat_attachname(struct aim_conn_t *conn, char *roomname);
471 char *aim_chat_getname(struct aim_conn_t *conn);
472 struct aim_conn_t *aim_chat_getconn(struct aim_session_t *, char *name);
473
474 u_long aim_chatnav_reqrights(struct aim_session_t *sess, struct aim_conn_t *conn);
475 u_long aim_chatnav_clientready(struct aim_session_t *sess, struct aim_conn_t *conn);
476
477 u_long aim_chat_invite(struct aim_session_t *sess, struct aim_conn_t *conn, char *sn, char *msg, u_short exchange, char *roomname, u_short instance);
478
479 struct aim_chat_exchangeinfo {
480   u_short number;
481   char *name;
482   char *charset1;
483   char *lang1;
484   char *charset2;
485   char *lang2;
486 };
487 int aim_chatnav_parse_info(struct aim_session_t *sess, struct command_rx_struct *command);
488 u_long aim_chatnav_createroom(struct aim_session_t *sess, struct aim_conn_t *conn, char *name, u_short exchange);
489 int aim_chat_leaveroom(struct aim_session_t *sess, char *name);
490
491 /* aim_util.c */
492 #ifdef AIMUTIL_USEMACROS
493 /*
494  * These are really ugly.  You'd think this was LISP.  I wish it was.
495  */
496 #define aimutil_put8(buf, data) ((*(buf) = (u_char)(data)&0xff),1)
497 #define aimutil_get8(buf) ((*(buf))&0xff)
498 #define aimutil_put16(buf, data) ( \
499                                   (*(buf) = (u_char)((data)>>8)&0xff), \
500                                   (*((buf)+1) = (u_char)(data)&0xff),  \
501                                   2)
502 #define aimutil_get16(buf) ((((*(buf))<<8)&0xff00) + ((*((buf)+1)) & 0xff))
503 #define aimutil_put32(buf, data) ( \
504                                   (*((buf)) = (u_char)((data)>>24)&0xff), \
505                                   (*((buf)+1) = (u_char)((data)>>16)&0xff), \
506                                   (*((buf)+2) = (u_char)((data)>>8)&0xff), \
507                                   (*((buf)+3) = (u_char)(data)&0xff), \
508                                   4)
509 #define aimutil_get32(buf) ((((*(buf))<<24)&0xff000000) + \
510                             (((*((buf)+1))<<16)&0x00ff0000) + \
511                             (((*((buf)+2))<< 8)&0x0000ff00) + \
512                             (((*((buf)+3)    )&0x000000ff)))
513 #else
514 #warning Not using aimutil macros.  May have performance problems.
515 int aimutil_put8(u_char *, u_char);
516 u_char aimutil_get8(u_char *buf);
517 int aimutil_put16(u_char *, u_short);
518 u_short aimutil_get16(u_char *);
519 int aimutil_put32(u_char *, u_long);
520 u_long aimutil_get32(u_char *);
521 #endif
522
523 int aimutil_putstr(u_char *, const u_char *, int);
524 int aimutil_tokslen(char *toSearch, int index, char dl);
525 int aimutil_itemcnt(char *toSearch, char dl);
526 char *aimutil_itemidx(char *toSearch, int index, char dl);
527
528 int aim_snlen(const char *sn);
529 int aim_sncmp(const char *sn1, const char *sn2);
530
531 /* aim_meta.c */
532 long long aim_getbuilddate(void);
533 long long aim_getbuildtime(void);
534 char *aim_getbuildstring(void);
535
536 #endif /* __AIM_H__ */
537
This page took 1.500112 seconds and 5 git commands to generate.