fix warnings - remove variables no longer needed
[citadel.git] / citadel / modules / ctdlproto / serv_messages.c
index 4d9230bddcf8107f22984813254fc8e1da2652a6..a6ebdc4e14cc392bff78a1fd2658e8f41f753a9c 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * represent messages to the citadel clients
  *
- * Copyright (c) 1987-2012 by the citadel.org team
+ * Copyright (c) 1987-2015 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.
  * 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 <sys/types.h>
-#include <regex.h>
-
-#include "md5.h"
-
 #include <libcitadel.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 "citserver.h"
-#include "room_ops.h"
+#include "ctdl_module.h"
+#include "internet_addressing.h"
 #include "user_ops.h"
-#include "file_ops.h"
+#include "room_ops.h"
 #include "config.h"
-#include "control.h"
-#include "genstamp.h"
-#include "internet_addressing.h"
-#include "euidindex.h"
-#include "journaling.h"
-#include "citadel_dirs.h"
-#include "clientsocket.h"
-#include "threads.h"
-
-#include "ctdl_module.h"
 
 extern char *msgkeys[];
 
 
-
 /*
  * Back end for the MSGS command: output message number only.
  */
@@ -78,7 +34,6 @@ void simple_listing(long msgnum, void *userdata)
 }
 
 
-
 /*
  * Back end for the MSGS command: output header summary.
  */
@@ -86,7 +41,7 @@ void headers_listing(long msgnum, void *userdata)
 {
        struct CtdlMessage *msg;
 
-       msg = CtdlFetchMessage(msgnum, 0);
+       msg = CtdlFetchMessage(msgnum, 0, 1);
        if (msg == NULL) {
                cprintf("%ld|0|||||\n", msgnum);
                return;
@@ -103,6 +58,44 @@ void headers_listing(long msgnum, void *userdata)
        CM_Free(msg);
 }
 
+typedef struct _msg_filter{
+       HashList *Filter;
+       HashPos *p;
+       StrBuf *buffer;
+}msg_filter;
+
+void headers_brief_filter(long msgnum, void *userdata)
+{
+       long i, l;
+       struct CtdlMessage *msg;
+       msg_filter *flt = (msg_filter*) userdata;
+
+       l = GetCount(flt->Filter);
+       msg = CtdlFetchMessage(msgnum, 0, 1);
+       StrBufPrintf(flt->buffer, "%ld", msgnum);
+       if (msg == NULL) {
+               for (i = 0; i < l; i++) {
+                       StrBufAppendBufPlain(flt->buffer, HKEY("|"), 0);
+               }
+       }
+       else {
+               const char *k;
+               long len;
+               void *v;
+               RewindHashPos(flt->Filter, flt->p, 0);
+               while (GetNextHashPos(flt->Filter, flt->p, &len, &k, &v)) {
+                       eMsgField f = (eMsgField) v;
+                       
+                       StrBufAppendBufPlain(flt->buffer, HKEY("|"), 0);
+                       if (!CM_IsEmpty(msg, f)) {
+                               StrBufAppendBufPlain(flt->buffer, CM_KEY(msg, f), 0);
+                       }
+               }
+       }
+       StrBufAppendBufPlain(flt->buffer, HKEY("\n"), 0);
+       cputbuf(flt->buffer);
+}
+
 /*
  * Back end for the MSGS command: output EUID header.
  */
@@ -110,7 +103,7 @@ void headers_euid(long msgnum, void *userdata)
 {
        struct CtdlMessage *msg;
 
-       msg = CtdlFetchMessage(msgnum, 0);
+       msg = CtdlFetchMessage(msgnum, 0, 1);
        if (msg == NULL) {
                cprintf("%ld||\n", msgnum);
                return;
@@ -137,9 +130,9 @@ void cmd_msgs(char *cmdbuf)
        char tfield[256];
        char tvalue[256];
        int cm_ref = 0;
-       int i;
        int with_template = 0;
        struct CtdlMessage *template = NULL;
+        msg_filter filt;
        char search_string[1024];
        ForEachMsgCallback CallBack;
 
@@ -161,6 +154,10 @@ void cmd_msgs(char *cmdbuf)
        case MSG_HDRS_EUID:
                CallBack = headers_euid;
                break;
+       case MSG_HDRS_BRIEFFILTER:
+               with_template = 2;
+               CallBack = headers_brief_filter;
+               break;
        }
 
        strcat(which, "   ");
@@ -181,13 +178,14 @@ void cmd_msgs(char *cmdbuf)
        else
                mode = MSGS_ALL;
 
-       if ( (mode == MSGS_SEARCH) && (!config.c_enable_fulltext) ) {
+       if ( (mode == MSGS_SEARCH) && (!CtdlGetConfigInt("c_enable_fulltext")) ) {
                cprintf("%d Full text index is not enabled on this server.\n",
                        ERROR + CMD_NOT_SUPPORTED);
                return;
        }
 
-       if (with_template) {
+       if (with_template == 1) {
+               memset(buf, 0, 5);
                unbuffer_output();
                cprintf("%d Send template then receive message list\n",
                        START_CHAT_MODE);
@@ -198,29 +196,66 @@ void cmd_msgs(char *cmdbuf)
                template->cm_anon_type = MES_NORMAL;
 
                while(client_getln(buf, sizeof buf) >= 0 && strcmp(buf,"000")) {
+                       eMsgField f;
                        long tValueLen;
-                       extract_token(tfield, buf, 0, '|', sizeof tfield);
-                       tValueLen = extract_token(tvalue, buf, 1, '|', sizeof tvalue);
-                       for (i='A'; i<='Z'; ++i) if (msgkeys[i]!=NULL) {
-                               if (!strcasecmp(tfield, msgkeys[i])) {
-                                       CM_SetField(template, i, tvalue, tValueLen);
+
+                       tValueLen = extract_token(tfield, buf, 0, '|', sizeof tfield);
+                       if ((tValueLen == 4) && GetFieldFromMnemonic(&f, tfield))
+                       {
+                               tValueLen = extract_token(tvalue, buf, 1, '|', sizeof tvalue);
+                               if (tValueLen >= 0) {
+                                       CM_SetField(template, f, tvalue, tValueLen);
                                }
                        }
                }
                buffer_output();
        }
+       else if (with_template == 2) {
+               long i = 0;
+               memset(buf, 0, 5);
+               cprintf("%d Send list of headers\n",
+                       START_CHAT_MODE);
+               filt.Filter = NewHash(1, lFlathash);
+               filt.buffer = NewStrBufPlain(NULL, 1024);
+               while(client_getln(buf, sizeof buf) >= 0 && strcmp(buf,"000")) {
+                       eMsgField f;
+       
+                       if (GetFieldFromMnemonic(&f, buf))
+                       {
+                               Put(filt.Filter, LKEY(i), (void*)f, reference_free_handler);
+                               i++;
+                       }
+               }
+               filt.p = GetNewHashPos(filt.Filter, 0);
+               buffer_output();
+       }
        else {
                cprintf("%d  \n", LISTING_FOLLOWS);
        }
 
-       CtdlForEachMessage(mode,
-                          ( (mode == MSGS_SEARCH) ? 0 : cm_ref ),
-                          ( (mode == MSGS_SEARCH) ? search_string : NULL ),
-                          NULL,
-                          template,
-                          CallBack,
-                          NULL);
-       if (template != NULL) CM_Free(template);
+       if (with_template < 2) {
+               CtdlForEachMessage(mode,
+                                  ( (mode == MSGS_SEARCH) ? 0 : cm_ref ),
+                                  ( (mode == MSGS_SEARCH) ? search_string : NULL ),
+                                  NULL,
+                                  template,
+                                  CallBack,
+                                  NULL);
+               if (template != NULL) CM_Free(template);
+       }
+       else {
+               CtdlForEachMessage(mode,
+                                  ( (mode == MSGS_SEARCH) ? 0 : cm_ref ),
+                                  ( (mode == MSGS_SEARCH) ? search_string : NULL ),
+                                  NULL,
+                                  NULL,
+                                  CallBack,
+                                  &filt);
+               DeleteHashPos(&filt.p);
+               DeleteHash(&filt.Filter);
+               FreeStrBuf(&filt.buffer);
+               
+       }
        cprintf("000\n");
 }
 
@@ -235,7 +270,7 @@ void cmd_msg0(char *cmdbuf)
        msgid = extract_long(cmdbuf, 0);
        headers_only = extract_int(cmdbuf, 1);
 
-       CtdlOutputMsg(msgid, MT_CITADEL, headers_only, 1, 0, NULL, 0, NULL, NULL);
+       CtdlOutputMsg(msgid, MT_CITADEL, headers_only, 1, 0, NULL, 0, NULL, NULL, NULL);
        return;
 }
 
@@ -251,7 +286,7 @@ void cmd_msg2(char *cmdbuf)
        msgid = extract_long(cmdbuf, 0);
        headers_only = extract_int(cmdbuf, 1);
 
-       CtdlOutputMsg(msgid, MT_RFC822, headers_only, 1, 1, NULL, 0, NULL, NULL);
+       CtdlOutputMsg(msgid, MT_RFC822, headers_only, 1, 1, NULL, 0, NULL, NULL, NULL);
 }
 
 
@@ -272,7 +307,7 @@ void cmd_msg3(char *cmdbuf)
        }
 
        msgnum = extract_long(cmdbuf, 0);
-       msg = CtdlFetchMessage(msgnum, 1);
+       msg = CtdlFetchMessage(msgnum, 1, 1);
        if (msg == NULL) {
                cprintf("%d Message %ld not found.\n", 
                        ERROR + MESSAGE_NOT_FOUND, msgnum);
@@ -305,7 +340,7 @@ void cmd_msg4(char *cmdbuf)
 
        msgid = extract_long(cmdbuf, 0);
        extract_token(section, cmdbuf, 1, '|', sizeof section);
-       CtdlOutputMsg(msgid, MT_MIME, 0, 1, 0, (section[0] ? section : NULL) , 0, NULL, NULL);
+       CtdlOutputMsg(msgid, MT_MIME, 0, 1, 0, (section[0] ? section : NULL) , 0, NULL, NULL, NULL);
 }
 
 
@@ -338,7 +373,7 @@ void cmd_opna(char *cmdbuf)
        extract_token(desired_section, cmdbuf, 1, '|', sizeof desired_section);
        safestrncpy(CC->download_desired_section, desired_section,
                sizeof CC->download_desired_section);
-       CtdlOutputMsg(msgid, MT_DOWNLOAD, 0, 1, 1, NULL, 0, NULL, NULL);
+       CtdlOutputMsg(msgid, MT_DOWNLOAD, 0, 1, 1, NULL, 0, NULL, NULL, NULL);
 }                      
 
 
@@ -354,7 +389,7 @@ void cmd_dlat(char *cmdbuf)
        extract_token(desired_section, cmdbuf, 1, '|', sizeof desired_section);
        safestrncpy(CC->download_desired_section, desired_section,
                sizeof CC->download_desired_section);
-       CtdlOutputMsg(msgid, MT_SPEW_SECTION, 0, 1, 1, NULL, 0, NULL, NULL);
+       CtdlOutputMsg(msgid, MT_SPEW_SECTION, 0, 1, 1, NULL, 0, NULL, NULL, NULL);
 }
 
 /*
@@ -376,10 +411,10 @@ void cmd_ent0(char *entargs)
        int anonymous = 0;
        char errmsg[SIZ];
        int err = 0;
-       struct recptypes *valid = NULL;
-       struct recptypes *valid_to = NULL;
-       struct recptypes *valid_cc = NULL;
-       struct recptypes *valid_bcc = NULL;
+       recptypes *valid = NULL;
+       recptypes *valid_to = NULL;
+       recptypes *valid_cc = NULL;
+       recptypes *valid_bcc = NULL;
        char subject[SIZ];
        int subject_required = 0;
        int do_confirm = 0;
@@ -404,6 +439,7 @@ void cmd_ent0(char *entargs)
        switch(CC->room.QRdefaultview) {
        case VIEW_NOTES:
        case VIEW_WIKI:
+       case VIEW_WIKIMD:
                extract_token(supplied_euid, entargs, 9, '|', sizeof supplied_euid);
                break;
        default:
@@ -642,13 +678,13 @@ void cmd_ent0(char *entargs)
        }
        free(all_recps);
 
-       if ((valid != NULL) && (valid->num_room == 1))
+       if ((valid != NULL) && (valid->num_room == 1) && !IsEmptyStr(valid->recp_orgroom))
        {
                /* posting into an ML room? set the envelope from 
                 * to the actual mail address so others get a valid
                 * reply-to-header.
                 */
-               msg->cm_fields[eenVelopeTo] = strdup(valid->recp_orgroom);
+               CM_SetField(msg, eenVelopeTo, valid->recp_orgroom, strlen(valid->recp_orgroom));
        }
 
        if (msg != NULL) {