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