home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 February / chip_20022115.iso / amiga / chiputil / aminetrate.lha / AminetRate.rexx < prev   
OS/2 REXX Batch file  |  2001-12-12  |  15KB  |  507 lines

  1. /*
  2. ** $VER: AminetRate.rexx 1.5 (12.12.2001)
  3. ** Copyright ⌐1999-2001 by Olivier Fabre <off@free.fr>.
  4. **
  5. ** A big thank you to Robin Evans for his ARexxGuide !
  6. **
  7. ** Requires RexxDOSSupport.library (Copyright (C) 1994-1997 by hartmut Goebel).
  8. **
  9. ** Vote for Aminet files made easy.
  10. **
  11. ** v1.5 (12.12.2001)
  12. **    ╖ Fixed MOVEREADME bug [Michael Taylor].
  13. ** v1.4 (04.12.2001):
  14. **    ╖ Improved SMTP client. Added "HELO" message in the SMTP dialog (the
  15. **      domain is based on the email address). [thanks to Michael Taylor]
  16. **    ╖ Improved a little bit the documentation.
  17. ** v1.3 (14.09.2001):
  18. **    ╖ Handles "http://" and "ftp://" so that this script can be integrated
  19. **      as a link context menu in a web browser (AWeb).
  20. **    ╖ Localised (French and English for now).
  21. **    ╖ Now remembers the mark given to a file in the archive.
  22. **    ╖ Minor improvements and/or bug additions since I didn't check
  23. **      everything ;)
  24. **    ╖ Added examples of usage.
  25. ** v1.02 (21.05.2000):
  26. **    ╖ When voting a second time for a file, removes the older entry from
  27. **    the marks archive.
  28. **    ╖ Does not open a window with "0" after sending the mail anymore.
  29. **    ╖ Encloses mail addresses between "<" and ">" when sending the mail.
  30. **
  31. ** Template: FILE/M,MARK/K/N,MAIL/S,DELFILE/S,DELREADME/S,MOVEFILE/S,MOVEREADME/S
  32. **
  33. **  FILE/M
  34. **     Either a readme or an Aminet file (eg #?.lha). Might start with
  35. **     "http://" or "ftp://". AminetRate will try to find the Aminet directory
  36. **     with these methods:
  37. **     - look in the readme for a "Type:" line (if the file is local)
  38. **     - look if the file is in an Aminet-like directory (eg DOWNLOAD:comm/misc)
  39. **     (- if online, have a look on Aminet) ->TODO ?
  40. **     - ask the user :-)
  41. **
  42. **     You can rate several files at the same time. If you do not provide a
  43. **     MARK value, then you will be asked a mark for each file; otherwise they
  44. **     will all get the same mark.
  45. **
  46. **  MARK/K/N
  47. **     How much you rate this file, from 0 to 10. If not given, and a FILE is
  48. **     given, you will be asked to select a mark in a requester.
  49. **
  50. **  MAIL/S
  51. **     Tells AminetRate to send a mail with all the votes stored so far.
  52. **     This needs the "TCP:" stuff (works at least with Miami 3).
  53. **
  54. **  DELFILE/S, DELREADME/S
  55. **     Tells AminetRate to delete the file/readme once the mark is stored.
  56. **
  57. **  MOVEFILE/S, MOVEREADME/S
  58. **     Tells AminetRate to move the file/readme into another directory once
  59. **     the mark is stored.
  60. **
  61. **
  62. **  EXAMPLES:
  63. **
  64. **    AminetRate.rexx dev/c/vbcc.lha MARK=10
  65. ** or AminetRate.rexx dev/c/vbcc.readme MARK=10
  66. **      gives a 10/10 to dev/c/vbcc.lha.
  67. **
  68. **    AminetRate.rexx vbcc.lha MARK=10
  69. **      Same, but might ask you to type the directory if it can't be found
  70. **      in the associated readme or from the file directory.
  71. **
  72. **    AminetRate.rexx ftp://us.aminet.net/pub/aminet/dev/c/vbcc.lha
  73. **      opens a requester to ask your mark for vbcc.
  74. **
  75. **    AminetRate.rexx dev/c/vbcc.lha dev/c/CLib37x.readme MARK=9
  76. **      gives 9/10 to VBCC and CLib37x.
  77. **
  78. **    SPat AminetRate.rexx #?.readme
  79. **      to rate all the Aminet files of the directory.
  80. **
  81. **    AminetRate.rexx MAIL
  82. **      sends a mail with all your votes to the Aminet server.
  83. **
  84. **
  85. **  Please edit the following lines to suit your taste...
  86. */
  87.  
  88. replyto          = "@"                    /* Your mail address */
  89. mailserver       = "smtp"                /* Your mail server */
  90. movereadmedir    = "DOWNLOAD:Store"                /* Where to move readme files */
  91. movefiledir      = "DOWNLOAD:Store"                /* Where to move files */
  92. marksfile        = "DOWNLOAD:AminetMarks"        /* Where to store the marks */
  93. marksarchivefile = "DOWNLOAD:AminetMarksArchive"    /* Where to store the name of files you've already voted for */
  94. language         = "english"                /* "english" or "franτais" */
  95. logging          = "no"                        /* Log SMTP dialog into T:AminetRate.smtp.log or not.
  96.                                        If "AminetRate MAIL" doesn't work, send me the log. */
  97.  
  98. /* The following shouldn't need to be changed, but you can... */
  99.  
  100. aminetserver     = "aminet-server@wuarchive.wustl.edu"    /* Where to send your votes */
  101. aminetdirs       = "/biz/comm/demo/dev/disk/docs/game/gfx/hard/misc/mods/mus/pix/text/util/"        /* List of all Aminet directories */
  102. maxaminetsubdirlength = 5                    /* for sub-dirs check */
  103. aminetdirsdir    = "T:AminetRate"        /* Temporary directory */
  104. arv              = "AminetRate"            /* Name of the environment variable used for
  105.                                                      getting return values from shell commands */
  106.  
  107. /* Language catalog */
  108.  
  109. /* English first, replaced by another language if applicable */
  110. cat.norexxdoss    = "RexxDOSSupport.library is missing."
  111. cat.errarg        = "Error parsing arguments."
  112. cat.twodirs        = "Two different Aminet directories were found ! Choose the good one..."
  113. cat.fromloc        = "from local dir"
  114. cat.fromread    = "from readme"
  115. cat.typedir        = "Please type the Aminet directory of"
  116. cat.selectmark    = "Please select a mark for"
  117. cat.cancel        = "Cancel"
  118. cat.by         = "by"
  119. cat.already        = "You already voted for"
  120. cat.on         = "on"
  121. cat.anyway         = "Vote anyway"
  122. cat.cnxfail        = "Connection to the mail server failed."
  123. cat.abortmail    = "Aborting mail transfer."
  124. cat.errmarks    = "Error opening" marksfile "for reading."
  125. cat.smtperr        = "SMTP error"
  126. cat.mailsent   = "Mail sent !"
  127. cat.nothing        = "Nothing to send."
  128.  
  129. IF language = "franτais" THEN DO
  130.     cat.norexxdoss    = "RexxDOSSupport.library manquante."
  131.     cat.errarg        = "Erreur lors de l'analyse des arguments."
  132.     cat.twodirs        = "Deux rΘpertoires Aminet diffΘrents ont ΘtΘ trouvΘs ! Choisissez le bon..."
  133.     cat.fromloc        = "rΘp. local"
  134.     cat.fromread    = "readme"
  135.     cat.typedir        = "Tapez le nom du rΘpertoire Aminet de"
  136.     cat.selectmark    = "Choisissez une note pour"
  137.     cat.cancel        = "Annuler"
  138.     cat.by            = "par"
  139.     cat.already        = "Vous avez dΘjα votΘ pour"
  140.     cat.on            = "le"
  141.     cat.anyway        = "Voter quand mΩme"
  142.     cat.cnxfail        = "La connexion au serveur de courrier a ΘchouΘ."
  143.     cat.abortmail    = "Transfert de courrier annulΘ."
  144.     cat.errmarks    = "Erreur α l'ouverture de" marksfile "en lecture."
  145.     cat.smtperr        = "Erreur SMTP"
  146.     cat.mailsent    = "Mail envoyΘ !"
  147.     cat.nothing        = "Rien α envoyer."
  148. END
  149.  
  150.  
  151. /********************************/
  152. /* Do not change anything below */
  153. /********************************/
  154.  
  155. wintitle = "AminetRate 1.5 ⌐1999-2001 by Olivier Fabre"
  156.  
  157. IF ~SHOW( 'L', "rexxdossupport.library" ) THEN DO
  158.     IF ~ADDLIB( "rexxdossupport.library", 0, -30, 2 ) THEN DO
  159.         Error( cat.norexxdoss, 20 )
  160.     END
  161. END
  162.  
  163. PARSE ARG args
  164.  
  165. template = "FILE/M,MARK/K/N,MAIL/S,DELFILE/S,DELREADME/S,MOVEFILE/S,MOVEREADME/S"
  166.  
  167. IF ~ReadArgs( args, template ) THEN DO
  168.     Error( cat.errarg, 20 )
  169. END
  170.  
  171. IF file.count=0 & ~mail THEN DO
  172.     SHELL COMMAND "Which >ENV:"arv" AminetRate.rexx"
  173.     IF LENGTH( GetVar(arv) )<=1 THEN SHELL COMMAND "Which >ENV:"arv" AminetRate"
  174.     IF OPEN( f, GetVar(arv), "R" ) THEN DO
  175.         DO WHILE ~EOF( f )
  176.             l = READLN( f )
  177.             IF FIND( l, "Please edit" )>0 THEN DO
  178.                 SAY "*" || "/"
  179.                 EXIT
  180.             END
  181.             SAY l
  182.         END
  183.     END
  184.     DeleteVar(arv)
  185. END
  186.  
  187.  
  188. /* Process each given file */
  189. /* ----------------------- */
  190.  
  191. IF file.count>0 THEN DO
  192.  
  193.     IF ~EXISTS( aminetdirsdir ) THEN DO
  194.         MakeDir( aminetdirsdir )
  195.     END
  196.  
  197.     DO f=0 FOR file.count
  198.  
  199.         /* Find path, file and readme names */
  200.         /* -------------------------------- */
  201.  
  202.         /* First check if name starts with http:// or ftp:// */
  203.         distantFile = 0
  204.         IF LEFT( file.f, 7 ) = "http://" | LEFT( file.f, 6 ) = "ftp://" THEN DO
  205.             distantFile = 1
  206.             file.f = SUBSTR( file.f, 7 )
  207.         END
  208.  
  209.         totalpath = AbsolutePath( file.f )
  210.  
  211.         path = PathPart( totalpath )
  212.  
  213.         IF LENGTH( path )=0 THEN DO
  214.             path = PathPart( file.f )
  215.         END
  216.  
  217.         thisfile = FilePart( file.f )
  218.  
  219.         IF RIGHT( thisfile, 7 ) = ".readme" THEN DO
  220.             thisfile = LEFT( thisfile, LENGTH( thisfile )-6 )
  221.             /*SHELL COMMAND "List >ENV:"arv AddPart( path, thisfile )"#? LFORMAT=%f%s"*/
  222.             thisfile = thisfile"lha"
  223.         END
  224.  
  225.         thisreadme = LEFT( thisfile, LASTPOS( ".", thisfile ) )"readme"
  226.  
  227.  
  228.         /* Find type according to the local directory */
  229.         /* ------------------------------------------ */
  230.  
  231.         dir = ""
  232.         subdir = ""
  233.  
  234.         IF path ~= "" THEN DO
  235.             slashpos = LASTPOS( "/", path )
  236.  
  237.             IF slashpos>2 THEN DO
  238.                 subdir = SUBSTR( path, slashpos+1 )
  239.  
  240.                 IF LENGTH( subdir ) <= maxaminetsubdirlength THEN DO
  241.                     cutpath = LEFT( path, slashpos-1 )
  242.                     slashpos = LASTPOS( "/", cutpath )
  243.                     IF slashpos=0 THEN slashpos = LASTPOS( ":", cutpath )
  244.  
  245.                     dir = SUBSTR( cutpath, slashpos+1 )
  246.  
  247.                     IF POS( "/"dir"/", aminetdirs )=0 THEN DO
  248.                         dir = ""    /* acts as a flag */
  249.                     END
  250.  
  251.                 END    /* IF LENGTH( subdir ) <= maxaminetsubdirlength */
  252.  
  253.             END    /* IF slashpos>0 */
  254.  
  255.         END    /* IF path~="" */
  256.  
  257.         IF dir = "" THEN subdir = ""
  258.  
  259.  
  260.         /* Find type according to the "Type:" line in the readme */
  261.         /* ----------------------------------------------------- */
  262.  
  263.         IF ~distantFile & OPEN( readmef, AddPart( path, thisreadme ), "R" ) THEN DO
  264.  
  265.             DO WHILE ~EOF( readmef )
  266.                 line = COMPRESS( READLN( readmef ) )
  267.  
  268.                 IF UPPER( LEFT( line, 5 ) )="TYPE:" THEN DO
  269.                     slashpos = POS( "/", line )
  270.  
  271.                     IF slashpos > 6 THEN DO
  272.                         newdir = SUBSTR( line, 6, slashpos-6 )
  273.                         newsubdir = SUBSTR( line, slashpos+1 )
  274.  
  275.                         IF dir ~= "" THEN DO
  276.  
  277.                             IF newdir ~= dir | newsubdir ~= subdir THEN DO
  278.                                 SHELL COMMAND 'RequestChoice >ENV:'arv' "'wintitle'" "'cat.twodirs'" "'dir'/'subdir' ('cat.fromloc')" "'newdir'/'newsubdir' ('cat.fromread')"'
  279.  
  280.                                 IF GetVar( arv )=0 THEN DO
  281.                                     dir = newdir
  282.                                     subdir = newsubdir
  283.                                 END
  284.  
  285.                                 DeleteVar( arv )
  286.                             END
  287.  
  288.                         END    /* IF dir ~= "" */
  289.                         ELSE DO
  290.                             dir = newdir
  291.                             subdir = newsubdir
  292.                         END    /* IF dir = "" */
  293.  
  294.                     END    /* IF slashpos > 6 */
  295.  
  296.                     BREAK
  297.                 END    /* IF "TYPE:" */
  298.  
  299.             END    /* WHILE ~EOF( readmef ) */
  300.  
  301.             CLOSE( readmef )
  302.         END    /* IF ~distantFile & OPEN( readmef ) */
  303.  
  304.  
  305.         /* Ask for type if not found */
  306.         /* ------------------------- */
  307.  
  308.         IF dir = "" THEN DO
  309.             SHELL COMMAND 'RequestFile >ENV:'arv '"'aminetdirsdir'" TITLE="'cat.typedir thisfile'"'
  310.             dir = GetVar( arv )
  311.             IF LENGTH( dir ) > LENGTH( aminetdirsdir )+6 THEN DO
  312.                 dir = SUBSTR( dir, LENGTH( aminetdirsdir )+3 )
  313.                 dir = LEFT( dir, LENGTH( dir )-1 )
  314.                 DeleteVar( arv )
  315.                 IF RIGHT( dir, 1 ) = "/" THEN dir = LEFT( dir, LENGTH( dir )-1 )
  316.  
  317.                 slashpos = LASTPOS( "/", dir )
  318.  
  319.                 IF slashpos > 2 THEN DO
  320.                     subdir = SUBSTR( dir, slashpos+1 )
  321.  
  322.                     IF LENGTH( subdir ) <= maxaminetsubdirlength THEN DO
  323.                         cutdir = LEFT( dir, slashpos-1 )
  324.                         slashpos = LASTPOS( "/", cutdir )
  325.                         IF slashpos = 0 THEN slashpos = LASTPOS( ":", cutdir )
  326.  
  327.                         dir = SUBSTR( cutdir, slashpos+1 )
  328.  
  329.                         IF POS( "/"dir"/", aminetdirs ) = 0 THEN DO
  330.                             dir = ""
  331.                         END
  332.  
  333.                     END    /* IF subdir length */
  334.  
  335.                 END    /* IF slashpos > 2 */
  336.  
  337.             END    /* IF LENGTH( dir )... */
  338.  
  339.         END    /* IF dir = "" */
  340.  
  341.         IF dir = "" THEN subdir = ""
  342.  
  343.  
  344.         /* Ask the mark & add it to the mail file */
  345.         /* -------------------------------------- */
  346.  
  347.         dir = dir"/"subdir
  348.  
  349.         IF dir = "/" THEN Error( "Aborting", 0 )
  350.         ELSE DO
  351.             aminetfile = dir"/"thisfile
  352.  
  353.             flag = 1
  354.             IF EXISTS( marksarchivefile ) THEN DO
  355.                 SHELL COMMAND "Search >ENV:"arv marksarchivefile aminetfile "NONUM"
  356.                 IF GetVar( arv ) ~= "" THEN DO
  357.                     SHELL COMMAND 'RequestChoice >ENV:'arv '"'wintitle'" "'cat.already GetVar( arv )'" "'cat.anyway'" "'cat.cancel'"'
  358.                     flag = GetVar( arv )
  359.                 END
  360.             END
  361.  
  362.             IF flag THEN DO
  363.                 IF mark = "MARK" THEN mark = AskMark( aminetfile )
  364.  
  365.                 IF mark >= 0 & mark <= 10 THEN DO
  366.  
  367.                     SHELL COMMAND "Echo >>"marksfile "rate" aminetfile mark
  368.  
  369.                     /* Removes any older entry in the marks archive file */
  370.                     /* ------------------------------------------------- */
  371.                     SHELL COMMAND "Copy" marksarchivefile marksarchivefile".tmp QUIET"
  372.                     IF OPEN( maf2, marksarchivefile".tmp", "R" ) THEN DO
  373.                         IF OPEN( maf, marksarchivefile, "W" ) THEN DO
  374.                             DO WHILE ~EOF( maf2 )
  375.                                 line = READLN( maf2 )
  376.                                 IF line ~= "" & POS( aminetfile, line ) ~= 1 THEN WRITELN( maf, line )
  377.                             END
  378.                             CLOSE( maf )
  379.                         END
  380.                         CLOSE( maf2 )
  381.                     END
  382.                     SHELL COMMAND "Delete" marksarchivefile".tmp QUIET"
  383.  
  384.                     SHELL COMMAND "Echo >>"marksarchivefile aminetfile cat.on "`Date` ("mark"/10)"
  385.  
  386.                     /* Moves & | delete files if asked by the user */
  387.                     /* ------------------------------------------- */
  388.  
  389.                     IF ~distantFile THEN DO
  390.  
  391.                         IF movefile THEN DO
  392.                             IF ~EXISTS( movefiledir ) THEN MakeDir( movefiledir )
  393.                             Rename( AddPart( path, thisfile ), AddPart( movefiledir, thisfile ) )
  394.                         END
  395.                         ELSE IF delfile THEN DO
  396.                             Delete( AddPart( path, thisfile ) )
  397.                         END
  398.  
  399.                         IF movereadme THEN DO
  400.                             IF ~EXISTS( movereadmedir ) THEN MakeDir( movereadmedir )
  401.                             Rename( AddPart( path, thisreadme ), AddPart( movereadmedir, thisreadme ) )
  402.                         END
  403.                         ELSE IF delreadme THEN DO
  404.                             Delete( AddPart( path, thisreadme ) )
  405.                         END
  406.  
  407.                     END    /* IF ~distantFile */
  408.  
  409.                 END    /* IF mark >= 0 & mark <= 10 */
  410.  
  411.             END    /* IF flag */
  412.  
  413.         END    /* IF dir ~= "/" */
  414.  
  415.     END    /* DO f=0 FOR file.count */
  416.  
  417. END    /* IF file.count > 0 */
  418.  
  419.  
  420. /* Send the mail */
  421. /* ------------- */
  422.  
  423. IF mail & EXISTS( marksfile ) THEN DO
  424.     IF ~OPEN( smtp, "TCP:"mailserver"/smtp", "W" ) THEN DO
  425.         Error( cat.cnxfail cat.abortmail, 10 )
  426.     END
  427.     IF logging="yes" THEN IF ~OPEN( log, "T:AminetRate.smtp.log", "W" ) THEN DO
  428.         Error( "Opening of T:AminetRate.smtp.log failed.", 10 )
  429.     END
  430.     IF ~OPEN( mf, marksfile, "R" ) THEN DO
  431.         Error( cat.errmarks cat.abortmail, 10 )
  432.     END
  433.  
  434.     l = ReadSMTP()
  435.     IF LEFT( l, 3 ) ~= "220" THEN Error( cat.smtperr ":" l, 10 )
  436.  
  437.     WriteSMTP( "HELO" SUBSTR( replyto, LASTPOS("@",replyto)+1 ) )
  438.     l = ReadSMTP()
  439.     IF LEFT( l, 3 ) ~= "250" THEN Error( cat.smtperr ":" l, 10 )
  440.  
  441.     WriteSMTP( "MAIL FROM:" "<"replyto">" )
  442.     l = ReadSMTP()
  443.     IF LEFT( l, 3 ) ~= "250" THEN Error( cat.smtperr ":" l, 10 )
  444.  
  445.     WriteSMTP( "RCPT TO:" "<"aminetserver">" )
  446.     l = ReadSMTP()
  447.     IF LEFT( l, 3 ) ~= "250" & LEFT( l, 3 ) ~= "251" THEN Error( cat.smtperr ":" l, 10 )
  448.  
  449.     WriteSMTP( "DATA" )
  450.     l = ReadSMTP()
  451.     IF LEFT( l, 3 ) ~= "354" THEN Error( cat.smtperr ":" l, 10 )
  452.  
  453.     WriteSMTP( "To:" aminetserver )
  454.     WriteSMTP( "" )
  455.  
  456.     DO WHILE ~EOF( mf )
  457.         l = READLN( mf )
  458.         WriteSMTP( l )
  459.     END
  460.  
  461.     WriteSMTP( "." )
  462.     l = ReadSMTP()
  463.     IF LEFT( l, 3 ) ~= "250" THEN Error( cat.smtperr ":" l, 10 )
  464.  
  465.     WriteSMTP( "quit" )
  466.     ReadSMTP()    /* 221 for succes... should I care ? */
  467.  
  468.     IF logging="yes" THEN CLOSE( log )
  469.     CLOSE( smtp )
  470.     CLOSE( mf )
  471.     Delete( marksfile )
  472.     Error( cat.mailsent, 0 )
  473. END
  474.  
  475. IF mail & ~EXISTS( marksfile ) THEN Error( cat.nothing, 0 )
  476.  
  477. EXIT
  478.  
  479.  
  480. /* Fonctions */
  481. /* --------- */
  482.  
  483. AskMark: PROCEDURE EXPOSE wintitle cat.
  484.     PARSE ARG file
  485.     SHELL COMMAND 'RequestChoice >ENV:'arv' "'wintitle'" "'cat.selectmark file'" 0 1 2 3 4 5 6 7 8 9 10 "'cat.cancel'"'
  486.     askmark = GetVar( arv )
  487.     DeleteVar( arv )
  488.     RETURN askmark-1
  489.  
  490. Error: PROCEDURE EXPOSE wintitle
  491.     PARSE ARG texte,code
  492.     SHELL COMMAND 'RequestChoice >NIL: "' wintitle '" "'texte'" "Ok"'
  493.     EXIT code
  494.  
  495. ReadSMTP: PROCEDURE EXPOSE smtp log logging
  496.     DO UNTIL SUBSTR( l, 4, 1 ) ~= "-"
  497.         l = READLN( smtp )
  498.         IF logging="yes" THEN WRITELN( log, l )
  499.     END
  500.     RETURN l
  501.  
  502. WriteSMTP: PROCEDURE EXPOSE smtp log logging
  503.     PARSE ARG l
  504.     WRITELN( smtp, l )
  505.     IF logging="yes" THEN WRITELN( log, l )
  506.     RETURN 0
  507.