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