home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lan / netx / bm / bmutil.c < prev    next >
Text File  |  1987-06-11  |  8KB  |  303 lines

  1. /* bmutil.c */
  2. #include <stdio.h>
  3. #include "../src/smtp.h"
  4. #include "bm.h"
  5.  
  6. /* list headers of a notesfile a message */
  7. listnotes(notefn)
  8. char *notefn;
  9. {
  10.     char    notefile[LINELEN], 
  11.         smtp_date[LINELEN],
  12.         smtp_from[LINELEN],
  13.         smtp_subject[LINELEN], 
  14.         tstring[LINELEN];        /* temp for integer conv */
  15.     char    *tch1, *tch2, *index();
  16.     int    sequence,current;
  17.     FILE    *mfile;
  18.  
  19.     current = seqget(notefn);    /* get current notesfile index */
  20.     sprintf(notefile,"%s/%s.txt",maildir,notefn);
  21.     if((mfile = fopen(notefile,"r")) == NULL) {
  22.         printf("Cannot open notesfile %s \n",notefn);
  23.         return ;
  24.     }
  25.     sequence = 1;
  26.     while(!feof(mfile)){
  27.         fgets(tstring,LINELEN,mfile);
  28.  
  29.     /* scan for Received: i.e. begin of a message */
  30.  
  31.         if(strncmp(tstring,"Received",8) == 0) {
  32.             strncpy(smtp_date,&tstring[10],16);
  33.             if(strlen(smtp_date) > 16)
  34.                 smtp_date[16] = '\0';
  35.     /* scan for From: in message */
  36.             fgets(tstring,LINELEN,mfile); /* read empty line */
  37.             while(strncmp(tstring,"From:",5) != 0) 
  38.                 fgets(tstring,LINELEN,mfile);
  39.             strncpy(smtp_from,&tstring[10],13);
  40.             if(strlen(smtp_from) > 13)
  41.                 smtp_from[13] = '\0';
  42.     /* try to make a nice user@system string */
  43.             if((tch1 = index(smtp_from,'@')) != NULL)
  44.                 if((tch2 = index(tch1,'.')) != NULL)
  45.                     *tch2 = '\0';
  46.             fgets(tstring,LINELEN,mfile); /* read To:   line */
  47.             fgets(tstring,LINELEN,mfile); /* read Subj: line */
  48.             strncpy(smtp_subject,&tstring[10],34);
  49.             rip(smtp_subject);
  50.             printf("%c%4d | %16s | %14s | %s \n",
  51.                 ( sequence == current ? '*' : ' '), sequence,
  52.                 smtp_date,smtp_from,smtp_subject);
  53.              sequence++;
  54.             if((sequence % 24) == 0) {
  55.                 printf(" - more -");
  56.                 getchar();
  57.             }    
  58.         }
  59.     }
  60. }
  61.  
  62. /*  readmsg - read a single message in current notesfile */
  63. readmsg(notefn,msg)
  64. char *notefn;
  65. int msg;
  66. {
  67.     char    notefile[LINELEN], 
  68.         tstring[LINELEN];        /* temp for integer conv */
  69.     int    sequence,current,flag;
  70.     FILE    *mfile;
  71.  
  72.     current = seqget(notefn);    /* get current notesfile index */
  73.     sprintf(notefile,"%s/%s.txt",maildir,notefn);
  74.     if((mfile = fopen(notefile,"r")) == NULL) {
  75.         printf("Cannot open notesfile %s \n",notefn);
  76.         return ;
  77.     }
  78.     sequence = 1;
  79.     flag = 0;
  80.     while(!feof(mfile)){
  81.         fgets(tstring,LINELEN,mfile);
  82.  
  83.     /* scan for Received: i.e. begin of a message */
  84.  
  85.         if(strncmp(tstring,"Received",8) == 0) {
  86.             if( sequence == msg) 
  87.                 flag = 1;
  88.             else 
  89.                 flag = 0;
  90.     /* scan for From: in message */
  91.             while((strncmp(tstring,"From:",5) != 0) && !feof(mfile)) {
  92.                 if(msg == sequence || flag )
  93.                     printf("%s",tstring);
  94.                 fgets(tstring,LINELEN,mfile);
  95.             }
  96.              sequence++;
  97.         }
  98.         if(flag)
  99.             printf("%s",tstring);
  100.     }
  101.     fclose(mfile);
  102.     if(++current == msg)
  103.         seqset(notefn,msg);
  104.     return;
  105. }
  106.  
  107. /*  tmpmsg - write message in current notesfile  to file*/
  108. tmpmsg(notefn,msg,tfile)
  109. char *notefn;
  110. int msg;
  111. FILE *tfile;   /* already open for write */
  112. {
  113.     char    notefile[LINELEN], 
  114.         tstring[LINELEN];        /* temp for integer conv */
  115.     int    sequence,current,flag;
  116.     FILE    *mfile;
  117.  
  118.     current = seqget(notefn);    /* get current notesfile index */
  119.     sprintf(notefile,"%s/%s.txt",maildir,notefn);
  120.     if((mfile = fopen(notefile,"r")) == NULL) {
  121.         printf("Cannot open notesfile %s \n",notefn);
  122.         return ;
  123.     }
  124.     sequence = 0;
  125.     flag = 0;
  126.     while(!feof(mfile)){
  127.         fgets(tstring,LINELEN,mfile);
  128.  
  129.     /* scan for Received: i.e. begin of a message */
  130.  
  131.         if(strncmp(tstring,"Received",8) == 0) {
  132.              sequence++;
  133.             if( sequence == msg) 
  134.                 flag = 1;
  135.             else 
  136.                 flag = 0;
  137.     /* scan for From: in message */
  138.             while((strncmp(tstring,"From:",5) != 0) && !feof(mfile)) {
  139.                 if( flag )
  140.                     fprintf(tfile,"%s",tstring);
  141.                 fgets(tstring,LINELEN,mfile);
  142.             }
  143.         }
  144.         if(flag)
  145.             fprintf(tfile,"%s",tstring);
  146.     }
  147.     fclose(mfile);
  148.     if(++current == msg)
  149.         seqset(notefn,msg);
  150.     return;
  151. }
  152. /*  updmsg - read new messages in current notesfile */
  153. updmsg(notefn)
  154. char *notefn;
  155. {
  156.     char    notefile[LINELEN], 
  157.         tstring[LINELEN];        /* temp for integer conv */
  158.     int    sequence,current,flag;
  159.     FILE    *mfile;
  160.  
  161.     current = seqget(notefn);    /* get current notesfile index */
  162.     sprintf(notefile,"%s/%s.txt",maildir,notefn);
  163.     if((mfile = fopen(notefile,"r")) == NULL) {
  164.         printf("Cannot open notesfile %s \n",notefn);
  165.         return ;
  166.     }
  167.     sequence = 0;
  168.     flag = 0;
  169.     while(!feof(mfile)){
  170.         fgets(tstring,LINELEN,mfile);
  171.  
  172.     /* scan for Received: i.e. begin of a message */
  173.  
  174.         if(strncmp(tstring,"Received",8) == 0) {
  175.              sequence++;
  176.             if( sequence > current) {
  177.                 flag = 1;
  178.                 printf("------------------------------------ %d\n",sequence);
  179.             } else 
  180.                 flag = 0;
  181.     /* scan for From: in message */
  182.             while((strncmp(tstring,"From:",5) != 0) && !feof(mfile)) {
  183.                 if( flag )
  184.                     printf("%s",tstring);
  185.                 fgets(tstring,LINELEN,mfile);
  186.             }
  187.         }
  188.         if(flag)
  189.             printf("%s",tstring);
  190.     }
  191.     fclose(mfile);
  192.     if(sequence > current)
  193.         seqset(notefn,sequence);
  194.     else
  195.         printf("No new messages in notefile\n");
  196. }
  197. /*  delmsg - delete message in current notesfile */
  198. delmsg(notefn,msg)
  199. char *notefn;
  200. int msg;
  201. {
  202.  
  203.     char    notefile[LINELEN], 
  204.         tempfile[LINELEN],
  205.         tstring[LINELEN];        /* temp for integer conv */
  206.     char    *tch1, *tch2;
  207.     char    ch;
  208.     int    sequence,current, flag;
  209.     FILE    *mfile,*tfile;
  210.  
  211.     current = seqget(notefn);    /* get current notesfile index */
  212.     if(current < msg) {
  213.         printf("Warning: You are deleting a message you did not yet read \n");
  214.         printf("Do you want to continue?");
  215.         gets(tstring);
  216.         if(toupper(tstring[0]) != 'Y')
  217.             return;
  218.     }
  219.     printf("Deleting message #%d from notesfile \"%s\" \n",msg,notefn);
  220.     sprintf(notefile,"%s/%s.txt",maildir,notefn);
  221.     if((mfile = fopen(notefile,"r")) == NULL) {
  222.         printf("Cannot open notesfile %s \n",notefn);
  223.         return ;
  224.     }
  225.     sprintf(tempfile,"%s/%s.tmp",maildir,notefn);
  226.     tfile = fopen(tempfile,"w");
  227.     sequence = 1;
  228.     while(!feof(mfile)){
  229.  
  230.     /* scan for Received: i.e. begin of a message */
  231.         fgets(tstring,LINELEN,mfile);
  232.  
  233.         if(strncmp(tstring,"Received",8) == 0) {
  234.             if( sequence == msg) 
  235.                 flag = 1;
  236.             else 
  237.                 flag = 0;
  238.  
  239.     /* scan for From: in message */
  240.             while((strncmp(tstring,"From:",5) != 0) && !feof(mfile)) {
  241.                 if(msg != sequence || !flag )
  242.                     fputs(tstring,tfile);
  243.                 fgets(tstring,LINELEN,mfile);
  244.             }
  245.     /* We are shure the header of a message is skipped. */
  246.              sequence++;
  247.         }
  248.         if(!flag)
  249.             fputs(tstring,tfile);
  250.     }
  251.     fclose(tfile);
  252.     fclose(mfile);
  253.     unlink(notefile);
  254.     rename(tempfile,notefile);
  255.     if(msg <= current)
  256.         seqset(notefn,--current);
  257. }
  258.  
  259. /*  seqset - set the sequence number (last read) for the current notesfile */
  260. seqset(notefn,num)
  261. char *notefn;
  262. int num;
  263. {
  264.     FILE *sfile;
  265.     char sfilename[80];
  266.  
  267. /* open the sequence file */
  268.     sprintf(sfilename,"%s/%s.seq",maildir,notefn);
  269.     if((sfile = fopen(sfilename,"w")) == NULL) {
  270.         printf("Cannot create Notefile Sequence file!\n");
  271.         return;
  272.     }
  273.     fprintf(sfile,"%d",num);
  274. /* close the sequence file */
  275.     fclose(sfile);
  276.  
  277. }
  278.  
  279. /*  seqget - get the sequence number (last read) for the current notesfile */
  280. int
  281. seqget(notefn)
  282. char *notefn;
  283. {
  284.     FILE *sfile;
  285.     char sfilename[80],
  286.          tstring[LINELEN];
  287.     int num;
  288. /* open the sequence file */
  289.     sprintf(sfilename,"%s/%s.seq",maildir,notefn);
  290.     if((sfile = fopen(sfilename,"r")) != NULL) {
  291.         fgets(tstring,LINELEN,sfile);
  292.         num = atoi(tstring);
  293.     } else {
  294. /* sequence number did not exist yet,create one and write to sequence file */
  295.         num = 1;
  296.         sfile = fopen(sfilename,"w");
  297.         fprintf(sfile,"%d",num);
  298.     }
  299. /* close the sequence file */
  300.     fclose(sfile);
  301.     return num;
  302. }
  303.