Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. "github.com/goshuirc/irc-go/ircmsg"
  9. )
  10. // HelpEntryType represents the different sorts of help entries that can exist.
  11. type HelpEntryType int
  12. const (
  13. // CommandHelpEntry is a help entry explaining a client command.
  14. CommandHelpEntry HelpEntryType = 0
  15. // InformationHelpEntry is a help entry explaining general server info.
  16. InformationHelpEntry HelpEntryType = 1
  17. // ISupportHelpEntry is a help entry explaining a specific RPL_ISUPPORT token.
  18. ISupportHelpEntry HelpEntryType = 2
  19. )
  20. // HelpEntry represents an entry in the Help map.
  21. type HelpEntry struct {
  22. oper bool
  23. text string
  24. helpType HelpEntryType
  25. duplicate bool
  26. }
  27. // used for duplicates
  28. var (
  29. cmodeHelpText = `== Channel Modes ==
  30. Oragono supports the following channel modes:
  31. +b | Client masks that are banned from the channel (e.g. *!*@127.0.0.1)
  32. +e | Client masks that are exempted from bans.
  33. +I | Client masks that are exempted from the invite-only flag.
  34. +i | Invite-only mode, only invited clients can join the channel.
  35. +k | Key required when joining the channel.
  36. +l | Client join limit for the channel.
  37. +m | Moderated mode, only privileged clients can talk on the channel.
  38. +n | No-outside-messages mode, only users that are on the channel can send
  39. | messages to it.
  40. +r | Only registered users can talk in the channel.
  41. +s | Secret mode, channel won't show up in /LIST or whois replies.
  42. +t | Only channel opers can modify the topic.
  43. = Prefixes =
  44. +q (~) | Founder channel mode.
  45. +a (&) | Admin channel mode.
  46. +o (@) | Operator channel mode.
  47. +h (%) | Halfop channel mode.
  48. +v (+) | Voice channel mode.`
  49. umodeHelpText = `== User Modes ==
  50. Oragono supports the following user modes:
  51. +a | User is marked as being away. This mode is set with the /AWAY command.
  52. +i | User is marked as invisible (their channels are hidden from whois replies).
  53. +o | User is an IRC operator.
  54. +s | Server Notice Masks (see help with /HELPOP snomasks).
  55. +Z | User is connected via TLS.`
  56. snomaskHelpText = `== Server Notice Masks ==
  57. Oragono supports the following server notice masks for operators:
  58. a | Local announcements.
  59. c | Local client connections.
  60. j | Local channel actions.
  61. k | Local kills.
  62. n | Local nick changes.
  63. o | Local oper actions.
  64. q | Local quits.
  65. t | Local /STATS usage.
  66. u | Local client account actions.
  67. x | Local X-lines (DLINE/KLINE/etc).
  68. To set a snomask, do this with your nickname:
  69. /MODE <nick> +s <chars>
  70. For instance, this would set the kill, oper, account and xline snomasks on dan:
  71. /MODE dan +s koux`
  72. )
  73. // Help contains the help strings distributed with the IRCd.
  74. var Help = map[string]HelpEntry{
  75. // Commands
  76. "acc": {
  77. text: `ACC REGISTER <accountname> [callback_namespace:]<callback> [cred_type] :<credential>
  78. ACC VERIFY <accountname> <auth_code>
  79. Used in account registration. See the relevant specs for more info:
  80. http://oragono.io/specs.html`,
  81. },
  82. "ambiance": {
  83. text: `AMBIANCE <target> <text to be sent>
  84. The AMBIANCE command is used to send a scene notification to the given target.`,
  85. },
  86. "authenticate": {
  87. text: `AUTHENTICATE
  88. Used during SASL authentication. See the IRCv3 specs for more info:
  89. http://ircv3.net/specs/extensions/sasl-3.1.html`,
  90. },
  91. "away": {
  92. text: `AWAY [message]
  93. If [message] is sent, marks you away. If [message] is not sent, marks you no
  94. longer away.`,
  95. },
  96. "cap": {
  97. text: `CAP <subcommand> [:<capabilities>]
  98. Used in capability negotiation. See the IRCv3 specs for more info:
  99. http://ircv3.net/specs/core/capability-negotiation-3.1.html
  100. http://ircv3.net/specs/core/capability-negotiation-3.2.html`,
  101. },
  102. "chanserv": {
  103. text: `CHANSERV <subcommand> [params]
  104. ChanServ controls channel registrations.`,
  105. },
  106. "cs": {
  107. text: `CS <subcommand> [params]
  108. ChanServ controls channel registrations.`,
  109. },
  110. "debug": {
  111. oper: true,
  112. text: `DEBUG <option>
  113. Prints debug information about the IRCd. <option> can be one of:
  114. * GCSTATS: Garbage control statistics.
  115. * NUMGOROUTINE: Number of goroutines in use.
  116. * STARTCPUPROFILE: Starts the CPU profiler.
  117. * STOPCPUPROFILE: Stops the CPU profiler.
  118. * PROFILEHEAP: Writes out the CPU profiler info.`,
  119. },
  120. "dline": {
  121. oper: true,
  122. text: `DLINE [ANDKILL] [MYSELF] [duration] <ip>/<net> [ON <server>] [reason [| oper reason]]
  123. Bans an IP address or network from connecting to the server. If the duration is
  124. given then only for that long. The reason is shown to the user themselves, but
  125. everyone else will see a standard message. The oper reason is shown to
  126. operators getting info about the DLINEs that exist.
  127. Bans are saved across subsequent launches of the server.
  128. "ANDKILL" means that all matching clients are also removed from the server.
  129. "MYSELF" is required when the DLINE matches the address the person applying it is connected
  130. from. If "MYSELF" is not given, trying to DLINE yourself will result in an error.
  131. [duration] can be of the following forms:
  132. 1y 12mo 31d 10h 8m 13s
  133. <net> is specified in typical CIDR notation. For example:
  134. 127.0.0.1/8
  135. 8.8.8.8/24
  136. ON <server> specifies that the ban is to be set on that specific server.
  137. [reason] and [oper reason], if they exist, are separated by a vertical bar (|).`,
  138. },
  139. "help": {
  140. text: `HELP <argument>
  141. Get an explanation of <argument>, or "index" for a list of help topics.`,
  142. },
  143. "helpop": {
  144. text: `HELPOP <argument>
  145. Get an explanation of <argument>, or "index" for a list of help topics.`,
  146. },
  147. "invite": {
  148. text: `INVITE <nickname> <channel>
  149. Invites the given user to the given channel, so long as you have the
  150. appropriate channel privs.`,
  151. },
  152. "ison": {
  153. text: `ISON <nickname>{ <nickname>}
  154. Returns whether the given nicks exist on the network.`,
  155. },
  156. "join": {
  157. text: `JOIN <channel>{,<channel>} [<key>{,<key>}]
  158. Joins the given channels with the matching keys.`,
  159. },
  160. "kick": {
  161. text: `KICK <channel> <user> [reason]
  162. Removes the user from the given channel, so long as you have the appropriate
  163. channel privs.`,
  164. },
  165. "kill": {
  166. oper: true,
  167. text: `KILL <nickname> [reason]
  168. Removes the given user from the network, showing them the reason if it is
  169. supplied.`,
  170. },
  171. "kline": {
  172. oper: true,
  173. text: `KLINE [ANDKILL] [MYSELF] [duration] <mask> [ON <server>] [reason [| oper reason]]
  174. Bans a mask from connecting to the server. If the duration is given then only for that
  175. long. The reason is shown to the user themselves, but everyone else will see a standard
  176. message. The oper reason is shown to operators getting info about the KLINEs that exist.
  177. Bans are saved across subsequent launches of the server.
  178. "ANDKILL" means that all matching clients are also removed from the server.
  179. "MYSELF" is required when the KLINE matches the address the person applying it is connected
  180. from. If "MYSELF" is not given, trying to KLINE yourself will result in an error.
  181. [duration] can be of the following forms:
  182. 1y 12mo 31d 10h 8m 13s
  183. <mask> is specified in typical IRC format. For example:
  184. dan
  185. dan!5*@127.*
  186. ON <server> specifies that the ban is to be set on that specific server.
  187. [reason] and [oper reason], if they exist, are separated by a vertical bar (|).`,
  188. },
  189. "list": {
  190. text: `LIST [<channel>{,<channel>}] [<elistcond>{,<elistcond>}]
  191. Shows information on the given channels (or if none are given, then on all
  192. channels). <elistcond>s modify how the channels are selected.`,
  193. //TODO(dan): Explain <elistcond>s in more specific detail
  194. },
  195. "lusers": {
  196. text: `LUSERS [<mask> [<server>]]
  197. Shows statistics about the size of the network. If <mask> is given, only
  198. returns stats for servers matching the given mask. If <server> is given, the
  199. command is processed by that server.`,
  200. },
  201. "mode": {
  202. text: `MODE <target> [<modestring> [<mode arguments>...]]
  203. Sets and removes modes from the given target. For more specific information on
  204. mode characters, see the help for "modes".`,
  205. },
  206. "monitor": {
  207. text: `MONITOR <subcmd>
  208. Allows the monitoring of nicknames, for alerts when they are online and
  209. offline. The subcommands are:
  210. MONITOR + target{,target}
  211. Adds the given names to your list of monitored nicknames.
  212. MONITOR - target{,target}
  213. Removes the given names from your list of monitored nicknames.
  214. MONITOR C
  215. Clears your list of monitored nicknames.
  216. MONITOR L
  217. Lists all the nicknames you are currently monitoring.
  218. MONITOR S
  219. Lists whether each nick in your MONITOR list is online or offline.`,
  220. },
  221. "motd": {
  222. text: `MOTD [server]
  223. Returns the message of the day for this, or the given, server.`,
  224. },
  225. "names": {
  226. text: `NAMES [<channel>{,<channel>}]
  227. Views the clients joined to a channel and their channel membership prefixes. To
  228. view the channel membership prefixes supported by this server, see the help for
  229. "PREFIX".`,
  230. },
  231. "nick": {
  232. text: `NICK <newnick>
  233. Sets your nickname to the new given one.`,
  234. },
  235. "nickserv": {
  236. text: `NICKSERV <subcommand> [params]
  237. NickServ controls accounts and user registrations.`,
  238. },
  239. "notice": {
  240. text: `NOTICE <target>{,<target>} <text to be sent>
  241. Sends the text to the given targets as a NOTICE.`,
  242. },
  243. "npc": {
  244. text: `NPC <target> <sourcenick> <text to be sent>
  245. The NPC command is used to send a message to the target as the source.
  246. Requires the roleplay mode (+E) to be set on the target.`,
  247. },
  248. "npca": {
  249. text: `NPCA <target> <sourcenick> <text to be sent>
  250. The NPC command is used to send an action to the target as the source.
  251. Requires the roleplay mode (+E) to be set on the target.`,
  252. },
  253. "ns": {
  254. text: `NS <subcommand> [params]
  255. NickServ controls accounts and user registrations.`,
  256. },
  257. "oper": {
  258. text: `OPER <name> <password>
  259. If the correct details are given, gives you IRCop privs.`,
  260. },
  261. "part": {
  262. text: `PART <channel>{,<channel>} [reason]
  263. Leaves the given channels and shows people the given reason.`,
  264. },
  265. "pass": {
  266. text: `PASS <password>
  267. When the server requires a connection password to join, used to send us the
  268. password.`,
  269. },
  270. "ping": {
  271. text: `PING <args>...
  272. Requests a PONG. Used to check link connectivity.`,
  273. },
  274. "pong": {
  275. text: `PONG <args>...
  276. Replies to a PING. Used to check link connectivity.`,
  277. },
  278. "privmsg": {
  279. text: `PRIVMSG <target>{,<target>} <text to be sent>
  280. Sends the text to the given targets as a PRIVMSG.`,
  281. },
  282. "rename": {
  283. text: `RENAME <channel> <newname> [<reason>]
  284. Renames the given channel with the given reason, if possible.
  285. For example:
  286. RENAME #ircv2 #ircv3 :Protocol upgrades!`,
  287. },
  288. "sanick": {
  289. oper: true,
  290. text: `SANICK <currentnick> <newnick>
  291. Gives the given user a new nickname.`,
  292. },
  293. "samode": {
  294. oper: true,
  295. text: `SAMODE <target> [<modestring> [<mode arguments>...]]
  296. Forcibly sets and removes modes from the given target -- only available to
  297. opers. For more specific information on mode characters, see the help for
  298. "cmode" and "umode".`,
  299. },
  300. "scene": {
  301. text: `SCENE <target> <text to be sent>
  302. The SCENE command is used to send a scene notification to the given target.`,
  303. },
  304. "tagmsg": {
  305. text: `@+client-only-tags TAGMSG <target>{,<target>}
  306. Sends the given client-only tags to the given targets as a TAGMSG. See the IRCv3
  307. specs for more info: http://ircv3.net/specs/core/message-tags-3.3.html`,
  308. },
  309. "quit": {
  310. text: `QUIT [reason]
  311. Indicates that you're leaving the server, and shows everyone the given reason.`,
  312. },
  313. "rehash": {
  314. oper: true,
  315. text: `REHASH
  316. Reloads the config file and updates TLS certificates on listeners`,
  317. },
  318. "time": {
  319. text: `TIME [server]
  320. Shows the time of the current, or the given, server.`,
  321. },
  322. "topic": {
  323. text: `TOPIC <channel> [topic]
  324. If [topic] is given, sets the topic in the channel to that. If [topic] is not
  325. given, views the current topic on the channel.`,
  326. },
  327. "undline": {
  328. oper: true,
  329. text: `UNDLINE <ip>/<net>
  330. Removes an existing ban on an IP address or a network.
  331. <net> is specified in typical CIDR notation. For example:
  332. 127.0.0.1/8
  333. 8.8.8.8/24`,
  334. },
  335. "unkline": {
  336. oper: true,
  337. text: `UNKLINE <mask>
  338. Removes an existing ban on a mask.
  339. For example:
  340. dan
  341. dan!5*@127.*`,
  342. },
  343. "user": {
  344. text: `USER <username> 0 * <realname>
  345. Used in connection registration, sets your username and realname to the given
  346. values (though your username may also be looked up with Ident).`,
  347. },
  348. "userhost": {
  349. text: `USERHOST <nickname>{ <nickname>}
  350. Shows information about the given users. Takes up to 10 nicknames.`,
  351. },
  352. "version": {
  353. text: `VERSION [server]
  354. Views the version of software and the RPL_ISUPPORT tokens for the given server.`,
  355. },
  356. "who": {
  357. text: `WHO <name> [o]
  358. Returns information for the given user.`,
  359. },
  360. "whois": {
  361. text: `WHOIS <client>{,<client>}
  362. Returns information for the given user(s).`,
  363. },
  364. "whowas": {
  365. text: `WHOWAS <nickname>
  366. Returns historical information on the last user with the given nickname.`,
  367. },
  368. // Informational
  369. "modes": {
  370. text: cmodeHelpText + "\n\n" + umodeHelpText,
  371. helpType: InformationHelpEntry,
  372. },
  373. "cmode": {
  374. text: cmodeHelpText,
  375. helpType: InformationHelpEntry,
  376. },
  377. "cmodes": {
  378. text: cmodeHelpText,
  379. helpType: InformationHelpEntry,
  380. duplicate: true,
  381. },
  382. "umode": {
  383. text: umodeHelpText,
  384. helpType: InformationHelpEntry,
  385. },
  386. "umodes": {
  387. text: umodeHelpText,
  388. helpType: InformationHelpEntry,
  389. duplicate: true,
  390. },
  391. "snomask": {
  392. text: snomaskHelpText,
  393. helpType: InformationHelpEntry,
  394. oper: true,
  395. duplicate: true,
  396. },
  397. "snomasks": {
  398. text: snomaskHelpText,
  399. helpType: InformationHelpEntry,
  400. oper: true,
  401. },
  402. // RPL_ISUPPORT
  403. "casemapping": {
  404. text: `RPL_ISUPPORT CASEMAPPING
  405. Oragono supports an experimental unicode casemapping designed for extended
  406. Unicode support. This casemapping is based off RFC 7613 and the draft rfc7613
  407. casemapping spec here: http://oragono.io/specs.html`,
  408. helpType: ISupportHelpEntry,
  409. },
  410. "prefix": {
  411. text: `RPL_ISUPPORT PREFIX
  412. Oragono supports the following channel membership prefixes:
  413. +q (~) | Founder channel mode.
  414. +a (&) | Admin channel mode.
  415. +o (@) | Operator channel mode.
  416. +h (%) | Halfop channel mode.
  417. +v (+) | Voice channel mode.`,
  418. helpType: ISupportHelpEntry,
  419. },
  420. }
  421. // HelpIndex contains the list of all help topics for regular users.
  422. var HelpIndex = "list of all help topics for regular users"
  423. // HelpIndexOpers contains the list of all help topics for opers.
  424. var HelpIndexOpers = "list of all help topics for opers"
  425. // GenerateHelpIndex is used to generate HelpIndex.
  426. func GenerateHelpIndex(forOpers bool) string {
  427. newHelpIndex := `= Help Topics =
  428. Commands:
  429. %s
  430. RPL_ISUPPORT Tokens:
  431. %s
  432. Information:
  433. %s`
  434. // generate them
  435. var commands, isupport, information []string
  436. var line string
  437. for name, info := range Help {
  438. if info.duplicate {
  439. continue
  440. }
  441. if info.oper && !forOpers {
  442. continue
  443. }
  444. line = fmt.Sprintf(" %s", name)
  445. if info.helpType == CommandHelpEntry {
  446. commands = append(commands, line)
  447. } else if info.helpType == ISupportHelpEntry {
  448. isupport = append(isupport, line)
  449. } else if info.helpType == InformationHelpEntry {
  450. information = append(information, line)
  451. }
  452. }
  453. // sort the lines
  454. sort.Strings(commands)
  455. sort.Strings(isupport)
  456. sort.Strings(information)
  457. // sub them in
  458. newHelpIndex = fmt.Sprintf(newHelpIndex, strings.Join(commands, "\n"), strings.Join(isupport, "\n"), strings.Join(information, "\n"))
  459. return newHelpIndex
  460. }
  461. // sendHelp sends the client help of the given string.
  462. func (client *Client) sendHelp(name string, text string) {
  463. splitName := strings.Split(name, " ")
  464. textLines := strings.Split(text, "\n")
  465. for i, line := range textLines {
  466. args := splitName
  467. args = append(args, line)
  468. if i == 0 {
  469. client.Send(nil, client.server.name, RPL_HELPSTART, args...)
  470. } else {
  471. client.Send(nil, client.server.name, RPL_HELPTXT, args...)
  472. }
  473. }
  474. args := splitName
  475. args = append(args, "End of /HELPOP")
  476. client.Send(nil, client.server.name, RPL_ENDOFHELP, args...)
  477. }
  478. // helpHandler returns the appropriate help for the given query.
  479. func helpHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
  480. argument := strings.ToLower(strings.TrimSpace(strings.Join(msg.Params, " ")))
  481. if len(argument) < 1 {
  482. client.sendHelp("HELPOP", `HELPOP <argument>
  483. Get an explanation of <argument>, or "index" for a list of help topics.`)
  484. return false
  485. }
  486. // handle index
  487. if argument == "index" {
  488. if client.flags[Operator] {
  489. client.sendHelp("HELP", HelpIndexOpers)
  490. } else {
  491. client.sendHelp("HELP", HelpIndex)
  492. }
  493. return false
  494. }
  495. helpHandler, exists := Help[argument]
  496. if exists && (!helpHandler.oper || (helpHandler.oper && client.flags[Operator])) {
  497. client.sendHelp(strings.ToUpper(argument), helpHandler.text)
  498. } else {
  499. args := msg.Params
  500. args = append(args, "Help not found")
  501. client.Send(nil, server.name, ERR_HELPNOTFOUND, args...)
  502. }
  503. return false
  504. }