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 23KB

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