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