* msgbase.c: reimplemented cmd_move()
authorArt Cancro <ajc@citadel.org>
Thu, 15 Oct 1998 23:29:08 +0000 (23:29 +0000)
committerArt Cancro <ajc@citadel.org>
Thu, 15 Oct 1998 23:29:08 +0000 (23:29 +0000)
          room_ops.c: wrote AddMessageToRoom() which is used for both entering
          and moving messages.

citadel/ChangeLog
citadel/msgbase.c
citadel/room_ops.c
citadel/room_ops.h

index 778e6ee409cd63a9047799a0274787f32ea9fc99..3bd2c2e2ad099d71747474efb68cbb435a0f6a1a 100644 (file)
@@ -1,6 +1,10 @@
+Thu Oct 15 19:27:32 EDT 1998 Art Cancro <ajc@uncnsrd.mt-kisco.ny.us>
+       * msgbase.c: reimplemented cmd_move()
+         room_ops.c: wrote AddMessageToRoom() which is used for both entering
+         and moving messages.
+
 Wed Oct 14 22:41:16 EDT 1998 Art Cancro <ajc@uncnsrd.mt-kisco.ny.us>
        * Misc code cleanup
-       * Initial work on new implementation of move_message()
 
 1998-10-13 Nathan Bryant <bryant@cs.usm.maine.edu>
        * configure.in: don't check for -lcrypt unless autologin is enabled
index 4a6a1a735d365919e818fe4702868dc7a900b46d..9b825fbb2e8b7edf53cb07ddb26c680d69407c98 100644 (file)
@@ -743,21 +743,9 @@ void save_message(char *mtmp,      /* file containing proper message */
 
        /* read in the quickroom record, obtaining a lock... */
        lgetroom(&CC->quickroom, actual_rm);
-       get_msglist(&CC->quickroom);
-
-       /* FIX here's where we have to handle message expiry!! */
-
-       /* Now add the new message */
-       CC->num_msgs = CC->num_msgs + 1;
-       CC->msglist = realloc(CC->msglist,
-               ((CC->num_msgs) * sizeof(long)) );
-       if (CC->msglist == NULL) {
-               lprintf(3, "ERROR: can't realloc message list!\n");
-               }
-       SetMessageInList(CC->num_msgs - 1, newmsgid);
 
-       /* Write it back to disk. */
-       put_msglist(&CC->quickroom);
+       /* Add the message pointer to the room */
+       AddMessageToRoom(&CC->quickroom, newmsgid);
 
        /* update quickroom */
        CC->quickroom.QRhighest = newmsgid;
@@ -1192,13 +1180,8 @@ void cmd_move(char *args)
 
        /* put the message into the target room */
        lgetroom(&qtemp, targ);
-       get_msglist(&qtemp);
-       CC->num_msgs = CC->num_msgs + 1;
-       CC->msglist = realloc(CC->msglist, (CC->num_msgs * sizeof(long)) );
-       SetMessageInList(CC->num_msgs - 1, num);
-       CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
-       put_msglist(&qtemp);
+       AddMessageToRoom(&qtemp, num);
        lputroom(&qtemp, targ);
 
-       cprintf("%d I think this worked, FIX check to make sure\n", OK);
+       cprintf("%d Message moved.\n", OK);
        }
index b412c54b908e95d2d892ecd6952f1fb8d9a9c970..241d158763c886bef7a779c49e317037f7ea5911 100644 (file)
@@ -259,15 +259,23 @@ void ForEachRoom(void (*CallBack)(struct quickroom *EachRoom)) {
 
 
 /*
- * get_msglist()  -  retrieve room message pointers
+ * Create a database key for storage of message lists
  */
-void get_msglist(struct quickroom *whichroom) {
-       struct cdbdata *cdbfr;
-       char dbkey[256];
+void msglist_key(char *dbkey, struct quickroom *whichroom) {
        int a;
        
        sprintf(dbkey, "%s%ld", whichroom->QRname, whichroom->QRgen);
        for (a=0; a<strlen(dbkey); ++a) dbkey[a]=tolower(dbkey[a]);
+       }
+
+/*
+ * get_msglist()  -  retrieve room message pointers
+ */
+void get_msglist(struct quickroom *whichroom) {
+       struct cdbdata *cdbfr;
+       char dbkey[256];
+
+       msglist_key(dbkey, whichroom);  
        
        if (CC->msglist != NULL) {
                free(CC->msglist);
@@ -276,7 +284,6 @@ void get_msglist(struct quickroom *whichroom) {
        CC->num_msgs = 0;
 
        cdbfr = cdb_fetch(CDB_MSGLISTS, dbkey, strlen(dbkey));
-
        if (cdbfr == NULL) {
                return;
                }
@@ -293,11 +300,8 @@ void get_msglist(struct quickroom *whichroom) {
  */
 void put_msglist(struct quickroom *whichroom) {
        char dbkey[256];
-       int a;
-
-       sprintf(dbkey, "%s%ld", whichroom->QRname, whichroom->QRgen);
-       for (a=0; a<strlen(dbkey); ++a) dbkey[a]=tolower(dbkey[a]);
 
+       msglist_key(dbkey, whichroom);  
        cdb_store(CDB_MSGLISTS, dbkey, strlen(dbkey),
                CC->msglist, CC->num_msgs * sizeof(long));
        }
@@ -308,15 +312,57 @@ void put_msglist(struct quickroom *whichroom) {
  */
 void delete_msglist(struct quickroom *whichroom) {
        char dbkey[256];
-       int a;
-
-       sprintf(dbkey, "%s%ld", whichroom->QRname, whichroom->QRgen);
-       for (a=0; a<strlen(dbkey); ++a) dbkey[a]=tolower(dbkey[a]);
 
+       msglist_key(dbkey, whichroom);  
        cdb_delete(CDB_MSGLISTS, dbkey, strlen(dbkey));
        }
 
 
+/* 
+ * Add a message number to a room's message list.  
+ * So, why doesn't this function use the get_msglist() and put_msglist() stuff
+ * defined above?  Because the room the message number is being written to
+ * may not be the current room (as is the case with cmd_move() for example).
+ */
+void AddMessageToRoom(struct quickroom *whichroom, long newmsgid) {
+       char dbkey[256];
+       struct cdbdata *cdbfr;
+       int num_msgs;
+       long *msglist;
+       
+       msglist_key(dbkey, whichroom);
+       cdbfr = cdb_fetch(CDB_MSGLISTS, dbkey, strlen(dbkey));
+       if (cdbfr == NULL) {
+               msglist = NULL;
+               num_msgs = 0;
+               }
+       else {
+               msglist = malloc(cdbfr->len);
+               num_msgs = cdbfr->len / sizeof(long);
+               memcpy(msglist, cdbfr->ptr, cdbfr->len);
+               cdb_free(cdbfr);
+               }
+       
+       /* Now add the new message */
+       ++num_msgs;
+       msglist = realloc(msglist,
+               (num_msgs * sizeof(long)) );
+
+       if (msglist == NULL) {
+               lprintf(3, "ERROR: can't realloc message list!\n");
+               }
+
+       msglist[num_msgs - 1] = newmsgid;
+
+       /* Write it back to disk. */
+       cdb_store(CDB_MSGLISTS, dbkey, strlen(dbkey),
+               msglist, num_msgs * sizeof(long));
+
+       /* And finally, free up the memory we used. */
+       free(msglist);
+       }
+
+
 /*
  * MessageFromList()  -  get a message number from the list currently in memory
  */
index 6caffbb33ba7f158b114fd26a63340d4e3ac59dc..77aac6828dc31a2c8d0b56debba8ff38c026c24c 100644 (file)
@@ -14,6 +14,7 @@ void putfloor (struct floor *flbuf, int floor_num);
 void lputfloor (struct floor *flbuf, int floor_num);
 void get_msglist (struct quickroom *whichroom);
 void put_msglist (struct quickroom *whichroom);
+void AddMessageToRoom(struct quickroom *whichroom, long newmsgid);
 long int MessageFromList (int whichpos);
 void SetMessageInList (int whichpos, long int newmsgnum);
 int sort_msglist (long int *listptrs, int oldcount);