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