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