home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / pmmutl12.zip / strip.c < prev    next >
C/C++ Source or Header  |  1998-11-18  |  6KB  |  226 lines

  1. /* PMMail 1.5-2.0 attachement striper 1.2 (C) 1996-1998 Samuel Audet <guardia@cam.org> */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7.  
  8. #define BAGNAME "FOLDER.BAG"
  9. #define BAGBAKNAME "FOLDER.BAK"
  10.  
  11. int strip_attachements(char *directory, char *msgfilename)
  12. {
  13.    FILE *newfile, *oldfile;
  14.    char newfilename[512], oldfilename[512], line[512], boundary[128] = "";
  15.    enum {storing,uucode,mimebase64,binhex} mode;
  16.  
  17.    strcpy(newfilename,directory);
  18.    strcat(newfilename,"\\");
  19.    strcat(newfilename,msgfilename);
  20.  
  21.  
  22.    strcpy(oldfilename,directory);
  23.    strcat(oldfilename,"\\");
  24.    strncat(oldfilename,msgfilename,strchr(msgfilename,'.')-msgfilename);
  25.    strcat(oldfilename,".BAK");
  26.  
  27.    if(rename(newfilename,oldfilename))
  28.    {
  29.       fprintf(stderr,"Error, could not rename %s to %s.\n", newfilename,oldfilename);
  30.       return 0;
  31.    }
  32.  
  33.    newfile = fopen(newfilename,"w");
  34.    if(!newfile)
  35.    {
  36.       fprintf(stderr,"Error opening %s for writing.\n",newfilename);
  37.       return 0;
  38.    }
  39.  
  40.    oldfile = fopen(oldfilename,"r");
  41.    if(!oldfile)
  42.    {
  43.       fprintf(stderr,"Error opening %s for writing.\n",oldfilename);
  44.       return 0;
  45.    }
  46.  
  47.    mode = storing;
  48.  
  49.    while(fgets(line, sizeof(line), oldfile))
  50.    {
  51.       switch(mode)
  52.       {
  53.          case storing:
  54.          {
  55.             char *temp = NULL;
  56.             char upline[512];
  57.             strcpy(upline,line);
  58.             strupr(upline);
  59.  
  60.             if(!*boundary)
  61.             {
  62.                temp = strstr(upline,"BOUNDARY=\"");
  63.                if(temp)
  64.                {
  65.                   temp += 10;
  66.                   boundary[0] = boundary[1] = '-';
  67.                   sscanf(&line[temp-upline],"%45[^\"]\"",&boundary[2]);
  68.                   fputs(line,newfile);
  69.                   break;
  70.                }
  71.             }
  72.  
  73.             if(strstr(upline,"BEGIN 6") == upline)
  74.             {
  75.                mode = uucode;
  76.                fputs(line,newfile);
  77.                fputs("\n",newfile);
  78.                break;
  79.             }
  80.  
  81.             if(strstr(upline,"CONTENT-TRANSFER-ENCODING: BASE64") == upline)
  82.             {
  83.                mode = mimebase64;
  84.                fputs(line,newfile);
  85.                fputs("\n",newfile);
  86.                break;
  87.             }
  88.  
  89.             if(strstr(upline,"CONTENT-DISPOSITION: ATTACHMENT; FILENAME=\"") == upline)
  90.             {
  91.                mode = binhex;
  92.                fputs(line,newfile);
  93.                fputs("\n",newfile);
  94.                break;
  95.             }
  96.  
  97.             /* if nothing */
  98.             fputs(line,newfile);
  99.             break;
  100.          }
  101.  
  102.          case uucode:
  103.             if(strstr(line,"end") == line)
  104.             {
  105.                mode = storing;
  106.                fputs(line,newfile);
  107.             }
  108.             break;
  109.  
  110.          case mimebase64:
  111.          case binhex:
  112.             if(strstr(line,boundary) == line)
  113.             {
  114.                mode = storing;
  115.                fputs(line,newfile);
  116.             }
  117.             break;
  118.       }
  119.    }
  120.  
  121.  
  122.    fclose(newfile);
  123.    fclose(oldfile);
  124.    remove(oldfilename);
  125.  
  126.    return 1;
  127. }
  128.  
  129. int main(int argc, char *argv[])
  130. {
  131.    char *bagpath;
  132.    char newbagname[512], oldbagname[512], line[512];
  133.    FILE *newbag, *oldbag;
  134.    int i,e;
  135.  
  136.    printf("Attachment Stripper 1.2 for PMMail 1.5-2.0 - Copyright 1996-1998 Samuel Audet\n");
  137.    if(argc > 1)
  138.    {
  139.       char *temp;
  140.       bagpath = argv[1];
  141.       temp = strrchr(bagpath,'\\')+1;
  142.       if(*(temp+1) == 0) *temp = 0;
  143.    }
  144.    else
  145.       bagpath = ".";
  146.  
  147.    strcpy(newbagname,bagpath);
  148.    strcat(newbagname,"\\");
  149.    strcat(newbagname,BAGNAME);
  150.  
  151.    strcpy(oldbagname,bagpath);
  152.    strcat(oldbagname,"\\");
  153.    strcat(oldbagname,BAGBAKNAME);
  154.  
  155.    if(rename(newbagname,oldbagname))
  156.    {
  157.       fprintf(stderr,"Error, could not rename %s to %s.\n", newbagname,oldbagname);
  158.       return 1;
  159.    }
  160.  
  161.    newbag = fopen(newbagname,"w");
  162.    if(!newbag)
  163.    {
  164.       fprintf(stderr,"Error opening %s for writing.\n",newbagname);
  165.       return 2;
  166.    }
  167.    oldbag = fopen(oldbagname,"r");
  168.    if(!oldbag)
  169.    {
  170.       fprintf(stderr,"Error opening %s for reading.\n",oldbagname);
  171.       return 3;
  172.    }
  173.  
  174.  
  175.    i = 0;
  176.    e = 0;
  177.    while(fgets(line, sizeof(line), oldbag))
  178.    {
  179.       enum {unread=0,read,replied,sent,unsent} status;
  180.       int attachements = 0;
  181.       char date[16], time[16], size[16];
  182.       char subject[256];
  183.       char fromemail[256],fromname[256];
  184.       char toemail[256],toname[256];
  185.       char msgfilename[16] = "";
  186.       char uninteresting[256] = "";
  187.  
  188.       i++; /* line counter */
  189.  
  190.       /* don't use codepage 850 to view this */
  191.       sscanf(line, "%d▐%d▐%15[^▐]▐%15[^▐]▐%255[^▐]▐%255[^▐]▐%255[^▐]▐%255[^▐]▐%255[^▐]▐%15[^▐]▐%15[^▐]▐%255[^\n]",
  192.          &status,&attachements,date,time,subject,toemail,toname,fromemail,fromname,size,msgfilename,uninteresting);
  193.  
  194.       if(!*msgfilename)
  195.          fprintf(stderr,"Error: bad entry in %s at line %d, continuing with other entries.\n", oldbagname, i);
  196.       /* KB is our tag, so let's skip these entries */
  197.       else if(attachements && !strstr(size,"KB"))
  198.       {
  199.          struct stat fi;
  200.          char msgpathfilename[272];
  201.  
  202.          printf("%s\n",msgfilename); e++; /* file processed counter */
  203.          strip_attachements(bagpath, msgfilename);
  204.  
  205.          strcpy(msgpathfilename, bagpath);
  206.          strcpy(msgpathfilename, msgfilename);
  207.          stat(msgpathfilename, &fi);
  208.          sprintf(size,"%dKB",(int)(fi.st_size/1024.0+1));
  209.  
  210.          sprintf(line, "%d▐%d▐%s▐%s▐%s▐%s▐%s▐%s▐%s▐%s▐%s▐%s\n",
  211.             status,attachements,date,time,subject,toemail,toname,fromemail,fromname,size,msgfilename,uninteresting);
  212.       }
  213.  
  214.       fputs(line, newbag);
  215.    }
  216.  
  217.    fclose(newbag);
  218.    fclose(oldbag);
  219.  
  220.    remove(oldbagname);
  221.  
  222.    printf("%d message file processed.\n",e);
  223.  
  224.    return 0;
  225. }
  226.