2 * Main libfaim header. Must be included in client for prototypes/macros.
9 #define FAIM_VERSION_MAJOR 0
10 #define FAIM_VERSION_MINOR 99
11 #define FAIM_VERSION_MINORMINOR 0
13 #include <faim/faimconfig.h>
14 #include <faim/aim_cbtypes.h>
16 #ifndef FAIM_USEPTHREADS
17 #error pthreads are currently required.
23 #include <sys/types.h>
28 #ifdef FAIM_USEPTHREADS
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
42 #include <sys/socket.h>
43 #include <netinet/in.h>
48 /* Portability stuff (DMP) */
52 #define strlen(x) (int)strlen(x) /* win32 has a unsigned size_t */
55 #if defined(_WIN32) || (defined(mach) && defined(__APPLE__))
56 #define gethostbyname2(x,y) gethostbyname(x) /* revert to IPv4 */
60 * Current Maximum Length for Screen Names (not including NULL)
65 * Current Maximum Length for Instant Messages
67 * This was found basically by experiment, but not wholly
68 * accurate experiment. It should not be regarded
69 * as completely correct. But its a decent approximation.
71 * Note that although we can send this much, its impossible
72 * for WinAIM clients (up through the latest (4.0.1957)) to
73 * send any more than 1kb. Amaze all your windows friends
74 * with utterly oversized instant messages!
77 #define MAXMSGLEN 7988
80 * Current Maximum Length for Chat Room Messages
82 * This is actually defined by the protocol to be
83 * dynamic, but I have yet to see due cause to
84 * define it dynamically here. Maybe later.
87 #define MAXCHATMSGLEN 512
90 * Standard size of an AIM authorization cookie
92 #define AIM_COOKIELEN 0x100
95 #define faimdprintf(l, x...) {if (l >= debug) printf(x); }
97 #define faimdprintf(l, x...)
101 * Login info. Passes information from the Authorization
102 * stage of login to the service (BOS, etc) connection
106 struct aim_login_struct {
107 char screen_name[MAXSNLEN+1];
109 char cookie[AIM_COOKIELEN];
117 * Client info. Filled in by the client and passed
118 * in to aim_login(). The information ends up
119 * getting passed to OSCAR through the initial
122 * XXX: Should this be per-session? -mid
125 struct client_info_s {
126 char clientstring[100]; /* arbitrary size */
140 * These could be arbitrary, but its easier to use the actual AIM values
142 #define AIM_CONN_TYPE_AUTH 0x0007
143 #define AIM_CONN_TYPE_ADS 0x0005
144 #define AIM_CONN_TYPE_BOS 0x0002
145 #define AIM_CONN_TYPE_CHAT 0x000e
146 #define AIM_CONN_TYPE_CHATNAV 0x000d
149 * Status values returned from aim_conn_new(). ORed together.
151 #define AIM_CONN_STATUS_READY 0x0001
152 #define AIM_CONN_STATUS_INTERNALERR 0x0002
153 #define AIM_CONN_STATUS_RESOLVERR 0x0080
154 #define AIM_CONN_STATUS_CONNERR 0x0040
161 void *priv; /* misc data the client may want to store */
162 time_t lastactivity; /* time of last transmit */
164 struct aim_rxcblist_t *handlerlist;
165 faim_mutex_t active; /* lock around read/writes */
166 faim_mutex_t seqnum_lock; /* lock around ->seqnum changes */
169 /* struct for incoming commands */
170 struct command_rx_struct {
171 /* byte 1 assumed to always be 0x2a */
172 char type; /* type code (byte 2) */
173 u_int seqnum; /* sequence number (bytes 3 and 4) */
174 u_int commandlen; /* total packet len - 6 (bytes 5 and 6) */
175 u_char *data; /* packet data (from 7 byte on) */
176 u_int lock; /* 0 = open, !0 = locked */
177 u_int handled; /* 0 = new, !0 = been handled */
178 u_int nofree; /* 0 = free data on purge, 1 = only unlink */
179 struct aim_conn_t *conn; /* the connection it came in on... */
180 struct command_rx_struct *next; /* ptr to next struct in list */
183 /* struct for outgoing commands */
184 struct command_tx_struct {
185 /* byte 1 assumed to be 0x2a */
186 char type; /* type/family code */
187 u_int seqnum; /* seqnum dynamically assigned on tx */
188 u_int commandlen; /* SNAC length */
189 u_char *data; /* packet data */
190 u_int lock; /* 0 = open, !0 = locked */
191 u_int sent; /* 0 = pending, !0 = has been sent */
192 struct aim_conn_t *conn;
193 struct command_tx_struct *next; /* ptr to next struct in list */
198 * AIM Session: The main client-data interface.
201 struct aim_session_t {
203 /* ---- Client Accessible ------------------------ */
205 * Login information. See definition above.
208 struct aim_login_struct logininfo;
211 * Pointer to anything the client wants to
212 * explicitly associate with this session.
217 /* ---- Internal Use Only ------------------------ */
219 * Connection information
221 struct aim_conn_t conns[AIM_CONN_MAX];
226 struct command_tx_struct *queue_outgoing;
227 struct command_rx_struct *queue_incoming;
230 * Tx Enqueuing function
232 int (*tx_enqueue)(struct aim_session_t *, struct command_tx_struct *);
235 * This is a dreadful solution to the what-room-are-we-joining
236 * problem. (There's no connection between the service
237 * request and the resulting redirect.)
242 * Outstanding snac handling
244 * XXX: Should these be per-connection? -mid
246 struct aim_snac_t *outstanding_snacs;
252 * AIM User Info, Standard Form.
254 struct aim_userinfo_s {
262 u_short capabilities;
265 #define AIM_CLASS_TRIAL 0x0001
266 #define AIM_CLASS_UNKNOWN2 0x0002
267 #define AIM_CLASS_AOL 0x0004
268 #define AIM_CLASS_UNKNOWN4 0x0008
269 #define AIM_CLASS_FREE 0x0010
270 #define AIM_CLASS_AWAY 0x0020
271 #define AIM_CLASS_UNKNOWN40 0x0040
272 #define AIM_CLASS_UNKNOWN80 0x0080
278 /* Generic TLV structure. */
286 struct aim_tlvlist_t {
287 struct aim_tlv_t *tlv;
288 struct aim_tlvlist_t *next;
291 /* TLV-handling functions */
292 struct aim_tlvlist_t *aim_readtlvchain(u_char *buf, int maxlen);
293 void aim_freetlvchain(struct aim_tlvlist_t **list);
294 struct aim_tlv_t *aim_grabtlv(u_char *src);
295 struct aim_tlv_t *aim_grabtlvstr(u_char *src);
296 struct aim_tlv_t *aim_gettlv(struct aim_tlvlist_t *, u_short, int);
297 char *aim_gettlv_str(struct aim_tlvlist_t *, u_short, int);
298 int aim_puttlv (u_char *dest, struct aim_tlv_t *newtlv);
299 struct aim_tlv_t *aim_createtlv(void);
300 int aim_freetlv(struct aim_tlv_t **oldtlv);
301 int aim_puttlv_16(u_char *, u_short, u_short);
302 int aim_puttlv_32(u_char *, u_short, u_long);
303 int aim_puttlv_str(u_char *buf, u_short t, u_short l, u_char *v);
304 int aim_writetlvchain(u_char *buf, int buflen, struct aim_tlvlist_t **list);
305 int aim_addtlvtochain16(struct aim_tlvlist_t **list, unsigned short type, unsigned short val);
306 int aim_addtlvtochain32(struct aim_tlvlist_t **list, unsigned short type, unsigned long val);
307 int aim_addtlvtochain_str(struct aim_tlvlist_t **list, unsigned short type, char *str, int len);
308 int aim_counttlvchain(struct aim_tlvlist_t **list);
311 * Get command from connections / Dispatch commands
314 int aim_get_command(struct aim_session_t *, struct aim_conn_t *);
315 int aim_rxdispatch(struct aim_session_t *);
317 int aim_logoff(struct aim_session_t *);
320 typedef int (*rxcallback_t)(struct aim_session_t *, struct command_rx_struct *, ...);
321 int aim_register_callbacks(rxcallback_t *);
323 u_long aim_genericreq_n(struct aim_session_t *, struct aim_conn_t *conn, u_short family, u_short subtype);
324 u_long aim_genericreq_l(struct aim_session_t *, struct aim_conn_t *conn, u_short family, u_short subtype, u_long *);
325 u_long aim_genericreq_s(struct aim_session_t *, struct aim_conn_t *conn, u_short family, u_short subtype, u_short *);
328 int aim_sendconnack(struct aim_session_t *sess, struct aim_conn_t *conn);
329 int aim_request_login (struct aim_session_t *sess, struct aim_conn_t *conn, char *sn);
330 int aim_send_login (struct aim_session_t *, struct aim_conn_t *, char *, char *, struct client_info_s *);
331 int aim_encode_password(const char *, u_char *);
332 int aimicq_encode_password(const char *password, u_char *encoded);
333 unsigned long aim_sendauthresp(struct aim_session_t *sess,
334 struct aim_conn_t *conn,
335 char *sn, char *bosip,
336 char *cookie, char *email,
338 int aim_gencookie(unsigned char *buf);
339 int aim_sendserverready(struct aim_session_t *sess, struct aim_conn_t *conn);
340 unsigned long aim_sendredirect(struct aim_session_t *sess,
341 struct aim_conn_t *conn,
342 unsigned short servid,
345 void aim_purge_rxqueue(struct aim_session_t *);
348 int aim_parse_unknown(struct aim_session_t *, struct command_rx_struct *command, ...);
349 int aim_parse_missed_im(struct aim_session_t *, struct command_rx_struct *, ...);
350 int aim_parse_last_bad(struct aim_session_t *, struct command_rx_struct *, ...);
352 struct command_tx_struct *aim_tx_new(int, struct aim_conn_t *, int);
353 int aim_tx_enqueue__queuebased(struct aim_session_t *, struct command_tx_struct *);
354 int aim_tx_enqueue__immediate(struct aim_session_t *, struct command_tx_struct *);
355 #define aim_tx_enqueue(x, y) ((*(x->tx_enqueue))(x, y))
356 int aim_tx_sendframe(struct command_tx_struct *cur);
357 u_int aim_get_next_txseqnum(struct aim_conn_t *);
358 int aim_tx_flushqueue(struct aim_session_t *);
359 int aim_tx_printqueue(struct aim_session_t *);
360 void aim_tx_purgequeue(struct aim_session_t *);
362 struct aim_rxcblist_t {
365 rxcallback_t handler;
367 struct aim_rxcblist_t *next;
370 int aim_conn_setlatency(struct aim_conn_t *conn, int newval);
372 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);
373 rxcallback_t aim_callhandler(struct aim_conn_t *conn, u_short family, u_short type);
374 int aim_clearhandlers(struct aim_conn_t *conn);
377 * Generic SNAC structure. Rarely if ever used.
386 struct aim_snac_t *next;
388 u_long aim_newsnac(struct aim_session_t *, struct aim_snac_t *newsnac);
389 struct aim_snac_t *aim_remsnac(struct aim_session_t *, u_long id);
390 int aim_cleansnacs(struct aim_session_t *, int maxage);
391 int aim_putsnac(u_char *, int, int, int, u_long);
394 void aim_connrst(struct aim_session_t *);
395 struct aim_conn_t *aim_conn_getnext(struct aim_session_t *);
396 void aim_conn_close(struct aim_conn_t *deadconn);
397 struct aim_conn_t *aim_getconn_type(struct aim_session_t *, int type);
398 struct aim_conn_t *aim_newconn(struct aim_session_t *, int type, char *dest);
399 int aim_conngetmaxfd(struct aim_session_t *);
400 struct aim_conn_t *aim_select(struct aim_session_t *, struct timeval *, int *);
401 int aim_conn_isready(struct aim_conn_t *);
402 int aim_conn_setstatus(struct aim_conn_t *, int);
403 void aim_session_init(struct aim_session_t *);
407 #define AIM_VISIBILITYCHANGE_PERMITADD 0x05
408 #define AIM_VISIBILITYCHANGE_PERMITREMOVE 0x06
409 #define AIM_VISIBILITYCHANGE_DENYADD 0x07
410 #define AIM_VISIBILITYCHANGE_DENYREMOVE 0x08
412 u_long aim_bos_setidle(struct aim_session_t *, struct aim_conn_t *, u_long);
413 u_long aim_bos_changevisibility(struct aim_session_t *, struct aim_conn_t *, int, char *);
414 u_long aim_bos_setbuddylist(struct aim_session_t *, struct aim_conn_t *, char *);
415 u_long aim_bos_setprofile(struct aim_session_t *, struct aim_conn_t *, char *, char *, unsigned int);
416 u_long aim_bos_setgroupperm(struct aim_session_t *, struct aim_conn_t *, u_long);
417 u_long aim_bos_clientready(struct aim_session_t *, struct aim_conn_t *);
418 u_long aim_bos_reqrate(struct aim_session_t *, struct aim_conn_t *);
419 u_long aim_bos_ackrateresp(struct aim_session_t *, struct aim_conn_t *);
420 u_long aim_bos_setprivacyflags(struct aim_session_t *, struct aim_conn_t *, u_long);
421 u_long aim_bos_reqpersonalinfo(struct aim_session_t *, struct aim_conn_t *);
422 u_long aim_bos_reqservice(struct aim_session_t *, struct aim_conn_t *, u_short);
423 u_long aim_bos_reqrights(struct aim_session_t *, struct aim_conn_t *);
424 u_long aim_bos_reqbuddyrights(struct aim_session_t *, struct aim_conn_t *);
425 u_long aim_bos_reqlocaterights(struct aim_session_t *, struct aim_conn_t *);
426 u_long aim_bos_reqicbmparaminfo(struct aim_session_t *, struct aim_conn_t *);
427 u_long aim_setversions(struct aim_session_t *sess, struct aim_conn_t *conn);
429 /* aim_rxhandlers.c */
430 int aim_rxdispatch(struct aim_session_t *);
431 int aim_authparse(struct aim_session_t *, struct command_rx_struct *);
432 int aim_handleredirect_middle(struct aim_session_t *, struct command_rx_struct *, ...);
433 int aim_parse_unknown(struct aim_session_t *, struct command_rx_struct *, ...);
434 int aim_parse_last_bad(struct aim_session_t *, struct command_rx_struct *, ...);
435 int aim_parse_generalerrs(struct aim_session_t *, struct command_rx_struct *command, ...);
436 int aim_parsemotd_middle(struct aim_session_t *sess, struct command_rx_struct *command, ...);
439 #define AIM_IMFLAGS_AWAY 0x01 /* mark as an autoreply */
440 #define AIM_IMFLAGS_ACK 0x02 /* request a receipt notice */
442 u_long aim_send_im(struct aim_session_t *, struct aim_conn_t *, char *, u_int, char *);
443 int aim_parse_incoming_im_middle(struct aim_session_t *, struct command_rx_struct *);
444 u_long aim_seticbmparam(struct aim_session_t *, struct aim_conn_t *conn);
445 int aim_parse_msgerror_middle(struct aim_session_t *, struct command_rx_struct *);
446 int aim_negchan_middle(struct aim_session_t *sess, struct command_rx_struct *command);
449 #define AIM_CAPS_BUDDYICON 0x01
450 #define AIM_CAPS_VOICE 0x02
451 #define AIM_CAPS_IMIMAGE 0x04
452 #define AIM_CAPS_CHAT 0x08
453 #define AIM_CAPS_GETFILE 0x10
454 #define AIM_CAPS_SENDFILE 0x20
455 extern u_char aim_caps[6][16];
457 #define AIM_GETINFO_GENERALINFO 0x00001
458 #define AIM_GETINFO_AWAYMESSAGE 0x00003
460 u_long aim_getinfo(struct aim_session_t *, struct aim_conn_t *, const char *, unsigned short);
461 int aim_extractuserinfo(u_char *, struct aim_userinfo_s *);
462 int aim_parse_userinfo_middle(struct aim_session_t *, struct command_rx_struct *);
463 int aim_parse_oncoming_middle(struct aim_session_t *, struct command_rx_struct *);
464 int aim_parse_offgoing_middle(struct aim_session_t *, struct command_rx_struct *);
465 int aim_putuserinfo(u_char *buf, int buflen, struct aim_userinfo_s *info);
466 int aim_sendbuddyoncoming(struct aim_session_t *sess, struct aim_conn_t *conn, struct aim_userinfo_s *info);
467 int aim_sendbuddyoffgoing(struct aim_session_t *sess, struct aim_conn_t *conn, char *sn);
471 int aim_auth_sendcookie(struct aim_session_t *, struct aim_conn_t *, u_char *);
472 u_long aim_auth_clientready(struct aim_session_t *, struct aim_conn_t *);
473 u_long aim_auth_changepasswd(struct aim_session_t *, struct aim_conn_t *, char *, char *);
475 /* aim_buddylist.c */
476 u_long aim_add_buddy(struct aim_session_t *, struct aim_conn_t *, char *);
477 u_long aim_remove_buddy(struct aim_session_t *, struct aim_conn_t *, char *);
480 u_long aim_usersearch_address(struct aim_session_t *, struct aim_conn_t *, char *);
481 /* u_long aim_usersearch_name(struct aim_session_t *, struct aim_conn_t *, char *); */
484 struct aim_chat_roominfo {
489 int aim_chat_readroominfo(u_char *buf, struct aim_chat_roominfo *outinfo);
490 int aim_chat_parse_infoupdate(struct aim_session_t *sess, struct command_rx_struct *command);
491 int aim_chat_parse_joined(struct aim_session_t *sess, struct command_rx_struct *command);
492 int aim_chat_parse_leave(struct aim_session_t *sess, struct command_rx_struct *command);
493 int aim_chat_parse_incoming(struct aim_session_t *sess, struct command_rx_struct *command);
494 u_long aim_chat_send_im(struct aim_session_t *sess, struct aim_conn_t *conn, char *msg);
495 u_long aim_chat_join(struct aim_session_t *sess, struct aim_conn_t *conn, u_short exchange, const char *roomname);
496 u_long aim_chat_clientready(struct aim_session_t *sess, struct aim_conn_t *conn);
497 int aim_chat_attachname(struct aim_conn_t *conn, char *roomname);
498 char *aim_chat_getname(struct aim_conn_t *conn);
499 struct aim_conn_t *aim_chat_getconn(struct aim_session_t *, char *name);
501 u_long aim_chatnav_reqrights(struct aim_session_t *sess, struct aim_conn_t *conn);
502 u_long aim_chatnav_clientready(struct aim_session_t *sess, struct aim_conn_t *conn);
504 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);
506 struct aim_chat_exchangeinfo {
514 int aim_chatnav_parse_info(struct aim_session_t *sess, struct command_rx_struct *command);
515 u_long aim_chatnav_createroom(struct aim_session_t *sess, struct aim_conn_t *conn, char *name, u_short exchange);
516 int aim_chat_leaveroom(struct aim_session_t *sess, char *name);
519 #ifdef AIMUTIL_USEMACROS
521 * These are really ugly. You'd think this was LISP. I wish it was.
523 #define aimutil_put8(buf, data) ((*(buf) = (u_char)(data)&0xff),1)
524 #define aimutil_get8(buf) ((*(buf))&0xff)
525 #define aimutil_put16(buf, data) ( \
526 (*(buf) = (u_char)((data)>>8)&0xff), \
527 (*((buf)+1) = (u_char)(data)&0xff), \
529 #define aimutil_get16(buf) ((((*(buf))<<8)&0xff00) + ((*((buf)+1)) & 0xff))
530 #define aimutil_put32(buf, data) ( \
531 (*((buf)) = (u_char)((data)>>24)&0xff), \
532 (*((buf)+1) = (u_char)((data)>>16)&0xff), \
533 (*((buf)+2) = (u_char)((data)>>8)&0xff), \
534 (*((buf)+3) = (u_char)(data)&0xff), \
536 #define aimutil_get32(buf) ((((*(buf))<<24)&0xff000000) + \
537 (((*((buf)+1))<<16)&0x00ff0000) + \
538 (((*((buf)+2))<< 8)&0x0000ff00) + \
539 (((*((buf)+3) )&0x000000ff)))
541 #warning Not using aimutil macros. May have performance problems.
542 int aimutil_put8(u_char *, u_char);
543 u_char aimutil_get8(u_char *buf);
544 int aimutil_put16(u_char *, u_short);
545 u_short aimutil_get16(u_char *);
546 int aimutil_put32(u_char *, u_long);
547 u_long aimutil_get32(u_char *);
550 int aimutil_putstr(u_char *, const u_char *, int);
551 int aimutil_tokslen(char *toSearch, int index, char dl);
552 int aimutil_itemcnt(char *toSearch, char dl);
553 char *aimutil_itemidx(char *toSearch, int index, char dl);
555 int aim_snlen(const char *sn);
556 int aim_sncmp(const char *sn1, const char *sn2);
559 char *aim_getbuilddate(void);
560 char *aim_getbuildtime(void);
561 char *aim_getbuildstring(void);
563 #endif /* __AIM_H__ */