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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Copyright (c) 2018 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "fmt"
  6. "log"
  7. "sort"
  8. "strings"
  9. "github.com/goshuirc/irc-go/ircfmt"
  10. "github.com/goshuirc/irc-go/ircmsg"
  11. "github.com/oragono/oragono/irc/utils"
  12. )
  13. // defines an IRC service, e.g., NICKSERV
  14. type ircService struct {
  15. Name string
  16. ShortName string
  17. prefix string
  18. CommandAliases []string
  19. Commands map[string]*serviceCommand
  20. HelpBanner string
  21. }
  22. // defines a command associated with a service, e.g., NICKSERV IDENTIFY
  23. type serviceCommand struct {
  24. aliasOf string // marks this command as an alias of another
  25. capabs []string // oper capabs the given user has to have to access this command
  26. handler func(server *Server, client *Client, command string, params []string, rb *ResponseBuffer)
  27. help string
  28. helpStrings []string
  29. helpShort string
  30. authRequired bool
  31. hidden bool
  32. enabled func(*Config) bool // is this command enabled in the server config?
  33. minParams int
  34. maxParams int // split into at most n params, with last param containing remaining unsplit text
  35. }
  36. // looks up a command in the table of command definitions for a service, resolving aliases
  37. func lookupServiceCommand(commands map[string]*serviceCommand, command string) *serviceCommand {
  38. maxDepth := 1
  39. depth := 0
  40. for depth <= maxDepth {
  41. result, ok := commands[command]
  42. if !ok {
  43. return nil
  44. } else if result.aliasOf == "" {
  45. return result
  46. } else {
  47. command = result.aliasOf
  48. depth += 1
  49. }
  50. }
  51. return nil
  52. }
  53. // all services, by lowercase name
  54. var OragonoServices = map[string]*ircService{
  55. "nickserv": {
  56. Name: "NickServ",
  57. ShortName: "NS",
  58. CommandAliases: []string{"NICKSERV", "NS"},
  59. Commands: nickservCommands,
  60. HelpBanner: nickservHelp,
  61. },
  62. "chanserv": {
  63. Name: "ChanServ",
  64. ShortName: "CS",
  65. CommandAliases: []string{"CHANSERV", "CS"},
  66. Commands: chanservCommands,
  67. HelpBanner: chanservHelp,
  68. },
  69. "hostserv": {
  70. Name: "HostServ",
  71. ShortName: "HS",
  72. CommandAliases: []string{"HOSTSERV", "HS"},
  73. Commands: hostservCommands,
  74. HelpBanner: hostservHelp,
  75. },
  76. }
  77. // all service commands at the protocol level, by uppercase command name
  78. // e.g., NICKSERV, NS
  79. var oragonoServicesByCommandAlias map[string]*ircService
  80. // special-cased command shared by all services
  81. var servHelpCmd serviceCommand = serviceCommand{
  82. help: `Syntax: $bHELP [command]$b
  83. HELP returns information on the given command.`,
  84. helpShort: `$bHELP$b shows in-depth information about commands.`,
  85. }
  86. // generic handler for IRC commands like `/NICKSERV INFO`
  87. func serviceCmdHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *ResponseBuffer) bool {
  88. service, ok := oragonoServicesByCommandAlias[msg.Command]
  89. if !ok {
  90. server.logger.Warning("internal", "can't handle unrecognized service", msg.Command)
  91. return false
  92. }
  93. if len(msg.Params) == 0 {
  94. return false
  95. }
  96. commandName := strings.ToLower(msg.Params[0])
  97. params := msg.Params[1:]
  98. cmd := lookupServiceCommand(service.Commands, commandName)
  99. // for a maxParams command, join all final parameters together if necessary
  100. if cmd != nil && cmd.maxParams != 0 && cmd.maxParams < len(params) {
  101. newParams := make([]string, cmd.maxParams)
  102. copy(newParams, params[:cmd.maxParams-1])
  103. newParams[cmd.maxParams-1] = strings.Join(params[cmd.maxParams-1:], " ")
  104. params = newParams
  105. }
  106. serviceRunCommand(service, server, client, cmd, commandName, params, rb)
  107. return false
  108. }
  109. // generic handler for service PRIVMSG, like `/msg NickServ INFO`
  110. func servicePrivmsgHandler(service *ircService, server *Server, client *Client, message string, rb *ResponseBuffer) {
  111. params := strings.Fields(message)
  112. if len(params) == 0 {
  113. return
  114. }
  115. // look up the service command to see how to parse it
  116. commandName := strings.ToLower(params[0])
  117. cmd := lookupServiceCommand(service.Commands, commandName)
  118. // reparse if needed
  119. if cmd != nil && cmd.maxParams != 0 {
  120. params = utils.FieldsN(message, cmd.maxParams+1)[1:]
  121. } else {
  122. params = params[1:]
  123. }
  124. serviceRunCommand(service, server, client, cmd, commandName, params, rb)
  125. }
  126. // actually execute a service command
  127. func serviceRunCommand(service *ircService, server *Server, client *Client, cmd *serviceCommand, commandName string, params []string, rb *ResponseBuffer) {
  128. nick := rb.target.Nick()
  129. sendNotice := func(notice string) {
  130. rb.Add(nil, service.prefix, "NOTICE", nick, notice)
  131. }
  132. if cmd == nil {
  133. sendNotice(fmt.Sprintf(client.t("Unknown command. To see available commands, run: /%s HELP"), service.ShortName))
  134. return
  135. }
  136. if len(params) < cmd.minParams {
  137. sendNotice(fmt.Sprintf(client.t("Invalid parameters. For usage, do /msg %[1]s HELP %[2]s"), service.Name, strings.ToUpper(commandName)))
  138. return
  139. }
  140. if cmd.enabled != nil && !cmd.enabled(server.Config()) {
  141. sendNotice(client.t("This command has been disabled by the server administrators"))
  142. return
  143. }
  144. if 0 < len(cmd.capabs) && !client.HasRoleCapabs(cmd.capabs...) {
  145. sendNotice(client.t("Command restricted"))
  146. return
  147. }
  148. if cmd.authRequired && client.Account() == "" {
  149. sendNotice(client.t("You're not logged into an account"))
  150. return
  151. }
  152. server.logger.Debug("services", fmt.Sprintf("Client %s ran %s command %s", client.Nick(), service.Name, commandName))
  153. if commandName == "help" {
  154. serviceHelpHandler(service, server, client, params, rb)
  155. } else {
  156. cmd.handler(server, client, commandName, params, rb)
  157. }
  158. }
  159. // generic handler that displays help for service commands
  160. func serviceHelpHandler(service *ircService, server *Server, client *Client, params []string, rb *ResponseBuffer) {
  161. nick := rb.target.Nick()
  162. config := server.Config()
  163. sendNotice := func(notice string) {
  164. rb.Add(nil, service.prefix, "NOTICE", nick, notice)
  165. }
  166. sendNotice(ircfmt.Unescape(fmt.Sprintf("*** $b%s HELP$b ***", service.Name)))
  167. if len(params) == 0 {
  168. // show general help
  169. var shownHelpLines sort.StringSlice
  170. var disabledCommands bool
  171. for _, commandInfo := range service.Commands {
  172. // skip commands user can't access
  173. if 0 < len(commandInfo.capabs) && !client.HasRoleCapabs(commandInfo.capabs...) {
  174. continue
  175. }
  176. if commandInfo.aliasOf != "" || commandInfo.hidden {
  177. continue // don't show help lines for aliases
  178. }
  179. if commandInfo.enabled != nil && !commandInfo.enabled(config) {
  180. disabledCommands = true
  181. continue
  182. }
  183. shownHelpLines = append(shownHelpLines, " "+client.t(commandInfo.helpShort))
  184. }
  185. if disabledCommands {
  186. shownHelpLines = append(shownHelpLines, " "+client.t("... and other commands which have been disabled"))
  187. }
  188. // sort help lines
  189. sort.Sort(shownHelpLines)
  190. // assemble help text
  191. assembledHelpLines := strings.Join(shownHelpLines, "\n")
  192. fullHelp := ircfmt.Unescape(fmt.Sprintf(client.t(service.HelpBanner), assembledHelpLines))
  193. // push out help text
  194. for _, line := range strings.Split(fullHelp, "\n") {
  195. sendNotice(line)
  196. }
  197. } else {
  198. commandName := strings.ToLower(params[0])
  199. commandInfo := lookupServiceCommand(service.Commands, commandName)
  200. if commandInfo == nil {
  201. sendNotice(client.t(fmt.Sprintf("Unknown command. To see available commands, run /%s HELP", service.ShortName)))
  202. } else {
  203. helpStrings := commandInfo.helpStrings
  204. if helpStrings == nil {
  205. hsArray := [1]string{commandInfo.help}
  206. helpStrings = hsArray[:]
  207. }
  208. for i, helpString := range helpStrings {
  209. if 0 < i {
  210. sendNotice("")
  211. }
  212. for _, line := range strings.Split(ircfmt.Unescape(client.t(helpString)), "\n") {
  213. sendNotice(line)
  214. }
  215. }
  216. }
  217. }
  218. sendNotice(ircfmt.Unescape(fmt.Sprintf(client.t("*** $bEnd of %s HELP$b ***"), service.Name)))
  219. }
  220. func initializeServices() {
  221. // this modifies the global Commands map,
  222. // so it must be called from irc/commands.go's init()
  223. oragonoServicesByCommandAlias = make(map[string]*ircService)
  224. for serviceName, service := range OragonoServices {
  225. service.prefix = fmt.Sprintf("%s!%s@localhost", service.Name, service.Name)
  226. // make `/MSG ServiceName HELP` work correctly
  227. service.Commands["help"] = &servHelpCmd
  228. // reserve the nickname
  229. restrictedNicknames[serviceName] = true
  230. // register the protocol-level commands (NICKSERV, NS) that talk to the service
  231. var ircCmdDef Command
  232. ircCmdDef.handler = serviceCmdHandler
  233. for _, ircCmd := range service.CommandAliases {
  234. Commands[ircCmd] = ircCmdDef
  235. oragonoServicesByCommandAlias[ircCmd] = service
  236. }
  237. // force devs to write a help entry for every command
  238. for commandName, commandInfo := range service.Commands {
  239. if commandInfo.aliasOf == "" && !commandInfo.hidden {
  240. if (commandInfo.help == "" && commandInfo.helpStrings == nil) || commandInfo.helpShort == "" {
  241. log.Fatal(fmt.Sprintf("help entry missing for %s command %s", serviceName, commandName))
  242. }
  243. }
  244. }
  245. }
  246. }