random acts of style cleanup
[citadel.git] / citadel / euidindex.c
index dd9b171dc6e6d27b684cd88021535d671a883cd2..59d67deb95e30817b3ff72dcfcb6ae866fe5c2b9 100644 (file)
-/*
- * $Id$
- *
- * Index messages by EUID per room.
- *
- */
+// Index messages by EUID per room.
+//
+// Copyright (c) 1987-2022 by the citadel.org team
+//
+// This program is open source software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License, version 3.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
 
 #include "sysdep.h"
-#include <stdlib.h>
-#include <unistd.h>
 #include <stdio.h>
-#include <fcntl.h>
-
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-
-
-#include <ctype.h>
-#include <string.h>
-#include <limits.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <sys/stat.h>
-#include "citadel.h"
-#include "server.h"
-#include "serv_extensions.h"
-#include "database.h"
-#include "msgbase.h"
-#include "support.h"
-#include "sysdep_decls.h"
+#include <libcitadel.h>
+
 #include "citserver.h"
 #include "room_ops.h"
-#include "user_ops.h"
-#include "file_ops.h"
-#include "config.h"
-#include "control.h"
-#include "tools.h"
-#include "euidindex.h"
-
-/*
- * The structure of an euidindex record *key* is:
- *
- * |----room_number----|----------EUID-------------|
- *    (sizeof long)       (actual length of euid)
- *
- *
- * The structure of an euidindex record *value* is:
- *
- * |-----msg_number----|----room_number----|----------EUID-------------|
- *    (sizeof long)       (sizeof long)       (actual length of euid)
- *
- */
-
-
-
-/*
- * Return nonzero if the supplied room is one which should have
- * an EUID index.
- */
+
+// The structure of an euidindex record *key* is:
+//
+// |----room_number----|----------EUID-------------|
+//    (sizeof long)       (actual length of euid)
+//
+//
+// The structure of an euidindex record *value* is:
+//
+// |-----msg_number----|----room_number----|----------EUID-------------|
+//    (sizeof long)       (sizeof long)       (actual length of euid)
+
+// Return nonzero if the supplied room is one which should have
+// an EUID index.
 int DoesThisRoomNeedEuidIndexing(struct ctdlroom *qrbuf) {
 
        switch(qrbuf->QRdefaultview) {
                case VIEW_BBS:          return(0);
                case VIEW_MAILBOX:      return(0);
                case VIEW_ADDRESSBOOK:  return(1);
+               case VIEW_DRAFTS:       return(0);
                case VIEW_CALENDAR:     return(1);
                case VIEW_TASKS:        return(1);
                case VIEW_NOTES:        return(1);
                case VIEW_WIKI:         return(1);
+               case VIEW_BLOG:         return(1);
        }
        
        return(0);
 }
 
 
+// Locate a message in a given room with a given euid, and return
+// its message number.
+long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
+       return CtdlLocateMessageByEuid (euid, qrbuf);
+}
 
 
-
-
-/*
- * Locate a message in a given room with a given euid, and return
- * its message number.
- */
-long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
+long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf) {
        char *key;
        int key_len;
        struct cdbdata *cdb_euid;
        long msgnum = (-1L);
 
-       lprintf(CTDL_DEBUG, "Searching for EUID <%s> in <%s>\n", euid, qrbuf->QRname);
+       syslog(LOG_DEBUG, "euidindex: searching for EUID <%s> in <%s>", euid, qrbuf->QRname);
 
        key_len = strlen(euid) + sizeof(long) + 1;
        key = malloc(key_len);
@@ -109,28 +75,24 @@ long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
                msgnum = (-1L);
        }
        else {
-               /* The first (sizeof long) of the record is what we're
-                * looking for.  Throw away the rest.
-                */
+               // The first (sizeof long) of the record is what we're looking for.  Throw away the rest.
                memcpy(&msgnum, cdb_euid->ptr, sizeof(long));
                cdb_free(cdb_euid);
        }
-       lprintf(CTDL_DEBUG, "returning msgnum = %ld\n", msgnum);
+       syslog(LOG_DEBUG, "euidindex: returning msgnum = %ld", msgnum);
        return(msgnum);
 }
 
 
-/*
- * Store the euid index for a message, which has presumably just been
- * stored in this room by the caller.
- */
+// Store the euid index for a message, which has presumably just been
+// stored in this room by the caller.
 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
        char *key;
        int key_len;
        char *data;
        int data_len;
 
-       lprintf(CTDL_DEBUG, "Indexing message #%ld <%s> in <%s>\n", msgnum, euid, qrbuf->QRname);
+       syslog(LOG_DEBUG, "euidindex: indexing message #%ld <%s> in <%s>", msgnum, euid, qrbuf->QRname);
 
        key_len = strlen(euid) + sizeof(long) + 1;
        key = malloc(key_len);
@@ -149,19 +111,16 @@ void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
 }
 
 
-
-/*
- * Called by rebuild_euid_index_for_room() to index one message.
- */
+// Called by rebuild_euid_index_for_room() to index one message.
 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
-       struct CtdlMessage *msg;
+       struct CtdlMessage *msg = NULL;
 
        msg = CtdlFetchMessage(msgnum, 0);
        if (msg == NULL) return;
-       if (msg->cm_fields['E'] != NULL) {
-               index_message_by_euid(msg->cm_fields['E'], &CC->room, msgnum);
+       if (!CM_IsEmpty(msg, eExclusiveID)) {
+               index_message_by_euid(msg->cm_fields[eExclusiveID], &CC->room, msgnum);
        }
-       CtdlFreeMessage(msg);
+       CM_Free(msg);
 }
 
 
@@ -170,10 +129,9 @@ void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
        struct RoomProcList *ptr;
        struct ctdlroom qr;
 
-       /* Lazy programming here.  Call this function as a ForEachRoom backend
-        * in order to queue up the room names, or call it with a null room
-        * to make it do the processing.
-        */
+       // Lazy programming here.  Call this function as a CtdlForEachRoom backend
+       // in order to queue up the room names, or call it with a null room
+       // to make it do the processing.
        if (qrbuf != NULL) {
                ptr = (struct RoomProcList *)
                        malloc(sizeof (struct RoomProcList));
@@ -186,14 +144,13 @@ void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
        }
 
        while (rplist != NULL) {
-               if (getroom(&qr, rplist->name) == 0) {
+               if (CtdlGetRoom(&qr, rplist->name) == 0) {
                        if (DoesThisRoomNeedEuidIndexing(&qr)) {
-                               lprintf(CTDL_DEBUG,
-                                       "Rebuilding EUID index for <%s>\n",
+                               syslog(LOG_DEBUG,
+                                       "euidindex: rebuilding EUID index for <%s>",
                                        rplist->name);
-                               usergoto(rplist->name, 0, 0, NULL, NULL);
-                               CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
-                                       rebuild_euid_index_for_msg, NULL);
+                               CtdlUserGoto(rplist->name, 0, 0, NULL, NULL, NULL, NULL);
+                               CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, rebuild_euid_index_for_msg, NULL);
                        }
                }
                ptr = rplist;
@@ -203,20 +160,15 @@ void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
 }
 
 
-/*
- * Globally rebuild the EUID indices in every room.
- */
+// Globally rebuild the EUID indices in every room.
 void rebuild_euid_index(void) {
-       cdb_trunc(CDB_EUIDINDEX);               /* delete the old indices */
-       ForEachRoom(rebuild_euid_index_for_room, NULL); /* enumerate rm names */
-       rebuild_euid_index_for_room(NULL, NULL);        /* and index them */
+       cdb_trunc(CDB_EUIDINDEX);                               // delete the old indices
+       CtdlForEachRoom(rebuild_euid_index_for_room, NULL);     // enumerate room names
+       rebuild_euid_index_for_room(NULL, NULL);                // and index them
 }
 
 
-
-/*
- * Server command to fetch a message number given an euid.
- */
+// Server command to fetch a message number given an euid.
 void cmd_euid(char *cmdbuf) {
        char euid[256];
        long msgnum;
@@ -225,10 +177,10 @@ void cmd_euid(char *cmdbuf) {
         int num_msgs = 0;
        int i;
 
-       if (CtdlAccessCheck(ac_logged_in)) return;
+       if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
 
        extract_token(euid, cmdbuf, 0, '|', sizeof euid);
-       msgnum = locate_message_by_euid(euid, &CC->room);
+       msgnum = CtdlLocateMessageByEuid(euid, &CC->room);
        if (msgnum <= 0L) {
                cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
                return;
@@ -252,3 +204,11 @@ void cmd_euid(char *cmdbuf) {
 }
 
 
+CTDL_MODULE_INIT(euidindex)
+{
+       if (!threading) {
+               CtdlRegisterProtoHook(cmd_euid, "EUID", "Perform operations on Extended IDs for messages");
+       }
+       /* return our Subversion id for the Log */
+       return "euidindex";
+}