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