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