+
+
+/*
+ * Save a message pointer into a specified room
+ * (Returns 0 for success, nonzero for failure)
+ */
+int CtdlSaveMsgPointerInRoom(char *roomname, long msgid) {
+ int i;
+ struct quickroom qrbuf;
+ struct cdbdata *cdbfr;
+ int num_msgs;
+ long *msglist;
+ long highest_msg = 0L;
+
+ lprintf(9, "CtdlSaveMsgPointerInRoom(%s, %ld)\n", roomname, msgid);
+
+ if (lgetroom(&qrbuf, roomname) != 0) {
+ lprintf(9, "No such room <%s>\n", roomname);
+ return(ERROR + ROOM_NOT_FOUND);
+ }
+
+ cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
+ if (cdbfr == NULL) {
+ msglist = NULL;
+ num_msgs = 0;
+ } else {
+ msglist = mallok(cdbfr->len);
+ if (msglist == NULL)
+ lprintf(3, "ERROR malloc msglist!\n");
+ num_msgs = cdbfr->len / sizeof(long);
+ memcpy(msglist, cdbfr->ptr, cdbfr->len);
+ cdb_free(cdbfr);
+ }
+
+
+ /* Make sure the message doesn't already exist in this room. It
+ * is absolutely taboo to have more than one reference to the same
+ * message in a room.
+ */
+ if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
+ if (msglist[i] == msgid) {
+ lputroom(&qrbuf); /* unlock the room */
+ return(ERROR + ALREADY_EXISTS);
+ }
+ }
+
+ /* Now add the new message */
+ ++num_msgs;
+ msglist = reallok(msglist,
+ (num_msgs * sizeof(long)));
+
+ if (msglist == NULL) {
+ lprintf(3, "ERROR: can't realloc message list!\n");
+ }
+ msglist[num_msgs - 1] = msgid;
+
+ /* Sort the message list, so all the msgid's are in order */
+ num_msgs = sort_msglist(msglist, num_msgs);
+
+ /* Determine the highest message number */
+ highest_msg = msglist[num_msgs - 1];
+
+ /* Write it back to disk. */
+ cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long),
+ msglist, num_msgs * sizeof(long));
+
+ /* Free up the memory we used. */
+ phree(msglist);
+
+ /* Update the highest-message pointer and unlock the room. */
+ qrbuf.QRhighest = highest_msg;
+ lputroom(&qrbuf);
+
+ /* Bump the reference count for this message. */
+ AdjRefCount(msgid, +1);
+
+ /* Return success. */
+ lprintf(9, "CtdlSaveMsgPointerInRoom() succeeded\n");
+ return (0);
+}
+
+
+