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.

help.go 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. // Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "fmt"
  6. "sort"
  7. "strings"
  8. "sync"
  9. "github.com/oragono/oragono/irc/languages"
  10. )
  11. // HelpEntryType represents the different sorts of help entries that can exist.
  12. type HelpEntryType int
  13. const (
  14. // CommandHelpEntry is a help entry explaining a client command.
  15. CommandHelpEntry HelpEntryType = iota
  16. // InformationHelpEntry is a help entry explaining general server info.
  17. InformationHelpEntry
  18. // ISupportHelpEntry is a help entry explaining a specific RPL_ISUPPORT token.
  19. ISupportHelpEntry
  20. )
  21. // HelpEntry represents an entry in the Help map.
  22. type HelpEntry struct {
  23. oper bool
  24. text string
  25. textGenerator func(*Client) string
  26. helpType HelpEntryType
  27. duplicate bool
  28. }
  29. // used for duplicates
  30. var (
  31. cmodeHelpText = `== Channel Modes ==
  32. Oragono supports the following channel modes:
  33. +b | Client masks that are banned from the channel (e.g. *!*@127.0.0.1)
  34. +e | Client masks that are exempted from bans.
  35. +I | Client masks that are exempted from the invite-only flag.
  36. +i | Invite-only mode, only invited clients can join the channel.
  37. +k | Key required when joining the channel.
  38. +l | Client join limit for the channel.
  39. +m | Moderated mode, only privileged clients can talk on the channel.
  40. +n | No-outside-messages mode, only users that are on the channel can send
  41. | messages to it.
  42. +R | Only registered users can join the channel.
  43. +M | Only registered or voiced users can speak in the channel.
  44. +s | Secret mode, channel won't show up in /LIST or whois replies.
  45. +t | Only channel opers can modify the topic.
  46. +E | Roleplaying commands are enabled in the channel.
  47. +C | Clients are blocked from sending CTCP messages in the channel.
  48. +u | Auditorium mode: JOIN, PART, QUIT, NAMES, and WHO are hidden
  49. hidden from unvoiced clients.
  50. +U | Op-moderated mode: messages from unprivileged clients are sent
  51. only to channel operators.
  52. = Prefixes =
  53. +q (~) | Founder channel mode.
  54. +a (&) | Admin channel mode.
  55. +o (@) | Operator channel mode.
  56. +h (%) | Halfop channel mode.
  57. +v (+) | Voice channel mode.`
  58. umodeHelpText = `== User Modes ==
  59. Oragono supports the following user modes:
  60. +a | User is marked as being away. This mode is set with the /AWAY command.
  61. +i | User is marked as invisible (their channels are hidden from whois replies).
  62. +o | User is an IRC operator.
  63. +R | User only accepts messages from other registered users.
  64. +s | Server Notice Masks (see help with /HELPOP snomasks).
  65. +Z | User is connected via TLS.
  66. +B | User is a bot.
  67. +E | User can receive roleplaying commands.
  68. +T | CTCP messages to the user are blocked.`
  69. snomaskHelpText = `== Server Notice Masks ==
  70. Oragono supports the following server notice masks for operators:
  71. a | Local announcements.
  72. c | Local client connections.
  73. j | Local channel actions.
  74. k | Local kills.
  75. n | Local nick changes.
  76. o | Local oper actions.
  77. q | Local quits.
  78. t | Local /STATS usage.
  79. u | Local client account actions.
  80. x | Local X-lines (DLINE/KLINE/etc).
  81. v | Local vhost changes.
  82. To set a snomask, do this with your nickname:
  83. /MODE <nick> +s <chars>
  84. For instance, this would set the kill, oper, account and xline snomasks on dan:
  85. /MODE dan +s koux`
  86. )
  87. // Help contains the help strings distributed with the IRCd.
  88. var Help = map[string]HelpEntry{
  89. // Commands
  90. "acc": {
  91. text: `ACC LS
  92. ACC REGISTER <accountname> [callback_namespace:]<callback> [cred_type] :<credential>
  93. ACC VERIFY <accountname> <auth_code>
  94. Used in account registration. See the relevant specs for more info:
  95. https://oragono.io/specs.html`,
  96. },
  97. "ambiance": {
  98. text: `AMBIANCE <target> <text to be sent>
  99. The AMBIANCE command is used to send a scene notification to the given target.`,
  100. },
  101. "authenticate": {
  102. text: `AUTHENTICATE
  103. Used during SASL authentication. See the IRCv3 specs for more info:
  104. http://ircv3.net/specs/extensions/sasl-3.1.html`,
  105. },
  106. "away": {
  107. text: `AWAY [message]
  108. If [message] is sent, marks you away. If [message] is not sent, marks you no
  109. longer away.`,
  110. },
  111. "batch": {
  112. text: `BATCH {+,-}reference-tag type [params...]
  113. BATCH initiates an IRCv3 client-to-server batch. You should never need to
  114. issue this command manually.`,
  115. },
  116. "brb": {
  117. text: `BRB [message]
  118. Disconnects you from the server, while instructing the server to keep you
  119. present for a short time window. During this window, you can either resume
  120. or reattach to your nickname. If [message] is sent, it is used as your away
  121. message (and as your quit message if you don't return in time).`,
  122. },
  123. "cap": {
  124. text: `CAP <subcommand> [:<capabilities>]
  125. Used in capability negotiation. See the IRCv3 specs for more info:
  126. http://ircv3.net/specs/core/capability-negotiation-3.1.html
  127. http://ircv3.net/specs/core/capability-negotiation-3.2.html`,
  128. },
  129. "chathistory": {
  130. text: `CHATHISTORY [params]
  131. CHATHISTORY is a history replay command associated with the IRCv3
  132. specification draft/chathistory. See this document:
  133. https://github.com/ircv3/ircv3-specifications/pull/393`,
  134. },
  135. "debug": {
  136. oper: true,
  137. text: `DEBUG <option>
  138. Provides various debugging commands for the IRCd. <option> can be one of:
  139. * GCSTATS: Garbage control statistics.
  140. * NUMGOROUTINE: Number of goroutines in use.
  141. * STARTCPUPROFILE: Starts the CPU profiler.
  142. * STOPCPUPROFILE: Stops the CPU profiler.
  143. * PROFILEHEAP: Writes a memory profile.
  144. * CRASHSERVER: Crashes the server (for use in failover testing)`,
  145. },
  146. "defcon": {
  147. oper: true,
  148. text: `DEFCON [level]
  149. The DEFCON system can disable server features at runtime, to mitigate
  150. spam or other hostile activity. It has five levels, which are cumulative
  151. (i.e., level 3 includes all restrictions from level 4 and so on):
  152. 5: Normal operation
  153. 4: No new account or channel registrations
  154. 3: All users are +R; no changes to vhosts
  155. 2: No new unauthenticated connections; all channels are +R
  156. 1: No new connections except from localhost or other trusted IPs`,
  157. },
  158. "deoper": {
  159. oper: true,
  160. text: `DEOPER
  161. DEOPER removes the IRCop privileges granted to you by a successful /OPER.`,
  162. },
  163. "dline": {
  164. oper: true,
  165. text: `DLINE [ANDKILL] [MYSELF] [duration] <ip>/<net> [ON <server>] [reason [| oper reason]]
  166. DLINE LIST
  167. Bans an IP address or network from connecting to the server. If the duration is
  168. given then only for that long. The reason is shown to the user themselves, but
  169. everyone else will see a standard message. The oper reason is shown to
  170. operators getting info about the DLINEs that exist.
  171. Bans are saved across subsequent launches of the server.
  172. "ANDKILL" means that all matching clients are also removed from the server.
  173. "MYSELF" is required when the DLINE matches the address the person applying it is connected
  174. from. If "MYSELF" is not given, trying to DLINE yourself will result in an error.
  175. [duration] can be of the following forms:
  176. 1y 12mo 31d 10h 8m 13s
  177. <net> is specified in typical CIDR notation. For example:
  178. 127.0.0.1/8
  179. 8.8.8.8/24
  180. ON <server> specifies that the ban is to be set on that specific server.
  181. [reason] and [oper reason], if they exist, are separated by a vertical bar (|).
  182. If "DLINE LIST" is sent, the server sends back a list of our current DLINEs.`,
  183. },
  184. "extjwt": {
  185. text: `EXTJWT <target> [service_name]
  186. Get a JSON Web Token for target (either * or a channel name).`,
  187. },
  188. "help": {
  189. text: `HELP <argument>
  190. Get an explanation of <argument>, or "index" for a list of help topics.`,
  191. },
  192. "helpop": {
  193. text: `HELPOP <argument>
  194. Get an explanation of <argument>, or "index" for a list of help topics.`,
  195. },
  196. "history": {
  197. text: `HISTORY <target> [limit]
  198. Replay message history. <target> can be a channel name, "me" to replay direct
  199. message history, or a nickname to replay another client's direct message
  200. history (they must be logged into the same account as you). [limit] can be
  201. either an integer (the maximum number of messages to replay), or a time
  202. duration like 10m or 1h (the time window within which to replay messages).`,
  203. },
  204. "info": {
  205. text: `INFO
  206. Sends information about the server, developers, etc.`,
  207. },
  208. "invite": {
  209. text: `INVITE <nickname> <channel>
  210. Invites the given user to the given channel, so long as you have the
  211. appropriate channel privs.`,
  212. },
  213. "ison": {
  214. text: `ISON <nickname>{ <nickname>}
  215. Returns whether the given nicks exist on the network.`,
  216. },
  217. "join": {
  218. text: `JOIN <channel>{,<channel>} [<key>{,<key>}]
  219. Joins the given channels with the matching keys.`,
  220. },
  221. "kick": {
  222. text: `KICK <channel> <user> [reason]
  223. Removes the user from the given channel, so long as you have the appropriate
  224. channel privs.`,
  225. },
  226. "kill": {
  227. oper: true,
  228. text: `KILL <nickname> [reason]
  229. Removes the given user from the network, showing them the reason if it is
  230. supplied.`,
  231. },
  232. "kline": {
  233. oper: true,
  234. text: `KLINE [ANDKILL] [MYSELF] [duration] <mask> [ON <server>] [reason [| oper reason]]
  235. KLINE LIST
  236. Bans a mask from connecting to the server. If the duration is given then only for that
  237. long. The reason is shown to the user themselves, but everyone else will see a standard
  238. message. The oper reason is shown to operators getting info about the KLINEs that exist.
  239. Bans are saved across subsequent launches of the server.
  240. "ANDKILL" means that all matching clients are also removed from the server.
  241. "MYSELF" is required when the KLINE matches the address the person applying it is connected
  242. from. If "MYSELF" is not given, trying to KLINE yourself will result in an error.
  243. [duration] can be of the following forms:
  244. 1y 12mo 31d 10h 8m 13s
  245. <mask> is specified in typical IRC format. For example:
  246. dan
  247. dan!5*@127.*
  248. ON <server> specifies that the ban is to be set on that specific server.
  249. [reason] and [oper reason], if they exist, are separated by a vertical bar (|).
  250. If "KLINE LIST" is sent, the server sends back a list of our current KLINEs.`,
  251. },
  252. "language": {
  253. text: `LANGUAGE <code>{ <code>}
  254. Sets your preferred languages to the given ones.`,
  255. },
  256. "list": {
  257. text: `LIST [<channel>{,<channel>}] [<elistcond>{,<elistcond>}]
  258. Shows information on the given channels (or if none are given, then on all
  259. channels). <elistcond>s modify how the channels are selected.`,
  260. //TODO(dan): Explain <elistcond>s in more specific detail
  261. },
  262. "lusers": {
  263. text: `LUSERS [<mask> [<server>]]
  264. Shows statistics about the size of the network. If <mask> is given, only
  265. returns stats for servers matching the given mask. If <server> is given, the
  266. command is processed by that server.`,
  267. },
  268. "mode": {
  269. text: `MODE <target> [<modestring> [<mode arguments>...]]
  270. Sets and removes modes from the given target. For more specific information on
  271. mode characters, see the help for "modes".`,
  272. },
  273. "monitor": {
  274. text: `MONITOR <subcmd>
  275. Allows the monitoring of nicknames, for alerts when they are online and
  276. offline. The subcommands are:
  277. MONITOR + target{,target}
  278. Adds the given names to your list of monitored nicknames.
  279. MONITOR - target{,target}
  280. Removes the given names from your list of monitored nicknames.
  281. MONITOR C
  282. Clears your list of monitored nicknames.
  283. MONITOR L
  284. Lists all the nicknames you are currently monitoring.
  285. MONITOR S
  286. Lists whether each nick in your MONITOR list is online or offline.`,
  287. },
  288. "motd": {
  289. text: `MOTD [server]
  290. Returns the message of the day for this, or the given, server.`,
  291. },
  292. "names": {
  293. text: `NAMES [<channel>{,<channel>}]
  294. Views the clients joined to a channel and their channel membership prefixes. To
  295. view the channel membership prefixes supported by this server, see the help for
  296. "PREFIX".`,
  297. },
  298. "nick": {
  299. text: `NICK <newnick>
  300. Sets your nickname to the new given one.`,
  301. },
  302. "notice": {
  303. text: `NOTICE <target>{,<target>} <text to be sent>
  304. Sends the text to the given targets as a NOTICE.`,
  305. },
  306. "npc": {
  307. text: `NPC <target> <sourcenick> <text to be sent>
  308. The NPC command is used to send a message to the target as the source.
  309. Requires the roleplay mode (+E) to be set on the target.`,
  310. },
  311. "npca": {
  312. text: `NPCA <target> <sourcenick> <text to be sent>
  313. The NPC command is used to send an action to the target as the source.
  314. Requires the roleplay mode (+E) to be set on the target.`,
  315. },
  316. "oper": {
  317. text: `OPER <name> [password]
  318. If the correct details are given, gives you IRCop privs.`,
  319. },
  320. "part": {
  321. text: `PART <channel>{,<channel>} [reason]
  322. Leaves the given channels and shows people the given reason.`,
  323. },
  324. "pass": {
  325. text: `PASS <password>
  326. When the server requires a connection password to join, used to send us the
  327. password.`,
  328. },
  329. "ping": {
  330. text: `PING <args>...
  331. Requests a PONG. Used to check link connectivity.`,
  332. },
  333. "pong": {
  334. text: `PONG <args>...
  335. Replies to a PING. Used to check link connectivity.`,
  336. },
  337. "privmsg": {
  338. text: `PRIVMSG <target>{,<target>} <text to be sent>
  339. Sends the text to the given targets as a PRIVMSG.`,
  340. },
  341. "relaymsg": {
  342. text: `RELAYMSG <channel> <spoofed nick> :<message>
  343. This command lets channel operators relay messages to their
  344. channel from other messaging systems using relay bots. The
  345. spoofed nickname MUST contain a forwardslash.
  346. For example:
  347. RELAYMSG #ircv3 Mallory/D :Welp, we linked Discord...`,
  348. },
  349. "rename": {
  350. text: `RENAME <channel> <newname> [<reason>]
  351. Renames the given channel with the given reason, if possible.
  352. For example:
  353. RENAME #ircv2 #ircv3 :Protocol upgrades!`,
  354. },
  355. "sajoin": {
  356. oper: true,
  357. text: `SAJOIN [nick] #channel{,#channel}
  358. Forcibly joins a user to a channel, ignoring restrictions like bans, user limits
  359. and channel keys. If [nick] is omitted, it defaults to the operator.`,
  360. },
  361. "sanick": {
  362. oper: true,
  363. text: `SANICK <currentnick> <newnick>
  364. Gives the given user a new nickname.`,
  365. },
  366. "samode": {
  367. oper: true,
  368. text: `SAMODE <target> [<modestring> [<mode arguments>...]]
  369. Forcibly sets and removes modes from the given target -- only available to
  370. opers. For more specific information on mode characters, see the help for
  371. "cmode" and "umode".`,
  372. },
  373. "scene": {
  374. text: `SCENE <target> <text to be sent>
  375. The SCENE command is used to send a scene notification to the given target.`,
  376. },
  377. "setname": {
  378. text: `SETNAME <realname>
  379. The SETNAME command updates the realname to be the newly-given one.`,
  380. },
  381. "summon": {
  382. text: `SUMMON [parameters]
  383. The SUMMON command is not implemented.`,
  384. },
  385. "tagmsg": {
  386. text: `@+client-only-tags TAGMSG <target>{,<target>}
  387. Sends the given client-only tags to the given targets as a TAGMSG. See the IRCv3
  388. specs for more info: http://ircv3.net/specs/core/message-tags-3.3.html`,
  389. },
  390. "quit": {
  391. text: `QUIT [reason]
  392. Indicates that you're leaving the server, and shows everyone the given reason.`,
  393. },
  394. "register": {
  395. text: `REGISTER <email | *> <password>
  396. Registers an account in accordance with the draft/register capability.`,
  397. },
  398. "rehash": {
  399. oper: true,
  400. text: `REHASH
  401. Reloads the config file and updates TLS certificates on listeners`,
  402. },
  403. "resume": {
  404. text: `RESUME <oldnick> [timestamp]
  405. Sent before registration has completed, this indicates that the client wants to
  406. resume their old connection <oldnick>.`,
  407. },
  408. "time": {
  409. text: `TIME [server]
  410. Shows the time of the current, or the given, server.`,
  411. },
  412. "topic": {
  413. text: `TOPIC <channel> [topic]
  414. If [topic] is given, sets the topic in the channel to that. If [topic] is not
  415. given, views the current topic on the channel.`,
  416. },
  417. "undline": {
  418. oper: true,
  419. text: `UNDLINE <ip>/<net>
  420. Removes an existing ban on an IP address or a network.
  421. <net> is specified in typical CIDR notation. For example:
  422. 127.0.0.1/8
  423. 8.8.8.8/24`,
  424. },
  425. "unkline": {
  426. oper: true,
  427. text: `UNKLINE <mask>
  428. Removes an existing ban on a mask.
  429. For example:
  430. dan
  431. dan!5*@127.*`,
  432. },
  433. "user": {
  434. text: `USER <username> 0 * <realname>
  435. Used in connection registration, sets your username and realname to the given
  436. values (though your username may also be looked up with Ident).`,
  437. },
  438. "users": {
  439. text: `USERS [parameters]
  440. The USERS command is not implemented.`,
  441. },
  442. "userhost": {
  443. text: `USERHOST <nickname>{ <nickname>}
  444. Shows information about the given users. Takes up to 10 nicknames.`,
  445. },
  446. "verify": {
  447. text: `VERIFY <account> <password>
  448. Verifies an account in accordance with the draft/register capability.`,
  449. },
  450. "version": {
  451. text: `VERSION [server]
  452. Views the version of software and the RPL_ISUPPORT tokens for the given server.`,
  453. },
  454. "webirc": {
  455. oper: true, // not really, but it's restricted anyways
  456. text: `WEBIRC <password> <gateway> <hostname> <ip> [:<flags>]
  457. Used by web<->IRC gateways and bouncers, the WEBIRC command allows gateways to
  458. pass-through the real IP addresses of clients:
  459. ircv3.net/specs/extensions/webirc.html
  460. <flags> is a list of space-separated strings indicating various details about
  461. the connection from the client to the gateway, such as:
  462. - tls: this flag indicates that the client->gateway connection is secure`,
  463. },
  464. "who": {
  465. text: `WHO <name> [o]
  466. Returns information for the given user.`,
  467. },
  468. "whois": {
  469. text: `WHOIS <client>{,<client>}
  470. Returns information for the given user(s).`,
  471. },
  472. "whowas": {
  473. text: `WHOWAS <nickname>
  474. Returns historical information on the last user with the given nickname.`,
  475. },
  476. "znc": {
  477. text: `ZNC <module> [params]
  478. Used to emulate features of the ZNC bouncer. This command is not intended
  479. for direct use by end users.`,
  480. duplicate: true,
  481. },
  482. // Informational
  483. "modes": {
  484. textGenerator: modesTextGenerator,
  485. helpType: InformationHelpEntry,
  486. },
  487. "cmode": {
  488. text: cmodeHelpText,
  489. helpType: InformationHelpEntry,
  490. },
  491. "cmodes": {
  492. text: cmodeHelpText,
  493. helpType: InformationHelpEntry,
  494. duplicate: true,
  495. },
  496. "umode": {
  497. text: umodeHelpText,
  498. helpType: InformationHelpEntry,
  499. },
  500. "umodes": {
  501. text: umodeHelpText,
  502. helpType: InformationHelpEntry,
  503. duplicate: true,
  504. },
  505. "snomask": {
  506. text: snomaskHelpText,
  507. helpType: InformationHelpEntry,
  508. oper: true,
  509. duplicate: true,
  510. },
  511. "snomasks": {
  512. text: snomaskHelpText,
  513. helpType: InformationHelpEntry,
  514. oper: true,
  515. },
  516. // RPL_ISUPPORT
  517. "casemapping": {
  518. text: `RPL_ISUPPORT CASEMAPPING
  519. Oragono supports an experimental unicode casemapping designed for extended
  520. Unicode support. This casemapping is based off RFC 7613 and the draft rfc7613
  521. casemapping spec here: https://oragono.io/specs.html`,
  522. helpType: ISupportHelpEntry,
  523. },
  524. "prefix": {
  525. text: `RPL_ISUPPORT PREFIX
  526. Oragono supports the following channel membership prefixes:
  527. +q (~) | Founder channel mode.
  528. +a (&) | Admin channel mode.
  529. +o (@) | Operator channel mode.
  530. +h (%) | Halfop channel mode.
  531. +v (+) | Voice channel mode.`,
  532. helpType: ISupportHelpEntry,
  533. },
  534. }
  535. // modesTextGenerator generates the text for the 'modes' help entry.
  536. // it exists only so we can translate this entry appropriately.
  537. func modesTextGenerator(client *Client) string {
  538. return client.t(cmodeHelpText) + "\n\n" + client.t(umodeHelpText)
  539. }
  540. type HelpIndexManager struct {
  541. sync.RWMutex // tier 1
  542. langToIndex map[string]string
  543. langToOperIndex map[string]string
  544. }
  545. // GenerateHelpIndex is used to generate HelpIndex.
  546. // Returns: a map from language code to the help index in that language.
  547. func GenerateHelpIndex(lm *languages.Manager, forOpers bool) map[string]string {
  548. // generate the help entry lists
  549. var commands, isupport, information []string
  550. var line string
  551. for name, info := range Help {
  552. if info.duplicate {
  553. continue
  554. }
  555. if info.oper && !forOpers {
  556. continue
  557. }
  558. line = fmt.Sprintf(" %s", name)
  559. if info.helpType == CommandHelpEntry {
  560. commands = append(commands, line)
  561. } else if info.helpType == ISupportHelpEntry {
  562. isupport = append(isupport, line)
  563. } else if info.helpType == InformationHelpEntry {
  564. information = append(information, line)
  565. }
  566. }
  567. // create the strings
  568. sort.Strings(commands)
  569. commandsString := strings.Join(commands, "\n")
  570. sort.Strings(isupport)
  571. isupportString := strings.Join(isupport, "\n")
  572. sort.Strings(information)
  573. informationString := strings.Join(information, "\n")
  574. // sub them in
  575. defaultHelpIndex := `= Help Topics =
  576. Commands:
  577. %[1]s
  578. RPL_ISUPPORT Tokens:
  579. %[2]s
  580. Information:
  581. %[3]s`
  582. newHelpIndex := make(map[string]string)
  583. newHelpIndex["en"] = fmt.Sprintf(defaultHelpIndex, commandsString, isupportString, informationString)
  584. for langCode := range lm.Languages {
  585. translatedHelpIndex := lm.Translate([]string{langCode}, defaultHelpIndex)
  586. if translatedHelpIndex != defaultHelpIndex {
  587. newHelpIndex[langCode] = fmt.Sprintf(translatedHelpIndex, commandsString, isupportString, informationString)
  588. }
  589. }
  590. return newHelpIndex
  591. }
  592. // GenerateIndices regenerates our help indexes for each currently enabled language.
  593. func (hm *HelpIndexManager) GenerateIndices(lm *languages.Manager) {
  594. // generate help indexes
  595. langToIndex := GenerateHelpIndex(lm, false)
  596. langToOperIndex := GenerateHelpIndex(lm, true)
  597. hm.Lock()
  598. defer hm.Unlock()
  599. hm.langToIndex = langToIndex
  600. hm.langToOperIndex = langToOperIndex
  601. }
  602. // sendHelp sends the client help of the given string.
  603. func (client *Client) sendHelp(name string, text string, rb *ResponseBuffer) {
  604. splitName := strings.Split(name, " ")
  605. textLines := strings.Split(text, "\n")
  606. for i, line := range textLines {
  607. args := splitName
  608. args = append(args, line)
  609. if i == 0 {
  610. rb.Add(nil, client.server.name, RPL_HELPSTART, args...)
  611. } else {
  612. rb.Add(nil, client.server.name, RPL_HELPTXT, args...)
  613. }
  614. }
  615. args := splitName
  616. args = append(args, client.t("End of /HELPOP"))
  617. rb.Add(nil, client.server.name, RPL_ENDOFHELP, args...)
  618. }
  619. // GetHelpIndex returns the help index for the given language.
  620. func (hm *HelpIndexManager) GetIndex(languages []string, oper bool) string {
  621. hm.RLock()
  622. langToIndex := hm.langToIndex
  623. if oper {
  624. langToIndex = hm.langToOperIndex
  625. }
  626. hm.RUnlock()
  627. for _, lang := range languages {
  628. index, exists := langToIndex[lang]
  629. if exists {
  630. return index
  631. }
  632. }
  633. // 'en' always exists
  634. return langToIndex["en"]
  635. }
  636. func init() {
  637. // startup check that we have HELP entries for every command
  638. for name := range Commands {
  639. _, exists := Help[strings.ToLower(name)]
  640. if !exists {
  641. panic(fmt.Sprintf("Help entry does not exist for command %s", name))
  642. }
  643. }
  644. }