You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gencapdefs.py 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #!/usr/bin/env python3
  2. """
  3. Updates the capability definitions at irc/caps/defs.go
  4. To add a capability, add it to the CAPDEFS list below,
  5. then run `make capdefs` from the project root.
  6. """
  7. import io
  8. import subprocess
  9. import sys
  10. from collections import namedtuple
  11. CapDef = namedtuple("CapDef", ['identifier', 'name', 'url', 'standard'])
  12. CAPDEFS = [
  13. CapDef(
  14. identifier="AccountNotify",
  15. name="account-notify",
  16. url="https://ircv3.net/specs/extensions/account-notify-3.1.html",
  17. standard="IRCv3",
  18. ),
  19. CapDef(
  20. identifier="AccountTag",
  21. name="account-tag",
  22. url="https://ircv3.net/specs/extensions/account-tag-3.2.html",
  23. standard="IRCv3",
  24. ),
  25. CapDef(
  26. identifier="AwayNotify",
  27. name="away-notify",
  28. url="https://ircv3.net/specs/extensions/away-notify-3.1.html",
  29. standard="IRCv3",
  30. ),
  31. CapDef(
  32. identifier="Batch",
  33. name="batch",
  34. url="https://ircv3.net/specs/extensions/batch-3.2.html",
  35. standard="IRCv3",
  36. ),
  37. CapDef(
  38. identifier="CapNotify",
  39. name="cap-notify",
  40. url="https://ircv3.net/specs/extensions/cap-notify-3.2.html",
  41. standard="IRCv3",
  42. ),
  43. CapDef(
  44. identifier="ChgHost",
  45. name="chghost",
  46. url="https://ircv3.net/specs/extensions/chghost-3.2.html",
  47. standard="IRCv3",
  48. ),
  49. CapDef(
  50. identifier="EchoMessage",
  51. name="echo-message",
  52. url="https://ircv3.net/specs/extensions/echo-message-3.2.html",
  53. standard="IRCv3",
  54. ),
  55. CapDef(
  56. identifier="ExtendedJoin",
  57. name="extended-join",
  58. url="https://ircv3.net/specs/extensions/extended-join-3.1.html",
  59. standard="IRCv3",
  60. ),
  61. CapDef(
  62. identifier="ExtendedMonitor",
  63. name="extended-monitor",
  64. url="https://ircv3.net/specs/extensions/extended-monitor.html",
  65. standard="IRCv3",
  66. ),
  67. CapDef(
  68. identifier="InviteNotify",
  69. name="invite-notify",
  70. url="https://ircv3.net/specs/extensions/invite-notify-3.2.html",
  71. standard="IRCv3",
  72. ),
  73. CapDef(
  74. identifier="LabeledResponse",
  75. name="labeled-response",
  76. url="https://ircv3.net/specs/extensions/labeled-response.html",
  77. standard="IRCv3",
  78. ),
  79. CapDef(
  80. identifier="Languages",
  81. name="draft/languages",
  82. url="https://gist.github.com/DanielOaks/8126122f74b26012a3de37db80e4e0c6",
  83. standard="proposed IRCv3",
  84. ),
  85. CapDef(
  86. identifier="MessageRedaction",
  87. name="draft/message-redaction",
  88. url="https://github.com/progval/ircv3-specifications/blob/redaction/extensions/message-redaction.md",
  89. standard="proposed IRCv3",
  90. ),
  91. CapDef(
  92. identifier="MessageTags",
  93. name="message-tags",
  94. url="https://ircv3.net/specs/extensions/message-tags.html",
  95. standard="IRCv3",
  96. ),
  97. CapDef(
  98. identifier="MultiPrefix",
  99. name="multi-prefix",
  100. url="https://ircv3.net/specs/extensions/multi-prefix-3.1.html",
  101. standard="IRCv3",
  102. ),
  103. CapDef(
  104. identifier="Relaymsg",
  105. name="draft/relaymsg",
  106. url="https://github.com/ircv3/ircv3-specifications/pull/417",
  107. standard="proposed IRCv3",
  108. ),
  109. CapDef(
  110. identifier="ChannelRename",
  111. name="draft/channel-rename",
  112. url="https://ircv3.net/specs/extensions/channel-rename",
  113. standard="draft IRCv3",
  114. ),
  115. CapDef(
  116. identifier="SASL",
  117. name="sasl",
  118. url="https://ircv3.net/specs/extensions/sasl-3.2.html",
  119. standard="IRCv3",
  120. ),
  121. CapDef(
  122. identifier="ServerTime",
  123. name="server-time",
  124. url="https://ircv3.net/specs/extensions/server-time-3.2.html",
  125. standard="IRCv3",
  126. ),
  127. CapDef(
  128. identifier="SetName",
  129. name="setname",
  130. url="https://ircv3.net/specs/extensions/setname.html",
  131. standard="IRCv3",
  132. ),
  133. CapDef(
  134. identifier="STS",
  135. name="sts",
  136. url="https://ircv3.net/specs/extensions/sts.html",
  137. standard="IRCv3",
  138. ),
  139. CapDef(
  140. identifier="UserhostInNames",
  141. name="userhost-in-names",
  142. url="https://ircv3.net/specs/extensions/userhost-in-names-3.2.html",
  143. standard="IRCv3",
  144. ),
  145. CapDef(
  146. identifier="ZNCSelfMessage",
  147. name="znc.in/self-message",
  148. url="https://wiki.znc.in/Query_buffers",
  149. standard="ZNC vendor",
  150. ),
  151. CapDef(
  152. identifier="EventPlayback",
  153. name="draft/event-playback",
  154. url="https://github.com/ircv3/ircv3-specifications/pull/362",
  155. standard="proposed IRCv3",
  156. ),
  157. CapDef(
  158. identifier="ZNCPlayback",
  159. name="znc.in/playback",
  160. url="https://wiki.znc.in/Playback",
  161. standard="ZNC vendor",
  162. ),
  163. CapDef(
  164. identifier="Nope",
  165. name="ergo.chat/nope",
  166. url="https://ergo.chat/nope",
  167. standard="Ergo vendor",
  168. ),
  169. CapDef(
  170. identifier="Multiline",
  171. name="draft/multiline",
  172. url="https://github.com/ircv3/ircv3-specifications/pull/398",
  173. standard="proposed IRCv3",
  174. ),
  175. CapDef(
  176. identifier="Chathistory",
  177. name="draft/chathistory",
  178. url="https://github.com/ircv3/ircv3-specifications/pull/393",
  179. standard="proposed IRCv3",
  180. ),
  181. CapDef(
  182. identifier="AccountRegistration",
  183. name="draft/account-registration",
  184. url="https://github.com/ircv3/ircv3-specifications/pull/435",
  185. standard="draft IRCv3",
  186. ),
  187. CapDef(
  188. identifier="ReadMarker",
  189. name="draft/read-marker",
  190. url="https://github.com/ircv3/ircv3-specifications/pull/489",
  191. standard="draft IRCv3",
  192. ),
  193. CapDef(
  194. identifier="Persistence",
  195. name="draft/persistence",
  196. url="https://github.com/ircv3/ircv3-specifications/pull/503",
  197. standard="proposed IRCv3",
  198. ),
  199. CapDef(
  200. identifier="Preaway",
  201. name="draft/pre-away",
  202. url="https://github.com/ircv3/ircv3-specifications/pull/514",
  203. standard="proposed IRCv3",
  204. ),
  205. CapDef(
  206. identifier="StandardReplies",
  207. name="standard-replies",
  208. url="https://github.com/ircv3/ircv3-specifications/pull/506",
  209. standard="IRCv3",
  210. ),
  211. CapDef(
  212. identifier="NoImplicitNames",
  213. name="draft/no-implicit-names",
  214. url="https://github.com/ircv3/ircv3-specifications/pull/527",
  215. standard="proposed IRCv3",
  216. ),
  217. CapDef(
  218. identifier="Bearer",
  219. name="draft/bearer",
  220. url="https://gist.github.com/slingamn/4fabc7a3d5f335da7bb313a7f0648f37",
  221. standard="proposed IRCv3",
  222. ),
  223. ]
  224. def validate_defs():
  225. CAPDEFS.sort(key=lambda d: d.name)
  226. numCaps = len(CAPDEFS)
  227. numNames = len(set(capdef.name for capdef in CAPDEFS))
  228. if numCaps != numNames:
  229. raise Exception("defs must have unique names, but found duplicates")
  230. numIdentifiers = len(set(capdef.identifier for capdef in CAPDEFS))
  231. if numCaps != numIdentifiers:
  232. raise Exception("defs must have unique identifiers, but found duplicates")
  233. def main():
  234. validate_defs()
  235. output = io.StringIO()
  236. print("""
  237. package caps
  238. /*
  239. WARNING: this file is autogenerated by `make capdefs`
  240. DO NOT EDIT MANUALLY.
  241. */
  242. """, file=output)
  243. numCapabs = len(CAPDEFS)
  244. bitsetLen = numCapabs // 32
  245. if numCapabs % 32 > 0:
  246. bitsetLen += 1
  247. print ("""
  248. const (
  249. // number of recognized capabilities:
  250. numCapabs = %d
  251. // length of the uint32 array that represents the bitset:
  252. bitsetLen = %d
  253. )
  254. """ % (numCapabs, bitsetLen), file=output)
  255. print("const (", file=output)
  256. for capdef in CAPDEFS:
  257. print("// %s is the %s capability named \"%s\":" % (capdef.identifier, capdef.standard, capdef.name), file=output)
  258. print("// %s" % (capdef.url,), file=output)
  259. print("%s Capability = iota" % (capdef.identifier,), file=output)
  260. print(file=output)
  261. print(")", file=output)
  262. print("// `capabilityNames[capab]` is the string name of the capability `capab`", file=output)
  263. print("""var ( capabilityNames = [numCapabs]string{""", file=output)
  264. for capdef in CAPDEFS:
  265. print("\"%s\"," % (capdef.name,), file=output)
  266. print("})", file=output)
  267. # run the generated code through `gofmt -s`, which will print it to stdout
  268. gofmt = subprocess.Popen(['gofmt', '-s'], stdin=subprocess.PIPE)
  269. gofmt.communicate(input=output.getvalue().encode('utf-8'))
  270. if gofmt.poll() != 0:
  271. print(output.getvalue())
  272. raise Exception("gofmt failed")
  273. return 0
  274. if __name__ == '__main__':
  275. sys.exit(main())