home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / MBOXFILE.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  7KB  |  296 lines

  1. /* These are the mailbox FILE commands */
  2. #include "global.h"
  3. #ifdef FILECMDS
  4. #include <time.h>
  5. #include <ctype.h>
  6. #ifdef MSDOS
  7. #include <alloc.h>
  8. #endif
  9. #ifdef  UNIX
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #endif
  13. #include "timer.h"
  14. #include "proc.h"
  15. #include "socket.h"
  16. #include "usock.h"
  17. #include "session.h"
  18. #include "smtp.h"
  19. #include "dirutil.h"
  20. #include "telnet.h"
  21. #include "ftp.h"
  22. #include "ftpserv.h"
  23. #include "commands.h"
  24. #include "netuser.h"
  25. #include "files.h"
  26. #include "bm.h"
  27. #include "pktdrvr.h"
  28. #include "ax25.h"
  29. #include "mailbox.h"
  30. #include "ax25mail.h"
  31. #include "nr4mail.h"
  32. #include "cmdparse.h"
  33. #include "mailfor.h"
  34.   
  35. extern char Noperm[];
  36.   
  37. /* uuencode a file -- translated from C++; both versions copyright 1990
  38.    by David R. Evans, G4AMJ/NQ0I
  39. */
  40.   
  41. int
  42. uuencode(infile,s,infilename)
  43. FILE *infile;
  44. int s;                  /* output socket */
  45. char *infilename;
  46. {
  47.     int n_read_so_far = 0, n_written_so_far = 0, in_chars, n, mode = 0755;
  48.     unsigned long cnt = 0;
  49.     unsigned char in[3], out[4], line[100];
  50. #ifdef UNIX
  51.     struct stat stb;
  52.   
  53.     if(stat(infilename,&stb) != -1)
  54.         mode = stb.st_mode & 0777;       /* get real file protection mode */
  55. #endif
  56.     usprintf(s, "begin %03o %s\n", mode, infilename);
  57.   
  58.   /* do the encode */
  59.     for(;;){
  60.         in_chars = fread(in, 1, 3, infile);
  61.         out[0] = in[0] >> 2;
  62.         out[1] = in[0] << 6;
  63.         out[1] = out[1] >> 2;
  64.         out[1] = out[1] | (in[1] >> 4);
  65.         out[2] = in[1] << 4;
  66.         out[2] = out[2] >> 2;
  67.         out[2] = out[2] | (in[2] >> 6);
  68.         out[3] = in[2] << 2;
  69.         out[3] = out[3] >> 2;
  70.         for (n = 0; n < 4; n++)
  71.             out[n] += ' ';
  72.         n_read_so_far += in_chars;
  73.         for (n = 0; n < 4; n++)
  74.             line[n_written_so_far++] = out[n];
  75.         if (((in_chars != 3) || (n_written_so_far == 60)) && n_read_so_far > 0) {
  76.             line[(n_read_so_far + 2) / 3 * 4] = '\0';
  77.   
  78.             usprintf(s,"%c%s\n",n_read_so_far + ' ', line);
  79.             cnt += n_read_so_far;
  80.             n_read_so_far = 0;
  81.             n_written_so_far = 0;
  82.         }
  83.         if (in_chars == 0)
  84.             break;
  85.     }
  86.     if (usprintf(s," \nend\nsize %lu\n", cnt) == EOF)
  87.         return 1;
  88.     return 0;
  89. }
  90.   
  91. int
  92. dodownload(argc,argv,p)
  93. int argc;
  94. char *argv[];
  95. void *p;
  96. {
  97.     struct mbx *m;
  98.     FILE *fp;
  99.     char *file,*path;
  100.   
  101.     m = (struct mbx *)p;
  102.   
  103.     /* build the full pathname for the file */
  104.     path = firstpath(m->path);
  105.     file = pathname(path,argv[1]);
  106.     free(path);
  107.   
  108.     if(!permcheck(m->path,RETR_CMD,file)){
  109.         tputs(Noperm);
  110. #ifdef MAILERROR
  111.         mail_error("%s: download denied: %s\n",m->name,cmd_line(argc,argv,m->stype));
  112. #endif
  113.         return 0;
  114.     }
  115. #ifdef TIPSERVER
  116. #ifdef XMODEM
  117.     if (m->stype=='X') {
  118.         if (m->type==TIP_LINK){   /* xmodem send tip only */
  119.             m->state = MBX_XMODEM_TX;
  120.             /* disable the mbox inactivity timeout timer - WG7J */
  121.             alarm(0L);
  122.             doxmodem('S',file,m);
  123.             return 0;
  124.         } else {
  125.             tputs("Xmodem on TIP connects only\n");
  126.             return 0;
  127.         }
  128.     }
  129. #endif
  130. #endif
  131.     m->state = MBX_DOWNLOAD;
  132.     if((fp = fopen(file,READ_TEXT)) == NULLFILE)
  133.         tprintf("Can't open \"%s\": %s\n",file,sys_errlist[errno]);
  134.     else {
  135.         /* disable the mbox inactivity timeout timer - WG7J */
  136.         alarm(0L);
  137.         if(m->stype == 'U'){            /* uuencode ? */
  138.             fclose(fp);
  139.             fp = fopen(file,READ_BINARY);   /* assume non-ascii */
  140.             uuencode(fp,m->user,file);
  141.         } else
  142.             sendfile(fp,m->user,ASCII_TYPE,0,NULL);
  143.     }
  144.     free(file);
  145.     fclose(fp);
  146.     return 0;
  147. }
  148.   
  149. int
  150. dombupload(argc,argv,p)
  151. int argc;
  152. char *argv[];
  153. void *p;
  154. {
  155.     struct mbx *m;
  156.     FILE *fp;
  157.     char *file,*path;
  158.   
  159.     m = (struct mbx *)p;
  160.   
  161.     /* build the full pathname for the file */
  162.     path = firstpath(m->path);
  163.     file = pathname(path,argv[1]);
  164.     free(path);
  165.   
  166.     if(!permcheck(m->path,STOR_CMD,file)){
  167.         tputs(Noperm);
  168. #ifdef MAILERROR
  169.         mail_error("%s: upload denied: %s\n",m->name,cmd_line(argc,argv,m->stype));
  170. #endif
  171.         return 0;
  172.     }
  173. #ifdef TIPSERVER
  174. #ifdef XMODEM
  175.     if (m->stype=='X'){
  176.         if (m->type==TIP_LINK){   /* xmodem receive tip only */
  177.             m->state = MBX_XMODEM_RX;
  178.             /* disable the mbox inactivity timeout timer - WG7J */
  179.             alarm(0L);
  180.             doxmodem('R',file,m);
  181.             return 0;
  182.         } else {
  183.             tputs("Xmodem on TIP connects only\n");
  184.             return 0;
  185.         }
  186.     }
  187. #endif
  188. #endif
  189.   
  190.     if((fp = fopen(file,WRITE_TEXT)) == NULLFILE){
  191.         tprintf("Can't create \"%s\": %s\n",file,sys_errlist[errno]);
  192.         free(file);
  193.         return 0;
  194.     }
  195.     log(m->user,"MBOX upload: %s",file);
  196.     m->state = MBX_UPLOAD;
  197.     tprintf("Send file,  %s",Howtoend);
  198.     for(;;){
  199.         if(mbxrecvline(m) == -1){
  200.             unlink(file);
  201.             break;
  202.         }
  203.         if(*m->line == 0x01){  /* CTRL-A */
  204.             unlink(file);
  205.             tputs(MsgAborted);
  206.             break;
  207.         }
  208.         if(*m->line == CTLZ || !stricmp("/ex",m->line))
  209.             break;
  210.         fputs(m->line,fp);
  211. #if !defined(UNIX) && !defined(__TURBOC__) && !defined(AMIGA)
  212.         /* Needed only if the OS uses a CR/LF
  213.          * convention and putc doesn't do
  214.          * an automatic translation
  215.          */
  216.         if(putc('\r',fp) == EOF)
  217.             break;
  218. #endif
  219.         if(putc('\n',fp) == EOF)
  220.             break;
  221.     }
  222.     free(file);
  223.     fclose(fp);
  224.     return 0;
  225. }
  226.   
  227. int
  228. dowhat(argc,argv,p)
  229. int argc;
  230. char *argv[];
  231. void *p;
  232. {
  233.     struct mbx *m;
  234.     FILE *fp;
  235.     char *file,*path;
  236.   
  237.     m = (struct mbx *)p;
  238.   
  239.     path = firstpath(m->path);
  240.     if(argc < 2)
  241.         file = strdup(path);
  242.     else
  243.         file = pathname(path,argv[1]);
  244.     free(path);
  245.   
  246.     if(!permcheck(m->path,RETR_CMD,file)){
  247.         tputs(Noperm);
  248. #ifdef MAILERROR
  249.         mail_error("%s: directory denied: %s\n",m->name,cmd_line(argc,argv,m->stype));
  250. #endif
  251.         return 0;
  252.     }
  253.     m->state = MBX_WHAT;
  254.     if((fp = dir(file,1)) == NULLFILE)
  255.         tprintf("Can't read directory: \"%s\": %s\n",file,sys_errlist[errno]);
  256.     else {
  257.         alarm(0L);
  258.         sendfile(fp,m->user,ASCII_TYPE,0,NULL);
  259.     }
  260.     free(file);
  261.     fclose(fp);
  262.     return 0;
  263. }
  264.   
  265. int
  266. dozap(argc,argv,p)
  267. int argc;
  268. char *argv[];
  269. void *p;
  270. {
  271.     struct mbx *m;
  272.     char *file,*path;
  273.   
  274.     m = (struct mbx *)p;
  275.   
  276.     path = firstpath(m->path);
  277.     file = pathname(path,argv[1]);
  278.     free(path);
  279.   
  280.   
  281.     if(!permcheck(m->path,DELE_CMD,file)){
  282.         tputs(Noperm);
  283. #ifdef MAILERROR
  284.         mail_error("%s: zap denied: %s\n",m->name,cmd_line(argc,argv,m->stype));
  285. #endif
  286.         return 0;
  287.     }
  288.     if(unlink(file))
  289.         tprintf("Zap failed: %s\n",sys_errlist[errno]);
  290.     log(m->user,"MBOX Zap: %s",file);
  291.     free(file);
  292.     return 0;
  293. }
  294.   
  295. #endif /* FILECMDS */
  296.