home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lan / netx / bm / main.c < prev    next >
Text File  |  1987-08-25  |  8KB  |  310 lines

  1. /* bm.c
  2.  *    Simple mail user interface for KA9Q IP/TCP package.
  3.  *    A.D. Barksdale Garbee II, aka Bdale, N3EUA
  4.  *    Copyright 1986 Bdale Garbee, All Rights Reserved.
  5.  *    Permission granted for non-commercial copying and use, provided
  6.  *    this notice is retained.
  7.  *
  8.  * revision history:
  9.  *
  10.  *  v2.6 870826 Bdale
  11.  *    integrate PA0GRI's interface/gateway to the WA7MBL PBBS
  12.  *  v2.5 870713 Bdale
  13.  *      integrate additional patches from PA0GRI, minor cleanup
  14.  *  v2.4 870614 - P. Karn
  15.  *      "smart gateway" function moved to smtp client code in net.exe.
  16.  *    User interface for sending mail reworked to resemble Berkeley Mail
  17.  *  v2.3 870524
  18.  *      Extensive addition/revision by Gerard PA0GRI.  Now supports a healthy
  19.  *      set of real commands.  
  20.  *  v2.1,2.2
  21.  *      exact change history lost.
  22.  *  v2.0 c.870115
  23.  *      First version with command parser, only send and quit work.
  24.  *  v1.0
  25.  *      First attempt.  Send messages only.
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include "../src/smtp.h"
  30. #include "bm.h"
  31.  
  32. extern char version[];
  33. static char copyright[] = 
  34. "Copyright 1987 Bdale Garbee, permission granted for non-commercial use.";
  35.  
  36. FILE    *rcfp;        /* handle for the configuration file */
  37. FILE    *mfile;
  38. char    rcline[LINELEN],    /* buffer for config file reading routines */
  39.      hostname[SLINELEN],    /* name of this host from rc file */
  40.     username[SLINELEN],    /* name of this user from rc file */
  41.     timezone[4],        /* users time zone */
  42.     maildir[LINELEN],    /* defined mail directory */
  43.     editor[SLINELEN];    /* user's favorite text editor */
  44.  
  45. /* command parser, handle at least read, send, headers, delete */
  46. main(argc,argv) 
  47. int argc;
  48. char *argv[];
  49. {
  50.     char command[80];    /* command line */
  51.     char    line[80];
  52.     char    notename[9];    /* name of notesfile */
  53.     char    notefile[80];    /* temorary for building strings */
  54.     char    *notel, *tch, *index();
  55.     int    notes, msgnum;
  56.     static int printer;    /* printer switch */
  57.     /* announce ourselves */
  58.     printf("\t\t%s\n%s\n\n",version,copyright);
  59.  
  60.     /* initialize globals from config file */
  61.     *editor = 0x00;  /* default no editor? */
  62.     *maildir = 0x00;
  63.     strcpy(timezone,"GMT");
  64.     printer = 0;
  65.     read_rc();
  66.  
  67.     if(*maildir == '\0')
  68.         strcpy(maildir,"/spool/mail");
  69.     strncpy(notename,username,8);
  70.     if(argc > 1){
  71.         dosmtpsend(NULL,argv[1]);
  72.         exit(0);
  73.     }
  74.     printf(" Type ? for help.\n");
  75.     sprintf(notefile,"%s/%s.txt",maildir,notename);
  76.  
  77.     /* command parsing loop */
  78.     do {
  79.       printf("\"%s\"> ",notename);
  80.       gets(command);
  81.       switch (command[0]) {
  82.  
  83.         case 's':        /* send msg */
  84.             dosmtpsend(NULL,NULL);
  85.         break;
  86.  
  87.         case 't':        /* transmit msg in file*/
  88.             printf("Enter filename containing message :");
  89.         gets(command);
  90.         if((mfile = fopen(command,"r")) == NULL)
  91.             printf("\n Cannot open file %s\n",command);
  92.         else {
  93.             dosmtpsend(mfile,NULL);
  94.             fclose(mfile);
  95.         }
  96.         command[0] = 't'; /* just in case file started with q */
  97.         break;
  98.  
  99.         case 'q':        /* quit */
  100.             break;
  101.  
  102.         case 'p':        /* printer on or off */
  103.             if(printer) {
  104.     /* Bdale, i dont have the description of dos function to set 
  105.           printer echo on or off, but it looks nice to me if
  106.           it would be possible to do it.. gerard */
  107.         } else {
  108.         }
  109.         printer = !printer;
  110.         printf("Printer is now %s.\n", printer ? "on" : "off" );
  111.         break;
  112.  
  113.         case 'r':        /* read all messages */
  114.         if((mfile = fopen(notefile,"r")) == NULL)
  115.             printf(" Cannot open notesfile \"%s\" \n",notename);
  116.         else {
  117.             msgnum = 0;
  118.             while(!feof(mfile)) {
  119.                 fgets(line,80,mfile);
  120.                 rip(line);
  121.                 if((strncmp(line,"Subject:",8)) == 0)
  122.                     msgnum++;
  123.                 printf("%s\n",line);
  124.             }
  125.             fclose(mfile);
  126.             seqset(notename,msgnum);
  127.         }
  128.         break;
  129.  
  130.         case 'u':
  131.         updmsg(notename);
  132.         break;
  133.  
  134.         case 'f':        /* forward a message */
  135.         notel = command;
  136.     /* skip forward command */
  137.         while(*notel != '\0') {
  138.             if(*notel != ' ' && *notel != '\t')
  139.                 notel++;
  140.             else
  141.                 break;
  142.         }
  143.         if(*notel != '\0') {
  144.             msgnum = atoi(notel);
  145.             if((mfile = fopen("zqxwgri.tmp","w")) == NULL)
  146.                 printf("\n Cannot open temp file \n");
  147.             else {
  148.                 tmpmsg(notename,msgnum,mfile);
  149.                 fclose(mfile);
  150.                 mfile = fopen("zqxwgri.tmp","r");
  151.                 dosmtpsend(mfile,NULL);
  152.                 fclose(mfile);
  153.                 unlink("zqxwgri.tmp");
  154.             }
  155.         }
  156.         break;
  157.  
  158.         case 'l':        /* display unsent messages */
  159.             printf("List of unsent messages.\n");
  160.         filedir("/spool/mqueue/*.txt",0,line);
  161.             while(line[0] != '\0') {
  162.             printf("message in -> %s \n",line);
  163.             filedir("/spool/mqueue/*.txt",1,line);
  164.         }
  165.         break;
  166.  
  167.         case 'n':        /* display or change notefiles */
  168.         if(strlen(command) > 2) {
  169.             notel = command;
  170.         /* skip notefile command */
  171.             while(*notel != '\0') {
  172.                 if(*notel != ' ' && *notel != '\t')
  173.                     notel++;
  174.                 else
  175.                     break;
  176.             }
  177.             if(*notel == '\0')
  178.                 break;
  179.         /* skip white space */
  180.             while(*notel == ' ' || *notel == '\t')
  181.                 notel++;
  182.             if(*notel != '\0') {
  183.                 strncpy(notename,notel,8);
  184.                 sprintf(notefile,"%s/%s.txt",maildir,notename);
  185.             }
  186.         } else {  /* he wants to see what notefiles there are */
  187.             sprintf(notefile,"%s/*.txt",maildir);
  188.             filedir(notefile,0,line);
  189.             while(line[0] != '\0') {
  190.                 tch = index(line,'.');
  191.                 *tch = '\0';
  192.                 printf("notesfile -> %s \n",line);
  193.                 filedir(notefile,1,line);
  194.             }
  195.     /* after using notefile reset it to current */
  196.             sprintf(notefile,"%s/%s.txt",maildir,notename);
  197.         }
  198.         break;
  199.  
  200.         case 'd':        /* delete a message */
  201.         notel = command;
  202.     /* skip delete command */
  203.         while(*notel != '\0') {
  204.             if(*notel != ' ' && *notel != '\t')
  205.                 notel++;
  206.             else
  207.                 break;
  208.         }
  209.         if(*notel != '\0') {
  210.             msgnum = atoi(notel);
  211.             delmsg(notename,msgnum);
  212.         }
  213.         break;
  214.  
  215.         case 'h':        /* list message headers in notesfile */
  216.         listnotes(notename);
  217.         break;
  218.  
  219.         case '?':        /* help */
  220.             printf("    d nn       delete a message\n");
  221.             printf("    s          send a message\n");
  222.             printf("    t          send a message from a file\n");
  223.         printf("    r          read message in notesfile\n");
  224.         printf("    f          forward a message \n");
  225.         printf("    u          update me for new messages\n");
  226.         printf("    p          printer on or off (toggle) n.y.i.\n");
  227.             printf("    h          display message headers in notefile\n");
  228.         printf("    l          list unsent messages\n");
  229.             printf("    n file     display or set notesfile\n");
  230.         printf("    #          number of message to read in notesfile\n");
  231.             printf("    ?          print this help screen\n");
  232.             printf("    q          quit\n");
  233.         break;
  234.  
  235.         default:
  236.         if(( msgnum = atoi(command)) != 0 )
  237.             readmsg(notename,msgnum);
  238.         else
  239.             printf(" commands <d,s,t,r,f,u,p,h,l,n,#,?,q>\n");
  240.             break;
  241.       }
  242.     } while (command[0] != 'q');
  243. }
  244.  
  245. read_rc()
  246. {
  247.     if((rcfp = fopen("/bm.rc","r")) == NULL) {    /* open config file */
  248.         printf("Cannot open BM.RC in root, check your installation\n");
  249.         exit(-1);
  250.     } else
  251.         while (!feof(rcfp)) {
  252.         fgets(rcline,LINELEN,rcfp);
  253.         if (rcline[0] == ';') continue;
  254.         if (strlen(rcline) < 5) continue;
  255.         switch (rc_line_type(rcline)) {
  256.             case HOST:
  257.                 strcpy(hostname,&rcline[5]);
  258.                 rip(hostname);
  259.                 break;
  260.             case USER:
  261.                 strcpy(username,&rcline[5]);
  262.                 rip(username);
  263.                 break;
  264.             case EDIT:
  265.                 strcpy(editor,&rcline[5]);
  266.                 rip(editor);
  267.                 break;
  268.             case ZONE:
  269.                 strncpy(timezone,&rcline[5],3);
  270.                 timezone[3] = '\0';
  271.                 break;
  272.             case SMTP: 
  273.                 strcpy(maildir,&rcline[5]);
  274.                 rip(maildir);
  275.                 break;
  276.             case FIDO:
  277.             default:
  278.                 /* handle these cases in v2.0! */
  279.                 break;
  280.         }    
  281.         }
  282.     fclose(rcfp);
  283. }
  284.  
  285. /* return the line_type from a line of the configuration file */
  286. rc_line_type(s)
  287. char *s;
  288. {
  289.     if (strncmp("fido",s,4) == 0) return (FIDO);
  290.     if (strncmp("smtp",s,4) == 0) return (SMTP);
  291.     if (strncmp("host",s,4) == 0) return (HOST);
  292.     if (strncmp("user",s,4) == 0) return (USER);
  293.     if (strncmp("edit",s,4) == 0) return (EDIT);
  294.     if (strncmp("zone",s,4) == 0) return (ZONE);
  295.     return (NONE);
  296. }
  297.  
  298. /* replace terminating end of line marker(s) with null */
  299. rip(s)
  300. register char *s;
  301. {
  302.     register char *cp;
  303.     char *index();
  304.  
  305.     if((cp = index(s,'\r')) != NULL)
  306.         *cp = '\0';
  307.     if((cp = index(s,'\n')) != NULL)
  308.         *cp = '\0';
  309. }
  310.