NNTPReg.Vbs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. 'THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT
  2. 'WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
  3. 'INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
  4. 'OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  5. 'PURPOSE
  6. '------------------------------------------------------------------------------
  7. 'FILE DESCRIPTION: Script for registering for NNTP Protocol sinks.
  8. '
  9. 'File Name: nntpreg.vbs
  10. '
  11. '
  12. ' Copyright (c) Microsoft Corporation 1993-1999. All rights reserved.
  13. '------------------------------------------------------------------------------
  14. Option Explicit
  15. '
  16. ' these GUIDs are all defined in nntpfilt.idl
  17. '
  18. ' the OnPostEarly event GUID
  19. Const GUIDComCatOnPostEarly = "{C028FD86-F943-11d0-85BD-00C04FB960EA}"
  20. ' the OnPost event GUID
  21. Const GUIDComCatOnPost = "{C028FD83-F943-11d0-85BD-00C04FB960EA}"
  22. ' the OnPostFinal event GUID
  23. Const GUIDComCatOnPostFinal = "{C028FD85-F943-11d0-85BD-00C04FB960EA}"
  24. ' the NNTP source type
  25. Const GUIDSourceType = "{C028FD82-F943-11d0-85BD-00C04FB960EA}"
  26. ' the NNTP service display name. This is used to key which service to
  27. ' edit
  28. Const szService = "nntpsvc"
  29. ' the event manager object. This is used to communicate with the
  30. ' event binding database.
  31. Dim EventManager
  32. Set EventManager = WScript.CreateObject("Event.Manager")
  33. '
  34. ' register a new sink with event manager
  35. '
  36. ' iInstance - the instance to work against
  37. ' szEvent - OnPostEarly, OnPost or OnPostFinal
  38. ' szDisplayName - the display name for this new sink
  39. ' szProgID - the progid to call for this event
  40. ' szRule - the rule to set for this event
  41. '
  42. public sub RegisterSink(iInstance, szEvent, szDisplayName, szProgID, szRule)
  43. Dim SourceType
  44. Dim szSourceDisplayName
  45. Dim Source
  46. Dim Binding
  47. Dim GUIDComCat
  48. Dim PrioVal
  49. ' figure out which event they are trying to register with and set
  50. ' the comcat for this event in GUIDComCat
  51. select case LCase(szEvent)
  52. case "onpostearly"
  53. GUIDComCat = GUIDComCatOnPostEarly
  54. case "onpost"
  55. GUIDComCat = GUIDComCatOnPost
  56. case "onpostfinal"
  57. GUIDComCat = GUIDComCatOnPostFinal
  58. case else
  59. WScript.echo "invalid event: " & szEvent
  60. exit sub
  61. end select
  62. ' enumerate through each of the registered instances for the NNTP source
  63. ' type and look for the display name that matches the instance display
  64. ' name
  65. set SourceType = EventManager.SourceTypes(GUIDSourceType)
  66. szSourceDisplayName = szService & " " & iInstance
  67. for each Source in SourceType.Sources
  68. if Source.DisplayName = szSourceDisplayName then
  69. ' we've found the desired instance. now add a new binding
  70. ' with the right event GUID. by not specifying a GUID to the
  71. ' Add method we get server events to create a new ID for this
  72. ' event
  73. set Binding = Source.GetBindingManager.Bindings(GUIDComCat).Add("")
  74. ' set the binding properties
  75. Binding.DisplayName = szDisplayName
  76. Binding.SinkClass = szProgID
  77. ' register a rule with the binding
  78. Binding.SourceProperties.Add "Rule", szRule
  79. ' register a priority with the binding
  80. PrioVal = GetNextPrio(Source, GUIDComCat)
  81. If PrioVal < 0 then
  82. WScript.Echo "assigning priority to default value (24575)"
  83. Binding.SourceProperties.Add "Priority", 24575
  84. else
  85. WScript.Echo "assigning priority (" & PrioVal & " of 32767)"
  86. Binding.SourceProperties.Add "Priority", PrioVal
  87. end if
  88. ' save the binding
  89. Binding.Save
  90. WScript.Echo "registered " & szDisplayName
  91. exit sub
  92. end if
  93. next
  94. end sub
  95. '
  96. ' iterate through the bindings in a source, find the binding
  97. ' with the lowest priority, and return the next priority value.
  98. ' If the next value exceeds the range, return -1.
  99. '
  100. public function GetNextPrio(oSource, GUIDComCat)
  101. ' it's possible that priority values will not be
  102. ' numbers, so we add error handling for this case
  103. on error resume next
  104. Dim Bindings
  105. Dim Binding
  106. Dim nLowestPrio
  107. Dim nPrioVal
  108. nLowestPrio = 0
  109. set Bindings = oSource.GetBindingManager.Bindings(GUIDComCat)
  110. ' if the bindings collection is empty, then this is the first
  111. ' sink. It gets the highest priority (0).
  112. if Bindings.Count = 0 then
  113. GetNextPrio = 0
  114. else
  115. ' get the lowest existing priority value
  116. for each Binding in Bindings
  117. nPrioVal = Binding.SourceProperties.Item("Priority")
  118. if CInt(nPrioVal) > nLowestPrio then
  119. if err.number = 13 then
  120. err.clear
  121. else
  122. nLowestPrio = CInt(nPrioVal)
  123. end if
  124. end if
  125. next
  126. ' assign priority values in increments of 10 so priorities
  127. ' can be shuffled later without the need to reorder all
  128. ' binding priorities. Valid priority values are 0 - 32767
  129. if nLowestPrio + 10 > 32767 then
  130. GetNextPrio = -1
  131. else
  132. GetNextPrio = nLowestPrio + 10
  133. end if
  134. end if
  135. end function
  136. '
  137. ' unregister a previously registered sink
  138. '
  139. ' iInstance - the instance to work against
  140. ' szEvent - OnPostEarly, OnPost or OnPostFinal
  141. ' szDisplayName - the display name of the event to remove
  142. '
  143. public sub UnregisterSink(iInstance, szEvent, szDisplayName)
  144. Dim SourceType
  145. Dim GUIDComCat
  146. Dim szSourceDisplayName
  147. Dim Source
  148. Dim Bindings
  149. Dim Binding
  150. select case LCase(szEvent)
  151. case "onpostearly"
  152. GUIDComCat = GUIDComCatOnPostEarly
  153. case "onpost"
  154. GUIDComCat = GUIDComCatOnPost
  155. case "onpostfinal"
  156. GUIDComCat = GUIDComCatOnPostFinal
  157. case else
  158. WScript.echo "invalid event: " & szEvent
  159. exit sub
  160. end select
  161. ' find the source for this instance
  162. set SourceType = EventManager.SourceTypes(GUIDSourceType)
  163. szSourceDisplayName = szService & " " & iInstance
  164. for each Source in SourceType.Sources
  165. if Source.DisplayName = szSourceDisplayName then
  166. ' find the binding by display name. to do this we enumerate
  167. ' all of the bindings and try to match on the display name
  168. set Bindings = Source.GetBindingManager.Bindings(GUIDComCat)
  169. for each Binding in Bindings
  170. if Binding.DisplayName = szDisplayName then
  171. ' we've found the binding, now remove it
  172. Bindings.Remove(Binding.ID)
  173. WScript.Echo "removed " & szDisplayName & " " & Binding.ID
  174. end if
  175. next
  176. end if
  177. next
  178. end sub
  179. '
  180. ' add or remove a property from the source or sink propertybag for an event
  181. '
  182. ' iInstance - the NNTP instance to edit
  183. ' szEvent - the event type (OnPostEarly, OnPost or OnPostFinal)
  184. ' szDisplayName - the display name of the event
  185. ' szPropertyBag - the property bag to edit ("source" or "sink")
  186. ' szOperation - "add" or "remove"
  187. ' szPropertyName - the name to edit in the property bag
  188. ' szPropertyValue - the value to assign to the name (ignored for remove)
  189. '
  190. public sub EditProperty(iInstance, szEvent, szDisplayName, szPropertyBag, szOperation, szPropertyName, szPropertyValue)
  191. Dim SourceType
  192. Dim GUIDComCat
  193. Dim szSourceDisplayName
  194. Dim Source
  195. Dim Bindings
  196. Dim Binding
  197. Dim PropertyBag
  198. select case LCase(szEvent)
  199. case "onpostearly"
  200. GUIDComCat = GUIDComCatOnPostEarly
  201. case "onpost"
  202. GUIDComCat = GUIDComCatOnPost
  203. case "onpostfinal"
  204. GUIDComCat = GUIDComCatOnPostFinal
  205. case else
  206. WScript.echo "invalid event: " & szEvent
  207. exit sub
  208. end select
  209. ' find the source for this instance
  210. set SourceType = EventManager.SourceTypes(GUIDSourceType)
  211. szSourceDisplayName = szService & " " & iInstance
  212. for each Source in SourceType.Sources
  213. if Source.DisplayName = szSourceDisplayName then
  214. set Bindings = Source.GetBindingManager.Bindings(GUIDComCat)
  215. ' find the binding by display name. to do this we enumerate
  216. ' all of the bindings and try to match on the display name
  217. for each Binding in Bindings
  218. if Binding.DisplayName = szDisplayName then
  219. ' figure out which set of properties we want to modify
  220. ' based on the szPropertyBag parameter
  221. select case LCase(szPropertyBag)
  222. case "source"
  223. set PropertyBag = Binding.SourceProperties
  224. case "sink"
  225. set PropertyBag = Binding.SinkProperties
  226. case else
  227. WScript.echo "invalid propertybag: " & szPropertyBag
  228. exit sub
  229. end select
  230. ' figure out what operation we want to perform
  231. select case LCase(szOperation)
  232. case "remove"
  233. ' they want to remove szPropertyName from the
  234. ' property bag
  235. PropertyBag.Remove szPropertyName
  236. WScript.echo "removed property " & szPropertyName
  237. case "add"
  238. ' add szPropertyName to the property bag and
  239. ' set its value to szValue. if this value
  240. ' already exists then this will change the value
  241. ' it to szValue.
  242. PropertyBag.Add szPropertyName, szPropertyValue
  243. WScript.echo "set property " & szPropertyName & " to " & szPropertyValue
  244. case else
  245. WScript.echo "invalid operation: " & szOperation
  246. exit sub
  247. end select
  248. ' save the binding
  249. Binding.Save
  250. end if
  251. next
  252. end if
  253. next
  254. end sub
  255. '
  256. ' this helper function takes an IEventSource object and a event category
  257. ' and dumps all of the bindings for this category under the source
  258. '
  259. ' Source - the IEventSource object to display the bindings for
  260. ' GUIDComCat - the event category to display the bindings for
  261. '
  262. public sub DisplaySinksHelper(Source, GUIDComCat)
  263. Dim Binding
  264. Dim propval
  265. ' walk each of the registered bindings for this component category
  266. for each Binding in Source.GetBindingManager.Bindings(GUIDComCat)
  267. ' display the binding properties
  268. WScript.echo " Binding " & Binding.ID & " {"
  269. WScript.echo " DisplayName = " & Binding.DisplayName
  270. WScript.echo " SinkClass = " & Binding.SinkClass
  271. if Binding.Enabled = True then
  272. WScript.echo " Status = Enabled"
  273. else
  274. WScript.echo " Status = Disabled"
  275. end if
  276. ' walk each of the source properties and display them
  277. WScript.echo " SourceProperties {"
  278. for each propval in Binding.SourceProperties
  279. WScript.echo " " & propval & " = " & Binding.SourceProperties.Item(propval)
  280. next
  281. WScript.echo " }"
  282. ' walk each of the sink properties and display them
  283. WScript.echo " SinkProperties {"
  284. for each propval in Binding.SinkProperties
  285. WScript.echo " " & propval & " = " & Binding.SinkProperties.Item(propval)
  286. next
  287. WScript.echo " }"
  288. WScript.echo " }"
  289. next
  290. end sub
  291. ' check for a previously registered sink with the passed in name
  292. '
  293. ' iInstance - the instance to work against
  294. ' szEvent - OnMessage
  295. ' szDisplayName - the display name of the event to check
  296. ' bCheckError - Any errors returned
  297. public sub CheckSink(iInstance, szEvent, szDisplayName, bCheckError)
  298. Dim SourceType
  299. Dim GUIDComCat
  300. Dim szSourceDisplayName
  301. Dim Source
  302. Dim Bindings
  303. Dim Binding
  304. bCheckError = FALSE
  305. select case LCase(szEvent)
  306. case "onpostearly"
  307. GUIDComCat = GUIDComCatOnPostEarly
  308. case "onpost"
  309. GUIDComCat = GUIDComCatOnPost
  310. case "onpostfinal"
  311. GUIDComCat = GUIDComCatOnPostFinal
  312. case else
  313. WScript.echo "invalid event: " & szEvent
  314. exit sub
  315. end select
  316. ' find the source for this instance
  317. set SourceType = EventManager.SourceTypes(GUIDSourceType)
  318. szSourceDisplayName = szService & " " & iInstance
  319. for each Source in SourceType.Sources
  320. if Source.DisplayName = szSourceDisplayName then
  321. ' find the binding by display name. to do this we enumerate
  322. ' all of the bindings and try to match on the display name
  323. set Bindings = Source.GetBindingManager.Bindings(GUIDComCat)
  324. for each Binding in Bindings
  325. if Binding.DisplayName = szDisplayName then
  326. ' we've found the binding, now log an error
  327. WScript.Echo "Binding with the name " & szDisplayName & " already exists"
  328. exit sub
  329. end if
  330. next
  331. end if
  332. next
  333. bCheckError = TRUE
  334. end sub
  335. '
  336. '
  337. ' dumps all of the information in the binding database related to NNTP
  338. '
  339. public sub DisplaySinks
  340. Dim SourceType
  341. Dim Source
  342. ' look for each of the sources registered for the NNTP source type
  343. set SourceType = EventManager.SourceTypes(GUIDSourceType)
  344. for each Source in SourceType.Sources
  345. ' display the source properties
  346. WScript.echo "Source " & Source.ID & " {"
  347. WScript.echo " DisplayName = " & Source.DisplayName
  348. ' display all of the sinks registered for the OnPostEarly event
  349. WScript.echo " OnPostEarly Sinks {"
  350. call DisplaySinksHelper(Source, GUIDComCatOnPostEarly)
  351. WScript.echo " }"
  352. ' display all of the sinks registered for the OnPost event
  353. WScript.echo " OnPost Sinks {"
  354. call DisplaySinksHelper(Source, GUIDComCatOnPost)
  355. WScript.echo " }"
  356. ' display all of the sinks registered for the OnPostFinal event
  357. WScript.echo " OnPostFinal Sinks {"
  358. call DisplaySinksHelper(Source, GUIDComCatOnPostFinal)
  359. WScript.echo " }"
  360. WScript.echo "}"
  361. next
  362. end sub
  363. '
  364. ' enable/disable a registered sink
  365. '
  366. ' iInstance - the instance to work against
  367. ' szEvent - OnArrival
  368. ' szDisplayName - the display name for this new sink
  369. '
  370. public sub SetSinkEnabled(iInstance, szEvent, szDisplayName, szEnable)
  371. Dim SourceType
  372. Dim GUIDComCat
  373. Dim szSourceDisplayName
  374. Dim Source
  375. Dim Bindings
  376. Dim Binding
  377. select case LCase(szEvent)
  378. case "onpostearly"
  379. GUIDComCat = GUIDComCatOnPostEarly
  380. case "onpost"
  381. GUIDComCat = GUIDComCatOnPost
  382. case "onpostfinal"
  383. GUIDComCat = GUIDComCatOnPostFinal
  384. case else
  385. WScript.echo "invalid event: " + szEvent
  386. exit sub
  387. end select
  388. ' find the source for this instance
  389. set SourceType = EventManager.SourceTypes(GUIDSourceType)
  390. szSourceDisplayName = szService + " " + iInstance
  391. for each Source in SourceType.Sources
  392. if Source.DisplayName = szSourceDisplayName then
  393. ' find the binding by display name. to do this we enumerate
  394. ' all of the bindings and try to match on the display name
  395. set Bindings = Source.GetBindingManager.Bindings(GUIDComCat)
  396. for each Binding in Bindings
  397. if Binding.DisplayName = szDisplayName then
  398. ' we've found the binding, now enable/disable it
  399. ' we don't need "case else' because szEnable's value
  400. ' is set internally, not by users
  401. select case LCase(szEnable)
  402. case "true"
  403. Binding.Enabled = True
  404. Binding.Save
  405. WScript.Echo "enabled " + szDisplayName + " " + Binding.ID
  406. case "false"
  407. Binding.Enabled = False
  408. Binding.Save
  409. WScript.Echo "disabled " + szDisplayName + " " + Binding.ID
  410. end select
  411. end if
  412. next
  413. end if
  414. next
  415. end sub
  416. '
  417. ' display usage information for this script
  418. '
  419. public sub DisplayUsage
  420. WScript.echo "usage: cscript nntpreg.vbs <command> <arguments>"
  421. WScript.echo " commands:"
  422. WScript.echo " /add <Instance> <Event> <DisplayName> <SinkClass> <Rule>"
  423. WScript.echo " /remove <Instance> <Event> <DisplayName>"
  424. WScript.echo " /setprop <Instance> <Event> <DisplayName> <PropertyBag> <PropertyName> "
  425. WScript.echo " <PropertyValue>"
  426. WScript.echo " /delprop <Instance> <Event> <DisplayName> <PropertyBag> <PropertyName>"
  427. WScript.echo " /enable <Instance> <Event> <DisplayName>"
  428. WScript.echo " /disable <Instance> <Event> <DisplayName>"
  429. WScript.echo " /enum"
  430. WScript.echo " arguments:"
  431. WScript.echo " <Instance> is the NNTP instance to work against"
  432. WScript.echo " <Event> can be OnPostEarly, OnPost or OnPostFinal"
  433. WScript.echo " <DisplayName> is the display name of the event to edit"
  434. WScript.echo " <SinkClass> is the sink class for the event"
  435. WScript.echo " <Rule> is the rule to use for the event"
  436. WScript.echo " <PropertyBag> can be Source or Sink"
  437. WScript.echo " <PropertyName> is the name of the property to edit"
  438. WScript.echo " <PropertyValue> is the value to assign to the property"
  439. end sub
  440. Dim iInstance
  441. Dim szEvent
  442. Dim szDisplayName
  443. Dim szSinkClass
  444. Dim szRule
  445. Dim szPropertyBag
  446. Dim szPropertyName
  447. Dim szPropertyValue
  448. Dim bCheck
  449. '
  450. ' this is the main body of our script. it reads the command line parameters
  451. ' specified and then calls the appropriate function to perform the operation
  452. '
  453. if WScript.Arguments.Count = 0 then
  454. call DisplayUsage
  455. else
  456. Select Case LCase(WScript.Arguments(0))
  457. Case "/add"
  458. if not WScript.Arguments.Count = 6 then
  459. call DisplayUsage
  460. else
  461. iInstance = WScript.Arguments(1)
  462. szEvent = WScript.Arguments(2)
  463. szDisplayName = WScript.Arguments(3)
  464. szSinkClass = WScript.Arguments(4)
  465. szRule = WScript.Arguments(5)
  466. call CheckSink(iInstance, szEvent, szDisplayName, bCheck)
  467. if bCheck = TRUE then
  468. call RegisterSink(iInstance, szEvent, szDisplayName, szSinkClass, szRule)
  469. End if
  470. end if
  471. Case "/remove"
  472. if not WScript.Arguments.Count = 4 then
  473. call DisplayUsage
  474. else
  475. iInstance = WScript.Arguments(1)
  476. szEvent = WScript.Arguments(2)
  477. szDisplayName = WScript.Arguments(3)
  478. call UnregisterSink(iInstance, szEvent, szDisplayName)
  479. end if
  480. Case "/setprop"
  481. if not WScript.Arguments.Count = 7 then
  482. call DisplayUsage
  483. else
  484. iInstance = WScript.Arguments(1)
  485. szEvent = WScript.Arguments(2)
  486. szDisplayName = WScript.Arguments(3)
  487. szPropertyBag = WScript.Arguments(4)
  488. szPropertyName = WScript.Arguments(5)
  489. szPropertyValue = WScript.Arguments(6)
  490. call EditProperty(iInstance, szEvent, szDisplayName, szPropertyBag, "add", szPropertyName, szPropertyValue)
  491. end if
  492. Case "/delprop"
  493. if not WScript.Arguments.Count = 6 then
  494. call DisplayUsage
  495. else
  496. iInstance = WScript.Arguments(1)
  497. szEvent = WScript.Arguments(2)
  498. szDisplayName = WScript.Arguments(3)
  499. szPropertyBag = WScript.Arguments(4)
  500. szPropertyName = WScript.Arguments(5)
  501. call EditProperty(iInstance, szEvent, szDisplayName, szPropertyBag, "remove", szPropertyName, "")
  502. end if
  503. Case "/enable"
  504. if not WScript.Arguments.Count = 4 then
  505. call DisplayUsage
  506. else
  507. iInstance = WScript.Arguments(1)
  508. szEvent = WScript.Arguments(2)
  509. szDisplayName = WScript.Arguments(3)
  510. call SetSinkEnabled(iInstance, szEvent, szDisplayName, "True")
  511. end if
  512. Case "/disable"
  513. if not WScript.Arguments.Count = 4 then
  514. call DisplayUsage
  515. else
  516. iInstance = WScript.Arguments(1)
  517. szEvent = WScript.Arguments(2)
  518. szDisplayName = WScript.Arguments(3)
  519. call SetSinkEnabled(iInstance, szEvent, szDisplayName, "False")
  520. end if
  521. Case "/enum"
  522. if not WScript.Arguments.Count = 1 then
  523. call DisplayUsage
  524. else
  525. call DisplaySinks
  526. end if
  527. Case Else
  528. call DisplayUsage
  529. End Select
  530. end if