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