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.

hostserv.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // Copyright (c) 2018 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "errors"
  6. "fmt"
  7. "regexp"
  8. "time"
  9. )
  10. const hostservHelp = `HostServ lets you manage your vhost (i.e., the string displayed
  11. in place of your client's hostname/IP).
  12. To see in-depth help for a specific HostServ command, try:
  13. $b/HS HELP <command>$b
  14. Here are the commands you can use:
  15. %s`
  16. var (
  17. errVHostBadCharacters = errors.New("Vhost contains prohibited characters")
  18. errVHostTooLong = errors.New("Vhost is too long")
  19. // ascii only for now
  20. defaultValidVhostRegex = regexp.MustCompile(`^[0-9A-Za-z.\-_/]+$`)
  21. )
  22. func hostservEnabled(config *Config) bool {
  23. return config.Accounts.VHosts.Enabled
  24. }
  25. func hostservRequestsEnabled(config *Config) bool {
  26. return config.Accounts.VHosts.Enabled && config.Accounts.VHosts.UserRequests.Enabled
  27. }
  28. var (
  29. hostservCommands = map[string]*serviceCommand{
  30. "on": {
  31. handler: hsOnOffHandler,
  32. help: `Syntax: $bON$b
  33. ON enables your vhost, if you have one approved.`,
  34. helpShort: `$bON$b enables your vhost, if you have one approved.`,
  35. authRequired: true,
  36. enabled: hostservEnabled,
  37. },
  38. "off": {
  39. handler: hsOnOffHandler,
  40. help: `Syntax: $bOFF$b
  41. OFF disables your vhost, if you have one approved.`,
  42. helpShort: `$bOFF$b disables your vhost, if you have one approved.`,
  43. authRequired: true,
  44. enabled: hostservEnabled,
  45. },
  46. "request": {
  47. handler: hsRequestHandler,
  48. help: `Syntax: $bREQUEST <vhost>$b
  49. REQUEST requests that a new vhost by assigned to your account. The request must
  50. then be approved by a server operator.`,
  51. helpShort: `$bREQUEST$b requests a new vhost, pending operator approval.`,
  52. authRequired: true,
  53. enabled: hostservRequestsEnabled,
  54. minParams: 1,
  55. },
  56. "status": {
  57. handler: hsStatusHandler,
  58. help: `Syntax: $bSTATUS [user]$b
  59. STATUS displays your current vhost, if any, and the status of your most recent
  60. request for a new one. A server operator can view someone else's status.`,
  61. helpShort: `$bSTATUS$b shows your vhost and request status.`,
  62. enabled: hostservEnabled,
  63. },
  64. "set": {
  65. handler: hsSetHandler,
  66. help: `Syntax: $bSET <user> <vhost>$b
  67. SET sets a user's vhost, bypassing the request system.`,
  68. helpShort: `$bSET$b sets a user's vhost.`,
  69. capabs: []string{"vhosts"},
  70. enabled: hostservEnabled,
  71. minParams: 2,
  72. },
  73. "del": {
  74. handler: hsSetHandler,
  75. help: `Syntax: $bDEL <user>$b
  76. DEL deletes a user's vhost.`,
  77. helpShort: `$bDEL$b deletes a user's vhost.`,
  78. capabs: []string{"vhosts"},
  79. enabled: hostservEnabled,
  80. minParams: 1,
  81. },
  82. "waiting": {
  83. handler: hsWaitingHandler,
  84. help: `Syntax: $bWAITING$b
  85. WAITING shows a list of pending vhost requests, which can then be approved
  86. or rejected.`,
  87. helpShort: `$bWAITING$b shows a list of pending vhost requests.`,
  88. capabs: []string{"vhosts"},
  89. enabled: hostservEnabled,
  90. },
  91. "approve": {
  92. handler: hsApproveHandler,
  93. help: `Syntax: $bAPPROVE <user>$b
  94. APPROVE approves a user's vhost request.`,
  95. helpShort: `$bAPPROVE$b approves a user's vhost request.`,
  96. capabs: []string{"vhosts"},
  97. enabled: hostservEnabled,
  98. minParams: 1,
  99. },
  100. "reject": {
  101. handler: hsRejectHandler,
  102. help: `Syntax: $bREJECT <user> [<reason>]$b
  103. REJECT rejects a user's vhost request, optionally giving them a reason
  104. for the rejection.`,
  105. helpShort: `$bREJECT$b rejects a user's vhost request.`,
  106. capabs: []string{"vhosts"},
  107. enabled: hostservEnabled,
  108. minParams: 1,
  109. maxParams: 2,
  110. },
  111. }
  112. )
  113. // hsNotice sends the client a notice from HostServ
  114. func hsNotice(rb *ResponseBuffer, text string) {
  115. rb.Add(nil, "HostServ!HostServ@localhost", "NOTICE", rb.target.Nick(), text)
  116. }
  117. // hsNotifyChannel notifies the designated channel of new vhost activity
  118. func hsNotifyChannel(server *Server, message string) {
  119. chname := server.AccountConfig().VHosts.UserRequests.Channel
  120. channel := server.channels.Get(chname)
  121. if channel == nil {
  122. return
  123. }
  124. chname = channel.Name()
  125. for _, client := range channel.Members() {
  126. client.Send(nil, "HostServ", "PRIVMSG", chname, message)
  127. }
  128. }
  129. func hsOnOffHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  130. enable := false
  131. if command == "on" {
  132. enable = true
  133. }
  134. _, err := server.accounts.VHostSetEnabled(client, enable)
  135. if err == errNoVhost {
  136. hsNotice(rb, client.t(err.Error()))
  137. } else if err != nil {
  138. hsNotice(rb, client.t("An error occurred"))
  139. } else if enable {
  140. hsNotice(rb, client.t("Successfully enabled your vhost"))
  141. } else {
  142. hsNotice(rb, client.t("Successfully disabled your vhost"))
  143. }
  144. }
  145. func hsRequestHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  146. vhost := params[0]
  147. if validateVhost(server, vhost, false) != nil {
  148. hsNotice(rb, client.t("Invalid vhost"))
  149. return
  150. }
  151. accountName := client.Account()
  152. account, err := server.accounts.LoadAccount(client.Account())
  153. if err != nil {
  154. hsNotice(rb, client.t("An error occurred"))
  155. return
  156. }
  157. elapsed := time.Since(account.VHost.LastRequestTime)
  158. remainingTime := server.AccountConfig().VHosts.UserRequests.Cooldown - elapsed
  159. // you can update your existing request, but if you were rejected,
  160. // you can't spam a replacement request
  161. if account.VHost.RequestedVHost == "" && remainingTime > 0 {
  162. hsNotice(rb, fmt.Sprintf(client.t("You must wait an additional %v before making another request"), remainingTime))
  163. return
  164. }
  165. _, err = server.accounts.VHostRequest(accountName, vhost)
  166. if err != nil {
  167. hsNotice(rb, client.t("An error occurred"))
  168. } else {
  169. hsNotice(rb, fmt.Sprintf(client.t("Your vhost request will be reviewed by an administrator")))
  170. chanMsg := fmt.Sprintf("Account %s requests vhost %s", accountName, vhost)
  171. hsNotifyChannel(server, chanMsg)
  172. // TODO send admins a snomask of some kind
  173. }
  174. }
  175. func hsStatusHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  176. var accountName string
  177. if len(params) > 0 {
  178. if !client.HasRoleCapabs("vhosts") {
  179. hsNotice(rb, client.t("Command restricted"))
  180. return
  181. }
  182. accountName = params[0]
  183. } else {
  184. accountName = client.Account()
  185. if accountName == "" {
  186. hsNotice(rb, client.t("You're not logged into an account"))
  187. return
  188. }
  189. }
  190. account, err := server.accounts.LoadAccount(accountName)
  191. if err != nil {
  192. if err != errAccountDoesNotExist {
  193. server.logger.Warning("internal", "error loading account info", accountName, err.Error())
  194. }
  195. hsNotice(rb, client.t("No such account"))
  196. return
  197. }
  198. if account.VHost.ApprovedVHost != "" {
  199. hsNotice(rb, fmt.Sprintf(client.t("Account %[1]s has vhost: %[2]s"), accountName, account.VHost.ApprovedVHost))
  200. if !account.VHost.Enabled {
  201. hsNotice(rb, fmt.Sprintf(client.t("This vhost is currently disabled, but can be enabled with /HS ON")))
  202. }
  203. } else {
  204. hsNotice(rb, fmt.Sprintf(client.t("Account %s has no vhost"), accountName))
  205. }
  206. if account.VHost.RequestedVHost != "" {
  207. hsNotice(rb, fmt.Sprintf(client.t("A request is pending for vhost: %s"), account.VHost.RequestedVHost))
  208. }
  209. if account.VHost.RejectedVHost != "" {
  210. hsNotice(rb, fmt.Sprintf(client.t("A request was previously made for vhost: %s"), account.VHost.RejectedVHost))
  211. hsNotice(rb, fmt.Sprintf(client.t("It was rejected for reason: %s"), account.VHost.RejectionReason))
  212. }
  213. }
  214. func validateVhost(server *Server, vhost string, oper bool) error {
  215. ac := server.AccountConfig()
  216. if len(vhost) > ac.VHosts.MaxLength {
  217. return errVHostTooLong
  218. }
  219. if !ac.VHosts.ValidRegexp.MatchString(vhost) {
  220. return errVHostBadCharacters
  221. }
  222. return nil
  223. }
  224. func hsSetHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  225. user := params[0]
  226. var vhost string
  227. if command == "set" {
  228. vhost = params[1]
  229. if validateVhost(server, vhost, true) != nil {
  230. hsNotice(rb, client.t("Invalid vhost"))
  231. return
  232. }
  233. }
  234. // else: command == "del", vhost == ""
  235. _, err := server.accounts.VHostSet(user, vhost)
  236. if err != nil {
  237. hsNotice(rb, client.t("An error occurred"))
  238. } else if vhost != "" {
  239. hsNotice(rb, client.t("Successfully set vhost"))
  240. } else {
  241. hsNotice(rb, client.t("Successfully cleared vhost"))
  242. }
  243. }
  244. func hsWaitingHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  245. requests, total := server.accounts.VHostListRequests(10)
  246. hsNotice(rb, fmt.Sprintf(client.t("There are %[1]d pending requests for vhosts (%[2]d displayed)"), total, len(requests)))
  247. for i, request := range requests {
  248. hsNotice(rb, fmt.Sprintf(client.t("%[1]d. User %[2]s requests vhost: %[3]s"), i+1, request.Account, request.RequestedVHost))
  249. }
  250. }
  251. func hsApproveHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  252. user := params[0]
  253. vhostInfo, err := server.accounts.VHostApprove(user)
  254. if err != nil {
  255. hsNotice(rb, client.t("An error occurred"))
  256. } else {
  257. hsNotice(rb, fmt.Sprintf(client.t("Successfully approved vhost request for %s"), user))
  258. chanMsg := fmt.Sprintf("Oper %[1]s approved vhost %[2]s for account %[3]s", client.Nick(), vhostInfo.ApprovedVHost, user)
  259. hsNotifyChannel(server, chanMsg)
  260. for _, client := range server.accounts.AccountToClients(user) {
  261. client.Notice(client.t("Your vhost request was approved by an administrator"))
  262. }
  263. }
  264. }
  265. func hsRejectHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  266. var reason string
  267. user := params[0]
  268. if len(params) > 1 {
  269. reason = params[1]
  270. }
  271. vhostInfo, err := server.accounts.VHostReject(user, reason)
  272. if err != nil {
  273. hsNotice(rb, client.t("An error occurred"))
  274. } else {
  275. hsNotice(rb, fmt.Sprintf(client.t("Successfully rejected vhost request for %s"), user))
  276. chanMsg := fmt.Sprintf("Oper %s rejected vhost %s for account %s, with the reason: %v", client.Nick(), vhostInfo.RejectedVHost, user, reason)
  277. hsNotifyChannel(server, chanMsg)
  278. for _, client := range server.accounts.AccountToClients(user) {
  279. if reason == "" {
  280. client.Notice("Your vhost request was rejected by an administrator")
  281. } else {
  282. client.Notice(fmt.Sprintf(client.t("Your vhost request was rejected by an administrator. The reason given was: %s"), reason))
  283. }
  284. }
  285. }
  286. }