home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / BMCONV.C < prev    next >
C/C++ Source or Header  |  1991-01-27  |  7KB  |  267 lines

  1. /* This programs convert mail files in the "BM" format to the new mail
  2.    file format.
  3.    Link object with files.obj, misc.obj and dirutil.obj.
  4. */
  5. #include <stdio.h>
  6. #include "global.h"
  7. #include "files.h"
  8. static void convert __ARGS((char *dir));
  9. static int isarea __ARGS((char *area));
  10. static void areafile __ARGS((char *filename,char *area));
  11. static void mkdirs __ARGS((char *path));
  12.  
  13. /* Takes optional subdirectory in /spool/mail to convert as argument.
  14.    Defaults to /spool/mail
  15.  */
  16. main(argc,argv)
  17. int argc;
  18. char *argv[];
  19. {
  20.      char fname[256];
  21.      sprintf(fname,"%s/mail",Newsdir);
  22.      mkdirs(fname);
  23.      mkdirs(Userdir);
  24.      if(argc > 1)
  25.       sprintf(fname,"%s/%s",Mailspool,argv[1]);
  26.      else
  27.       strcpy(fname,Mailspool);
  28.      convert(fname);
  29. }
  30. static void
  31. convert(dir)
  32. char *dir;
  33. {
  34.      char fname[256],buf[1024],line[20],fpath[256],dirstr[256],*cp;
  35.      int msg, skip = 0;
  36.      FILE *mfile = NULLFILE, *fp;
  37.      printf("Scanning directory %s\n",dir);
  38.      sprintf(dirstr,"%s/*",dir);
  39.      filedir(dirstr,0,line);
  40.      while(line[0] != '\0') {
  41.       if(skip) {
  42.            if(stricmp(line,buf) == 0)
  43.             skip = 0;
  44.            filedir(dirstr,1,line);
  45.            continue;
  46.       }
  47.       if(strchr(line,'.') == NULLCHAR) { /* possible directory */
  48.            /* doesn't work if filedir() doesn't operate on directories */
  49.            sprintf(fname,"%s/%s",dir,line);
  50.            strcpy(buf,line);
  51.            skip = 1;
  52.            convert(fname);
  53.            filedir(dirstr,0,line);
  54.            continue;
  55.       }
  56.       if(strlen(line) > 3 && stricmp(&line[strlen(line)-4],".txt") == 0) {
  57.            sprintf(buf,"%s/%s",dir,line);
  58.            if((fp = fopen(buf,READ_TEXT)) == NULLFILE) {
  59.             filedir(dirstr,1,line);
  60.             continue;
  61.            }
  62.            printf("Processing %s\n",buf);
  63.            line[strlen(line)-4] = '\0';
  64.            if(isarea(line) || stricmp(dir,Mailspool) != 0) {
  65.             if(strlen(dir) > strlen(Mailspool)) {
  66.              sprintf(buf,"%s.%s",&dir[strlen(Mailspool)+1],line);
  67.              while((cp = strchr(buf,'/')) != NULLCHAR)
  68.                   *cp = '.';
  69.             }
  70.             else
  71.              sprintf(buf,"%s",line);
  72.            }
  73.            else
  74.             sprintf(buf,"mail.%s",line);
  75.            areafile(fpath,buf);
  76.            mkdirs(fpath);
  77.            msg = 0;
  78.            mfile = NULLFILE;
  79.            while(fgets(buf,sizeof(buf),fp) != NULLCHAR) {
  80.             if(strnicmp("From ",buf,5) == 0) {
  81.              fclose(mfile);
  82.              sprintf(fname,"%s/%u",fpath,++msg);
  83.              if((mfile = fopen(fname,WRITE_TEXT)) == NULLFILE){
  84.                   printf("Can't write %s\n",fname);
  85.                   exit(1);
  86.              }
  87.              if((cp = strchr(buf+5,' ')) != NULLCHAR)
  88.                   *cp = '\0';
  89.              fprintf(mfile,"Return-Path: %s\n",buf+5);
  90.             }
  91.             else
  92.              fputs(buf,mfile);
  93.            }
  94.            fclose(mfile);
  95.            fclose(fp);
  96.            sprintf(buf,"%s/active",Newsdir);
  97.            if((fp = fopen(buf,APPEND_TEXT)) == NULLFILE) {
  98.             printf("Can't write %s\n",buf);
  99.             exit(1);
  100.            }
  101.            if(isarea(line) || stricmp(dir,Mailspool) != 0) {
  102.             if(strlen(dir) > strlen(Mailspool)) {
  103.              sprintf(buf,"%s.%s",&dir[strlen(Mailspool)+1],line);
  104.              while((cp = strchr(buf,'/')) != NULLCHAR)
  105.                   *cp = '.';
  106.             }
  107.             else
  108.              strcpy(buf,line);
  109.             fprintf(fp,"%s ",buf);
  110.            }
  111.            else
  112.             fprintf(fp,"mail.%s ",line);
  113.            if(msg)
  114.             fprintf(fp,"%u 1 y",msg);
  115.            fprintf(fp,"\n");
  116.            fclose(fp);
  117.       }
  118.       filedir(dirstr,1,line);
  119.      }
  120. }
  121.  
  122. /* Returns 1 if name is a public message Area, 0 otherwise */
  123. static int
  124. isarea(name)
  125. char *name;
  126. {
  127.     char buf[1024], *cp;
  128.     FILE *fp;
  129.     if((fp = fopen(Arealist,READ_TEXT)) == NULLFILE)
  130.         return 0;
  131.     while(fgets(buf,sizeof(buf),fp) != NULLCHAR) {
  132.         /* The first word on each line is all that matters */
  133.         if((cp = strchr(buf,' ')) == NULLCHAR)
  134.             if((cp = strchr(buf,'\t')) == NULLCHAR)
  135.                 continue;
  136.         *cp = '\0';
  137.         if((cp = strchr(buf,'\t')) != NULLCHAR)
  138.             *cp = '\0';
  139.         if(stricmp(name,buf) == 0) {    /* found it */
  140.             fclose(fp);
  141.             return 1;
  142.         }
  143.     }
  144.     fclose(fp);
  145.     return 0;
  146. }
  147.  
  148. static void
  149. areafile(filename,area)
  150. char *filename;
  151. char *area;
  152. {
  153.      FILE *fp;
  154.      char buf[1024], *cp;
  155.      static char *delim = " \t\n";
  156.      if((fp = fopen(Pointerfile,READ_TEXT)) != NULLFILE){
  157.       while(fgets(buf,sizeof(buf),fp) != NULLCHAR){
  158.            if((cp = strtok(buf,delim)) == NULLCHAR)
  159.             continue;
  160.            if(stricmp(cp,area) == 0 &&
  161.           (cp = strtok(NULLCHAR,delim)) != NULLCHAR){
  162.             strcpy(filename,cp);
  163.             fclose(fp);
  164.             return;
  165.            }
  166.       }
  167.       fclose(fp);
  168.      }
  169.      strcpy(buf,area);
  170.      while((cp = strchr(buf,'.')) != NULLCHAR)
  171.       *cp = '/';
  172.      sprintf(filename,"%s/%s",Newsdir,buf);
  173. }
  174. static void
  175. mkdirs(path)
  176. char *path;
  177. {
  178.     char *cp, buf[128];
  179.     strcpy(buf,path);
  180.     if(path[strlen(path)-1] != '/')
  181.         strcat(buf,"/");
  182.     cp = buf;
  183.     while((cp = strchr(cp,'/')) != NULLCHAR) {
  184.         *cp = '\0';
  185. #ifdef    UNIX
  186.         mkdir(buf,0755);
  187. #else
  188.         mkdir(buf);
  189. #endif
  190.         *cp++ = '/';
  191.     }
  192. }
  193.  
  194. #ifdef UNIX
  195. #include <sys/types.h>
  196. #include <dirent.h>
  197. /* wildcard filename lookup */
  198. filedir(name, times, ret_str)
  199. char    *name;
  200. int    times;
  201. char    *ret_str;
  202. {
  203.     static char    dname[128], fname[128];
  204.     static DIR *dirp = NULL;
  205.     struct dirent *dp;
  206.     char    *cp, temp[128];
  207.  
  208.     /*
  209.      * Make sure that the NULL is there in case we don't find anything
  210.      */
  211.     ret_str[0] = '\0';
  212.  
  213.     if (times == 0) {
  214.         /* default a null name to *.* */
  215.         if (name == NULL || *name == '\0')
  216.             name = "*.*";
  217.         /* split path into directory and filename */
  218.         if ((cp = strrchr(name, '/')) == NULL) {
  219.             strcpy(dname, ".");
  220.             strcpy(fname, name);
  221.         } else {
  222.             strcpy(dname, name);
  223.             dname[cp - name] = '\0';
  224.             strcpy(fname, cp + 1);
  225.             /* root directory */
  226.             if (dname[0] == '\0')
  227.                 strcpy(dname, "/");
  228.             /* trailing '/' */
  229.             if (fname[0] == '\0')
  230.                 strcpy(fname, "*.*");
  231.         }
  232.         /* close directory left over from another call */
  233.         if (dirp != NULL)
  234.             closedir(dirp);
  235.         /* open directory */
  236.         if ((dirp = opendir(dname)) == NULL) {
  237.             printf("Could not open DIR (%s)\n", dname);
  238.             return;
  239.         }
  240.     } else {
  241.         /* for people who don't check return values */
  242.         if (dirp == NULL)
  243.             return;
  244.     }
  245.  
  246.     /* scan directory */
  247.     while ((dp = readdir(dirp)) != NULL) {
  248.         /* test for name match */
  249. /*        if (wildmat(dp->d_name, fname)) {*/
  250.         if (!strcmp(dp->d_name, fname) || (fname[0] == '*' &&
  251.             !strcmp(&dp->d_name[strlen(dp->d_name) - strlen(fname)+1],
  252.             &fname[1]))) { /* ...when we do not use wildmat */
  253.             /* test for regular file */
  254.             sprintf(temp, "%s/%s", dname, dp->d_name);
  255.             strcpy(ret_str, dp->d_name);
  256.             break;
  257.         }
  258.     }
  259.  
  260.     /* close directory if we hit the end */
  261.     if (dp == NULL) {
  262.         closedir(dirp);
  263.         dirp = NULL;
  264.     }
  265. }
  266. #endif
  267.