home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / JOE_SOUR.LHA / Sources.lha / s!x / bgcheck / System / bgcheck.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-04  |  12.9 KB  |  543 lines

  1. #define PROGNAME "BackGroundChecker"
  2. #include"bbs:bgcheck/bgcheck.h"
  3. #include "aedoor.h"
  4. #include "semis.h"
  5. static const char VersionTag[] = "\0$VER: "PROGNAME" "REVISION" ("REVDATE")";
  6.  
  7. #define ws WriteStr
  8.  
  9. #define CLEAR   0
  10. #define LOCAL    1
  11. #define ZMODEM    2
  12. #define HYDRA    3
  13. #define    SMODEM    4
  14.  
  15.  
  16. void Start(int node);
  17. void upperstring(char *s);
  18. char *getconfig(char **main_search,char *lookfor);
  19. void timenow(char *buffer2);
  20. void Date(char *buffer,UBYTE format);
  21.  
  22. void main(int argc, char *argv[])
  23. {
  24.     int    Node=0;
  25.     if(argc<2)
  26.     {
  27.         printf("Sorry, %s must be called from S!X or /X\n",argv[0]);
  28.         exit(0);
  29.     }
  30.     Node=atoi(argv[1]);
  31.     Register(Node);
  32.     XIM_Msg->Command=140;                    
  33.     CheckMessage();
  34.     SetTaskPri(FindTask(0),atol(XIM_Msg->String));
  35.     Start(Node);
  36. }
  37.  
  38.  
  39. VOID Shutdown(VOID)
  40. {
  41.     Jhmsg->Command=2;
  42.     PutMsg(port,(struct Message *)Jhmsg); 
  43.     Wait(1<<replymp->mp_SigBit);
  44.     while(msg=(struct JHMessage *)GetMsg((struct MsgPort *)replymp));
  45.     DeletePort((struct MsgPort *)replymp);
  46.     FreeMem(Jhmsg,sizeof(struct JHMessage));
  47. }
  48.  
  49.  
  50. void Start(Node)
  51. {
  52.     char    buffer[255]="",
  53.             handle[50]="",
  54.             filetemp[200]="",
  55.             filename[255]="",
  56.             playpen[255],
  57.             confpath[255],
  58.             slot[6],
  59.             date[10],
  60.             time[6],
  61.             postdate[10],
  62.             posttime[10],
  63.             axxstr[10],
  64.             **main_config=NULL,
  65.             **main_filename=NULL,
  66.             *p=NULL;
  67.     long    transtype=CLEAR,
  68.             dirinkbyte=0,
  69.             axx=255,
  70.             axx2=0,
  71.             pos,
  72.             len;
  73.     int        result=0,
  74.             confnumber=0,
  75.             status=0,
  76.             laststatus=2,
  77.             priority=0;
  78.     BOOL     successatt=FALSE,
  79.             msgbase=FALSE,
  80.             uploaddelay=FALSE,
  81.             addsem=FALSE,
  82.             nozmodembg=FALSE,
  83.             nohydrabg=FALSE;
  84.     BPTR    readfh=NULL;
  85.     struct SinglePort *Singleport;
  86.     struct MultiPort *Nodes;
  87.     struct HydraPort *hydra=NULL;
  88.     struct DateTime  *dtime=NULL;
  89.     struct DateStamp *stmp=NULL,
  90.                      *stmp2=NULL;
  91.  
  92.     struct Filedata  *filedata=NULL;
  93.  
  94.     ws("",1);
  95.     ws("C            BGCheck "REVISION" Joe Cool             ",1);
  96.     ws("",1);
  97.  
  98.     sprintf(buffer,"t:UploadDelay.%d",Node);
  99.     DeleteFile(buffer);
  100.  
  101.     sprintf(buffer,"t:MSGBASE.%d",Node);
  102.     readfh=Lock(buffer,ACCESS_READ);
  103.     if (readfh)
  104.     {
  105.         UnLock(readfh);
  106.         msgbase=TRUE;
  107.     }
  108.  
  109.     Date(date,FORMAT_USA);
  110.     timenow(time);
  111.  
  112.     GetDT(DT_NAME,"");
  113.     strcpy(handle,XIM_Msg->String);
  114.  
  115.     GetDT(BB_CONFNUM,"");
  116.     confnumber=atoi(XIM_Msg->String);
  117.     confnumber++;
  118.  
  119.     strcpy(buffer,"bbs:bgcheck/bgfilehandler.config");
  120.     readfh=Open(buffer,MODE_OLDFILE);
  121.     pos=0;
  122.     if (!readfh && pos!=60)
  123.     {
  124.         pos++;
  125.         readfh=Open(buffer,MODE_OLDFILE);
  126.         if (!readfh) Delay(TICKS_PER_SECOND/3);
  127.     }
  128.     main_config=(char **) realloc(main_config,sizeof(char *));
  129.     main_config[0]=NULL;
  130.     if (readfh) 
  131.     for (pos=0;FGets(readfh,buffer,254);pos++)
  132.     {
  133.         main_config=(char **) realloc(main_config,(pos+1)*sizeof(char *));
  134.         main_config[pos]=NULL;
  135.         len=strlen(buffer);
  136.         if (buffer[len-1]=='\n') buffer[--len]='\0';
  137.         upperstring(buffer);
  138.         main_config[pos]=(char *) malloc (len+1);
  139.         if (main_config[pos]!=NULL)
  140.             strcpy(main_config[pos],buffer);
  141.     }
  142.     Close(readfh);
  143.     main_config=(char **) realloc(main_config,(pos+1)*sizeof(char *));
  144.     main_config[pos]=NULL;
  145.  
  146.     p=getconfig(main_config,"DELAY_UPLOAD_AXX");
  147.     if (p)
  148.     {
  149.         strncpy(axxstr,p,9);
  150.         if (atoi(axxstr))
  151.         {
  152.             axx=atoi(axxstr);
  153.         }
  154.     }
  155.  
  156.     if (p=getconfig(main_config,"PRIORITY")) priority=atoi(p);
  157.  
  158.     sprintf(buffer,"NO_HYDRA_BGCHECK_%d",Node);
  159.     if (p=getconfig(main_config,buffer)) nohydrabg=TRUE;
  160.     sprintf(buffer,"NO_ZMODEM_BGCHECK_%d",Node);
  161.     if (p=getconfig(main_config,buffer)) nozmodembg=TRUE;
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.     GetDT(DT_SECSTATUS,"");
  169.     axx2=atoi(XIM_Msg->String);
  170.  
  171.  
  172.     upperstring(handle);
  173.     sprintf(buffer,"DELAY_UPLOAD_AXX_%s",handle);
  174.     p=getconfig(main_config,buffer);
  175.     if (p)
  176.     {
  177.         axx2=axx+1;
  178.     }
  179.  
  180.  
  181.     for (pos=0;main_config[pos]!=NULL;pos++)
  182.         free(main_config[pos]);
  183.     free (main_config);
  184.  
  185.     SendCmd(SIG_PLAYPEN);
  186.     strcpy(playpen,XIM_Msg->String);
  187.  
  188.     GetDT(BB_CONFLOCAL,"");
  189.     strcpy(confpath,XIM_Msg->String);
  190.  
  191.     GetDT(913,"");
  192.     dirinkbyte=XIM_Msg->Data; 
  193.  
  194.     GetDT(DT_SLOTNUMBER,"");
  195.     strcpy(slot,XIM_Msg->String);
  196.     
  197.     GetDT(531,"");
  198.     Nodes=(struct MultiPort *)XIM_Msg->Semi;
  199.     
  200.     if (axx<=axx2 && !msgbase)
  201.     {
  202.         Hotkey("      Do you want your Uploads to be posted at a special time ? (y/N) ");
  203.         if (XIM_Msg->String[0]=='y' || XIM_Msg->String[0]=='Y')
  204.         {
  205.             ws("yes!",1);
  206.             ws("",1);
  207.             ws(" Please enter the filenames (patterns allowed) and the delay time of uploading!",1);
  208.             ws("",1);
  209.  
  210.             pos=0;
  211.             do
  212.             {
  213.                 
  214.                 sprintf(buffer,"CFilename [%2d]: ",pos);
  215.                 Prompt(30,buffer); 
  216.                 strcpy (buffer,XIM_Msg->String);
  217.                 upperstring(buffer);
  218.                 
  219.                 main_filename=(char **) realloc(main_filename,(pos+1)*sizeof(char *));
  220.                 main_filename[pos]=NULL;
  221.                 len=strlen(buffer);
  222.                 main_filename[pos]=(char *) malloc (len+1);
  223.                 if (main_filename[pos]!=NULL)
  224.                     strcpy(main_filename[pos],buffer);
  225.                 pos++;
  226.             }
  227.                 while (strlen(buffer));
  228.             main_filename=(char **) realloc(main_filename,(pos+1)*sizeof(char *));
  229.             main_filename[pos]=NULL;
  230.  
  231.             if (main_filename[0][0]!=0)
  232.             {
  233.                 ws("",1);
  234.                 sprintf(buffer,"CThe local date and time is     %s %s",date,time);
  235.                 ws(buffer,1);
  236.                 sprintf(buffer,"CPlease enter date to post files: C[%s]D",date);
  237.                 Prompt(8,buffer);
  238.                 if (XIM_Msg->String[0]!=0) strcpy(postdate,XIM_Msg->String);
  239.                 else strcpy(postdate,date);
  240.                 sprintf(buffer,"CPlease enter time to post files: C[%s]D",time);
  241.                 Prompt(5,buffer);
  242.                 if (XIM_Msg->String[0]!=0) sprintf(posttime,"%s:00",XIM_Msg->String);
  243.                 else sprintf(posttime,"%s:00",time);
  244.  
  245.                 stmp=(struct DateStamp *) malloc (sizeof(struct DateStamp));
  246.                 dtime=(struct DateTime *) malloc (sizeof(struct DateTime));
  247.  
  248.                 dtime->dat_Format=FORMAT_USA;
  249.                 dtime->dat_Flags=DTF_SUBST; /*ignored*/
  250.                 dtime->dat_StrDay=NULL; /*ignored*/
  251.                 dtime->dat_StrDate=postdate;
  252.                 dtime->dat_StrTime=posttime;
  253.                 
  254.                 if (!StrToDate(dtime))
  255.  
  256.                 {
  257.                         ws("",1);
  258.                         ws("C     Couldn`t convert your time information      ",1);
  259.                         ws("",1);
  260.                     ws("Your time Information couldn`t be converted!",1);
  261.                 }
  262.                 else
  263.                 {
  264.  
  265.                     stmp2=(struct DateStamp *) malloc (sizeof(struct DateStamp));
  266.                     stmp=&dtime->dat_Stamp;
  267.                     DateStamp( stmp2 );
  268.                     result=CompareDates(stmp,stmp2);
  269.                     free(stmp2);
  270.  
  271.                     if (result>=0)
  272.                     {
  273.                         ws("",1);
  274.                         ws("C       There is no need to delay your files      ",1);
  275.                         ws("C  because your time is earlier than the current  ",1);
  276.                         ws("",1);
  277.                     }
  278.                     else
  279.                     {
  280.  
  281.                         ws("",1);
  282.  
  283.                         sprintf(buffer,"CYour files will be posted at : %s  %s",postdate,posttime);
  284.                         ws(buffer,1);
  285.                         ws("",1);
  286.                         sprintf(buffer,"t:UploadDelay.%d",Node);
  287.                         readfh=Open(buffer,MODE_NEWFILE);
  288.                         if(readfh)
  289.                         {
  290.                             FPuts(readfh,postdate);
  291.                             FPuts(readfh,"\n");
  292.                             FPuts(readfh,posttime);
  293.                             FPuts(readfh,"\n");
  294.                             for(pos=0;main_filename[pos];pos++)
  295.                             {
  296.                                 FPuts(readfh,main_filename[pos]);
  297.                                 FPuts(readfh,"\n");
  298.  
  299.                             }
  300.                             Close(readfh);
  301.                             uploaddelay=TRUE;
  302.                         }
  303.                     }
  304.                 }
  305.                 free (stmp);
  306.                 free(dtime);
  307.             }
  308.             for(pos=0;main_filename[pos];pos++)
  309.                 free(main_filename[pos]);
  310.             free(main_filename);
  311.             
  312.         
  313.  
  314.  
  315.  
  316.         }
  317.         else
  318.         {
  319.             ws("",1);
  320.         }
  321.  
  322.     }
  323.     
  324.  
  325.     
  326.     SendCmd(BB_LOGONTYPE);
  327.     if (XIM_Msg->Data==1 || XIM_Msg->Data==2) transtype=LOCAL;
  328.  
  329.     GetDT(DT_SECSTATUS,"");
  330.     axx=atoi(XIM_Msg->String);
  331.  
  332.     Shutdown();
  333.  
  334.     ObtainSemaphore((struct SignalSemaphore *)Nodes);
  335.     Singleport=(struct SinglePort *)Nodes->MyNode[Node].s;
  336.     ReleaseSemaphore((struct SignalSemaphore *)Nodes);
  337.  
  338.     ObtainSemaphore((struct SignalSemaphore *)Singleport);
  339.     status=Singleport->Status;
  340.     ReleaseSemaphore((struct SignalSemaphore *)Singleport);
  341.  
  342.     while (status==2 || status==30 || status==1 || status==3)
  343.     {    
  344.  
  345.  
  346.         if (status==30 && !nohydrabg)
  347.         {
  348.             sprintf(buffer,"HydraPort%d",Node);
  349.             if (!hydra)
  350.             {
  351.                 Forbid();
  352.                 hydra=(struct HydraPort *)FindSemaphore(buffer);
  353.                 Permit();
  354.             }
  355.             if (hydra)    
  356.             {
  357.                 successatt=AttemptSemaphore((struct SignalSemaphore *)hydra);
  358.                 if (successatt)
  359.                 {
  360.                     strcpy(filename,hydra->UpFileName);
  361.                     ReleaseSemaphore((struct SignalSemaphore *)hydra);
  362.                 }
  363.                     
  364.  
  365.                 if (strcmp(filetemp,filename)) 
  366.                 {
  367.                     if (strncmp(filetemp,"Init",4) && strncmp(filetemp,"End of batch",12) && filetemp[0]!=0 && (strlen(filetemp)<13)) 
  368.                     {
  369.                         strcpy(buffer,filetemp);
  370.                         upperstring(buffer);
  371.                         Forbid();
  372.                         filedata=(struct  Filedata    *)FindSemaphore(buffer);
  373.                         Permit();
  374.                         addsem=FALSE;
  375.                         if (!filedata) 
  376.                         {
  377.                             filedata=(struct Filedata *) AllocMem(sizeof(struct Filedata),MEMF_PUBLIC|MEMF_CLEAR);
  378.                             addsem=TRUE;
  379.                         }
  380.                         if (filedata)
  381.                         {
  382.                             if (!addsem) 
  383.                             {
  384.                                 ObtainSemaphore((struct SignalSemaphore *) filedata);
  385.                             }
  386.                             else
  387.                             {
  388.                                 filedata->semi.ss_Link.ln_Pri=0;
  389.                                 filedata->semi.ss_Link.ln_Name=filedata->filename;
  390.                                 strcpy(filedata->filename,buffer);
  391.                             }
  392.                             
  393.                             strcpy(filedata->filepath,playpen);
  394.                             sprintf(buffer,"%d",Node);
  395.                             strcpy(filedata->nodenumber,buffer);
  396.  
  397.                             ObtainSemaphore((struct SignalSemaphore *)Singleport);
  398.                             strcpy(filedata->handle,Singleport->Handle);
  399.                             ReleaseSemaphore((struct SignalSemaphore *)Singleport);
  400.  
  401.                             strcpy(filedata->confpath,confpath);
  402.                             sprintf(buffer,"%d",dirinkbyte);
  403.                             strcpy(filedata->dirinkbyte,buffer);
  404.                             strcpy(filedata->slotnumber,slot);
  405.                             filedata->confnumber=confnumber;
  406.                         
  407.                             filedata->Main_Fileid=NULL;
  408.                             filedata->Filesize=0;
  409.  
  410.                             filedata->MsgBase=msgbase;
  411.                             filedata->Hold=FALSE;
  412.                             filedata->Delay=uploaddelay;
  413.                             filedata->Immediately=FALSE;
  414.  
  415.                             filedata->bghandler=FALSE;
  416.                             filedata->add_fileid=FALSE;
  417.                             filedata->axx=axx;
  418.                 
  419.                             filedata->Filecheck_passed=FALSE;
  420.                             filedata->Filecheck_ok=FALSE;
  421.                             filedata->file_id_passed=FALSE;
  422.                             filedata->file_id=FALSE;
  423.                             filedata->all_passed=FALSE;
  424.                             filedata->dupe=FALSE;
  425.  
  426.                             if (addsem) AddSemaphore((struct SignalSemaphore *)filedata);    
  427.                             else        ReleaseSemaphore((struct SignalSemaphore *) filedata);
  428.  
  429.                             sprintf(buffer,"run >nil: bbs:bgcheck/bgfilehandler \"%s\"",filetemp);    
  430.                             SystemTags(buffer,NP_Priority,priority,NULL);
  431.                         }            
  432.                     filedata=NULL;
  433.                     }
  434.             
  435.                     strcpy(filetemp,filename);
  436.                 }
  437.                 if (filetemp[0]==0) strcpy(filetemp,filename);
  438.             }
  439.         }
  440.         
  441.  
  442.  
  443.         if ( (status==2) && (transtype!=LOCAL) && !nozmodembg)
  444.         {
  445.     
  446.             ObtainSemaphore((struct SignalSemaphore *)Singleport);
  447.             strcpy (filename,Singleport->Misc1);
  448.             ReleaseSemaphore((struct SignalSemaphore *)Singleport);
  449.             if (strcmp(filetemp,filename)) 
  450.             {
  451.                     
  452.  
  453.                 if (filetemp[0]!=0 && strlen(filetemp)<13 && strcmp(filename,"UPLOAD FINISHED")) 
  454.                 {
  455.  
  456.                     strcpy(buffer,filetemp);
  457.                     upperstring(buffer);
  458.                     addsem=FALSE;
  459.                     Forbid();
  460.                     filedata=(struct  Filedata    *)FindSemaphore(buffer);
  461.                     Permit();
  462.                     if (!filedata)
  463.                     {
  464.                         filedata=(struct Filedata *) AllocMem(sizeof(struct Filedata),MEMF_PUBLIC|MEMF_CLEAR);
  465.                         addsem=TRUE;
  466.                     }
  467.                     if (filedata)
  468.                     {
  469.  
  470.                             if (!addsem) 
  471.                             {
  472.                                 ObtainSemaphore((struct SignalSemaphore *) filedata);
  473.                             }
  474.                             else
  475.                             {
  476.                                 filedata->semi.ss_Link.ln_Pri=0;
  477.                                 filedata->semi.ss_Link.ln_Name=filedata->filename;
  478.                                 strcpy(filedata->filename,buffer);
  479.                             }
  480.  
  481.                             strcpy(filedata->filepath,playpen);
  482.                             sprintf(buffer,"%d",Node);
  483.                             strcpy(filedata->nodenumber,buffer);
  484.                     
  485.                             ObtainSemaphore((struct SignalSemaphore *)Singleport);
  486.                             strcpy(filedata->handle,Singleport->Handle);
  487.                             ReleaseSemaphore((struct SignalSemaphore *)Singleport);
  488.  
  489.                             strcpy(filedata->confpath,confpath);
  490.                             sprintf(buffer,"%d",dirinkbyte);
  491.                             strcpy(filedata->dirinkbyte,buffer);
  492.                             strcpy(filedata->slotnumber,slot);
  493.                             filedata->confnumber=confnumber;
  494.                         
  495.                             filedata->Main_Fileid=NULL;
  496.                             filedata->Filesize=0;
  497.  
  498.                             filedata->MsgBase=msgbase;
  499.                             filedata->Hold=FALSE;
  500.                             filedata->Delay=uploaddelay;
  501.                             filedata->Immediately=FALSE;
  502.  
  503.                             filedata->bghandler=FALSE;
  504.                             filedata->add_fileid=FALSE;
  505.                             filedata->axx=axx;
  506.  
  507.                             filedata->Filecheck_passed=FALSE;
  508.                             filedata->Filecheck_ok=FALSE;
  509.                             filedata->file_id_passed=FALSE;
  510.                             filedata->file_id=FALSE;
  511.                             filedata->all_passed=FALSE;
  512.                             filedata->dupe=FALSE;
  513.  
  514.  
  515.                             if (addsem) AddSemaphore((struct SignalSemaphore *)filedata);    
  516.                             else        ReleaseSemaphore((struct SignalSemaphore *) filedata);
  517.  
  518.                             sprintf(buffer,"run >nil: bbs:bgcheck/bgfilehandler \"%s\"",filetemp);    
  519.                             SystemTags(buffer,NP_Priority,priority,NULL);
  520.                                     
  521.                     }
  522.                     filedata=NULL;
  523.                 }
  524.                 strcpy(filetemp,filename);
  525.             }
  526.             if (filetemp[0]==0) strcpy(filetemp,filename);
  527.         }
  528.         
  529.  
  530.  
  531.     
  532.         ObtainSemaphore((struct SignalSemaphore *)Nodes);
  533.         Singleport=(struct SinglePort *)Nodes->MyNode[Node].s;
  534.         ReleaseSemaphore((struct SignalSemaphore *)Nodes);
  535.  
  536.         ObtainSemaphore((struct SignalSemaphore *)Singleport);
  537.         status=Singleport->Status;
  538.         ReleaseSemaphore((struct SignalSemaphore *)Singleport);
  539.  
  540.         Delay (10);
  541.     }
  542. }
  543.