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