home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / XGRP_000.SZH / RESCAN.C < prev    next >
C/C++ Source or Header  |  1991-08-22  |  10KB  |  344 lines

  1. /* echomail rescanner */
  2.  
  3. #include "xgroup.h"
  4.  
  5. #define MAXKLUDGE 256
  6.  
  7. #define SETSCANNED(x) (x.m_attr |= MSGSCANNED)
  8. #define SETSENT(x) (x.attr |= MSGSENT)
  9. #define RESETSENT(x) (x.attr &= (~MSGSENT))
  10. #define SETDELETED(x) (x.m_attr |= MSGDELETED)
  11.  
  12. /* function declarations specific to this module */
  13.  
  14. static int  _fastcall open_pkt (ADDR *addr);
  15. static int  _fastcall write_pkt_msg (int fp,XMSG *amsg,char *npth,
  16.                                      char *text,char *area);
  17. static int  _fastcall do_export(ECHOREC *einfo,int dataptr,
  18.                                 int textptr,ADDR *addr,
  19.                                 unsigned int startat);
  20.  
  21. /* external var references */
  22.  
  23. extern GROUP    *group;
  24. extern CONTROLS control;
  25. extern ADDR     *myaddr;
  26. extern ECHOREC  *echos;
  27. extern char     *groupin;
  28. extern char     *groupout;
  29. extern char     *grouphold;
  30. extern char     *msgdir;
  31. extern char     *archive;
  32. extern char     *unarchive;
  33. extern char     *outbound;
  34. extern char     *inbound;
  35. extern word     packsize;
  36. extern char     buffer[1024];
  37. extern word     netarea;
  38. extern DUPES    *dupes;
  39. extern word     nextdupe,maxdupes;
  40. extern char     founddupes;
  41.  
  42.  
  43.  
  44.  
  45. void _fastcall rescan_echo_mail (ECHOREC *currecho,ADDR *addr,
  46.                                  unsigned int startat) {
  47.  
  48.     word        exported;
  49.     int         dataptr,textptr;
  50.     struct stat st;
  51.  
  52.  
  53.     if(!currecho || !addr) return;
  54.     printf("\nRescanning echo area \"%s\"for %u:%u/%u.%u@%s...",currecho->tag,
  55.            addr->zone,addr->net,addr->node,addr->point,addr->domain);
  56.     sprintf(buffer,"%s/XDATA.%03x",msgdir,currecho->areano);
  57.     if(stat(buffer,&st)) st.st_size = 0L;
  58.     if(!st.st_size) {
  59.         printf("no msgs\n\x1b[K");
  60.         return;
  61.     }
  62.     else printf("%u msg%s\n\x1b[K",(word)(st.st_size / (long)sizeof(XMSG)),
  63.                 &"s"[((word)(st.st_size / (long)sizeof(XMSG)) == 1)]);
  64.     dataptr = sopen(buffer,O_RDWR | O_BINARY,SH_DENYNO);
  65.     if(dataptr == -1) {
  66.         printf("\nCouldn't open data file %s\n",buffer);
  67.         return;
  68.     }
  69.     sprintf(buffer,"%s/XTEXT.%03x",msgdir,currecho->areano);
  70.     textptr = sopen(buffer,O_RDWR | O_BINARY,SH_DENYNO);
  71.     if(textptr == -1) {
  72.         close(dataptr);
  73.         printf("\nCouldn't open text file %s\n",buffer);
  74.         return;
  75.     }
  76.     exported = do_export(currecho,dataptr,textptr,addr,startat);
  77.     close(textptr);
  78.     close(dataptr);
  79.  
  80.     if(exported)
  81.       logf("Rescanned %u msg%s from area #%u \"%s\" for %u:%u/%u.%u@%s",
  82.            exported,&"s"[(exported == 1)],currecho->areano,currecho->tag,
  83.            addr->zone,addr->net,addr->node,addr->point,addr->domain);
  84. }
  85.  
  86.  
  87.  
  88. static int _fastcall do_export (ECHOREC *einfo,int dataptr,
  89.                                  int textptr,ADDR *addr,
  90.                                  unsigned int startat) {
  91.  
  92.     word          x,tries,test,exported = 0;
  93.     word          nummsgs;
  94.     long          pos;
  95.     int           fp;
  96.     char          *text = NULL;
  97.     XMSG          xmsg;
  98.     ADDR          *maddr;
  99.  
  100.  
  101.     tries = 0;
  102.     while(tries < 50) {
  103.         if(lseek(dataptr,0L,SEEK_END) != -1L) break;
  104.         DosSleep(100L);
  105.         tries++;
  106.     }
  107.     if(tries >= 50) {
  108.         printf("\nLocking conflict; aborting...(#14)\n");
  109.         return 0;
  110.     }
  111.  
  112.     maddr = best_guess(addr,myaddr);
  113.  
  114.     fp = open_pkt(addr);
  115.     if(fp == -1) {
  116.         return 0;
  117.     }
  118.  
  119.     if(startat) startat--;
  120.  
  121.     nummsgs = (word)(tell(dataptr) / (long)sizeof(XMSG));
  122.     for(x = startat;x < nummsgs;x++) {
  123.  
  124.         tries = 0;
  125.         while(tries < 50) {
  126.             if(lseek(dataptr,(long)x * (long)sizeof(XMSG),SEEK_SET) != -1L) break;
  127.             DosSleep(100L);
  128.             tries++;
  129.         }
  130.         if(tries >= 50) {
  131.             printf("\nLocking conflict; aborting...(#15)\n");
  132.             if(fp != -1) close(fp);
  133.             return exported;
  134.         }
  135.  
  136.         pos = tell(dataptr);
  137.         tries = 0;
  138.         while(tries < 50) {
  139.             if((size_t)read(dataptr,&xmsg,sizeof(XMSG)) == sizeof(XMSG)) break;
  140.             DosSleep(100L);
  141.             tries++;
  142.             lseek(dataptr,pos,SEEK_SET);
  143.         }
  144.         if(tries >= 50) {
  145.             printf("\nLocking conflict; aborting...(#16)\n");
  146.             if(fp != -1) close(fp);
  147.             return -1;
  148.         }
  149.  
  150.         if(xmsg.m_attr & MSGDELETED) continue;
  151.         if(!(xmsg.m_attr & MSGECHO)) continue;
  152.  
  153.         /* Got one to export */
  154.  
  155.         tries = 0;
  156.         while(tries < 50) {
  157.             if(lseek(textptr,xmsg.start,SEEK_SET) != -1L) break;
  158.             DosSleep(100L);
  159.             tries++;
  160.         }
  161.         if(tries >= 50) {
  162.             printf("\nLocking conflict; aborting...(#17)\n");
  163.             if(fp != -1) close(fp);
  164.             return exported;
  165.         }
  166.  
  167.         text = (char *)malloc(xmsg.length + 2);
  168.         if(!text) continue;
  169.  
  170.         tries = 0;
  171.         while(tries < 50) {
  172.             *text = 0;
  173.             test = (word)read(textptr,text,xmsg.length + 1);
  174.             if(test != 65535U) {
  175.                 if(test >= (xmsg.length - 1)) break;
  176.             }
  177.             DosSleep(100L);
  178.             tries++;
  179.             lseek(textptr,xmsg.start,SEEK_SET);
  180.         }
  181.         if(tries >= 50) {
  182.             printf("\nLocking conflict; aborting...(#19)\n");
  183.             if(fp != -1) close(fp);
  184.             return exported;
  185.         }
  186.  
  187.         if(!*text){
  188.             my_free(text);
  189.             text = NULL;
  190.             continue;
  191.         }
  192.  
  193.         if(xmsg.m_attr & MSGPACKED) {
  194.             if(unpack_msg(&text) == NULL) {
  195.                 my_free(text);
  196.                 text = NULL;
  197.                 continue;
  198.             }
  199.         }
  200.  
  201.         exported++;
  202.  
  203.         xmsg.d_zone = addr->zone;
  204.         xmsg.dest_net = addr->net;
  205.         xmsg.dest = addr->node;
  206.         xmsg.d_point = addr->point;
  207.  
  208.         printf("#%u (#%u)\r",
  209.                x+1,exported);
  210.  
  211.         text[xmsg.length] = 0;
  212.         text[xmsg.length-1] = 0;
  213.  
  214.         write_pkt_msg(fp,&xmsg,NULL,text,einfo->tag);
  215.     }
  216.  
  217.     if(fp != -1) close(fp);
  218.     my_free(text);
  219.     text = NULL;
  220. }
  221.  
  222.  
  223.  
  224. static int _fastcall open_pkt (ADDR *addr) {
  225.  
  226.     int         fp;
  227.     long        pos;
  228.     PKTHDR      ph;
  229.     ADDR        *maddr;
  230.     static char currout[1027];
  231.     char        *p;
  232.  
  233.  
  234.     /* Open (create if necessary) a packet for address given.
  235.        Return a file handle for the packet positioned to eop */
  236.  
  237.     if(!control.xbbsos2) {
  238.         strcpy(currout,outbound);
  239.         if(addr->domain && *addr->domain && stricmp(myaddr->domain,addr->domain)) {
  240.                         p = strrchr(currout,'\\');
  241.             if(p) p++;
  242.             else p = &currout[strlen(currout)];
  243.             strcat(currout,addr->domain);
  244.         }
  245.         if(myaddr->zone != addr->zone) {
  246.              sprintf(&currout[strlen(currout)],".%03x",addr->zone);
  247.              DosMkDir(currout,0L);
  248.         }
  249.         sprintf(buffer,"%s/%04x%04x.OUT",currout,addr->net,addr->node);
  250.     }
  251.     else {
  252.         sprintf(buffer,"%s/P.%u.%u.%u.%u.%0.8s",outbound,addr->zone,addr->net,
  253.                 addr->node,addr->point,addr->domain);
  254.     }
  255.  
  256.     fp = sopen(buffer,O_RDWR | O_BINARY | O_CREAT,SH_DENYWR,S_IREAD | S_IWRITE);
  257.     if(fp != -1) {
  258.         lseek(fp,0L,SEEK_END);
  259.         if(!tell(fp)) {
  260.             memset(&ph,0,sizeof(PKTHDR));
  261.             ph.version = 2;
  262.             ph.subver = 2;
  263.             ph.dnode  = addr->node;
  264.             ph.dnet   = addr->net;
  265.             ph.dzone  = addr->zone;
  266.             ph.dpoint = addr->point;
  267.             strncpy(ph.ddomain,addr->domain,8);
  268.             maddr = best_guess(addr,myaddr);
  269.             ph.onode  = maddr->node;
  270.             ph.onet   = maddr->net;
  271.             ph.ozone  = maddr->zone;
  272.             ph.opoint = maddr->point;
  273.             strncpy(ph.odomain,maddr->domain,8);
  274.             write(fp,&ph,sizeof(PKTHDR));
  275.             write(fp,"\0",2);
  276.         }
  277.         pos = tell(fp);
  278.         if(pos) lseek(fp,pos - 2L,SEEK_SET);  /* Position to next msg spot */
  279.     }
  280.     return fp;
  281. }
  282.  
  283.  
  284.  
  285.  
  286. static int _fastcall write_pkt_msg (int fp,XMSG *amsg,char *npth,
  287.                                     char *text,char *area) {
  288.  
  289.     char pmsg[192],*p;
  290.     word x;
  291.  
  292.  
  293.     /* Write the message given to the end of the file given as a
  294.        packed msg */
  295.  
  296.     memset(pmsg,192,0);
  297.     *pmsg = 0x02;
  298.     pmsg[1] = 0x00;
  299.     memcpy(&pmsg[2],&amsg->orig,2);
  300.     memcpy(&pmsg[4],&amsg->dest,2);
  301.     memcpy(&pmsg[6],&amsg->orig_net,2);
  302.     memcpy(&pmsg[8],&amsg->dest_net,2);
  303.     memcpy(&pmsg[10],&amsg->attr,2);
  304.     x = 0;
  305.     memcpy(&pmsg[12],&x,2);
  306.     for(x = 0;x < 20;x++) if(!amsg->date[x]) amsg->date[x] = 'Q';
  307.     amsg->date[19] = 0;                   /* damn qmail anyway... */
  308.     memcpy(&pmsg[14],amsg->date,20);    
  309.     p = &pmsg[34];
  310.     strcpy(p,amsg->to);
  311.     x = 34;
  312.     while(*p){
  313.         x++;
  314.         p++;
  315.     }
  316.     p++;
  317.     x++;
  318.     strcpy(p,amsg->from);
  319.     while(*p){
  320.         x++;
  321.         p++;
  322.     }
  323.     x++;
  324.     p++;
  325.     strcpy(p,amsg->subj);
  326.     while(*p){
  327.         x++;
  328.         p++;
  329.     }
  330.     x++;
  331.     p++;
  332.     write(fp,pmsg,x);
  333.     if(area && *area) {             /* Prepend area tag */
  334.         write(fp,"AREA: ",6);
  335.         write(fp,area,strlen(area));
  336.         write(fp,"\r",1);
  337.     }
  338.     if(npth) write(fp,npth,strlen(npth));
  339.     write(fp,text,strlen(text));
  340.     write(fp,"\0\0",3);
  341.     lseek(fp,(tell(fp) - 2L),SEEK_SET);  /* Ready for another msg */
  342.     return 1;
  343. }
  344.