Add hashes of message id and references to msgs commands with headers
[citadel.git] / citadel / modules / ctdlproto / serv_messages.c
1 /*
2  * represent messages to the citadel clients
3  *
4  * Copyright (c) 1987-2017 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <stdio.h>
16 #include <libcitadel.h>
17
18 #include "citserver.h"
19 #include "ctdl_module.h"
20 #include "internet_addressing.h"
21 #include "user_ops.h"
22 #include "room_ops.h"
23 #include "config.h"
24
25 extern char *msgkeys[];
26
27
28 /*
29  * Back end for the MSGS command: output message number only.
30  */
31 void simple_listing(long msgnum, void *userdata)
32 {
33         cprintf("%ld\n", msgnum);
34 }
35
36
37 /*
38  * Back end for the MSGS command: output header summary.
39  */
40 void headers_listing(long msgnum, void *userdata)
41 {
42         struct CtdlMessage *msg;
43
44         msg = CtdlFetchMessage(msgnum, 0, 1);
45         if (msg == NULL) {
46                 cprintf("%ld|0|||||||\n", msgnum);
47                 return;
48         }
49
50         // output all fields except the references hash
51         cprintf("%ld|%s|%s|%s|%s|%s|%d|",
52                 msgnum,
53                 (!CM_IsEmpty(msg, eTimestamp) ? msg->cm_fields[eTimestamp] : "0"),
54                 (!CM_IsEmpty(msg, eAuthor) ? msg->cm_fields[eAuthor] : ""),
55                 (!CM_IsEmpty(msg, eNodeName) ? msg->cm_fields[eNodeName] : ""),
56                 (!CM_IsEmpty(msg, erFc822Addr) ? msg->cm_fields[erFc822Addr] : ""),
57                 (!CM_IsEmpty(msg, eMsgSubject) ? msg->cm_fields[eMsgSubject] : ""),
58                 (!CM_IsEmpty(msg, emessageId) ? HashLittle(msg->cm_fields[emessageId],strlen(msg->cm_fields[emessageId])) : 0)
59         );
60
61         // output the references hash (yes it's ok that we're trashing the source buffer by doing this)
62         if (!CM_IsEmpty(msg, eWeferences)) {
63                 char *token;
64                 char *rest = msg->cm_fields[eWeferences];
65                 char *prev = rest;
66                 while((token = strtok_r(rest, "|", &rest))) {
67                         cprintf("%d%s", HashLittle(token,rest-prev-(*rest==0?0:1)), (*rest==0?"":","));
68                         prev = rest;
69                 }
70         }
71
72         cprintf("|\n");
73         CM_Free(msg);
74 }
75
76 typedef struct _msg_filter{
77         HashList *Filter;
78         HashPos *p;
79         StrBuf *buffer;
80 }msg_filter;
81
82 void headers_brief_filter(long msgnum, void *userdata)
83 {
84         long i, l;
85         struct CtdlMessage *msg;
86         msg_filter *flt = (msg_filter*) userdata;
87
88         l = GetCount(flt->Filter);
89         msg = CtdlFetchMessage(msgnum, 0, 1);
90         StrBufPrintf(flt->buffer, "%ld", msgnum);
91         if (msg == NULL) {
92                 for (i = 0; i < l; i++) {
93                         StrBufAppendBufPlain(flt->buffer, HKEY("|"), 0);
94                 }
95         }
96         else {
97                 const char *k;
98                 long len;
99                 void *v;
100                 RewindHashPos(flt->Filter, flt->p, 0);
101                 while (GetNextHashPos(flt->Filter, flt->p, &len, &k, &v)) {
102                         eMsgField f = (eMsgField) v;
103                         
104                         StrBufAppendBufPlain(flt->buffer, HKEY("|"), 0);
105                         if (!CM_IsEmpty(msg, f)) {
106                                 StrBufAppendBufPlain(flt->buffer, CM_KEY(msg, f), 0);
107                         }
108                 }
109         }
110         StrBufAppendBufPlain(flt->buffer, HKEY("\n"), 0);
111         cputbuf(flt->buffer);
112 }
113
114 /*
115  * Back end for the MSGS command: output EUID header.
116  */
117 void headers_euid(long msgnum, void *userdata)
118 {
119         struct CtdlMessage *msg;
120
121         msg = CtdlFetchMessage(msgnum, 0, 1);
122         if (msg == NULL) {
123                 cprintf("%ld||\n", msgnum);
124                 return;
125         }
126
127         cprintf("%ld|%s|%s\n", 
128                 msgnum, 
129                 (!CM_IsEmpty(msg, eExclusiveID) ? msg->cm_fields[eExclusiveID] : ""),
130                 (!CM_IsEmpty(msg, eTimestamp) ? msg->cm_fields[eTimestamp] : "0"));
131         CM_Free(msg);
132 }
133
134
135
136 /*
137  * cmd_msgs()  -  get list of message #'s in this room
138  *              implements the MSGS server command using CtdlForEachMessage()
139  */
140 void cmd_msgs(char *cmdbuf)
141 {
142         int mode = 0;
143         char which[16];
144         char buf[256];
145         char tfield[256];
146         char tvalue[256];
147         int cm_ref = 0;
148         int with_template = 0;
149         struct CtdlMessage *template = NULL;
150         msg_filter filt;
151         char search_string[1024];
152         ForEachMsgCallback CallBack;
153
154         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
155
156         extract_token(which, cmdbuf, 0, '|', sizeof which);
157         cm_ref = extract_int(cmdbuf, 1);
158         extract_token(search_string, cmdbuf, 1, '|', sizeof search_string);
159         with_template = extract_int(cmdbuf, 2);
160         switch (extract_int(cmdbuf, 3))
161         {
162         default:
163         case MSG_HDRS_BRIEF:
164                 CallBack = simple_listing;
165                 break;
166         case MSG_HDRS_ALL:
167                 CallBack = headers_listing;
168                 break;
169         case MSG_HDRS_EUID:
170                 CallBack = headers_euid;
171                 break;
172         case MSG_HDRS_BRIEFFILTER:
173                 with_template = 2;
174                 CallBack = headers_brief_filter;
175                 break;
176         }
177
178         strcat(which, "   ");
179         if (!strncasecmp(which, "OLD", 3))
180                 mode = MSGS_OLD;
181         else if (!strncasecmp(which, "NEW", 3))
182                 mode = MSGS_NEW;
183         else if (!strncasecmp(which, "FIRST", 5))
184                 mode = MSGS_FIRST;
185         else if (!strncasecmp(which, "LAST", 4))
186                 mode = MSGS_LAST;
187         else if (!strncasecmp(which, "GT", 2))
188                 mode = MSGS_GT;
189         else if (!strncasecmp(which, "LT", 2))
190                 mode = MSGS_LT;
191         else if (!strncasecmp(which, "SEARCH", 6))
192                 mode = MSGS_SEARCH;
193         else
194                 mode = MSGS_ALL;
195
196         if ( (mode == MSGS_SEARCH) && (!CtdlGetConfigInt("c_enable_fulltext")) ) {
197                 cprintf("%d Full text index is not enabled on this server.\n",
198                         ERROR + CMD_NOT_SUPPORTED);
199                 return;
200         }
201
202         if (with_template == 1) {
203                 memset(buf, 0, 5);
204                 unbuffer_output();
205                 cprintf("%d Send template then receive message list\n",
206                         START_CHAT_MODE);
207                 template = (struct CtdlMessage *)
208                         malloc(sizeof(struct CtdlMessage));
209                 memset(template, 0, sizeof(struct CtdlMessage));
210                 template->cm_magic = CTDLMESSAGE_MAGIC;
211                 template->cm_anon_type = MES_NORMAL;
212
213                 while(client_getln(buf, sizeof buf) >= 0 && strcmp(buf,"000")) {
214                         eMsgField f;
215                         long tValueLen;
216
217                         tValueLen = extract_token(tfield, buf, 0, '|', sizeof tfield);
218                         if ((tValueLen == 4) && GetFieldFromMnemonic(&f, tfield))
219                         {
220                                 tValueLen = extract_token(tvalue, buf, 1, '|', sizeof tvalue);
221                                 if (tValueLen >= 0) {
222                                         CM_SetField(template, f, tvalue, tValueLen);
223                                 }
224                         }
225                 }
226                 buffer_output();
227         }
228         else if (with_template == 2) {
229                 long i = 0;
230                 memset(buf, 0, 5);
231                 cprintf("%d Send list of headers\n",
232                         START_CHAT_MODE);
233                 filt.Filter = NewHash(1, lFlathash);
234                 filt.buffer = NewStrBufPlain(NULL, 1024);
235                 while(client_getln(buf, sizeof buf) >= 0 && strcmp(buf,"000")) {
236                         eMsgField f;
237         
238                         if (GetFieldFromMnemonic(&f, buf))
239                         {
240                                 Put(filt.Filter, LKEY(i), (void*)f, reference_free_handler);
241                                 i++;
242                         }
243                 }
244                 filt.p = GetNewHashPos(filt.Filter, 0);
245                 buffer_output();
246         }
247         else {
248                 cprintf("%d  \n", LISTING_FOLLOWS);
249         }
250
251         if (with_template < 2) {
252                 CtdlForEachMessage(mode,
253                                    ( (mode == MSGS_SEARCH) ? 0 : cm_ref ),
254                                    ( (mode == MSGS_SEARCH) ? search_string : NULL ),
255                                    NULL,
256                                    template,
257                                    CallBack,
258                                    NULL);
259                 if (template != NULL) CM_Free(template);
260         }
261         else {
262                 CtdlForEachMessage(mode,
263                                    ( (mode == MSGS_SEARCH) ? 0 : cm_ref ),
264                                    ( (mode == MSGS_SEARCH) ? search_string : NULL ),
265                                    NULL,
266                                    NULL,
267                                    CallBack,
268                                    &filt);
269                 DeleteHashPos(&filt.p);
270                 DeleteHash(&filt.Filter);
271                 FreeStrBuf(&filt.buffer);
272                 
273         }
274         cprintf("000\n");
275 }
276
277 /*
278  * display a message (mode 0 - Citadel proprietary)
279  */
280 void cmd_msg0(char *cmdbuf)
281 {
282         long msgid;
283         int headers_only = HEADERS_ALL;
284
285         msgid = extract_long(cmdbuf, 0);
286         headers_only = extract_int(cmdbuf, 1);
287
288         CtdlOutputMsg(msgid, MT_CITADEL, headers_only, 1, 0, NULL, 0, NULL, NULL, NULL);
289         return;
290 }
291
292
293 /*
294  * display a message (mode 2 - RFC822)
295  */
296 void cmd_msg2(char *cmdbuf)
297 {
298         long msgid;
299         int headers_only = HEADERS_ALL;
300
301         msgid = extract_long(cmdbuf, 0);
302         headers_only = extract_int(cmdbuf, 1);
303
304         CtdlOutputMsg(msgid, MT_RFC822, headers_only, 1, 1, NULL, 0, NULL, NULL, NULL);
305 }
306
307
308
309 /* 
310  * display a message (mode 3 - IGnet raw format - internal programs only)
311  */
312 void cmd_msg3(char *cmdbuf)
313 {
314         long msgnum;
315         struct CtdlMessage *msg = NULL;
316         struct ser_ret smr;
317
318         if (CC->internal_pgm == 0) {
319                 cprintf("%d This command is for internal programs only.\n",
320                         ERROR + HIGHER_ACCESS_REQUIRED);
321                 return;
322         }
323
324         msgnum = extract_long(cmdbuf, 0);
325         msg = CtdlFetchMessage(msgnum, 1, 1);
326         if (msg == NULL) {
327                 cprintf("%d Message %ld not found.\n", 
328                         ERROR + MESSAGE_NOT_FOUND, msgnum);
329                 return;
330         }
331
332         CtdlSerializeMessage(&smr, msg);
333         CM_Free(msg);
334
335         if (smr.len == 0) {
336                 cprintf("%d Unable to serialize message\n",
337                         ERROR + INTERNAL_ERROR);
338                 return;
339         }
340
341         cprintf("%d %ld\n", BINARY_FOLLOWS, (long)smr.len);
342         client_write((char *)smr.ser, (int)smr.len);
343         free(smr.ser);
344 }
345
346
347
348 /* 
349  * Display a message using MIME content types
350  */
351 void cmd_msg4(char *cmdbuf)
352 {
353         long msgid;
354         char section[64];
355
356         msgid = extract_long(cmdbuf, 0);
357         extract_token(section, cmdbuf, 1, '|', sizeof section);
358         CtdlOutputMsg(msgid, MT_MIME, 0, 1, 0, (section[0] ? section : NULL) , 0, NULL, NULL, NULL);
359 }
360
361
362
363 /* 
364  * Client tells us its preferred message format(s)
365  */
366 void cmd_msgp(char *cmdbuf)
367 {
368         if (!strcasecmp(cmdbuf, "dont_decode")) {
369                 CC->msg4_dont_decode = 1;
370                 cprintf("%d MSG4 will not pre-decode messages.\n", CIT_OK);
371         }
372         else {
373                 safestrncpy(CC->preferred_formats, cmdbuf, sizeof(CC->preferred_formats));
374                 cprintf("%d Preferred MIME formats have been set.\n", CIT_OK);
375         }
376 }
377
378
379 /*
380  * Open a component of a MIME message as a download file 
381  */
382 void cmd_opna(char *cmdbuf)
383 {
384         long msgid;
385         char desired_section[128];
386
387         msgid = extract_long(cmdbuf, 0);
388         extract_token(desired_section, cmdbuf, 1, '|', sizeof desired_section);
389         safestrncpy(CC->download_desired_section, desired_section,
390                 sizeof CC->download_desired_section);
391         CtdlOutputMsg(msgid, MT_DOWNLOAD, 0, 1, 1, NULL, 0, NULL, NULL, NULL);
392 }                       
393
394
395 /*
396  * Open a component of a MIME message and transmit it all at once
397  */
398 void cmd_dlat(char *cmdbuf)
399 {
400         long msgid;
401         char desired_section[128];
402
403         msgid = extract_long(cmdbuf, 0);
404         extract_token(desired_section, cmdbuf, 1, '|', sizeof desired_section);
405         safestrncpy(CC->download_desired_section, desired_section,
406                 sizeof CC->download_desired_section);
407         CtdlOutputMsg(msgid, MT_SPEW_SECTION, 0, 1, 1, NULL, 0, NULL, NULL, NULL);
408 }
409
410 /*
411  * message entry  -  mode 0 (normal)
412  */
413 void cmd_ent0(char *entargs)
414 {
415         struct CitContext *CCC = CC;
416         int post = 0;
417         char recp[SIZ];
418         char cc[SIZ];
419         char bcc[SIZ];
420         char supplied_euid[128];
421         int anon_flag = 0;
422         int format_type = 0;
423         char newusername[256];
424         char newuseremail[256];
425         struct CtdlMessage *msg;
426         int anonymous = 0;
427         char errmsg[SIZ];
428         int err = 0;
429         recptypes *valid = NULL;
430         recptypes *valid_to = NULL;
431         recptypes *valid_cc = NULL;
432         recptypes *valid_bcc = NULL;
433         char subject[SIZ];
434         int subject_required = 0;
435         int do_confirm = 0;
436         long msgnum;
437         int i, j;
438         char buf[256];
439         int newuseremail_ok = 0;
440         char references[SIZ];
441         char *ptr;
442
443         unbuffer_output();
444
445         post = extract_int(entargs, 0);
446         extract_token(recp, entargs, 1, '|', sizeof recp);
447         anon_flag = extract_int(entargs, 2);
448         format_type = extract_int(entargs, 3);
449         extract_token(subject, entargs, 4, '|', sizeof subject);
450         extract_token(newusername, entargs, 5, '|', sizeof newusername);
451         do_confirm = extract_int(entargs, 6);
452         extract_token(cc, entargs, 7, '|', sizeof cc);
453         extract_token(bcc, entargs, 8, '|', sizeof bcc);
454         switch(CC->room.QRdefaultview) {
455         case VIEW_NOTES:
456         case VIEW_WIKI:
457         case VIEW_WIKIMD:
458                 extract_token(supplied_euid, entargs, 9, '|', sizeof supplied_euid);
459                 break;
460         default:
461                 supplied_euid[0] = 0;
462                 break;
463         }
464         extract_token(newuseremail, entargs, 10, '|', sizeof newuseremail);
465         extract_token(references, entargs, 11, '|', sizeof references);
466         for (ptr=references; *ptr != 0; ++ptr) {
467                 if (*ptr == '!') *ptr = '|';
468         }
469
470         /* first check to make sure the request is valid. */
471
472         err = CtdlDoIHavePermissionToPostInThisRoom(
473                 errmsg,
474                 sizeof errmsg,
475                 NULL,
476                 POST_LOGGED_IN,
477                 (!IsEmptyStr(references))               /* is this a reply?  or a top-level post? */
478                 );
479         if (err)
480         {
481                 cprintf("%d %s\n", err, errmsg);
482                 return;
483         }
484
485         /* Check some other permission type things. */
486
487         if (IsEmptyStr(newusername)) {
488                 strcpy(newusername, CCC->user.fullname);
489         }
490         if (  (CCC->user.axlevel < AxAideU)
491               && (strcasecmp(newusername, CCC->user.fullname))
492               && (strcasecmp(newusername, CCC->cs_inet_fn))
493                 ) {     
494                 cprintf("%d You don't have permission to author messages as '%s'.\n",
495                         ERROR + HIGHER_ACCESS_REQUIRED,
496                         newusername
497                         );
498                 return;
499         }
500
501
502         if (IsEmptyStr(newuseremail)) {
503                 newuseremail_ok = 1;
504         }
505
506         if (!IsEmptyStr(newuseremail)) {
507                 if (!strcasecmp(newuseremail, CCC->cs_inet_email)) {
508                         newuseremail_ok = 1;
509                 }
510                 else if (!IsEmptyStr(CCC->cs_inet_other_emails)) {
511                         j = num_tokens(CCC->cs_inet_other_emails, '|');
512                         for (i=0; i<j; ++i) {
513                                 extract_token(buf, CCC->cs_inet_other_emails, i, '|', sizeof buf);
514                                 if (!strcasecmp(newuseremail, buf)) {
515                                         newuseremail_ok = 1;
516                                 }
517                         }
518                 }
519         }
520
521         if (!newuseremail_ok) {
522                 cprintf("%d You don't have permission to author messages as '%s'.\n",
523                         ERROR + HIGHER_ACCESS_REQUIRED,
524                         newuseremail
525                         );
526                 return;
527         }
528
529         CCC->cs_flags |= CS_POSTING;
530
531         /* In mailbox rooms we have to behave a little differently --
532          * make sure the user has specified at least one recipient.  Then
533          * validate the recipient(s).  We do this for the Mail> room, as
534          * well as any room which has the "Mailbox" view set - unless it
535          * is the DRAFTS room which does not require recipients
536          */
537
538         if ( (  ( (CCC->room.QRflags & QR_MAILBOX) && (!strcasecmp(&CCC->room.QRname[11], MAILROOM)) )
539                 || ( (CCC->room.QRflags & QR_MAILBOX) && (CCC->curr_view == VIEW_MAILBOX) )
540                      ) && (strcasecmp(&CCC->room.QRname[11], USERDRAFTROOM)) !=0 ) {
541                 if (CCC->user.axlevel < AxProbU) {
542                         strcpy(recp, "sysop");
543                         strcpy(cc, "");
544                         strcpy(bcc, "");
545                 }
546
547                 valid_to = validate_recipients(recp, NULL, 0);
548                 if (valid_to->num_error > 0) {
549                         cprintf("%d %s\n", ERROR + NO_SUCH_USER, valid_to->errormsg);
550                         free_recipients(valid_to);
551                         return;
552                 }
553
554                 valid_cc = validate_recipients(cc, NULL, 0);
555                 if (valid_cc->num_error > 0) {
556                         cprintf("%d %s\n", ERROR + NO_SUCH_USER, valid_cc->errormsg);
557                         free_recipients(valid_to);
558                         free_recipients(valid_cc);
559                         return;
560                 }
561
562                 valid_bcc = validate_recipients(bcc, NULL, 0);
563                 if (valid_bcc->num_error > 0) {
564                         cprintf("%d %s\n", ERROR + NO_SUCH_USER, valid_bcc->errormsg);
565                         free_recipients(valid_to);
566                         free_recipients(valid_cc);
567                         free_recipients(valid_bcc);
568                         return;
569                 }
570
571                 /* Recipient required, but none were specified */
572                 if ( (valid_to->num_error < 0) && (valid_cc->num_error < 0) && (valid_bcc->num_error < 0) ) {
573                         free_recipients(valid_to);
574                         free_recipients(valid_cc);
575                         free_recipients(valid_bcc);
576                         cprintf("%d At least one recipient is required.\n", ERROR + NO_SUCH_USER);
577                         return;
578                 }
579
580                 if (valid_to->num_internet + valid_cc->num_internet + valid_bcc->num_internet > 0) {
581                         if (CtdlCheckInternetMailPermission(&CCC->user)==0) {
582                                 cprintf("%d You do not have permission "
583                                         "to send Internet mail.\n",
584                                         ERROR + HIGHER_ACCESS_REQUIRED);
585                                 free_recipients(valid_to);
586                                 free_recipients(valid_cc);
587                                 free_recipients(valid_bcc);
588                                 return;
589                         }
590                 }
591
592                 if ( ( (valid_to->num_internet + valid_to->num_ignet + valid_cc->num_internet + valid_cc->num_ignet + valid_bcc->num_internet + valid_bcc->num_ignet) > 0)
593                      && (CCC->user.axlevel < AxNetU) ) {
594                         cprintf("%d Higher access required for network mail.\n",
595                                 ERROR + HIGHER_ACCESS_REQUIRED);
596                         free_recipients(valid_to);
597                         free_recipients(valid_cc);
598                         free_recipients(valid_bcc);
599                         return;
600                 }
601         
602                 if ((RESTRICT_INTERNET == 1)
603                     && (valid_to->num_internet + valid_cc->num_internet + valid_bcc->num_internet > 0)
604                     && ((CCC->user.flags & US_INTERNET) == 0)
605                     && (!CCC->internal_pgm)) {
606                         cprintf("%d You don't have access to Internet mail.\n",
607                                 ERROR + HIGHER_ACCESS_REQUIRED);
608                         free_recipients(valid_to);
609                         free_recipients(valid_cc);
610                         free_recipients(valid_bcc);
611                         return;
612                 }
613
614         }
615
616         /* Is this a room which has anonymous-only or anonymous-option? */
617         anonymous = MES_NORMAL;
618         if (CCC->room.QRflags & QR_ANONONLY) {
619                 anonymous = MES_ANONONLY;
620         }
621         if (CCC->room.QRflags & QR_ANONOPT) {
622                 if (anon_flag == 1) {   /* only if the user requested it */
623                         anonymous = MES_ANONOPT;
624                 }
625         }
626
627         if ((CCC->room.QRflags & QR_MAILBOX) == 0) {
628                 recp[0] = 0;
629         }
630
631         /* Recommend to the client that the use of a message subject is
632          * strongly recommended in this room, if either the SUBJECTREQ flag
633          * is set, or if there is one or more Internet email recipients.
634          */
635         if (CCC->room.QRflags2 & QR2_SUBJECTREQ) subject_required = 1;
636         if ((valid_to)  && (valid_to->num_internet > 0))        subject_required = 1;
637         if ((valid_cc)  && (valid_cc->num_internet > 0))        subject_required = 1;
638         if ((valid_bcc) && (valid_bcc->num_internet > 0))       subject_required = 1;
639
640         /* If we're only checking the validity of the request, return
641          * success without creating the message.
642          */
643         if (post == 0) {
644                 cprintf("%d %s|%d\n", CIT_OK,
645                         ((valid_to != NULL) ? valid_to->display_recp : ""), 
646                         subject_required);
647                 free_recipients(valid_to);
648                 free_recipients(valid_cc);
649                 free_recipients(valid_bcc);
650                 return;
651         }
652
653         /* We don't need these anymore because we'll do it differently below */
654         free_recipients(valid_to);
655         free_recipients(valid_cc);
656         free_recipients(valid_bcc);
657
658         /* Read in the message from the client. */
659         if (do_confirm) {
660                 cprintf("%d send message\n", START_CHAT_MODE);
661         } else {
662                 cprintf("%d send message\n", SEND_LISTING);
663         }
664
665         msg = CtdlMakeMessage(&CCC->user, recp, cc,
666                               CCC->room.QRname, anonymous, format_type,
667                               newusername, newuseremail, subject,
668                               ((!IsEmptyStr(supplied_euid)) ? supplied_euid : NULL),
669                               NULL, references);
670
671         /* Put together one big recipients struct containing to/cc/bcc all in
672          * one.  This is for the envelope.
673          */
674         char *all_recps = malloc(SIZ * 3);
675         strcpy(all_recps, recp);
676         if (!IsEmptyStr(cc)) {
677                 if (!IsEmptyStr(all_recps)) {
678                         strcat(all_recps, ",");
679                 }
680                 strcat(all_recps, cc);
681         }
682         if (!IsEmptyStr(bcc)) {
683                 if (!IsEmptyStr(all_recps)) {
684                         strcat(all_recps, ",");
685                 }
686                 strcat(all_recps, bcc);
687         }
688         if (!IsEmptyStr(all_recps)) {
689                 valid = validate_recipients(all_recps, NULL, 0);
690         }
691         else {
692                 valid = NULL;
693         }
694         free(all_recps);
695
696         if ((valid != NULL) && (valid->num_room == 1) && !IsEmptyStr(valid->recp_orgroom))
697         {
698                 /* posting into an ML room? set the envelope from 
699                  * to the actual mail address so others get a valid
700                  * reply-to-header.
701                  */
702                 CM_SetField(msg, eenVelopeTo, valid->recp_orgroom, strlen(valid->recp_orgroom));
703         }
704
705         if (msg != NULL) {
706                 msgnum = CtdlSubmitMsg(msg, valid, "", QP_EADDR);
707                 if (do_confirm) {
708                         cprintf("%ld\n", msgnum);
709
710                         if (StrLength(CCC->StatusMessage) > 0) {
711                                 cprintf("%s\n", ChrPtr(CCC->StatusMessage));
712                         }
713                         else if (msgnum >= 0L) {
714                                 client_write(HKEY("Message accepted.\n"));
715                         }
716                         else {
717                                 client_write(HKEY("Internal error.\n"));
718                         }
719
720                         if (!CM_IsEmpty(msg, eExclusiveID)) {
721                                 cprintf("%s\n", msg->cm_fields[eExclusiveID]);
722                         } else {
723                                 cprintf("\n");
724                         }
725                         cprintf("000\n");
726                 }
727
728                 CM_Free(msg);
729         }
730         if (valid != NULL) {
731                 free_recipients(valid);
732         }
733         return;
734 }
735
736 /*
737  * Delete message from current room
738  */
739 void cmd_dele(char *args)
740 {
741         int num_deleted;
742         int i;
743         char msgset[SIZ];
744         char msgtok[32];
745         long *msgs;
746         int num_msgs = 0;
747
748         extract_token(msgset, args, 0, '|', sizeof msgset);
749         num_msgs = num_tokens(msgset, ',');
750         if (num_msgs < 1) {
751                 cprintf("%d Nothing to do.\n", CIT_OK);
752                 return;
753         }
754
755         if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom() == 0) {
756                 cprintf("%d Higher access required.\n",
757                         ERROR + HIGHER_ACCESS_REQUIRED);
758                 return;
759         }
760
761         /*
762          * Build our message set to be moved/copied
763          */
764         msgs = malloc(num_msgs * sizeof(long));
765         for (i=0; i<num_msgs; ++i) {
766                 extract_token(msgtok, msgset, i, ',', sizeof msgtok);
767                 msgs[i] = atol(msgtok);
768         }
769
770         num_deleted = CtdlDeleteMessages(CC->room.QRname, msgs, num_msgs, "");
771         free(msgs);
772
773         if (num_deleted) {
774                 cprintf("%d %d message%s deleted.\n", CIT_OK,
775                         num_deleted, ((num_deleted != 1) ? "s" : ""));
776         } else {
777                 cprintf("%d Message not found.\n", ERROR + MESSAGE_NOT_FOUND);
778         }
779 }
780
781
782
783 /*
784  * move or copy a message to another room
785  */
786 void cmd_move(char *args)
787 {
788         char msgset[SIZ];
789         char msgtok[32];
790         long *msgs;
791         int num_msgs = 0;
792
793         char targ[ROOMNAMELEN];
794         struct ctdlroom qtemp;
795         int err;
796         int is_copy = 0;
797         int ra;
798         int permit = 0;
799         int i;
800
801         extract_token(msgset, args, 0, '|', sizeof msgset);
802         num_msgs = num_tokens(msgset, ',');
803         if (num_msgs < 1) {
804                 cprintf("%d Nothing to do.\n", CIT_OK);
805                 return;
806         }
807
808         extract_token(targ, args, 1, '|', sizeof targ);
809         convert_room_name_macros(targ, sizeof targ);
810         targ[ROOMNAMELEN - 1] = 0;
811         is_copy = extract_int(args, 2);
812
813         if (CtdlGetRoom(&qtemp, targ) != 0) {
814                 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, targ);
815                 return;
816         }
817
818         if (!strcasecmp(qtemp.QRname, CC->room.QRname)) {
819                 cprintf("%d Source and target rooms are the same.\n", ERROR + ALREADY_EXISTS);
820                 return;
821         }
822
823         CtdlGetUser(&CC->user, CC->curr_user);
824         CtdlRoomAccess(&qtemp, &CC->user, &ra, NULL);
825
826         /* Check for permission to perform this operation.
827          * Remember: "CC->room" is source, "qtemp" is target.
828          */
829         permit = 0;
830
831         /* Admins can move/copy */
832         if (CC->user.axlevel >= AxAideU) permit = 1;
833
834         /* Room aides can move/copy */
835         if (CC->user.usernum == CC->room.QRroomaide) permit = 1;
836
837         /* Permit move/copy from personal rooms */
838         if ((CC->room.QRflags & QR_MAILBOX)
839             && (qtemp.QRflags & QR_MAILBOX)) permit = 1;
840
841         /* Permit only copy from public to personal room */
842         if ( (is_copy)
843              && (!(CC->room.QRflags & QR_MAILBOX))
844              && (qtemp.QRflags & QR_MAILBOX)) permit = 1;
845
846         /* Permit message removal from collaborative delete rooms */
847         if (CC->room.QRflags2 & QR2_COLLABDEL) permit = 1;
848
849         /* Users allowed to post into the target room may move into it too. */
850         if ((CC->room.QRflags & QR_MAILBOX) && 
851             (qtemp.QRflags & UA_POSTALLOWED))  permit = 1;
852
853         /* User must have access to target room */
854         if (!(ra & UA_KNOWN))  permit = 0;
855
856         if (!permit) {
857                 cprintf("%d Higher access required.\n",
858                         ERROR + HIGHER_ACCESS_REQUIRED);
859                 return;
860         }
861
862         /*
863          * Build our message set to be moved/copied
864          */
865         msgs = malloc(num_msgs * sizeof(long));
866         for (i=0; i<num_msgs; ++i) {
867                 extract_token(msgtok, msgset, i, ',', sizeof msgtok);
868                 msgs[i] = atol(msgtok);
869         }
870
871         /*
872          * Do the copy
873          */
874         err = CtdlSaveMsgPointersInRoom(targ, msgs, num_msgs, 1, NULL, 0);
875         if (err != 0) {
876                 cprintf("%d Cannot store message(s) in %s: error %d\n",
877                         err, targ, err);
878                 free(msgs);
879                 return;
880         }
881
882         /* Now delete the message from the source room,
883          * if this is a 'move' rather than a 'copy' operation.
884          */
885         if (is_copy == 0) {
886                 CtdlDeleteMessages(CC->room.QRname, msgs, num_msgs, "");
887         }
888         free(msgs);
889
890         cprintf("%d Message(s) %s.\n", CIT_OK, (is_copy ? "copied" : "moved") );
891 }
892
893
894 /*****************************************************************************/
895 /*                      MODULE INITIALIZATION STUFF                          */
896 /*****************************************************************************/
897 CTDL_MODULE_INIT(ctdl_message)
898 {
899         if (!threading) {
900
901                 CtdlRegisterProtoHook(cmd_msgs, "MSGS", "Output a list of messages in the current room");
902                 CtdlRegisterProtoHook(cmd_msg0, "MSG0", "Output a message in plain text format");
903                 CtdlRegisterProtoHook(cmd_msg2, "MSG2", "Output a message in RFC822 format");
904                 CtdlRegisterProtoHook(cmd_msg3, "MSG3", "Output a message in raw format (deprecated)");
905                 CtdlRegisterProtoHook(cmd_msg4, "MSG4", "Output a message in the client's preferred format");
906                 CtdlRegisterProtoHook(cmd_msgp, "MSGP", "Select preferred format for MSG4 output");
907                 CtdlRegisterProtoHook(cmd_opna, "OPNA", "Open an attachment for download");
908                 CtdlRegisterProtoHook(cmd_dlat, "DLAT", "Download an attachment");
909                 CtdlRegisterProtoHook(cmd_ent0, "ENT0", "Enter a message");
910                 CtdlRegisterProtoHook(cmd_dele, "DELE", "Delete a message");
911                 CtdlRegisterProtoHook(cmd_move, "MOVE", "Move or copy a message to another room");
912         }
913
914         /* return our Subversion id for the Log */
915         return "ctdl_message";
916 }