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.

services.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // Copyright (c) 2018 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "bytes"
  6. "fmt"
  7. "log"
  8. "sort"
  9. "strings"
  10. "time"
  11. "github.com/ergochat/ergo/irc/utils"
  12. "github.com/goshuirc/irc-go/ircfmt"
  13. "github.com/goshuirc/irc-go/ircmsg"
  14. )
  15. // defines an IRC service, e.g., NICKSERV
  16. type ircService struct {
  17. Name string
  18. ShortName string
  19. prefix string // NUH source of messages from this service
  20. CommandAliases []string
  21. Commands map[string]*serviceCommand
  22. HelpBanner string
  23. }
  24. // defines a command associated with a service, e.g., NICKSERV IDENTIFY
  25. type serviceCommand struct {
  26. aliasOf string // marks this command as an alias of another
  27. capabs []string // oper capabs the given user has to have to access this command
  28. handler func(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer)
  29. help string
  30. helpStrings []string
  31. helpShort string
  32. enabled func(*Config) bool // is this command enabled in the server config?
  33. authRequired bool
  34. hidden bool
  35. minParams int
  36. maxParams int // optional, if set it's an error if the user passes more than this many params
  37. unsplitFinalParam bool // split into at most maxParams, with last param containing unsplit text
  38. }
  39. // looks up a command in the table of command definitions for a service, resolving aliases
  40. func lookupServiceCommand(commands map[string]*serviceCommand, command string) *serviceCommand {
  41. maxDepth := 1
  42. depth := 0
  43. for depth <= maxDepth {
  44. result, ok := commands[command]
  45. if !ok {
  46. return nil
  47. } else if result.aliasOf == "" {
  48. return result
  49. } else {
  50. command = result.aliasOf
  51. depth += 1
  52. }
  53. }
  54. return nil
  55. }
  56. var (
  57. nickservService = &ircService{
  58. Name: "NickServ",
  59. ShortName: "NS",
  60. CommandAliases: []string{"NICKSERV", "NS"},
  61. Commands: nickservCommands,
  62. HelpBanner: nickservHelp,
  63. }
  64. chanservService = &ircService{
  65. Name: "ChanServ",
  66. ShortName: "CS",
  67. CommandAliases: []string{"CHANSERV", "CS"},
  68. Commands: chanservCommands,
  69. HelpBanner: chanservHelp,
  70. }
  71. hostservService = &ircService{
  72. Name: "HostServ",
  73. ShortName: "HS",
  74. CommandAliases: []string{"HOSTSERV", "HS"},
  75. Commands: hostservCommands,
  76. HelpBanner: hostservHelp,
  77. }
  78. histservService = &ircService{
  79. Name: "HistServ",
  80. ShortName: "HISTSERV",
  81. CommandAliases: []string{"HISTSERV"},
  82. Commands: histservCommands,
  83. HelpBanner: histservHelp,
  84. }
  85. )
  86. // all services, by lowercase name
  87. var OragonoServices = map[string]*ircService{
  88. "nickserv": nickservService,
  89. "chanserv": chanservService,
  90. "hostserv": hostservService,
  91. "histserv": histservService,
  92. }
  93. func (service *ircService) Notice(rb *ResponseBuffer, text string) {
  94. rb.Add(nil, service.prefix, "NOTICE", rb.target.Nick(), text)
  95. }
  96. // all service commands at the protocol level, by uppercase command name
  97. // e.g., NICKSERV, NS
  98. var oragonoServicesByCommandAlias map[string]*ircService
  99. // special-cased command shared by all services
  100. var servHelpCmd serviceCommand = serviceCommand{
  101. help: `Syntax: $bHELP [command]$b
  102. HELP returns information on the given command.`,
  103. helpShort: `$bHELP$b shows in-depth information about commands.`,
  104. }
  105. // generic handler for IRC commands like `/NICKSERV INFO`
  106. func serviceCmdHandler(server *Server, client *Client, msg ircmsg.Message, rb *ResponseBuffer) bool {
  107. service, ok := oragonoServicesByCommandAlias[msg.Command]
  108. if !ok {
  109. server.logger.Warning("internal", "can't handle unrecognized service", msg.Command)
  110. return false
  111. }
  112. if len(msg.Params) == 0 {
  113. return false
  114. }
  115. commandName := strings.ToLower(msg.Params[0])
  116. params := msg.Params[1:]
  117. cmd := lookupServiceCommand(service.Commands, commandName)
  118. // for a maxParams command, join all final parameters together if necessary
  119. if cmd != nil && cmd.unsplitFinalParam && cmd.maxParams < len(params) {
  120. newParams := make([]string, cmd.maxParams)
  121. copy(newParams, params[:cmd.maxParams-1])
  122. newParams[cmd.maxParams-1] = strings.Join(params[cmd.maxParams-1:], " ")
  123. params = newParams
  124. }
  125. serviceRunCommand(service, server, client, cmd, commandName, params, rb)
  126. return false
  127. }
  128. // generic handler for service PRIVMSG, like `/msg NickServ INFO`
  129. func servicePrivmsgHandler(service *ircService, server *Server, client *Client, message string, rb *ResponseBuffer) {
  130. if strings.HasPrefix(message, "\x01") {
  131. serviceCTCPHandler(service, client, message)
  132. return
  133. }
  134. params := strings.Fields(message)
  135. if len(params) == 0 {
  136. return
  137. }
  138. // look up the service command to see how to parse it
  139. commandName := strings.ToLower(params[0])
  140. cmd := lookupServiceCommand(service.Commands, commandName)
  141. // reparse if needed
  142. if cmd != nil && cmd.unsplitFinalParam {
  143. params = utils.FieldsN(message, cmd.maxParams+1)[1:]
  144. } else {
  145. params = params[1:]
  146. }
  147. serviceRunCommand(service, server, client, cmd, commandName, params, rb)
  148. }
  149. func serviceCTCPHandler(service *ircService, client *Client, message string) {
  150. ctcp := strings.TrimSuffix(message[1:], "\x01")
  151. ctcpSplit := utils.FieldsN(ctcp, 2)
  152. ctcpCmd := strings.ToUpper(ctcpSplit[0])
  153. ctcpOut := ""
  154. switch ctcpCmd {
  155. case "VERSION":
  156. ctcpOut = fmt.Sprintf("%s (%s)", service.Name, Ver)
  157. case "PING":
  158. if len(ctcpSplit) > 1 {
  159. ctcpOut = ctcpSplit[1]
  160. }
  161. case "TIME":
  162. ctcpOut = time.Now().UTC().Format(time.RFC1123)
  163. }
  164. if ctcpOut != "" {
  165. client.Send(nil, service.prefix, "NOTICE", client.Nick(), fmt.Sprintf("\x01%s %s\x01", ctcpCmd, ctcpOut))
  166. }
  167. }
  168. // actually execute a service command
  169. func serviceRunCommand(service *ircService, server *Server, client *Client, cmd *serviceCommand, commandName string, params []string, rb *ResponseBuffer) {
  170. nick := rb.target.Nick()
  171. sendNotice := func(notice string) {
  172. rb.Add(nil, service.prefix, "NOTICE", nick, notice)
  173. }
  174. if cmd == nil {
  175. sendNotice(fmt.Sprintf(client.t("Unknown command. To see available commands, run: /%s HELP"), service.ShortName))
  176. return
  177. }
  178. if len(params) < cmd.minParams || (0 < cmd.maxParams && cmd.maxParams < len(params)) {
  179. sendNotice(fmt.Sprintf(client.t("Invalid parameters. For usage, do /msg %[1]s HELP %[2]s"), service.Name, strings.ToUpper(commandName)))
  180. return
  181. }
  182. if cmd.enabled != nil && !cmd.enabled(server.Config()) {
  183. sendNotice(client.t("This command has been disabled by the server administrators"))
  184. return
  185. }
  186. if 0 < len(cmd.capabs) && !client.HasRoleCapabs(cmd.capabs...) {
  187. sendNotice(client.t("Command restricted"))
  188. return
  189. }
  190. if cmd.authRequired && client.Account() == "" {
  191. sendNotice(client.t("You're not logged into an account"))
  192. return
  193. }
  194. server.logger.Debug("services", fmt.Sprintf("Client %s ran %s command %s", client.Nick(), service.Name, commandName))
  195. if commandName == "help" {
  196. serviceHelpHandler(service, server, client, params, rb)
  197. } else {
  198. cmd.handler(service, server, client, commandName, params, rb)
  199. }
  200. }
  201. // generic handler that displays help for service commands
  202. func serviceHelpHandler(service *ircService, server *Server, client *Client, params []string, rb *ResponseBuffer) {
  203. nick := rb.target.Nick()
  204. config := server.Config()
  205. sendNotice := func(notice string) {
  206. rb.Add(nil, service.prefix, "NOTICE", nick, notice)
  207. }
  208. sendNotice(fmt.Sprintf(ircfmt.Unescape("*** $b%s HELP$b ***"), service.Name))
  209. if len(params) == 0 {
  210. helpBannerLines := strings.Split(client.t(service.HelpBanner), "\n")
  211. helpBannerLines = append(helpBannerLines, []string{
  212. "",
  213. client.t("To see in-depth help for a specific command, try:"),
  214. fmt.Sprintf(ircfmt.Unescape(client.t(" $b/msg %s HELP <command>$b")), service.Name),
  215. "",
  216. client.t("Here are the commands you can use:"),
  217. }...)
  218. // show general help
  219. var shownHelpLines sort.StringSlice
  220. var disabledCommands bool
  221. for _, commandInfo := range service.Commands {
  222. // skip commands user can't access
  223. if 0 < len(commandInfo.capabs) && !client.HasRoleCapabs(commandInfo.capabs...) {
  224. continue
  225. }
  226. if commandInfo.aliasOf != "" || commandInfo.hidden {
  227. continue // don't show help lines for aliases
  228. }
  229. if commandInfo.enabled != nil && !commandInfo.enabled(config) {
  230. disabledCommands = true
  231. continue
  232. }
  233. shownHelpLines = append(shownHelpLines, " "+ircfmt.Unescape(client.t(commandInfo.helpShort)))
  234. }
  235. if disabledCommands {
  236. shownHelpLines = append(shownHelpLines, " "+client.t("... and other commands which have been disabled"))
  237. }
  238. // sort help lines
  239. sort.Sort(shownHelpLines)
  240. // push out help text
  241. for _, line := range helpBannerLines {
  242. sendNotice(line)
  243. }
  244. for _, line := range shownHelpLines {
  245. sendNotice(line)
  246. }
  247. } else {
  248. commandName := strings.ToLower(params[0])
  249. commandInfo := lookupServiceCommand(service.Commands, commandName)
  250. if commandInfo == nil {
  251. sendNotice(client.t(fmt.Sprintf("Unknown command. To see available commands, run /%s HELP", service.ShortName)))
  252. } else {
  253. helpStrings := commandInfo.helpStrings
  254. if helpStrings == nil {
  255. hsArray := [1]string{commandInfo.help}
  256. helpStrings = hsArray[:]
  257. }
  258. for i, helpString := range helpStrings {
  259. if 0 < i {
  260. sendNotice("")
  261. }
  262. for _, line := range strings.Split(ircfmt.Unescape(client.t(helpString)), "\n") {
  263. sendNotice(line)
  264. }
  265. }
  266. }
  267. }
  268. sendNotice(fmt.Sprintf(ircfmt.Unescape(client.t("*** $bEnd of %s HELP$b ***")), service.Name))
  269. }
  270. func makeServiceHelpTextGenerator(cmd string, banner string) func(*Client) string {
  271. return func(client *Client) string {
  272. var buf bytes.Buffer
  273. fmt.Fprintf(&buf, client.t("%s <subcommand> [params]"), cmd)
  274. buf.WriteRune('\n')
  275. buf.WriteString(client.t(banner)) // may contain newlines, that's fine
  276. buf.WriteRune('\n')
  277. fmt.Fprintf(&buf, client.t("For more details, try /%s HELP"), cmd)
  278. return buf.String()
  279. }
  280. }
  281. func overrideServicePrefixes(hostname string) error {
  282. if hostname == "" {
  283. return nil
  284. }
  285. if !utils.IsHostname(hostname) {
  286. return fmt.Errorf("`%s` is an invalid services hostname", hostname)
  287. }
  288. for _, serv := range OragonoServices {
  289. serv.prefix = fmt.Sprintf("%s!%s@%s", serv.Name, serv.Name, hostname)
  290. }
  291. return nil
  292. }
  293. func initializeServices() {
  294. // this modifies the global Commands map,
  295. // so it must be called from irc/commands.go's init()
  296. oragonoServicesByCommandAlias = make(map[string]*ircService)
  297. for serviceName, service := range OragonoServices {
  298. service.prefix = fmt.Sprintf("%s!%s@localhost", service.Name, service.Name)
  299. // make `/MSG ServiceName HELP` work correctly
  300. service.Commands["help"] = &servHelpCmd
  301. // reserve the nickname
  302. restrictedNicknames = append(restrictedNicknames, service.Name)
  303. // register the protocol-level commands (NICKSERV, NS) that talk to the service,
  304. // and their associated help entries
  305. var ircCmdDef Command
  306. ircCmdDef.handler = serviceCmdHandler
  307. for _, ircCmd := range service.CommandAliases {
  308. Commands[ircCmd] = ircCmdDef
  309. oragonoServicesByCommandAlias[ircCmd] = service
  310. Help[strings.ToLower(ircCmd)] = HelpEntry{
  311. textGenerator: makeServiceHelpTextGenerator(ircCmd, service.HelpBanner),
  312. }
  313. }
  314. // force devs to write a help entry for every command
  315. for commandName, commandInfo := range service.Commands {
  316. if commandInfo.aliasOf == "" && !commandInfo.hidden {
  317. if (commandInfo.help == "" && commandInfo.helpStrings == nil) || commandInfo.helpShort == "" {
  318. log.Fatal(fmt.Sprintf("help entry missing for %s command %s", serviceName, commandName))
  319. }
  320. }
  321. if commandInfo.maxParams == 0 && commandInfo.unsplitFinalParam {
  322. log.Fatal("unsplitFinalParam requires use of maxParams")
  323. }
  324. }
  325. }
  326. for _, restrictedNickname := range restrictedNicknames {
  327. cfName, err := CasefoldName(restrictedNickname)
  328. if err != nil {
  329. panic(err)
  330. }
  331. restrictedCasefoldedNicks.Add(cfName)
  332. skeleton, err := Skeleton(restrictedNickname)
  333. if err != nil {
  334. panic(err)
  335. }
  336. restrictedSkeletons.Add(skeleton)
  337. }
  338. }