home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / philmail.lzh / readmail.c < prev   
C/C++ Source or Header  |  1992-06-27  |  9KB  |  373 lines

  1. /* see below for SENDMAIL hack... */
  2.  
  3. /* a simple mail reader.
  4.  * because reading mail was just getting TOO yeuchy long-distance.
  5.  * This was originally made so that I could grab my mailbox from my UNIX
  6.  * account, read it, then output a bunch of outbound mail files, with
  7.  * first line as To:, and second line as Subject:,
  8.  *  then a REALLY SIMPLE shell-script can mail each one on the
  9.  *   UNIX end.
  10.  *
  11.  *  (foreach i (mail.*) ; sendmail -t < $i; done)
  12.  *
  13.  * folks may use this source for whatever they want, as long as they
  14.  * mention that they used my source in their copyright/credits
  15.  * Philip Brown
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <time.h>
  21. #include <strings.h>
  22.  
  23. #define MAXARTICLES    50
  24. #define TRUE        1
  25. #define FALSE        0
  26.  
  27. char *includestring = "    ";
  28.  
  29. /*
  30.  * SENDMAIL HACK HERE ...
  31.  * send sendmail = "rmail"
  32.  *   or something that reads from stdin, and sends to its first argument.
  33.  *   ie "rmail user@machine <file"
  34.  */
  35.  
  36. char *sendmail = NULL;
  37.  
  38. /* gracefull quit.
  39.  * call only on user-invoked quit
  40.  */
  41. quit(){
  42.     puts(" quitting philmail.");
  43.     puts("Don't forget to delete mailfile manually if ya don't want it no mo.");
  44.     exit(0);
  45. }
  46. char *progname;
  47.  
  48. FILE *fp;
  49. typedef struct article ARTICLE;
  50.  
  51. struct article {
  52.     long position;
  53.     char From[512];
  54.     char Subject[512];
  55.     char Cc[1024];
  56. };
  57.  
  58.  
  59. ARTICLE *articles[MAXARTICLES];
  60. char buf[1024];
  61. int numofarticles=0;
  62. int currentarticle=0;
  63. int lastread=0;
  64.  
  65.  
  66.  
  67.  
  68. /* mail sends mail to a person , without
  69.  * replying to a prior specific  letter.
  70.  * Note: "to" WILL have a \n at the end, the way i call it.
  71.  */
  72. void mail(to)
  73. char *to;
  74. {
  75.     FILE *sendfile;
  76.     char filename[100];
  77.     long timeval;
  78.  
  79.     while(*to == ' ') to++;
  80.  
  81.     timeval = _getsys(D_Ticks,4);
  82.     sprintf(filename,"mail.%d",timeval);
  83.     if((sendfile=fopen(filename,"w")) == NULL){
  84.         fprintf(stderr,"Error: could not open file to send mail\n");
  85.         return;
  86.     }
  87.     printf("Successfully opened \"%s\" for reply\n",filename);
  88.     fprintf(sendfile,"To: %s",to);
  89.     if( index(to,'\n') == NULL)
  90.         fprintf(sendfile,"\n");
  91.     fprintf(sendfile,"Subject: <None>\n");
  92.     fprintf(sendfile,"Cc: \n");
  93.     fprintf(sendfile,"X-Mailer: philmail\n");
  94.     fprintf(sendfile,"\n");
  95.     
  96.     writemessage(sendfile,filename);    
  97. }
  98.  
  99.  
  100. ARTICLE *newarticle(){
  101.     ARTICLE *ret;
  102.     ret = (ARTICLE *) malloc(sizeof (ARTICLE));
  103.     if(ret == NULL){
  104.         fprintf(stderr,"out of memory\n");
  105.         exit(-1);
  106.     }
  107.     ret->position    =0L;
  108.     ret->From[0]    = '\0';
  109.     ret->Subject[0]    = '\0';
  110.     ret->Cc[0]    = '\0';
  111.     return (ret);
  112. }
  113. void getpositions(){
  114.     char *parse;
  115.     short inheader = 1;    /* always start in header!! */
  116.     int i=0;
  117.     
  118.     while( fgets(buf,1023,fp) != NULL){
  119.         switch(buf[0]){
  120.             case 10:
  121.             case 13:
  122.                 inheader = 0; /* end of header */
  123.             case 'C':
  124.                 if(inheader == 0) break;
  125.                 if(strncmp(buf,"Cc: ",4) == 0){
  126.                     strcpy(articles[i]->Cc,&buf[4]);
  127.                 }
  128.                 break;
  129.             /* From */
  130.             case 'F':
  131.                 if(strncmp(buf,"From ",5) == 0){
  132.                     /* this is actual message START:
  133.                      * set both From, and position
  134.                      */
  135.                     i++;
  136.                     inheader = 1;
  137.                     articles[i] = newarticle();
  138.                     articles[i]->position = ftell(fp) - strlen(buf);
  139.                     parse=&buf[4];
  140.                     while(*parse == ' ') parse++;
  141.                     strcpy(articles[i]->From,parse);
  142.  
  143.                 } else if(strncmp(buf,"From: ",6) == 0){
  144.                     if(inheader == 0) break;
  145.                     /* this is a "From:" adjustment */
  146.                     parse=&buf[5];
  147.                     while(*parse == ' ') parse++;
  148.                     setfrom(articles[i]->From,parse);
  149.                     
  150.                 }                
  151.                break;
  152.             /* Subject */
  153.             case 'S':
  154.                 if(inheader == 0) break;
  155.                 if(strncmp(buf,"Subject:",8) == 0){
  156.                     strcpy(articles[i]->Subject,&buf[9]);
  157.                 }
  158.                 break;
  159.             default:
  160.                break;
  161.         }
  162.     }
  163.     numofarticles= i + 1;
  164. }
  165. void help(){
  166.     puts("Wel, Mon, we got de 'h' comman, which give ye de headers");
  167.     puts("Den we got de 'p', wich stan fer de previus messeg");
  168.     puts("  Actually, it's de las' messeg read");
  169.     puts("   note dat the 's'ave messeg counts as reading a messeg");
  170.     puts("  OOPS... fer de s, use 's <number> filename'  ");
  171.     puts("\n  'n' an <enter> do de same ting, read de messeg");
  172.     puts("Reply to a messeg wid de 'r' comman, or sen' to EVER-one");
  173.     puts("  with 'R'. Note dat you kin give ea nummah wid dis.");
  174.     puts("Also, in addishaahn, you can give de number by itself, an");
  175.     puts("  you'll go to dat messeg, mon");
  176.     puts(" Den dere's  the 'c' command, wich let's ya change files, I hope");
  177.     puts("    Note dat with de 's' an 'r' comman', you kin use a numba after de lettr");
  178.     puts("\n       De '?' ting give yer dis ting heah!");
  179. }
  180.  
  181.  
  182. /* headers();
  183.  *      prints out headers of 
  184.  *      currentarticle to currentarticle+10, roughly.
  185.  *    also prints current article number
  186.  */
  187. void headers(){
  188.     int i;
  189.     i = currentarticle;
  190.     if(currentarticle >=numofarticles) {
  191.         puts("No more articles below");
  192.     }
  193.     do{
  194.         printf("%d ",i);fflush(stdout);
  195.         printheader(stdout,i);
  196.         i++;
  197.     } while(( i < numofarticles) &&( (i - currentarticle) <10 ));
  198.  
  199.     printf("Current article is %d ( %d is last article )\n",currentarticle,numofarticles-1);
  200. }
  201.  
  202.  
  203. void previous(){
  204.     /* This reads the LAST ARTICLE READ.
  205.      * That article then becomes the last article read.
  206.      *  this is to avoid nasty nastiness with "save", etc.
  207.      * Ergo, no hopping between #4 and #8.
  208.      */
  209.     currentarticle = lastread;
  210.     readmsg(stdout);
  211.         /* readmsg sets lastread */
  212. }
  213.  
  214.     
  215. main(argc,argv)
  216. int argc;
  217. char * argv[];
  218. {
  219.     char c;
  220.     int i;
  221.     char *parse;
  222.     char *filename;
  223.     char *envsendmail;
  224.  
  225.     progname = argv[0];
  226.     /* if we are called as "mail", we had better send mail using
  227.      * "rmail". sigh...
  228.      */
  229.     if(strncmp(progname,"mail",4) == 0) 
  230.         sendmail = "rmail";
  231.     if((envsendmail = (char *)getenv("SENDMAIL")) != NULL)
  232.         sendmail = envsendmail;
  233.     
  234.     switch(argc){
  235.         case 1:
  236.             if((filename = (char *) getenv("MAIL")) == NULL){
  237.                 fprintf("no mail file specified on comamnd line or in MAIL variable\n");
  238.                 exit(0);
  239.             }
  240.             break;
  241.         case 2:
  242.             if(argv[1][0] == '-') {
  243.                 usage();
  244.                 exit(0);
  245.             }            
  246.             mail(argv[1]);
  247.             exit(0);
  248.         case 3:
  249.             if(argv[1][0] == '-') {
  250.                 switch(argv[1][1]){
  251.                     case '?':
  252.                         usage();
  253.                         exit(0);
  254.                     case 'f':
  255.                         filename = argv[2];
  256.                         break;
  257.                     default:
  258.                         usage();
  259.                         exit(0);
  260.                 }
  261.             } /* if(argv....) */
  262.             break;
  263.         default:
  264.              usage();
  265.              exit(0);
  266.     }
  267.  
  268.     /* now open file and test if right format?!! */
  269.     puts("Welcome to philmail. Checking out file.....");
  270.     readfile(filename);
  271.  
  272.     puts("Hit enter to start reading, 'h' to show headings, '?' for help");
  273.     puts(" [ hit enter after each command]");
  274.  
  275.     for(;;){
  276.         /* MAIN LOOP HERE */
  277.         FILE *temp;
  278.     
  279.         printf("> ");fflush(stdout);
  280.         if(fgets(buf,1024,stdin) == NULL){
  281.             puts(" EOF encoutered. presuming exit desired");
  282.             quit();
  283.         }
  284.         parse = buf;
  285.         while(*parse == ' ') parse++;
  286.         switch(parse[0]){
  287.             case '0': case '1':
  288.             case '2': case '3':
  289.             case '4': case '5':
  290.             case '6': case '7':
  291.             case '8': case '9':
  292.                 setarticle(parse);
  293.                 printf("Current article is now %d\n",currentarticle);
  294.                 break;
  295.             case '?': help();
  296.                 break;
  297.             case 'c': 
  298.                 while(*++parse == ' ');
  299.                 if(*parse == '\n') break;
  300.                 if((temp=fopen(parse,"r")) == NULL){
  301.                     fprintf(stderr,"Can't open %s for reading\n",parse);
  302.                     break;
  303.                 }
  304.                 fgets(buf,1023,temp);
  305.                 if(strncmp("From ",buf,5) != 0){
  306.                     fprintf(stderr,"File is NOT in proper format\n",parse);
  307.                     break;
  308.                 }
  309.                 /* file is okay */
  310.                 fclose(temp);
  311.                 restart();
  312.                 readfile(parse);
  313.                 break;
  314.             case 'h': headers();
  315.                 break;
  316.             case 'm': mail(++parse);
  317.                 break;
  318.             case 'p': previous();
  319.                 break;
  320.             case 'q': quit();
  321.                 break;
  322.             /* 'r' for reply */
  323.             case 'r':
  324.                 /* skip whitespace */
  325.                 while(*++parse == ' ');
  326.                 if(isdigit(*parse))
  327.                     setarticle(parse);
  328.                 else
  329.                     currentarticle=lastread;
  330.                 reply(FALSE);
  331.                 break;
  332.             case 'R':
  333.                 /* skip whitespace */
  334.                 while(*++parse == ' ');
  335.                 if(isdigit(*parse))
  336.                     setarticle(parse);
  337.                 else
  338.                 /* hack */
  339.                     currentarticle=lastread;
  340.                 reply(TRUE);
  341.                 break;
  342.             case 's':
  343.                 /* saves "lastread" 
  344.                  * UNLESS number given. then we SET lastread
  345.                  * save counts as "reading"
  346.                  */
  347.                 while(*++parse == ' ');
  348.                 if(isdigit(*parse)){
  349.                     setarticle(parse);
  350.                     lastread = currentarticle++;
  351.                     while(isdigit(*++parse));
  352.                     while(*parse == ' ')parse++;
  353.                 }
  354.                 if(*parse == '\n') break;
  355.                 save(parse);
  356.                 /* hack */
  357.                 currentarticle = lastread+1;
  358.                 if(currentarticle == numofarticles)
  359.                     currentarticle--;
  360.                 break;
  361.             case 'n': 
  362.             case 10: 
  363.             case 13:
  364.                   readmsg(stdout);
  365.                 break;
  366.             default:
  367.                 puts("pardon?");
  368.                 while(fgets(buf,1023,stdin) != NULL);
  369.                 /* clear input buffer, above */
  370.                 break;
  371.         }
  372.     }
  373. }