]> code.citadel.org Git - citadel.git/blob - citadel/msgbase.c
Really got things working nicely
[citadel.git] / citadel / msgbase.c
1 /* $Id$ */
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <fcntl.h>
6 #include <time.h>
7 #include <ctype.h>
8 #include <string.h>
9 #include <syslog.h>
10 #include <pthread.h>
11 #include "citadel.h"
12 #include "server.h"
13 #include <errno.h>
14 #include <sys/stat.h>
15 #include "database.h"
16 #include "msgbase.h"
17 #include "support.h"
18 #include "sysdep_decls.h"
19 #include "room_ops.h"
20 #include "user_ops.h"
21 #include "file_ops.h"
22 #include "control.h"
23 #include "dynloader.h"
24 #include "tools.h"
25 #include "mime_parser.h"
26
27 #define MSGS_ALL        0
28 #define MSGS_OLD        1
29 #define MSGS_NEW        2
30 #define MSGS_FIRST      3
31 #define MSGS_LAST       4
32 #define MSGS_GT         5
33
34 extern struct config config;
35
36
37 /*
38  * This function is self explanatory.
39  * (What can I say, I'm in a weird mood today...)
40  */
41 void remove_any_whitespace_to_the_left_or_right_of_at_symbol(char *name) {
42         int i;
43
44         for (i=0; i<strlen(name); ++i) if (name[i]=='@') {
45                 if (i>0) if (isspace(name[i-1])) {
46                         strcpy(&name[i-1], &name[i]);
47                         i = 0;
48                         }
49                 while (isspace(name[i+1])) {
50                         strcpy(&name[i+1], &name[i+2]);
51                         }
52                 }
53         }
54
55
56 /*
57  * Aliasing for network mail.
58  * (Error messages have been commented out, because this is a server.)
59  */
60 int alias(char *name)           /* process alias and routing info for mail */
61              {
62         FILE *fp;
63         int a,b;
64         char aaa[300],bbb[300];
65
66         lprintf(9, "alias() called for <%s>\n", name);
67
68         remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
69         
70         fp=fopen("network/mail.aliases","r");
71         if (fp==NULL) fp=fopen("/dev/null","r");
72         if (fp==NULL) return(M_ERROR);
73 GNA:    strcpy(aaa,""); strcpy(bbb,"");
74         do {
75                 a=getc(fp);
76                 if (a==',') a=0;
77                 if (a>0) {
78                         b=strlen(aaa);
79                         aaa[b]=a;
80                         aaa[b+1]=0;
81                         }
82                 } while(a>0);
83         do {
84                 a=getc(fp);
85                 if (a==10) a=0;
86                 if (a>0) {
87                         b=strlen(bbb);
88                         bbb[b]=a;
89                         bbb[b+1]=0;
90                         }
91                 } while(a>0);
92         if (a<0) {
93                 fclose(fp);
94                 goto DETYPE;
95                 }
96         if (strcasecmp(name,aaa)) goto GNA;
97         fclose(fp);
98         strcpy(name,bbb);
99         lprintf(7, "Mail is being forwarded to %s\n", name);
100
101 DETYPE: /* determine local or remote type, see citadel.h */
102         for (a=0; a<strlen(name); ++a) if (name[a]=='!') return(M_INTERNET);
103         for (a=0; a<strlen(name); ++a)
104                 if (name[a]=='@')
105                         for (b=a; b<strlen(name); ++b)
106                                 if (name[b]=='.') return(M_INTERNET);
107         b=0; for (a=0; a<strlen(name); ++a) if (name[a]=='@') ++b;
108         if (b>1) {
109                 lprintf(7, "Too many @'s in address\n");
110                 return(M_ERROR);
111                 }
112         if (b==1) {
113                 for (a=0; a<strlen(name); ++a)
114                         if (name[a]=='@') strcpy(bbb,&name[a+1]);
115                 while (bbb[0]==32) strcpy(bbb,&bbb[1]);
116                 fp = fopen("network/mail.sysinfo","r");
117                 if (fp==NULL) return(M_ERROR);
118 GETSN:          do {
119                         a=getstring(fp,aaa);
120                         } while ((a>=0)&&(strcasecmp(aaa,bbb)));
121                 a=getstring(fp,aaa);
122                 if (!strncmp(aaa,"use ",4)) {
123                         strcpy(bbb,&aaa[4]);
124                         fseek(fp,0L,0);
125                         goto GETSN;
126                         }
127                 fclose(fp);
128                 if (!strncmp(aaa,"uum",3)) {
129                         strcpy(bbb,name);
130                         for (a=0; a<strlen(bbb); ++a) {
131                                 if (bbb[a]=='@') bbb[a]=0;
132                                 if (bbb[a]==' ') bbb[a]='_';
133                                 }
134                         while(bbb[strlen(bbb)-1]=='_') bbb[strlen(bbb)-1]=0;
135                         sprintf(name,&aaa[4],bbb);
136                         return(M_INTERNET);
137                         }
138                 if (!strncmp(aaa,"bin",3)) {
139                         strcpy(aaa,name); strcpy(bbb,name);
140                         while (aaa[strlen(aaa)-1]!='@') aaa[strlen(aaa)-1]=0;
141                         aaa[strlen(aaa)-1]=0;
142                         while (aaa[strlen(aaa)-1]==' ') aaa[strlen(aaa)-1]=0;
143                         while (bbb[0]!='@') strcpy(bbb,&bbb[1]);
144                         strcpy(bbb,&bbb[1]);
145                         while (bbb[0]==' ') strcpy(bbb,&bbb[1]);
146                         sprintf(name,"%s @%s",aaa,bbb);
147                         return(M_BINARY);
148                         }
149                 return(M_ERROR);
150                 }
151         return(M_LOCAL);
152         }
153
154
155 void get_mm(void) {
156         FILE *fp;
157
158         fp=fopen("citadel.control","r");
159         fread((char *)&CitControl,sizeof(struct CitControl),1,fp);
160         fclose(fp);
161         }
162
163 /*
164  * cmd_msgs()  -  get list of message #'s in this room
165  */
166 void cmd_msgs(char *cmdbuf)
167 {
168         int a = 0;
169         int mode = 0;
170         char which[256];
171         int cm_howmany = 0;
172         long cm_gt = 0L;
173         struct visit vbuf;
174
175         extract(which,cmdbuf,0);
176
177         mode = MSGS_ALL;
178         strcat(which,"   ");
179         if (!strncasecmp(which,"OLD",3))        mode = MSGS_OLD;
180         if (!strncasecmp(which,"NEW",3))        mode = MSGS_NEW;
181         if (!strncasecmp(which,"FIRST",5))      {
182                 mode = MSGS_FIRST;
183                 cm_howmany = extract_int(cmdbuf,1);
184                 }
185         if (!strncasecmp(which,"LAST",4))       {
186                 mode = MSGS_LAST;
187                 cm_howmany = extract_int(cmdbuf,1);
188                 }
189         if (!strncasecmp(which,"GT",2)) {
190                 mode = MSGS_GT;
191                 cm_gt = extract_long(cmdbuf,1);
192                 }
193
194         if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
195                 cprintf("%d not logged in\n",ERROR+NOT_LOGGED_IN);
196                 return;
197                 }
198         get_mm();
199         get_msglist(&CC->quickroom);
200         getuser(&CC->usersupp,CC->curr_user);
201         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
202
203         cprintf("%d Message list...\n",LISTING_FOLLOWS);
204         if (CC->num_msgs != 0) {
205            for (a=0; a<(CC->num_msgs); ++a) 
206                if ((MessageFromList(a) >=0)
207                && ( 
208
209 (mode==MSGS_ALL)
210 || ((mode==MSGS_OLD) && (MessageFromList(a) <= vbuf.v_lastseen))
211 || ((mode==MSGS_NEW) && (MessageFromList(a) > vbuf.v_lastseen))
212 || ((mode==MSGS_NEW) && (MessageFromList(a) >= vbuf.v_lastseen)
213                      && (CC->usersupp.flags & US_LASTOLD))
214 || ((mode==MSGS_LAST)&& (a>=(CC->num_msgs-cm_howmany)))
215 || ((mode==MSGS_FIRST)&&(a<cm_howmany))
216 || ((mode==MSGS_GT) && (MessageFromList(a) > cm_gt))
217
218                         )
219                 ) {
220                         cprintf("%ld\n", MessageFromList(a));
221                         }
222            }
223         cprintf("000\n");
224         }
225
226
227
228 /* 
229  * help_subst()  -  support routine for help file viewer
230  */
231 void help_subst(char *strbuf, char *source, char *dest)
232 {
233         char workbuf[256];
234         int p;
235
236         while (p=pattern2(strbuf,source), (p>=0)) {
237                 strcpy(workbuf,&strbuf[p+strlen(source)]);
238                 strcpy(&strbuf[p],dest);
239                 strcat(strbuf,workbuf);
240                 }
241         }
242
243
244 void do_help_subst(char *buffer)
245 {
246         char buf2[16];
247
248         help_subst(buffer,"^nodename",config.c_nodename);
249         help_subst(buffer,"^humannode",config.c_humannode);
250         help_subst(buffer,"^fqdn",config.c_fqdn);
251         help_subst(buffer,"^username",CC->usersupp.fullname);
252         sprintf(buf2,"%ld",CC->usersupp.usernum);
253         help_subst(buffer,"^usernum",buf2);
254         help_subst(buffer,"^sysadm",config.c_sysadm);
255         help_subst(buffer,"^variantname",CITADEL);
256         sprintf(buf2,"%d",config.c_maxsessions);
257         help_subst(buffer,"^maxsessions",buf2);
258         }
259
260
261
262 /*
263  * memfmout()  -  Citadel text formatter and paginator.
264  *             Although the original purpose of this routine was to format
265  *             text to the reader's screen width, all we're really using it
266  *             for here is to format text out to 80 columns before sending it
267  *             to the client.  The client software may reformat it again.
268  */
269 void memfmout(int width, char *mptr, char subst)
270                         /* screen width to use */
271                         /* where are we going to get our text from? */
272                         /* nonzero if we should use hypertext mode */
273         {
274         int a,b,c;
275         int real = 0;
276         int old = 0;
277         CIT_UBYTE ch;
278         char aaa[140];
279         char buffer[256];
280         
281         strcpy(aaa,""); old=255;
282         strcpy(buffer,"");
283         c=1; /* c is the current pos */
284
285 FMTA:   if (subst) {
286                 while (ch=*mptr, ((ch!=0) && (strlen(buffer)<126) )) {
287                         ch=*mptr++;
288                         buffer[strlen(buffer)+1] = 0;
289                         buffer[strlen(buffer)] = ch;
290                         }
291
292                 if (buffer[0]=='^') do_help_subst(buffer);
293
294                 buffer[strlen(buffer)+1] = 0;
295                 a=buffer[0];
296                 strcpy(buffer,&buffer[1]);
297                 }
298         
299         else ch=*mptr++;
300
301         old=real;
302         real=ch;
303         if (ch<=0) goto FMTEND;
304         
305         if ( ((ch==13)||(ch==10)) && (old!=13) && (old!=10) ) ch=32;
306         if ( ((old==13)||(old==10)) && (isspace(real)) ) {
307                 cprintf("\n");
308                 c=1;
309                 }
310         if (ch>126) goto FMTA;
311
312         if (ch>32) {
313         if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) )
314                 { cprintf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
315                 }
316          b=strlen(aaa); aaa[b]=ch; aaa[b+1]=0; }
317         if (ch==32) {
318                 if ((strlen(aaa)+c)>(width-5)) { 
319                         cprintf("\n");
320                         c=1;
321                         }
322                 cprintf("%s ",aaa); ++c; c=c+strlen(aaa);
323                 strcpy(aaa,"");
324                 goto FMTA;
325                 }
326         if ((ch==13)||(ch==10)) {
327                 cprintf("%s\n",aaa);
328                 c=1;
329                 strcpy(aaa,"");
330                 goto FMTA;
331                 }
332         goto FMTA;
333
334 FMTEND: cprintf("\n");
335         }
336
337
338
339 /*
340  * Callback function for mime parser that simply lists the part
341  */
342 void list_this_part(char *name, char *filename, char *partnum, char *disp,
343                         void *content, char *cbtype, size_t length) {
344
345         cprintf("part=%s|%s|%s|%s|%s|%d\n",
346                 name, filename, partnum, disp, cbtype, length);
347         }
348
349
350 /*
351  * Callback function for mime parser that wants to display text
352  */
353 void fixed_output(char *name, char *filename, char *partnum, char *disp,
354                         void *content, char *cbtype, size_t length) {
355
356         if (!strcasecmp(cbtype, "text/plain")) {
357                 client_write(content, length);
358                 }
359         else {
360                 cprintf("Part %s: %s (%s) (%d bytes)\n",
361                         partnum, filename, cbtype, length);
362                 }
363         }
364
365
366 /*
367  * Callback function for mime parser that opens a section for downloading
368  */
369 void mime_download(char *name, char *filename, char *partnum, char *disp,
370                         void *content, char *cbtype, size_t length) {
371
372         char tmpname[PATH_MAX];
373         static int seq = 0;
374
375         /* Silently go away if there's already a download open... */
376         if (CC->download_fp != NULL) return;
377
378         /* ...or if this is not the desired section */
379         if (strcasecmp(CC->desired_section, partnum)) return;
380
381         snprintf(tmpname, sizeof tmpname,
382                 "/tmp/CitServer.download.%4x.%4x", getpid(), ++seq);
383
384         CC->download_fp = fopen(tmpname, "wb+");
385         if (CC->download_fp == NULL) return;
386
387         /* Unlink the file while it's open, to guarantee that the
388          * temp file will always be deleted.
389          */
390         unlink(tmpname);
391
392         fwrite(content, length, 1, CC->download_fp);
393         fflush(CC->download_fp);
394         rewind(CC->download_fp);
395
396         OpenCmdResult(filename, cbtype);
397         }
398
399
400
401 /*
402  * Get a message off disk.  (return value is the message's timestamp)
403  * 
404  */
405 time_t output_message(char *msgid, int mode, int headers_only) {
406         long msg_num;
407         int a;
408         CIT_UBYTE ch, rch;
409         CIT_UBYTE format_type,anon_flag;
410         char buf[1024];
411         long msg_len;
412         int msg_ok = 0;
413
414         struct cdbdata *dmsgtext;
415         char *mptr;
416
417         /* buffers needed for RFC822 translation */
418         char suser[256];
419         char luser[256];
420         char snode[256];
421         char lnode[256];
422         char mid[256];
423         time_t xtime = 0L;
424         /*                                       */
425
426         msg_num = atol(msgid);
427
428
429         if ((!(CC->logged_in))&&(!(CC->internal_pgm))&&(mode!=MT_DATE)) {
430                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
431                 return(xtime);
432                 }
433
434         /* We used to need to check in the current room's message list
435          * to determine where the message's disk position.  We no longer need
436          * to do this, but we do it anyway as a security measure, in order to
437          * prevent rogue clients from reading messages not in the current room.
438          */
439
440         msg_ok = 0;
441         if (CC->num_msgs > 0) {
442                 for (a=0; a<CC->num_msgs; ++a) {
443                         if (MessageFromList(a) == msg_num) {
444                                 msg_ok = 1;
445                                 }
446                         }
447                 }
448
449         if (!msg_ok) {
450                 if (mode != MT_DATE)
451                         cprintf("%d Message %ld is not in this room.\n",
452                                 ERROR, msg_num);
453                 return(xtime);
454                 }
455         
456
457         dmsgtext = cdb_fetch(CDB_MSGMAIN, &msg_num, sizeof(long));
458         
459         if (dmsgtext == NULL) {
460                 if (mode != MT_DATE)
461                         cprintf("%d Can't find message %ld\n",
462                                 ERROR+INTERNAL_ERROR);
463                 return(xtime);
464                 }
465
466         msg_len = (long) dmsgtext->len;
467         mptr = dmsgtext->ptr;
468
469         /* this loop spews out the whole message if we're doing raw format */
470         if (mode == MT_RAW) {
471                 cprintf("%d %ld\n", BINARY_FOLLOWS, msg_len);
472                 client_write(dmsgtext->ptr, (int) msg_len);
473                 cdb_free(dmsgtext);
474                 return(xtime);
475                 }
476
477         /* Otherwise, we'll start parsing it field by field... */
478         ch = *mptr++;
479         if (ch != 255) {
480                 cprintf("%d Illegal message format on disk\n",
481                         ERROR+INTERNAL_ERROR);
482                 cdb_free(dmsgtext);
483                 return(xtime);
484                 }
485
486         anon_flag = *mptr++;
487         format_type = *mptr++;
488
489         /* Are we downloading a MIME component? */
490         if (mode == MT_DOWNLOAD) {
491                 if (format_type != 4) {
492                         cprintf("%d This is not a MIME message.\n",
493                                 ERROR);
494                         }
495                 else if (CC->download_fp != NULL) {
496                         cprintf("%d You already have a download open.\n",
497                                 ERROR);
498                         }
499                 else {
500                         /* Skip to the message body */
501                         while(ch = *mptr++, (ch!='M' && ch!=0)) {
502                                 buf[0] = 0;
503                                 do {
504                                         buf[strlen(buf)+1] = 0;
505                                         rch = *mptr++;
506                                         buf[strlen(buf)] = rch;
507                                         } while (rch > 0);
508                                 }
509                         /* Now parse it */
510                         mime_parser(mptr, NULL, *mime_download);
511                         /* If there's no file open by this time, the requested
512                          * section wasn't found, so print an error
513                          */
514                         if (CC->download_fp == NULL) {
515                                 cprintf("%d Section %s not found.\n",
516                                         ERROR+FILE_NOT_FOUND,
517                                         CC->desired_section);
518                                 }
519                         }
520                 cdb_free(dmsgtext);
521                 return(xtime);
522                 }
523
524         /* Are we just looking for the message date? */
525         if (mode == MT_DATE) while(ch = *mptr++, (ch!='M' && ch!=0)) {
526                 buf[0] = 0;
527                 do {
528                         buf[strlen(buf)+1] = 0;
529                         rch = *mptr++;
530                         buf[strlen(buf)] = rch;
531                         } while (rch > 0);
532
533                 if (ch=='T') {
534                         xtime = atol(buf);
535                         cdb_free(dmsgtext);
536                         return(xtime);
537                         }
538                 }
539
540
541         /* now for the user-mode message reading loops */
542         cprintf("%d Message %ld:\n",LISTING_FOLLOWS,msg_num);
543
544         if (mode == MT_CITADEL) cprintf("type=%d\n", format_type);
545
546         if ( (anon_flag == MES_ANON) && (mode == MT_CITADEL) ) {
547                 cprintf("nhdr=yes\n");
548                 }
549
550         /* begin header processing loop for Citadel message format */
551
552         if ((mode == MT_CITADEL)||(mode == MT_MIME))
553            while(ch = *mptr++, (ch!='M' && ch!=0)) {
554                 buf[0] = 0;
555                 do {
556                         buf[strlen(buf)+1] = 0;
557                         rch = *mptr++;
558                         buf[strlen(buf)] = rch;
559                         } while (rch > 0);
560
561                 if (ch=='A') {
562                         PerformUserHooks(buf, (-1L), EVT_OUTPUTMSG);
563                         if (anon_flag==MES_ANON) cprintf("from=****");
564                         else if (anon_flag==MES_AN2) cprintf("from=anonymous");
565                         else cprintf("from=%s",buf);
566                         if ((is_room_aide()) && ((anon_flag == MES_ANON)
567                            || (anon_flag == MES_AN2)))
568                                 cprintf(" [%s]",buf);
569                         cprintf("\n");
570                         }
571                 else if (ch=='P') cprintf("path=%s\n",buf);
572                 else if (ch=='U') cprintf("subj=%s\n",buf);
573                 else if (ch=='I') cprintf("msgn=%s\n",buf);
574                 else if (ch=='H') cprintf("hnod=%s\n",buf);
575                 else if (ch=='O') cprintf("room=%s\n",buf);
576                 else if (ch=='N') cprintf("node=%s\n",buf);
577                 else if (ch=='R') cprintf("rcpt=%s\n",buf);
578                 else if (ch=='T') cprintf("time=%s\n",buf);
579                 /* else cprintf("fld%c=%s\n",ch,buf); */
580                 }
581
582         /* begin header processing loop for RFC822 transfer format */
583
584         strcpy(suser, "");
585         strcpy(luser, "");
586         strcpy(snode, NODENAME);
587         strcpy(lnode, HUMANNODE);
588         if (mode == MT_RFC822) while(ch = *mptr++, (ch!='M' && ch!=0)) {
589                 buf[0] = 0;
590                 do {
591                         buf[strlen(buf)+1] = 0;
592                         rch = *mptr++;
593                         buf[strlen(buf)] = rch;
594                         } while (rch > 0);
595
596                 if (ch=='A') strcpy(luser, buf);
597                 else if (ch=='P') {
598                         cprintf("Path: %s\n",buf);
599                         for (a=0; a<strlen(buf); ++a) {
600                                 if (buf[a] == '!') {
601                                         strcpy(buf,&buf[a+1]);
602                                         a=0;
603                                         }
604                                 }
605                         strcpy(suser, buf);
606                         }
607                 else if (ch=='U') cprintf("Subject: %s\n",buf);
608                 else if (ch=='I') strcpy(mid, buf);
609                 else if (ch=='H') strcpy(lnode, buf);
610                 else if (ch=='O') cprintf("X-Citadel-Room: %s\n",buf);
611                 else if (ch=='N') strcpy(snode, buf);
612                 else if (ch=='R') cprintf("To: %s\n",buf);
613                 else if (ch=='T')  {
614                         xtime = atol(buf);
615                         cprintf("Date: %s", asctime(localtime(&xtime)));
616                         }
617                 }
618
619         if (mode == MT_RFC822) {
620                 if (!strcasecmp(snode, NODENAME)) {
621                         strcpy(snode, FQDN);
622                         }
623                 cprintf("Message-ID: <%s@%s>\n", mid, snode);
624                 PerformUserHooks(luser, (-1L), EVT_OUTPUTMSG);
625                 cprintf("From: %s@%s (%s)\n",
626                         suser, snode, luser);
627                 cprintf("Organization: %s\n", lnode);
628                 }
629
630         /* end header processing loop ... at this point, we're in the text */
631
632         if (ch==0) {
633                 cprintf("text\n*** ?Message truncated\n000\n");
634                 cdb_free(dmsgtext);
635                 return(xtime);
636                 }
637
638         /* do some sort of MIME output */
639         if (format_type == 4) {
640                 if ((mode == MT_CITADEL)||(mode == MT_MIME)) {
641                         mime_parser(mptr, NULL, *list_this_part);
642                         }
643                 if (mode == MT_MIME) { /* If MT_MIME then it's parts only */
644                         cprintf("000\n");
645                         cdb_free(dmsgtext);
646                         return(xtime);
647                         }
648                 }
649
650         if (headers_only) {
651                 /* give 'em a length */
652                 msg_len = 0L;
653                 while(ch = *mptr++, ch>0) {
654                         ++msg_len;
655                         }
656                 cprintf("mlen=%ld\n", msg_len);
657                 cprintf("000\n");
658                 cdb_free(dmsgtext);
659                 return(xtime);
660                 }
661
662         /* signify start of msg text */
663         if (mode == MT_CITADEL) cprintf("text\n");
664         if ( (mode == MT_RFC822) && (format_type != 4) ) cprintf("\n");
665
666         /* If the format type on disk is 1 (fixed-format), then we want
667          * everything to be output completely literally ... regardless of
668          * what message transfer format is in use.
669          */
670         if (format_type == 1) {
671                 strcpy(buf, "");
672                 while(ch = *mptr++, ch>0) {
673                         if (ch == 13) ch = 10;
674                         if ( (ch == 10) || (strlen(buf)>250) ) {
675                                 cprintf("%s\n", buf);
676                                 strcpy(buf, "");
677                                 }
678                         else {
679                                 buf[strlen(buf)+1] = 0;
680                                 buf[strlen(buf)] = ch;
681                                 }
682                         }
683                 if (strlen(buf)>0) cprintf("%s\n", buf);
684                 }
685         /* If the message on disk is format 0 (Citadel vari-format), we
686          * output using the formatter at 80 columns.  This is the final output
687          * form if the transfer format is RFC822, but if the transfer format
688          * is Citadel proprietary, it'll still work, because the indentation
689          * for new paragraphs is correct and the client will reformat the
690          * message to the reader's screen width.
691          */
692         if (format_type == 0) {
693                 memfmout(80,mptr,0);
694                 }
695         /* If the message on disk is format 4 (MIME), we've gotta hand it
696          * off to the MIME parser.  The client has already been told that
697          * this message is format 1 (fixed format), so the callback function
698          * we use will display those parts as-is.
699          */
700         if (format_type == 4) {
701                 mime_parser(mptr, NULL, *fixed_output);
702                 }
703
704         /* now we're done */
705         cprintf("000\n");
706         cdb_free(dmsgtext);
707         return(xtime);
708         }
709
710
711 /*
712  * display a message (mode 0 - Citadel proprietary)
713  */
714 void cmd_msg0(char *cmdbuf)
715 {
716         char msgid[256];
717         int headers_only = 0;
718
719         extract(msgid,cmdbuf,0);
720         headers_only = extract_int(cmdbuf, 1);
721
722         output_message(msgid, MT_CITADEL, headers_only);
723         return;
724         }
725
726
727 /*
728  * display a message (mode 2 - RFC822)
729  */
730 void cmd_msg2(char *cmdbuf)
731 {
732         char msgid[256];
733         int headers_only = 0;
734
735         extract(msgid,cmdbuf,0);
736         headers_only = extract_int(cmdbuf,1);
737
738         output_message(msgid,MT_RFC822,headers_only);
739         }
740
741 /* 
742  * display a message (mode 3 - IGnet raw format - internal programs only)
743  */
744 void cmd_msg3(char *cmdbuf)
745 {
746         char msgid[256];
747         int headers_only = 0;
748
749         if (CC->internal_pgm == 0) {
750                 cprintf("%d This command is for internal programs only.\n",
751                         ERROR);
752                 return;
753                 }
754
755         extract(msgid,cmdbuf,0);
756         headers_only = extract_int(cmdbuf,1);
757
758         output_message(msgid,MT_RAW,headers_only);
759         }
760
761 /* 
762  * display a message (mode 4 - MIME) (FIX ... still evolving, not complete)
763  */
764 void cmd_msg4(char *cmdbuf)
765 {
766         char msgid[256];
767
768         extract(msgid, cmdbuf, 0);
769
770         output_message(msgid, MT_MIME, 0);
771         }
772
773
774
775 /*
776  * Open a component of a MIME message as a download file 
777  */
778 void cmd_opna(char *cmdbuf)
779 {
780         char msgid[256];
781
782         extract(msgid, cmdbuf, 0);
783         extract(CC->desired_section, cmdbuf, 1);
784
785         output_message(msgid, MT_DOWNLOAD, 0);
786         }
787
788
789
790 /*
791  * Message base operation to send a message to the master file
792  * (returns new message number)
793  */
794 long send_message(char *message_in_memory,      /* pointer to buffer */
795                 size_t message_length,          /* length of buffer */
796                 int generate_id) {              /* 1 to generate an I field */
797
798         long newmsgid;
799         char *actual_message;
800         size_t actual_length;
801         long retval;
802         char msgidbuf[32];
803
804         /* Get a new message number */
805         newmsgid = get_new_message_number();
806
807         if (generate_id) {
808                 sprintf(msgidbuf, "I%ld", newmsgid);
809                 actual_length = message_length + strlen(msgidbuf) + 1;
810                 actual_message = mallok(actual_length);
811                 memcpy(actual_message, message_in_memory, 3);
812                 memcpy(&actual_message[3], msgidbuf, (strlen(msgidbuf)+1) );
813                 memcpy(&actual_message[strlen(msgidbuf)+4],
814                         &message_in_memory[3], message_length - 3);
815                 }
816         
817         else {
818                 actual_message = message_in_memory;
819                 actual_length = message_length;
820                 }
821
822         /* Write our little bundle of joy into the message base */
823         begin_critical_section(S_MSGMAIN);
824         if ( cdb_store(CDB_MSGMAIN, &newmsgid, sizeof(long),
825                         actual_message, actual_length) < 0 ) {
826                 lprintf(2, "Can't store message\n");
827                 retval = 0L;
828                 }
829         else {
830                 retval = newmsgid;
831                 }
832         end_critical_section(S_MSGMAIN);
833
834         if (generate_id) {
835                 phree(actual_message);
836                 }
837
838         /* Finally, return the pointers */
839         return(retval);
840         }
841
842
843
844 /*
845  * this is a simple file copy routine.
846  */
847 void copy_file(char *from, char *to)
848 {
849         FILE *ffp,*tfp;
850         int a;
851
852         ffp=fopen(from,"r");
853         if (ffp==NULL) return;
854         tfp=fopen(to,"w");
855         if (tfp==NULL) {
856                 fclose(ffp);
857                 return;
858                 }
859         while (a=getc(ffp), a>=0) {
860                 putc(a,tfp);
861                 }
862         fclose(ffp);
863         fclose(tfp);
864         return;
865         }
866
867
868
869 /*
870  * message base operation to save a message and install its pointers
871  */
872 void save_message(char *mtmp,   /* file containing proper message */
873                 char *rec,      /* Recipient (if mail) */
874                 char *force,    /* if non-zero length, force a room */
875                 int mailtype,   /* local or remote type, see citadel.h */
876                 int generate_id) /* set to 1 to generate an 'I' field */
877 {
878         char aaa[100];
879         char hold_rm[ROOMNAMELEN];
880         char actual_rm[ROOMNAMELEN];
881         char force_room[ROOMNAMELEN];
882         char recipient[256];
883         long newmsgid;
884         char *message_in_memory;
885         struct stat statbuf;
886         size_t templen;
887         FILE *fp;
888         struct usersupp userbuf;
889         int a;
890         static int seqnum = 0;
891
892         lprintf(9, "save_message(%s,%s,%s,%d,%d)\n",
893                 mtmp, rec, force_room, mailtype, generate_id);
894
895         strcpy(force_room, force);
896
897         /* Strip non-printable characters out of the recipient name */
898         strcpy(recipient, rec);
899         for (a=0; a<strlen(recipient); ++a)
900                 if (!isprint(recipient[a]))
901                         strcpy(&recipient[a], &recipient[a+1]);
902
903         /* Measure the message */
904         stat(mtmp, &statbuf);
905         templen = statbuf.st_size;
906
907         /* Now read it into memory */
908         message_in_memory = (char *) mallok(templen);
909         if (message_in_memory == NULL) {
910                 lprintf(2, "Can't allocate memory to save message!\n");
911                 return;
912                 }
913
914         fp = fopen(mtmp, "rb");
915         fread(message_in_memory, templen, 1, fp);
916         fclose(fp);
917
918         newmsgid = send_message(message_in_memory, templen, generate_id);
919         phree(message_in_memory);
920         if (newmsgid <= 0L) return;
921
922         strcpy(actual_rm, CC->quickroom.QRname);
923         strcpy(hold_rm, "");
924
925         /* If the user is a twit, move to the twit room for posting... */
926         if (TWITDETECT) if (CC->usersupp.axlevel==2) {
927                 strcpy(hold_rm, actual_rm);
928                 strcpy(actual_rm, config.c_twitroom);
929                 }
930
931         /* ...or if this is a private message, go to the target mailbox. */
932         lprintf(9, "mailbox aliasing loop\n");
933 lprintf(9, "1\n");
934         if (strlen(recipient) > 0) {
935 lprintf(9, "2\n");
936                 /* mailtype = alias(recipient); */
937                 if (mailtype == M_LOCAL) {
938 lprintf(9, "3\n");
939                         if (getuser(&userbuf, recipient)!=0) {
940                                 /* User not found, goto Aide */
941 lprintf(9, "4\n");
942                                 strcpy(force_room, AIDEROOM);
943                                 }
944                         else {
945 lprintf(9, "5\n");
946                                 strcpy(hold_rm, actual_rm);
947 lprintf(9, "6\n");
948                                 MailboxName(actual_rm, &userbuf, MAILROOM);
949                                 }
950                         }
951                 }
952
953         /* ...or if this message is destined for Aide> then go there. */
954         lprintf(9, "actual room forcing loop\n");
955         if (strlen(force_room) > 0) {
956                 strcpy(hold_rm, actual_rm);
957                 strcpy(actual_rm, force_room);
958                 }
959
960         /* This call to usergoto() changes rooms if necessary.  It also
961          * causes the latest message list to be read into memory.
962          */
963         usergoto(actual_rm, 0);
964
965         /* read in the quickroom record, obtaining a lock... */
966         lgetroom(&CC->quickroom, actual_rm);
967
968         /* Fix an obscure bug */
969         if (!strcasecmp(CC->quickroom.QRname, AIDEROOM)) {
970                 CC->quickroom.QRflags = CC->quickroom.QRflags & ~QR_MAILBOX;
971                 }
972
973         /* Add the message pointer to the room */
974         CC->quickroom.QRhighest = AddMessageToRoom(&CC->quickroom, newmsgid);
975
976         /* update quickroom */
977         lputroom(&CC->quickroom, actual_rm);
978
979         /* Network mail - send a copy to the network program. */
980         if ( (strlen(recipient)>0) && (mailtype != M_LOCAL) ) {
981                 sprintf(aaa,"./network/spoolin/netmail.%04lx.%04x.%04x",
982                         (long)getpid(), CC->cs_pid, ++seqnum);
983                 copy_file(mtmp,aaa);
984                 system("exec nohup ./netproc -i >/dev/null 2>&1 &");
985                 }
986
987         /* Bump this user's messages posted counter. */
988         lgetuser(&CC->usersupp, CC->curr_user);
989         CC->usersupp.posted = CC->usersupp.posted + 1;
990         lputuser(&CC->usersupp, CC->curr_user);
991
992         /* If we've posted in a room other than the current room, then we
993          * have to now go back to the current room...
994          */
995         if (strlen(hold_rm) > 0) {
996                 usergoto(hold_rm, 0);
997                 }
998         unlink(mtmp);           /* delete the temporary file */
999         }
1000
1001
1002 /*
1003  * Generate an administrative message and post it in the Aide> room.
1004  */
1005 void aide_message(char *text)
1006 {
1007         FILE *fp;
1008
1009         fp=fopen(CC->temp,"wb");
1010         fprintf(fp,"%c%c%c",255,MES_NORMAL,0);
1011         fprintf(fp,"Psysop%c",0);
1012         fprintf(fp,"T%ld%c", time(NULL), 0);
1013         fprintf(fp,"ACitadel%c",0);
1014         fprintf(fp,"OAide%c",0);
1015         fprintf(fp,"N%s%c",NODENAME,0);
1016         fprintf(fp,"M%s\n%c",text,0);
1017         fclose(fp);
1018         save_message(CC->temp,"",AIDEROOM,M_LOCAL,1);
1019         syslog(LOG_NOTICE,text);
1020         }
1021
1022
1023
1024 /*
1025  * Build a binary message to be saved on disk.
1026  */
1027 void make_message(
1028         char *filename,                 /* temporary file name */
1029         struct usersupp *author,        /* author's usersupp structure */
1030         char *recipient,                /* NULL if it's not mail */
1031         char *room,                     /* room where it's going */
1032         int type,                       /* see MES_ types in header file */
1033         int net_type,                   /* see MES_ types in header file */
1034         int format_type,                /* local or remote (see citadel.h) */
1035         char *fake_name) {              /* who we're masquerading as */
1036
1037         FILE *fp;
1038         int a;
1039         time_t now;
1040         char dest_node[32];
1041         char buf[256];
1042
1043         /* Don't confuse the poor folks if it's not routed mail. */
1044         strcpy(dest_node, "");
1045
1046
1047         /* If net_type is M_BINARY, split out the destination node. */
1048         if (net_type == M_BINARY) {
1049                 strcpy(dest_node,NODENAME);
1050                 for (a=0; a<strlen(recipient); ++a) {
1051                         if (recipient[a]=='@') {
1052                                 recipient[a]=0;
1053                                 strcpy(dest_node,&recipient[a+1]);
1054                                 }
1055                         }
1056                 }
1057
1058         /* if net_type is M_INTERNET, set the dest node to 'internet' */
1059         if (net_type == M_INTERNET) {
1060                 strcpy(dest_node,"internet");
1061                 }
1062
1063         while (isspace(recipient[strlen(recipient)-1]))
1064                 recipient[strlen(recipient)-1] = 0;
1065
1066         time(&now);
1067         fp=fopen(filename,"w");
1068         putc(255,fp);
1069         putc(type,fp);  /* Normal or anonymous, see MES_ flags */
1070         putc(format_type,fp);   /* Formatted or unformatted */
1071         fprintf(fp,"Pcit%ld%c",author->usernum,0);      /* path */
1072         fprintf(fp,"T%ld%c",now,0);                     /* date/time */
1073         if (fake_name[0])
1074            fprintf(fp,"A%s%c",fake_name,0);
1075         else
1076            fprintf(fp,"A%s%c",author->fullname,0);      /* author */
1077
1078         if (CC->quickroom.QRflags & QR_MAILBOX) {       /* room */
1079                 fprintf(fp,"O%s%c", &CC->quickroom.QRname[11], 0);
1080                 }
1081         else {
1082                 fprintf(fp,"O%s%c",CC->quickroom.QRname,0);
1083                 }
1084
1085         fprintf(fp,"N%s%c",NODENAME,0);                 /* nodename */
1086         fprintf(fp,"H%s%c",HUMANNODE,0);                /* human nodename */
1087
1088         if (recipient[0]!=0) fprintf(fp, "R%s%c", recipient, 0);
1089         if (dest_node[0]!=0) fprintf(fp, "D%s%c", dest_node, 0);
1090
1091         putc('M',fp);
1092
1093         while (client_gets(buf), strcmp(buf,"000"))
1094         {
1095            fprintf(fp,"%s\n",buf);
1096         }
1097         syslog(LOG_INFO, "Closing message");
1098         putc(0,fp);
1099         fclose(fp);
1100         }
1101
1102
1103
1104
1105
1106 /*
1107  * message entry  -  mode 0 (normal) <bc>
1108  */
1109 void cmd_ent0(char *entargs)
1110 {
1111         int post = 0;
1112         char recipient[256];
1113         int anon_flag = 0;
1114         int format_type = 0;
1115         char newusername[256];          /* <bc> */
1116
1117         int a,b;
1118         int e = 0;
1119         int mtsflag = 0;
1120         struct usersupp tempUS;
1121         char buf[256];
1122
1123         post = extract_int(entargs,0);
1124         extract(recipient,entargs,1);
1125         anon_flag = extract_int(entargs,2);
1126         format_type = extract_int(entargs,3);
1127
1128         /* first check to make sure the request is valid. */
1129
1130         if (!(CC->logged_in)) {
1131                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1132                 return;
1133                 }
1134         if ((CC->usersupp.axlevel<2)&&((CC->quickroom.QRflags&QR_MAILBOX)==0)) {
1135                 cprintf("%d Need to be validated to enter ",
1136                         ERROR+HIGHER_ACCESS_REQUIRED);
1137                 cprintf("(except in %s> to sysop)\n", MAILROOM);
1138                 return;
1139                 }
1140         if ((CC->usersupp.axlevel<4)&&(CC->quickroom.QRflags&QR_NETWORK)) {
1141                 cprintf("%d Need net privileges to enter here.\n",
1142                         ERROR+HIGHER_ACCESS_REQUIRED);
1143                 return;
1144                 }
1145         if ((CC->usersupp.axlevel<6)&&(CC->quickroom.QRflags&QR_READONLY)) {
1146                 cprintf("%d Sorry, this is a read-only room.\n",
1147                         ERROR+HIGHER_ACCESS_REQUIRED);
1148                 return;
1149                 }
1150
1151         mtsflag=0;
1152         
1153                 
1154         if (post==2) {                  /* <bc> */
1155            if (CC->usersupp.axlevel<6)
1156            {
1157               cprintf("%d You don't have permission to do an aide post.\n",
1158                 ERROR+HIGHER_ACCESS_REQUIRED);
1159               return;
1160            }
1161            extract(newusername,entargs,4);
1162            memset(CC->fake_postname, 0, 32);
1163            strcpy(CC->fake_postname, newusername);
1164            cprintf("%d Ok\n",OK);
1165            return;
1166         }
1167         
1168         CC->cs_flags |= CS_POSTING;
1169         
1170         buf[0]=0;
1171         if (CC->quickroom.QRflags & QR_MAILBOX) {
1172                 if (CC->usersupp.axlevel>=2) {
1173                         strcpy(buf,recipient);
1174                         }
1175                 else strcpy(buf,"sysop");
1176                 e=alias(buf);                   /* alias and mail type */
1177                 if ((buf[0]==0) || (e==M_ERROR)) {
1178                         cprintf("%d Unknown address - cannot send message.\n",
1179                                 ERROR+NO_SUCH_USER);
1180                         return;
1181                         }
1182                 if ((e!=M_LOCAL)&&(CC->usersupp.axlevel<4)) {
1183                         cprintf("%d Net privileges required for network mail.\n",
1184                                 ERROR+HIGHER_ACCESS_REQUIRED);
1185                         return;
1186                         }
1187                 if ((RESTRICT_INTERNET==1)&&(e==M_INTERNET)
1188                    &&((CC->usersupp.flags&US_INTERNET)==0)
1189                    &&(!CC->internal_pgm) ) {
1190                         cprintf("%d You don't have access to Internet mail.\n",
1191                                 ERROR+HIGHER_ACCESS_REQUIRED);
1192                         return;
1193                         }
1194                 if (!strcasecmp(buf,"sysop")) {
1195                         mtsflag=1;
1196                         goto SKFALL;
1197                         }
1198                 if (e!=M_LOCAL) goto SKFALL;    /* don't search local file  */
1199                 if (!strcasecmp(buf,CC->usersupp.fullname)) {
1200                         cprintf("%d Can't send mail to yourself!\n",
1201                                 ERROR+NO_SUCH_USER);
1202                         return;
1203                         }
1204
1205                 /* Check to make sure the user exists; also get the correct
1206                 * upper/lower casing of the name. 
1207                 */
1208                 a = getuser(&tempUS,buf);
1209                 if (a != 0) {
1210                         cprintf("%d No such user.\n",ERROR+NO_SUCH_USER);
1211                         return;
1212                         }
1213                 strcpy(buf,tempUS.fullname);
1214                 }
1215         
1216 SKFALL: b=MES_NORMAL;
1217         if (CC->quickroom.QRflags&QR_ANONONLY) b=MES_ANON;
1218         if (CC->quickroom.QRflags&QR_ANONOPT) {
1219                 if (anon_flag==1) b=MES_AN2;
1220                 }
1221         if ((CC->quickroom.QRflags & QR_MAILBOX) == 0) buf[0]=0;
1222
1223         /* If we're only checking the validity of the request, return
1224          * success without creating the message.
1225          */
1226         if (post==0) {
1227                 cprintf("%d %s\n",OK,buf);
1228                 return;
1229                 }
1230         
1231         cprintf("%d send message\n",SEND_LISTING);
1232         if (CC->fake_postname[0])
1233            make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_postname);
1234         else
1235            if (CC->fake_username[0])
1236               make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_username);
1237            else
1238               make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, "");
1239         save_message(CC->temp,buf, (mtsflag ? AIDEROOM : ""), e,1);
1240         CC->fake_postname[0]='\0';
1241         return;
1242         }
1243
1244
1245
1246 /* 
1247  * message entry - mode 3 (raw)
1248  */
1249 void cmd_ent3(char *entargs)
1250 {
1251         char recp[256];
1252         char buf[256];
1253         int a;
1254         int e = 0;
1255         struct usersupp tempUS;
1256         long msglen;
1257         long bloklen;
1258         FILE *fp;
1259
1260         if (CC->internal_pgm == 0) {
1261                 cprintf("%d This command is for internal programs only.\n",
1262                         ERROR);
1263                 return;
1264                 }
1265
1266         /* See if there's a recipient, but make sure it's a real one */
1267         extract(recp, entargs, 1);
1268         for (a=0; a<strlen(recp); ++a)
1269                 if (!isprint(recp[a]))
1270                         strcpy(&recp[a], &recp[a+1]);
1271         while (isspace(recp[0])) strcpy(recp, &recp[1]);
1272         while (isspace(recp[strlen(recp)-1])) recp[strlen(recp)-1] = 0;
1273
1274         /* If we're in Mail, check the recipient */
1275         if (strlen(recp) > 0) {
1276                 e=alias(recp);                  /* alias and mail type */
1277                 if ((recp[0]==0) || (e==M_ERROR)) {
1278                         cprintf("%d Unknown address - cannot send message.\n",
1279                                 ERROR+NO_SUCH_USER);
1280                         return;
1281                         }
1282                 if (e == M_LOCAL) {
1283                         a = getuser(&tempUS,recp);
1284                         if (a!=0) {
1285                                 cprintf("%d No such user.\n",
1286                                         ERROR+NO_SUCH_USER);
1287                                 return;
1288                                 }
1289                         }
1290                 }
1291
1292         /* At this point, message has been approved. */
1293         if (extract_int(entargs, 0) == 0) {
1294                 cprintf("%d OK to send\n", OK);
1295                 return;
1296                 }
1297
1298         /* open a temp file to hold the message */
1299         fp = fopen(CC->temp, "wb");
1300         if (fp == NULL) {
1301                 cprintf("%d Cannot open %s: %s\n", 
1302                         ERROR + INTERNAL_ERROR,
1303                         CC->temp, strerror(errno) );
1304                 return;
1305                 }
1306
1307         msglen = extract_long(entargs, 2);
1308         cprintf("%d %ld\n", SEND_BINARY, msglen);
1309         while(msglen > 0L) {
1310                 bloklen = ((msglen >= 255L) ? 255 : msglen);
1311                 client_read(buf, (int)bloklen );
1312                 fwrite(buf, (int)bloklen, 1, fp);
1313                 msglen = msglen - bloklen;
1314                 }
1315         fclose(fp);
1316
1317         save_message(CC->temp, recp, "", e, 0);
1318         }
1319
1320
1321 /*
1322  * Delete message from current room
1323  */
1324 void cmd_dele(char *delstr)
1325 {
1326         long delnum;
1327         int a,ok;
1328
1329         getuser(&CC->usersupp,CC->curr_user);
1330         if ((CC->usersupp.axlevel < 6)
1331            && (CC->usersupp.usernum != CC->quickroom.QRroomaide)
1332            && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
1333                 cprintf("%d Higher access required.\n",
1334                         ERROR+HIGHER_ACCESS_REQUIRED);
1335                 return;
1336                 }
1337
1338         delnum = extract_long(delstr, 0);
1339         
1340         /* get room records, obtaining a lock... */
1341         lgetroom(&CC->quickroom,CC->quickroom.QRname);
1342         get_msglist(&CC->quickroom);
1343
1344         ok = 0;
1345         if (CC->num_msgs > 0) for (a=0; a<(CC->num_msgs); ++a) {
1346                 if (MessageFromList(a) == delnum) {
1347                         SetMessageInList(a, 0L);
1348                         ok = 1;
1349                         }
1350                 }
1351
1352         CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1353         CC->quickroom.QRhighest = MessageFromList(CC->num_msgs - 1);
1354
1355         put_msglist(&CC->quickroom);
1356         lputroom(&CC->quickroom,CC->quickroom.QRname);
1357         if (ok==1) {
1358                 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
1359                 cprintf("%d Message deleted.\n",OK);
1360                 }
1361         else cprintf("%d No message %ld.\n",ERROR,delnum);
1362         } 
1363
1364
1365 /*
1366  * move a message to another room
1367  */
1368 void cmd_move(char *args)
1369 {
1370         long num;
1371         char targ[32];
1372         int a;
1373         struct quickroom qtemp;
1374         int foundit;
1375
1376         num = extract_long(args,0);
1377         extract(targ,args,1);
1378         
1379         getuser(&CC->usersupp,CC->curr_user);
1380         if ((CC->usersupp.axlevel < 6)
1381            && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
1382                 cprintf("%d Higher access required.\n",
1383                         ERROR+HIGHER_ACCESS_REQUIRED);
1384                 return;
1385                 }
1386
1387         if (getroom(&qtemp, targ) != 0) {
1388                 cprintf("%d '%s' does not exist.\n",ERROR,targ);
1389                 return;
1390                 }
1391
1392         /* yank the message out of the current room... */
1393         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1394         get_msglist(&CC->quickroom);
1395
1396         foundit = 0;
1397         for (a=0; a<(CC->num_msgs); ++a) {
1398                 if (MessageFromList(a) == num) {
1399                         foundit = 1;
1400                         SetMessageInList(a, 0L);
1401                         }
1402                 }
1403         if (foundit) {
1404                 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1405                 put_msglist(&CC->quickroom);
1406                 CC->quickroom.QRhighest = MessageFromList((CC->num_msgs)-1);
1407                 }
1408         lputroom(&CC->quickroom,CC->quickroom.QRname);
1409         if (!foundit) {
1410                 cprintf("%d msg %ld does not exist.\n",ERROR,num);
1411                 return;
1412                 }
1413
1414         /* put the message into the target room */
1415         lgetroom(&qtemp, targ);
1416         qtemp.QRhighest = AddMessageToRoom(&qtemp, num);
1417         lputroom(&qtemp, targ);
1418
1419         cprintf("%d Message moved.\n", OK);
1420         }