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