home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 July / PCWorld_2001-07_cd.bin / Software / Topware / w2ksp2en / w2ksp2.exe / i386 / ins.cab / nntp_rexpire.vbs < prev    next >
Encoding:
Text File  |  1999-12-29  |  10.3 KB  |  440 lines

  1. REM
  2. REM LOCALIZATION
  3. REM
  4.  
  5. L_SWITCH_OPERATION      = "-t"
  6. L_SWITCH_SERVER         = "-s"
  7. L_SWITCH_INSTANCE_ID    = "-v"
  8. L_SWITCH_EXPIRE_ID      = "-i"
  9. L_SWITCH_TIME            = "-h"
  10. L_SWITCH_NEWSGROUPS     = "-n"
  11. L_SWITCH_NAME           = "-p"
  12. L_SWITCH_ONETIME        = "-o"
  13.  
  14. L_OP_ADD            = "a"
  15. L_OP_DELETE         = "d"
  16. L_OP_GET            = "g"
  17. L_OP_SET            = "s"
  18. L_OP_ENUMERATE      = "e"
  19.  
  20. L_DESC_PROGRAM      = "rexpire - Set server expiration policies"
  21. L_DESC_ADD          = "add expiration policy"
  22. L_DESC_DELETE       = "delete expiration policy"
  23. L_DESC_GET          = "get expiration policy"
  24. L_DESC_SET          = "set expiration policy"
  25. L_DESC_ENUMERATE    = "enumerate expiration policies"
  26.  
  27. L_DESC_OPERATIONS    = "<operations>"
  28. L_DESC_SERVER       = "<server> Specify computer to configure"
  29. L_DESC_INSTANCE_ID  = "<virtual server id> Specify virtual server id"
  30. L_DESC_EXPIRE_ID    = "<expire id> Specify expiration policy id"
  31. L_DESC_TIME            = "<expire time> Specify number of hours until articles are expired"
  32. L_DESC_NEWSGROUPS   = "<newsgroups> Specify newsgroup to which policy is applied"
  33. L_DESC_NAME         = "<policy name> Expire policy name"
  34. L_DESC_ONETIME        = "[true | false] One time expire policy?"
  35.  
  36. L_DESC_EXAMPLES     = "Examples:"
  37. L_DESC_EXAMPLE1     = "rexpire.vbs -t e -v 1"
  38. L_DESC_EXAMPLE2     = "rexpire.vbs -t a -n alt.binaries.* -h 24"
  39. L_DESC_EXAMPLE3     = "rexpire.vbs -t s -i 1 -h 24"
  40. L_DESC_EXAMPLE4     = "rexpire.vbs -t d -i 1"
  41.  
  42. L_STR_EXPIRE_NAME    = "Name:"
  43. L_STR_EXPIRE_ID        = "Expire ID:"
  44. L_STR_EXPIRE_TIME    = "Time horizon:"
  45. L_STR_NEWSGROUPS    = "Newsgroups:"
  46.  
  47. L_STR_OLD_POLICY    = "Old Policy"
  48. L_STR_NEW_POLICY    = "New Policy"
  49.  
  50. L_ERR_MUST_ENTER_ID = "You must enter an expire policy ID"
  51. L_ERR_EXPIRE_ID_NOT_FOUND    = "Error: There is no expiration policy with that ID"
  52.  
  53. REM
  54. REM END LOCALIZATION
  55. REM
  56.  
  57. REM
  58. REM --- Globals ---
  59. REM
  60.  
  61. dim g_dictParms
  62. dim    g_admin
  63.  
  64. set g_dictParms = CreateObject ( "Scripting.Dictionary" )
  65. set g_admin = CreateObject ( "NntpAdm.Expiration" )
  66.  
  67. REM
  68. REM --- Set argument defaults ---
  69. REM
  70.  
  71. g_dictParms(L_SWITCH_OPERATION)        = ""
  72. g_dictParms(L_SWITCH_SERVER)        = ""
  73. g_dictParms(L_SWITCH_INSTANCE_ID)    = "1"
  74. g_dictParms(L_SWITCH_EXPIRE_ID)        = ""
  75. g_dictParms(L_SWITCH_TIME)            = "-1"
  76. g_dictParms(L_SWITCH_NEWSGROUPS)    = ""
  77. g_dictParms(L_SWITCH_NAME)            = ""
  78. g_dictParms(L_SWITCH_ONETIME)        = False
  79.  
  80. REM
  81. REM --- Begin Main Program ---
  82. REM
  83.  
  84. if NOT ParseCommandLine ( g_dictParms, WScript.Arguments ) then
  85.     usage
  86.     WScript.Quit ( 0 )
  87. end if
  88.  
  89. dim cExpires
  90. dim i
  91. dim id
  92. dim index
  93.  
  94. REM
  95. REM    Debug: print out command line arguments:
  96. REM
  97. REM switches = g_dictParms.keys
  98. REM args = g_dictParms.items
  99. REM
  100. REM
  101. REM for i = 0 to g_dictParms.Count - 1
  102. REM     WScript.echo switches(i) & " = " & args(i)
  103. REM next
  104. REM
  105.  
  106. g_admin.Server = g_dictParms(L_SWITCH_SERVER)
  107. g_admin.ServiceInstance = g_dictParms(L_SWITCH_INSTANCE_ID)
  108.     On Error Resume Next
  109.     g_admin.Enumerate
  110.    if ( Err.Number <> 0 ) then
  111.         WScript.echo " Error enumerating Expiration: " & Err.Description & "(" & Err.Number & ")"
  112.     end if
  113.  
  114. id = g_dictParms ( L_SWITCH_EXPIRE_ID )
  115.  
  116. select case g_dictParms(L_SWITCH_OPERATION)
  117. case L_OP_ENUMERATE
  118.     REM
  119.     REM    List the existing expiration policies:
  120.     REM
  121.  
  122.     cExpires = g_admin.Count
  123.     for i = 0 to cExpires - 1
  124.  
  125.     On Error Resume Next
  126.     g_admin.GetNth i
  127.    if ( Err.Number <> 0 ) then
  128.         WScript.echo " Error getting Expiration: " & Err.Description & "(" & Err.Number & ")"
  129.    else
  130.     PrintExpire g_admin
  131.     end if
  132.     next
  133.  
  134. case L_OP_ADD
  135.     REM
  136.     REM    Add a new expiration policy
  137.     REM
  138.  
  139.     if ( g_dictParms ( L_SWITCH_NEWSGROUPS ) = "" ) then
  140.         usage
  141.     else
  142.         dim    strPolicyName
  143.  
  144.         strPolicyName = g_dictParms ( L_SWITCH_NAME )
  145.  
  146.         if ( g_dictParms ( L_SWITCH_ONETIME ) ) then
  147.             strPolicyName = strPolicyName & "@EXPIRE:ROADKILL"
  148.         end if
  149.  
  150.         g_admin.PolicyName            = strPolicyName
  151.         g_admin.ExpireTime            = g_dictParms ( L_SWITCH_TIME )
  152.         g_admin.ExpireSize            = "-1"
  153.         g_admin.NewsgroupsVariant    = SemicolonListToArray ( g_dictParms ( L_SWITCH_NEWSGROUPS ) )
  154.  
  155.  
  156.         g_admin.Add
  157.         PrintExpire g_admin
  158.  
  159.     end if
  160.  
  161. case L_OP_DELETE
  162.     REM
  163.     REM    Delete an expiration policy
  164.     REM
  165.  
  166.     if id = "" OR NOT IsNumeric ( id ) then
  167.         WScript.Echo L_ERR_MUST_ENTER_ID
  168.         WScript.Quit 0
  169.     end if
  170.  
  171.     index = g_admin.FindID ( id )
  172.     if index = -1 then
  173.         WScript.Echo L_ERR_EXPIRE_ID_NOT_FOUND
  174.         WScript.Quit 0
  175.     end if
  176.  
  177.     On Error Resume Next
  178.     g_admin.Remove id
  179.  if ( Err.Number <> 0 ) then
  180.         WScript.echo " Error Removing Expiration: " & Err.Description & "(" & Err.Number & ")"
  181.    else
  182.     PrintExpire g_admin
  183.     end if
  184.  
  185. case L_OP_GET
  186.     REM
  187.     REM    Get a specific expiration policy:
  188.     REM
  189.  
  190.     index = g_admin.FindID(id)
  191.  
  192.     if index = -1 then
  193.         WScript.Echo L_ERR_EXPIRE_ID_NOT_FOUND
  194.         WScript.Quit 0
  195.     end if
  196.  
  197.     On Error Resume Next
  198.     g_admin.GetNth index
  199.  if ( Err.Number <> 0 ) then
  200.         WScript.echo " Error getting Expiration: " & Err.Description & "(" & Err.Number & ")"
  201.    else
  202.     PrintExpire g_admin
  203.     end if
  204.  
  205. case L_OP_SET
  206.     REM
  207.     REM    Change an existing expiration policy:
  208.     REM
  209.  
  210.     dim strNewName
  211.     dim nNewTime
  212.     dim rgNewGroups
  213.  
  214.     index = g_admin.FindID(id)
  215.  
  216.     if index = -1 then
  217.         WScript.Echo L_ERR_EXPIRE_ID_NOT_FOUND
  218.         WScript.Quit 0
  219.     end if
  220.  
  221.     On Error Resume Next
  222.     g_admin.GetNth index
  223.  if ( Err.Number <> 0 ) then
  224.         WScript.echo " Error getting Expiration: " & Err.Description & "(" & Err.Number & ")"
  225.    else
  226.     WScript.echo L_STR_OLD_POLICY
  227.     PrintExpire g_admin
  228.     WScript.echo
  229.     WScript.echo L_STR_NEW_POLICY
  230.     end if
  231.  
  232.     strNewName    = g_admin.PolicyName
  233.     nNewTime    = g_admin.ExpireTime
  234.     rgNewGroups    = g_admin.NewsgroupsVariant
  235.  
  236.     if g_dictParms ( L_SWITCH_NAME ) <> "" then
  237.         strNewName = g_dictParms ( L_SWITCH_NAME )
  238.     end if
  239.         
  240.     if g_dictParms ( L_SWITCH_TIME ) <> "" then
  241.         nNewTime = g_dictParms ( L_SWITCH_TIME )
  242.     end if
  243.         
  244.     if g_dictParms ( L_SWITCH_NEWSGROUPS ) <> "" then
  245.         rgNewGroups = SemicolonListToArray ( g_dictParms ( L_SWITCH_NEWSGROUPS ) )
  246.     end if
  247.  
  248.     if g_dictParms ( L_SWITCH_ONETIME ) then
  249.         strNewName = strNewName & "@EXPIRE:ROADKILL"
  250.     end if
  251.  
  252.     g_admin.PolicyName            = strNewName
  253.     g_admin.ExpireTime            = nNewTime
  254.     g_admin.NewsgroupsVariant    = rgNewGroups
  255.  
  256.     On Error Resume Next
  257.     g_admin.Set
  258.  if ( Err.Number <> 0 ) then
  259.         WScript.echo " Error setting Expiration: " & Err.Description & "(" & Err.Number & ")"
  260.    else
  261.     PrintExpire g_admin 
  262.    end if
  263.  
  264. case else
  265.     usage
  266.  
  267. end select
  268.  
  269. WScript.Quit 0
  270.  
  271. REM
  272. REM --- End Main Program ---
  273. REM
  274.  
  275. REM
  276. REM ParseCommandLine ( dictParameters, cmdline )
  277. REM     Parses the command line parameters into the given dictionary
  278. REM
  279. REM Arguments:
  280. REM     dictParameters  - A dictionary containing the global parameters
  281. REM     cmdline - Collection of command line arguments
  282. REM
  283. REM Returns - Success code
  284. REM
  285.  
  286. Function ParseCommandLine ( dictParameters, cmdline )
  287.     dim     fRet
  288.     dim     cArgs
  289.     dim     i
  290.     dim     strSwitch
  291.     dim     strArgument
  292.  
  293.     fRet    = TRUE
  294.     cArgs   = cmdline.Count
  295.     i       = 0
  296.     
  297.     do while (i < cArgs)
  298.  
  299.         REM
  300.         REM Parse the switch and its argument
  301.         REM
  302.  
  303.         if i + 1 >= cArgs then
  304.             REM
  305.             REM Not enough command line arguments - Fail
  306.             REM
  307.  
  308.             fRet = FALSE
  309.             exit do
  310.         end if
  311.  
  312.         strSwitch = cmdline(i)
  313.         i = i + 1
  314.  
  315.         strArgument = cmdline(i)
  316.         i = i + 1
  317.  
  318.         REM
  319.         REM Add the switch,argument pair to the dictionary
  320.         REM
  321.  
  322.         if NOT dictParameters.Exists ( strSwitch ) then
  323.             REM
  324.             REM Bad switch - Fail
  325.             REM
  326.  
  327.             fRet = FALSE
  328.             exit do
  329.         end if 
  330.  
  331.         dictParameters(strSwitch) = strArgument
  332.  
  333.     loop
  334.  
  335.     ParseCommandLine = fRet
  336. end function
  337.  
  338. REM
  339. REM    SemicolonListToArray ( strList )
  340. REM        Converts a semi colon delimited list to an array of strings
  341. REM
  342. REM        eg. str1;str2;str3;str4 -> ["str1", "str2", "str3", "str4"]
  343. REM
  344.  
  345. Function SemicolonListToArray ( strListIn )
  346.  
  347.     dim        rgItems
  348.     dim        strItem
  349.     dim        strList
  350.     dim        index
  351.     dim        i
  352.  
  353.     ReDim rgItems ( 10 )
  354.  
  355.     strList = strListIn
  356.     i = 0
  357.  
  358.     do until strList = ""
  359.  
  360.         REM
  361.         REM Debug: print out newsgroup list as we go:
  362.         REM    WScript.Echo strList
  363.         REM
  364.  
  365.         index = InStr ( strList, ";" )
  366.  
  367.         if index = 0 then
  368.             REM No trailing ";", so use the whole string
  369.  
  370.             strItem = strList
  371.             strList = ""
  372.         else
  373.             REM Use the string up to the ";"
  374.  
  375.             strItem = Left ( strList, index - 1 )
  376.             strList = Right ( strList, Len ( strList ) - index )
  377.         end if
  378.  
  379.         ReDim preserve rgItems ( i )
  380.         rgItems ( i ) = strItem
  381.         i = i + 1
  382.  
  383.     loop
  384.  
  385.     REM return the array
  386.     SemicolonListToArray = rgItems
  387. end function
  388.  
  389. REM
  390. REM Usage ()
  391. REM     prints out the description of the command line arguments
  392. REM
  393.  
  394. Sub Usage
  395.  
  396.     WScript.Echo L_DESC_PROGRAM
  397.     WScript.Echo vbTab & L_SWITCH_OPERATION & " " & L_DESC_OPERATIONS
  398.     WScript.Echo vbTab & vbTab & L_OP_ADD & vbTab & L_DESC_ADD
  399.     WScript.Echo vbTab & vbTab & L_OP_DELETE & vbTab & L_DESC_DELETE
  400.     WScript.Echo vbTab & vbTab & L_OP_GET & vbTab & L_DESC_GET
  401.     WScript.Echo vbTab & vbTab & L_OP_SET & vbTab & L_DESC_SET
  402.     WScript.Echo vbTab & vbTab & L_OP_ENUMERATE & vbTab & L_DESC_ENUMERATE
  403.     WScript.Echo vbTab & L_SWITCH_SERVER & " " & L_DESC_SERVER
  404.     WScript.Echo vbTab & L_SWITCH_INSTANCE_ID & " " & L_DESC_INSTANCE_ID
  405.     WScript.Echo vbTab & L_SWITCH_EXPIRE_ID & " " & L_DESC_EXPIRE_ID
  406.     WScript.Echo vbTab & L_SWITCH_TIME & " " & L_DESC_TIME
  407.     WScript.Echo vbTab & L_SWITCH_NEWSGROUPS & " " & L_DESC_NEWSGROUPS
  408.     WScript.Echo vbTab & L_SWITCH_NAME & " " & L_DESC_NAME
  409.     WScript.Echo vbTab & L_SWITCH_ONETIME & " " & L_DESC_ONETIME
  410.  
  411.     WScript.Echo
  412.     WScript.Echo L_DESC_EXAMPLES
  413.     WScript.Echo L_DESC_EXAMPLE1
  414.     WScript.Echo L_DESC_EXAMPLE2
  415.     WScript.Echo L_DESC_EXAMPLE3
  416.     WScript.Echo L_DESC_EXAMPLE4
  417.  
  418. end sub
  419.  
  420. Sub PrintExpire ( admobj )
  421.  
  422.     WScript.Echo L_STR_EXPIRE_ID & " " & admobj.ExpireId
  423.     WScript.Echo L_STR_EXPIRE_NAME & " " & admobj.PolicyName
  424.     WScript.Echo L_STR_EXPIRE_TIME & " " & admobj.ExpireTime
  425.  
  426.     dim newsgroups
  427.     dim    cGroups
  428.     dim i
  429.  
  430.     newsgroups = admobj.NewsgroupsVariant
  431.  
  432.     cGroups = UBound ( newsgroups )
  433.  
  434.     for i = 0 to cGroups
  435.         WScript.Echo L_STR_NEWSGROUPS & " " & newsgroups(i)
  436.     next
  437.  
  438. end sub
  439.  
  440.