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