]> andersk Git - libfaim.git/blobdiff - aim_msgcookie.c
- Sun Feb 11 01:07:36 UTC 2001
[libfaim.git] / aim_msgcookie.c
index 3481162d4c85f59aee530541c8ff6fc9e310cd69..8978b9c771e2f44923534ee21eb6cff1e44ed838 100644 (file)
+/*
+ * Cookie Caching stuff. Adam wrote this, apparently just some
+ * derivatives of n's SNAC work. I cleaned it up, added comments.
+ * 
+ * I'm going to rewrite this stuff eventually, honest. -jbm
+ * 
+ */
 
 /*
- *
- *
+ * I'm assuming that cookies are type-specific. that is, we can have
+ * "1234578" for type 1 and type 2 concurrently. if i'm wrong, then we
+ * lose some error checking. if we assume cookies are not type-specific and are
+ * wrong, we get quirky behavior when cookies step on each others' toes.
  */
 
+#define FAIM_INTERNAL
 #include <faim/aim.h>
 
-int aim_cachecookie(struct aim_session_t *sess,
-                   struct aim_msgcookie_t *cookie)
+/*
+ * aim_cachecookie: 
+ * appends a cookie to the cookie list for sess.
+ * - if cookie->cookie for type cookie->type is found, -1 is returned
+ * - copies cookie struct; you need to free() it afterwards;
+ * - cookie->data is not copied, but passed along. don't free it.
+ * - cookie->type is just passed across.
+ * 
+ * returns -1 on error, 0 on success.  
+ */
+faim_internal int aim_cachecookie(struct aim_session_t *sess,
+                                 struct aim_msgcookie_t *cookie)
 {
-  struct aim_msgcookie_t *newcook = NULL, *cur = NULL;
-  
-  if (!cookie)
+  struct aim_msgcookie_t *newcook;
+
+  if (!sess || !cookie)
     return -1;
 
+  printf("\t\tCC cache %d %s", cookie->type, cookie->cookie);
+  if(cookie->type == AIM_COOKIETYPE_OFTGET) {
+    struct aim_filetransfer_priv *priv;
+    priv = cookie->data;
+    printf("%s\n", priv->sn);
+  } else
+    printf("\n");
+
+  if( (newcook = aim_checkcookie(sess, cookie->cookie, cookie->type)) ) {
+    printf("aim_cachecookie: cookie already cached\n");
+    return -1;
+  }
+  
   if (!(newcook = malloc(sizeof(struct aim_msgcookie_t))))
     return -1;
   memcpy(newcook, cookie, sizeof(struct aim_msgcookie_t));
-  newcook->addtime = time(NULL);
-  newcook->next = NULL;
-
-  cur = sess->msgcookies;
   
-  if (cur == NULL) {
-    sess->msgcookies = newcook;
-    return 0;
-  }
-  while (cur->next != NULL)
-    cur = cur->next;
-  cur->next = newcook;
+  newcook->next = sess->msgcookies;
+  sess->msgcookies = newcook;
 
   return 0;
 }
 
-struct aim_msgcookie_t *aim_uncachecookie(struct aim_session_t *sess, 
-                                         char *cookie)
+/*
+ * aim_uncachecookie:
+ * takes a cookie string and grabs the cookie struct associated with
+ * it. removes struct from chain.  returns the struct if found, or
+ * NULL on not found.
+ */
+faim_internal struct aim_msgcookie_t *aim_uncachecookie(struct aim_session_t *sess, unsigned char *cookie, int type)
 {
-  struct aim_msgcookie_t *cur;
+  struct aim_msgcookie_t *cur, **prev;
 
-  if (!cookie)
+  if (!cookie || !sess->msgcookies)
     return NULL;
 
-  if (!sess->msgcookies)
-    return NULL;
+  printf("\t\tCC uncache %d %s\n", type, cookie);
 
-  if (memcmp(sess->msgcookies->cookie, cookie, 8) == 0) {
-    cur = sess->msgcookies;
-    sess->msgcookies = cur->next;
-    return cur;
-  } 
-
-  cur = sess->msgcookies;
-  while (cur->next) {
-    if (memcmp(cur->next->cookie, cookie, 8) == 0) {
-      struct aim_msgcookie_t *tmp;
-      
-      tmp = cur->next;
-      cur->next = cur->next->next;
-      return tmp;
+  for (prev = &sess->msgcookies; (cur = *prev); ) {
+    if ((cur->type == type) && 
+       (memcmp(cur->cookie, cookie, 8) == 0)) {
+      *prev = cur->next;
+      return cur;
     }
-    cur = cur->next;
+    prev = &cur->next;
   }
+
   return NULL;
 }
 
-/*
- */
-int aim_purgecookies(struct aim_session_t *sess)
+faim_internal struct aim_msgcookie_t *aim_mkcookie(unsigned char *c, int type, void *data) 
 {
-  int maxage = 5*60;
-  struct aim_msgcookie_t *cur;
-  struct aim_msgcookie_t *remed = NULL;
-  time_t curtime;
-  cur = sess->msgcookies;
+  struct aim_msgcookie_t *cookie;
+
+  if (!c)
+    return NULL;
+
+  if (!(cookie = calloc(1, sizeof(struct aim_msgcookie_t))))
+    return NULL;
   
-  curtime = time(&curtime);
-  while (cur) {
-    if ( (cur) && (((cur->addtime) + maxage) < curtime)) {
-#if DEBUG > 1
-      printf("aimmsgcookie: WARNING purged obsolete message cookie %x%x%x%x %x%x%x%x\n",
-            cur->cookie[0], cur->cookie[1], cur->cookie[2], cur->cookie[3],
-            cur->cookie[4], cur->cookie[5], cur->cookie[6], cur->cookie[7]);
-#endif
-      remed = aim_uncachecookie(sess, cur->cookie);
-      if (remed) {
-       if (remed->data)
-         free(remed->data);
-       free(remed);
-      }
-    }
-    cur = cur->next;
-  }
+  cookie->data = data;
+  cookie->type = type;
+  memcpy(cookie->cookie, c, 8);
   
-  return 0;
+  return cookie;
+}
+  
+faim_internal struct aim_msgcookie_t *aim_checkcookie(struct aim_session_t *sess, const unsigned char *cookie, const int type)
+{
+  struct aim_msgcookie_t *cur;
+
+  printf("\t\tCC check %d %s\n", type, cookie);  
+
+  for (cur = sess->msgcookies; cur; cur = cur->next) {
+    if ((cur->type == type) && 
+       (memcmp(cur->cookie, cookie, 8) == 0))
+      return cur;   
+  }
+
+  return NULL;
 }
 
+faim_internal int aim_freecookie(struct aim_session_t *sess, struct aim_msgcookie_t *cookie) {
+  struct aim_msgcookie_t *cur, **prev;
+
+  if (!sess || !cookie)
+    return -1;
+
+  /* 
+   * Make sure its not in the list somewhere still.
+   *
+   * If this actually happens, theres been a major coding failure 
+   * on my part. However, that does not reduce its occurance likelyhood.
+   */
+  for (prev = &sess->msgcookies; (cur = *prev); ) {
+    if (cur == cookie) {
+      *prev = cur->next;
+    } else
+      prev = &cur->next;
+  }
+
+  free(cookie);
+
+  return 0;
+} 
+
+faim_internal int aim_msgcookie_gettype(int reqclass) {
+  /* XXX: hokey-assed. needs fixed. */
+  switch(reqclass) {
+  case AIM_CAPS_BUDDYICON:
+    return AIM_COOKIETYPE_OFTICON;
+    break;
+  case AIM_CAPS_VOICE:
+    return AIM_COOKIETYPE_OFTVOICE;
+    break;
+  case AIM_CAPS_IMIMAGE:
+    return AIM_COOKIETYPE_OFTIMAGE;
+    break;
+  case AIM_CAPS_CHAT:
+    return AIM_COOKIETYPE_CHAT;
+    break;
+  case AIM_CAPS_GETFILE:
+    return AIM_COOKIETYPE_OFTGET;
+    break;
+  case AIM_CAPS_SENDFILE:
+    return AIM_COOKIETYPE_OFTSEND;
+    break;
+  default:
+    return AIM_COOKIETYPE_UNKNOWN;
+    break;
+  }           
+}
This page took 0.081273 seconds and 4 git commands to generate.