home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / BBS / MISC / XDEV_117.ZIP / MAKEMSG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-26  |  4.2 KB  |  209 lines

  1. #include "msgg.h"
  2.  
  3. word export (char *,word,char *,char *);
  4. void leftt (char *,char *,int);
  5. int  makemsg (char *file,struct _xmsg *xmsg, char *text);
  6. extern char * pascal unpack_msg (char **hold);
  7.  
  8. word     textsize;
  9. word     codesize;
  10.  
  11. int main (int argc,char *argv[]) {
  12.  
  13.     word howmany;
  14.  
  15.     if (argc<4) {
  16.         fputs("\n\nError calling MakeMsg.\n",stdout);
  17.         fputs("\nUsage: MakeMsg <msg dir\\> <base#> <netmail_dir\\> <to_whom>\n",stdout);
  18.         fputs("\nI'll check the message base for messages addressed to <to_whom>",stdout);
  19.         fputs("\nand export, then mark deleted, any I find.  This lets you use",stdout);
  20.         fputs("\nthings like AreaFix that rely on netmail *.MSG directories.\n",stdout);
  21.         fputs("\nReturns 0 (none exported), 1 (exported ok), or 2-4 (fatal error)\n",stdout);
  22.         fputs("\nExample: MakeMsg C:\\XBBS\\MESS\\ 5 C:\\BT\\MSG\\ \"AreaFix\"\n\n",stdout);
  23.         return 0;
  24.     }
  25.     howmany=export(argv[1],(word)atol(argv[2]),argv[3],argv[4]);
  26.     if(howmany) {
  27.         printf("\nExported %u message(s).\n");
  28.         return 1;
  29.     }
  30.     else return 0;
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. word export (char *dir,word base,char *netdir,char *whoto) {
  39.  
  40.  word howmany=0;
  41.  char filename[127];
  42.  char textname[127];
  43.  char text[124];
  44.  FILE *fp;
  45.  FILE *pf;
  46.  char once;
  47.  long pos;
  48.  word count=1;
  49.  struct _xmsg msg2;
  50.  char *hold=NULL;
  51.  struct ffblk filestat;
  52.  struct _xmsg msg;
  53.  word nomess,messno;
  54.  
  55.  sprintf(filename,"%sXDATA.%03x",dir,base);
  56.  sprintf(textname,"%sXTEXT.%03x",dir,base);
  57.  
  58.  if (findfirst(filename,&filestat,0)!=0) {
  59.     printf("\nCan't find %s\n",stdout);
  60.     return 0;
  61.  }
  62.  if(filestat.ff_fsize==0L) {
  63.     printf("\nZero-length filebase.\n");
  64.     return 0;
  65.  }
  66.  nomess=(word)(filestat.ff_fsize/(long)sizeof(struct _xmsg));
  67.  
  68.  if(!(pf=fopen(textname,"rb"))) {
  69.     printf("\nCan't open %s\n",textname);
  70.     return 0;
  71.  }
  72.  if(!(fp=fopen(filename,"r+b"))) {
  73.     fclose(pf);
  74.     printf("\nCan't open %s\n",filename);
  75.     return 0;
  76.  }
  77.  
  78.  fseek(fp,0L,SEEK_SET);
  79.  for(messno=0;messno<nomess;messno++) {
  80.     pos=ftell(fp);
  81.     fread(&msg,sizeof(struct _xmsg),1,fp);
  82.     if(msg.m_attr & MSGDELETED) continue;
  83.  
  84.     /* A quick note: at this point it would be simple to check other things
  85.        and export based on those checks... */
  86.  
  87.     if(stricmp(msg.to,whoto)) continue;
  88.  
  89.       once=0;
  90. TryThatOver:
  91.       if (fseek(pf,msg.start,SEEK_SET)!=0) {
  92.           if (ferror(pf)) {
  93.             if (!once) {
  94.                 once++;
  95.                 goto TryThatOver;
  96.             }
  97.             else {
  98.                 perror ("\nXTEXT SEEK ERROR");
  99.                 fclose(fp);
  100.                 fclose(pf);
  101.                 exit(2);
  102.             }
  103.           }
  104.       }
  105.       hold=(char *)malloc(msg.length+1);
  106.       if (hold==NULL) {
  107.         fputs("\nOut of memory.\n",stdout);
  108.         fclose(pf);
  109.         fclose(fp);
  110.         exit(3);
  111.       }
  112.       fread(hold,msg.length,1,pf);
  113.       if(msg.m_attr & MSGPACKED) {
  114.             unpack_msg(&hold);
  115.             if(hold==NULL) {
  116.                 fputs("\nCan't uncompress message...\n",stdout);
  117.                 exit(4);
  118.             }
  119.             else {
  120.                 msg.length=strlen(hold)+1;
  121.             }
  122.       }
  123.       hold[msg.length-1]=0;
  124.  
  125.       sprintf(text,"%s\\%u.MSG",netdir,count);
  126.       while(!findfirst(text,&filestat,0)) {
  127.         count++;
  128.         if(!count) {
  129.             fprintf(stderr,"\nDamn Msg directory is full\n");
  130.             return howmany;
  131.         }
  132.         sprintf(text,"%s\\%u.MSG",netdir,count);
  133.       }
  134.  
  135.       printf("Exporting #%lu as %u.MSG...\n",(pos/(long)sizeof(struct _xmsg))+1L,count);
  136.       if(makemsg(text,&msg,hold)) {
  137.         msg.m_attr |= MSGDELETED;    /* Mark deleted so it won't export again */
  138.         fseek(fp,pos,SEEK_SET);
  139.         fwrite(&msg,sizeof(struct _xmsg),1,fp);
  140.       }
  141.       else printf("Export error...\n");
  142.  
  143.       if(hold)free(hold);
  144.       hold=NULL;
  145.  }
  146.  fclose(pf);
  147.  fclose(fp);
  148.  return howmany;
  149. }
  150.  
  151.  
  152.  
  153.  
  154. int makemsg (char *file,struct _xmsg *xmsg, char *text) {
  155.  
  156.  FILE *pp;
  157.  
  158.  pp=fopen(file,"wb");
  159.  if(!pp) {
  160.      printf("\nCan't open %s\n",file);
  161.      return 0;
  162.  }
  163.  fwrite(xmsg,sizeof(struct _xmsg),1,pp);
  164.  fwrite(text,strlen(text),1,pp);
  165.  fputc('\0',pp);
  166.  fclose(pp);
  167.  return 1;
  168. }
  169.  
  170.  
  171.  
  172. void leftt(a,b,x)
  173.  
  174. char *a,*b;
  175. int x;
  176.  
  177. {
  178.  
  179.   int i=0;
  180.  
  181.   x = (x <= strlen(b)) ? x : strlen(b);
  182.  
  183.   while (i++ < x) *a++ = *b++;
  184.  
  185.     *a = '\0';
  186.  
  187. }
  188.  
  189.  
  190. char * pascal rstrip (char *a) {
  191.  
  192.   register int x;
  193.  
  194.   x=strlen(a);
  195.   while (x && a && a[x-1]==' ') a[--x]=0;
  196.   return a;
  197. }
  198.  
  199.  
  200.  
  201. char * pascal lstrip (char *a) {
  202.  
  203.   register int x;
  204.  
  205.   x=strlen(a);
  206.   while (x && *a==' ') memmove (a,(a+1),x--);
  207.   return (a);
  208. }
  209.