]> andersk Git - libfaim.git/blobdiff - aim_chat.c
- Fri Feb 9 22:31:22 UTC 2001
[libfaim.git] / aim_chat.c
index 85cf21564f70b0af8e636ca4be5f92eadf27188c..cf57449e8824c3718e6624a4824e8e3dd9528516 100644 (file)
@@ -5,9 +5,10 @@
  *
  */
 
+#define FAIM_INTERNAL
 #include <faim/aim.h> 
 
-char *aim_chat_getname(struct aim_conn_t *conn)
+faim_export char *aim_chat_getname(struct aim_conn_t *conn)
 {
   if (!conn)
     return NULL;
@@ -17,47 +18,52 @@ char *aim_chat_getname(struct aim_conn_t *conn)
   return (char *)conn->priv; /* yuck ! */
 }
 
-struct aim_conn_t *aim_chat_getconn(struct aim_session_t *sess, char *name)
+faim_export struct aim_conn_t *aim_chat_getconn(struct aim_session_t *sess, char *name)
 {
-  int i;
-
-  for (i=0;i<AIM_CONN_MAX;i++)
-    {
-      if (sess->conns[i].type == AIM_CONN_TYPE_CHAT)
-       {
-         if (sess->conns[i].priv)
-           if (!strcmp((char *)sess->conns[i].priv, name))
-             {
-               return &sess->conns[i];
-             }
-       }
+  struct aim_conn_t *cur;
+  
+  faim_mutex_lock(&sess->connlistlock);
+  for (cur = sess->connlist; cur; cur = cur->next) {
+    if (cur->type != AIM_CONN_TYPE_CHAT)
+      continue;
+    if (!cur->priv) {
+      printf("faim: chat: chat connection with no name! (fd = %d)\n", cur->fd);
+      continue;
     }
-  return NULL;
+    if (strcmp((char *)cur->priv, name) == 0)
+      break;
+  }
+  faim_mutex_unlock(&sess->connlistlock);
+
+  return cur;
 }
 
-int aim_chat_attachname(struct aim_conn_t *conn, char *roomname)
+faim_export int aim_chat_attachname(struct aim_conn_t *conn, char *roomname)
 {
   if (!conn || !roomname)
     return -1;
 
-  conn->priv = malloc(strlen(roomname)+1);
-  strcpy(conn->priv, roomname);
+  if (conn->priv)
+    free(conn->priv);
+
+  conn->priv = strdup(roomname);
 
   return 0;
 }
 
-u_long aim_chat_send_im(struct aim_session_t *sess,
-                       struct aim_conn_t *conn, 
-                       char *msg)
+faim_export unsigned long aim_chat_send_im(struct aim_session_t *sess,
+                                          struct aim_conn_t *conn, 
+                                          char *msg)
 {   
 
   int curbyte,i;
   struct command_tx_struct *newpacket;
+  struct aim_msgcookie_t *cookie;
 
   if (!sess || !conn || !msg)
     return 0;
   
-  if (!(newpacket = aim_tx_new(0x0002, conn, 1152)))
+  if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152)))
     return -1;
 
   newpacket->lock = 1; /* lock struct */
@@ -70,7 +76,12 @@ u_long aim_chat_send_im(struct aim_session_t *sess,
    * Generate a random message cookie 
    */
   for (i=0;i<8;i++)
-    curbyte += aimutil_put8(newpacket->data+curbyte, (u_char) random());
+    curbyte += aimutil_put8(newpacket->data+curbyte, (u_char) rand());
+
+  cookie = aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_CHAT, NULL);
+  cookie->data = strdup(conn->priv); /* chat hack dependent */
+
+  aim_cachecookie(sess, cookie);
 
   /*
    * metaTLV start.  -- i assume this is a metaTLV.  it could be the
@@ -121,10 +132,10 @@ u_long aim_chat_send_im(struct aim_session_t *sess,
  * and room name.
  *
  */
-u_long aim_chat_join(struct aim_session_t *sess,
-                    struct aim_conn_t *conn, 
-                    u_short exchange,
-                    const char *roomname)
+faim_export unsigned long aim_chat_join(struct aim_session_t *sess,
+                                       struct aim_conn_t *conn, 
+                                       u_short exchange,
+                                       const char *roomname)
 {
   struct command_tx_struct *newpacket;
   int i;
@@ -132,7 +143,7 @@ u_long aim_chat_join(struct aim_session_t *sess,
   if (!sess || !conn || !roomname)
     return 0;
   
-  if (!(newpacket = aim_tx_new(0x0002, conn, 10+9+strlen(roomname)+2)))
+  if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+9+strlen(roomname)+2)))
     return -1;
 
   newpacket->lock = 1;
@@ -149,10 +160,8 @@ u_long aim_chat_join(struct aim_session_t *sess,
   i+= aimutil_put16(newpacket->data+i, 2+1+strlen(roomname)+2);
   i+= aimutil_put16(newpacket->data+i, exchange);
   i+= aimutil_put8(newpacket->data+i, strlen(roomname));
-  memcpy(newpacket->data+i, roomname, strlen(roomname));
-  i+= strlen(roomname);
-  //i+= aimutil_putstr(newpacket->data+i, roomname, strlen(roomname));
-  i+= aimutil_put16(newpacket->data+i, 0x0000);
+  i+= aimutil_putstr(newpacket->data+i, roomname, strlen(roomname));
+  i+= aimutil_put16(newpacket->data+i, 0x0000); /* instance? */
 
   /*
    * Chat hack.
@@ -163,32 +172,18 @@ u_long aim_chat_join(struct aim_session_t *sess,
    *      redirect!
    *
    */
-  sess->pendingjoin = (char *)malloc(strlen(roomname)+1);
-  strcpy(sess->pendingjoin, roomname);
+  sess->pendingjoin = strdup(roomname);
+  sess->pendingjoinexchange = exchange;
 
   newpacket->lock = 0;
   aim_tx_enqueue(sess, newpacket);
 
-#if 0
-  {
-    struct aim_snac_t snac;
-    
-    snac.id = sess->snac_nextid;
-    snac.family = 0x0001;
-    snac.type = 0x0004;
-    snac.flags = 0x0000;
-
-    snac.data = malloc(strlen(roomname)+1);
-    strcpy(snac.data, roomname);
+  aim_cachesnac(sess, 0x0001, 0x0004, 0x0000, roomname, strlen(roomname)+1);
 
-    aim_newsnac(sess, &snac);
-  }
-
-#endif
-  return (sess->snac_nextid++);
+  return sess->snac_nextid;
 }
 
-int aim_chat_readroominfo(u_char *buf, struct aim_chat_roominfo *outinfo)
+faim_internal int aim_chat_readroominfo(u_char *buf, struct aim_chat_roominfo *outinfo)
 {
   int namelen = 0;
   int i = 0;
@@ -211,7 +206,7 @@ int aim_chat_readroominfo(u_char *buf, struct aim_chat_roominfo *outinfo)
   i += 2;
   
   return i;
-};
+}
 
 
 /*
@@ -223,8 +218,8 @@ int aim_chat_readroominfo(u_char *buf, struct aim_chat_roominfo *outinfo)
  *
  * SNAC 000e/0002
  */
-int aim_chat_parse_infoupdate(struct aim_session_t *sess,
-                             struct command_rx_struct *command)
+faim_internal int aim_chat_parse_infoupdate(struct aim_session_t *sess,
+                                           struct command_rx_struct *command)
 {
   struct aim_userinfo_s *userinfo = NULL;
   rxcallback_t userfunc=NULL;  
@@ -236,7 +231,6 @@ int aim_chat_parse_infoupdate(struct aim_session_t *sess,
   u_short tlvcount = 0;
   struct aim_tlvlist_t *tlvlist;
   char *roomdesc = NULL;
-  struct aim_tlv_t *tmptlv;
   unsigned short unknown_c9 = 0;
   unsigned long creationtime = 0;
   unsigned short maxmsglen = 0;
@@ -250,7 +244,7 @@ int aim_chat_parse_infoupdate(struct aim_session_t *sess,
 
   if (detaillevel != 0x02) {
     if (detaillevel == 0x01)
-      printf("faim: chat_roomupdateinfo: detail level 2 not supported\n");
+      printf("faim: chat_roomupdateinfo: detail level 1 not supported\n");
     else
       printf("faim: chat_roomupdateinfo: unknown detail level %d\n", detaillevel);
     return 1;
@@ -273,12 +267,8 @@ int aim_chat_parse_infoupdate(struct aim_session_t *sess,
   /*
    * Type 0x006f: Number of occupants.
    */
-  if (aim_gettlv(tlvlist, 0x006f, 1)) {
-    struct aim_tlv_t *tmptlv;
-    tmptlv = aim_gettlv(tlvlist, 0x006f, 1);
-    
-    usercount = aimutil_get16(tmptlv->value);
-  }
+  if (aim_gettlv(tlvlist, 0x006f, 1))
+    usercount = aim_gettlv16(tlvlist, 0x006f, 1);
 
   /*
    * Type 0x0073:  Occupant list.
@@ -300,26 +290,26 @@ int aim_chat_parse_infoupdate(struct aim_session_t *sess,
   /* 
    * Type 0x00c9: Unknown. (2 bytes)
    */
-  if ((tmptlv = aim_gettlv(tlvlist, 0x00c9, 1)))
-    unknown_c9 = aimutil_get16(tmptlv->value);
+  if (aim_gettlv(tlvlist, 0x00c9, 1))
+    unknown_c9 = aim_gettlv16(tlvlist, 0x00c9, 1);
   
   /* 
    * Type 0x00ca: Creation time (4 bytes)
    */
-  if ((tmptlv = aim_gettlv(tlvlist, 0x00ca, 1)))
-    creationtime = aimutil_get32(tmptlv->value);
+  if (aim_gettlv(tlvlist, 0x00ca, 1))
+    creationtime = aim_gettlv32(tlvlist, 0x00ca, 1);
 
   /* 
    * Type 0x00d1: Maximum Message Length
    */
-  if ((tmptlv = aim_gettlv(tlvlist, 0x00d1, 1)))
-    maxmsglen = aimutil_get16(tmptlv->value);
+  if (aim_gettlv(tlvlist, 0x00d1, 1))
+    maxmsglen = aim_gettlv16(tlvlist, 0x00d1, 1);
 
   /* 
    * Type 0x00d2: Unknown. (2 bytes)
    */
-  if ((tmptlv = aim_gettlv(tlvlist, 0x00d2, 1)))
-    unknown_d2 = aimutil_get16(tmptlv->value);;
+  if (aim_gettlv(tlvlist, 0x00d2, 1))
+    unknown_d2 = aim_gettlv16(tlvlist, 0x00d2, 1);
 
   /* 
    * Type 0x00d3: Room Description
@@ -330,12 +320,11 @@ int aim_chat_parse_infoupdate(struct aim_session_t *sess,
   /* 
    * Type 0x00d5: Unknown. (1 byte)
    */
-  if ((tmptlv = aim_gettlv(tlvlist, 0x00d5, 1)))
-    unknown_d5 = aimutil_get8(tmptlv->value);;
+  if (aim_gettlv(tlvlist, 0x00d5, 1))
+    unknown_d5 = aim_gettlv8(tlvlist, 0x00d5, 1);
 
 
-  userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_ROOMINFOUPDATE);
-  if (userfunc) {
+  if ((userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_ROOMINFOUPDATE))) {
     ret = userfunc(sess,
                   command, 
                   &roominfo,
@@ -358,8 +347,8 @@ int aim_chat_parse_infoupdate(struct aim_session_t *sess,
   return ret;
 }
 
-int aim_chat_parse_joined(struct aim_session_t *sess,
-                             struct command_rx_struct *command)
+faim_internal int aim_chat_parse_joined(struct aim_session_t *sess,
+                                       struct command_rx_struct *command)
 {
   struct aim_userinfo_s *userinfo = NULL;
   rxcallback_t userfunc=NULL;  
@@ -371,8 +360,7 @@ int aim_chat_parse_joined(struct aim_session_t *sess,
     i += aim_extractuserinfo(command->data+i, &userinfo[curcount-1]);
   }
 
-  userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_USERJOIN);
-  if (userfunc) {      
+  if ((userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_USERJOIN))) {
     ret = userfunc(sess,
                   command, 
                   curcount,
@@ -384,8 +372,8 @@ int aim_chat_parse_joined(struct aim_session_t *sess,
   return ret;
 }            
 
-int aim_chat_parse_leave(struct aim_session_t *sess,
-                             struct command_rx_struct *command)
+faim_internal int aim_chat_parse_leave(struct aim_session_t *sess,
+                                      struct command_rx_struct *command)
 {
 
   struct aim_userinfo_s *userinfo = NULL;
@@ -398,8 +386,7 @@ int aim_chat_parse_leave(struct aim_session_t *sess,
     i += aim_extractuserinfo(command->data+i, &userinfo[curcount-1]);
   }
 
-  userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_USERLEAVE);
-  if (userfunc) {
+  if ((userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_USERLEAVE))) {
     ret = userfunc(sess,
                   command, 
                   curcount,
@@ -416,27 +403,34 @@ int aim_chat_parse_leave(struct aim_session_t *sess,
  * code as channel 0x0003, however, since only the start
  * would be the same, we might as well do it here.
  */
-int aim_chat_parse_incoming(struct aim_session_t *sess,
-                             struct command_rx_struct *command)
+faim_internal int aim_chat_parse_incoming(struct aim_session_t *sess,
+                                         struct command_rx_struct *command)
 {
   struct aim_userinfo_s userinfo;
   rxcallback_t userfunc=NULL;  
   int ret = 1, i = 0, z = 0;
-  u_char cookie[8];
+  unsigned char cookie[8];
   int channel;
   struct aim_tlvlist_t *outerlist;
   char *msg = NULL;
+  struct aim_msgcookie_t *ck;
 
   memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
 
   i = 10; /* skip snac */
 
   /*
-   * ICBM Cookie.  Ignore it.
+   * ICBM Cookie.  Cache it.
    */ 
   for (z=0; z<8; z++,i++)
     cookie[z] = command->data[i];
 
+  if ((ck = aim_uncachecookie(sess, cookie, AIM_COOKIETYPE_CHAT))) {
+    if (ck->data)
+      free(ck->data);
+    free(ck);
+  }
+
   /*
    * Channel ID
    *
@@ -509,13 +503,13 @@ int aim_chat_parse_incoming(struct aim_session_t *sess,
   return ret;
 }            
 
-u_long aim_chat_clientready(struct aim_session_t *sess,
-                           struct aim_conn_t *conn)
+faim_export unsigned long aim_chat_clientready(struct aim_session_t *sess,
+                                              struct aim_conn_t *conn)
 {
   struct command_tx_struct *newpacket;
   int i;
 
-  if (!(newpacket = aim_tx_new(0x0002, conn, 0x20)))
+  if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 0x20)))
     return -1;
 
   newpacket->lock = 1;
@@ -540,43 +534,41 @@ u_long aim_chat_clientready(struct aim_session_t *sess,
   return (sess->snac_nextid++);
 }
 
-int aim_chat_leaveroom(struct aim_session_t *sess, char *name)
+faim_export int aim_chat_leaveroom(struct aim_session_t *sess, char *name)
 {
-  int i;
+  struct aim_conn_t *conn;
 
-  for (i=0;i<AIM_CONN_MAX;i++)
-    {
-      if (sess->conns[i].type == AIM_CONN_TYPE_CHAT)
-       {
-         if (sess->conns[i].priv)
-           if (!strcmp((char *)sess->conns[i].priv, name))
-             {
-               aim_conn_close(&sess->conns[i]);
-               return 0;
-             }
-       }
-    }
-  return -1;
+  if ((conn = aim_chat_getconn(sess, name)))
+    aim_conn_close(conn);
+
+  if (!conn)
+    return -1;
+  return 0;
 }
 
 /*
  * conn must be a BOS connection!
  */
-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)
+faim_export unsigned 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)
 {
   struct command_tx_struct *newpacket;
   int i,curbyte=0;
+  struct aim_msgcookie_t *cookie;
+  struct aim_invite_priv *priv;
 
   if (!sess || !conn || !sn || !msg || !roomname)
-    return 0;
+    return -1;
+
+  if (conn->type != AIM_CONN_TYPE_BOS)
+    return -1;
 
-  if (!(newpacket = aim_tx_new(0x0002, conn, 1152+strlen(sn)+strlen(roomname)+strlen(msg))))
+  if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152+strlen(sn)+strlen(roomname)+strlen(msg))))
     return -1;
 
   newpacket->lock = 1;
@@ -589,6 +581,18 @@ u_long aim_chat_invite(struct aim_session_t *sess,
   for (i=0;i<8;i++)
     curbyte += aimutil_put8(newpacket->data+curbyte, (u_char)rand());
 
+  /* XXX this should get uncached by the unwritten 'invite accept' handler */
+  if(!(priv = calloc(sizeof(struct aim_invite_priv), 1)))
+    return -1;
+  priv->sn = strdup(sn);
+  priv->roomname = strdup(roomname);
+  priv->exchange = exchange;
+  priv->instance = instance;
+
+  if(!(cookie = aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_INVITE, priv)))
+    return -1;
+  aim_cachecookie(sess, cookie);
+
   /*
    * Channel (2)
    */
This page took 0.963155 seconds and 4 git commands to generate.