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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. unsplitFinalParam: true,
  111. },
  112. }
  113. )
  114. // hsNotice sends the client a notice from HostServ
  115. func hsNotice(rb *ResponseBuffer, text string) {
  116. rb.Add(nil, "HostServ!HostServ@localhost", "NOTICE", rb.target.Nick(), text)
  117. }
  118. // hsNotifyChannel notifies the designated channel of new vhost activity
  119. func hsNotifyChannel(server *Server, message string) {
  120. chname := server.AccountConfig().VHosts.UserRequests.Channel
  121. channel := server.channels.Get(chname)
  122. if channel == nil {
  123. return
  124. }
  125. chname = channel.Name()
  126. for _, client := range channel.Members() {
  127. client.Send(nil, "HostServ", "PRIVMSG", chname, message)
  128. }
  129. }
  130. func hsOnOffHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  131. enable := false
  132. if command == "on" {
  133. enable = true
  134. }
  135. _, err := server.accounts.VHostSetEnabled(client, enable)
  136. if err == errNoVhost {
  137. hsNotice(rb, client.t(err.Error()))
  138. } else if err != nil {
  139. hsNotice(rb, client.t("An error occurred"))
  140. } else if enable {
  141. hsNotice(rb, client.t("Successfully enabled your vhost"))
  142. } else {
  143. hsNotice(rb, client.t("Successfully disabled your vhost"))
  144. }
  145. }
  146. func hsRequestHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  147. vhost := params[0]
  148. if validateVhost(server, vhost, false) != nil {
  149. hsNotice(rb, client.t("Invalid vhost"))
  150. return
  151. }
  152. accountName := client.Account()
  153. account, err := server.accounts.LoadAccount(client.Account())
  154. if err != nil {
  155. hsNotice(rb, client.t("An error occurred"))
  156. return
  157. }
  158. elapsed := time.Since(account.VHost.LastRequestTime)
  159. remainingTime := server.AccountConfig().VHosts.UserRequests.Cooldown - elapsed
  160. // you can update your existing request, but if you were rejected,
  161. // you can't spam a replacement request
  162. if account.VHost.RequestedVHost == "" && remainingTime > 0 {
  163. hsNotice(rb, fmt.Sprintf(client.t("You must wait an additional %v before making another request"), remainingTime))
  164. return
  165. }
  166. _, err = server.accounts.VHostRequest(accountName, vhost)
  167. if err != nil {
  168. hsNotice(rb, client.t("An error occurred"))
  169. } else {
  170. hsNotice(rb, fmt.Sprintf(client.t("Your vhost request will be reviewed by an administrator")))
  171. chanMsg := fmt.Sprintf("Account %s requests vhost %s", accountName, vhost)
  172. hsNotifyChannel(server, chanMsg)
  173. // TODO send admins a snomask of some kind
  174. }
  175. }
  176. func hsStatusHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  177. var accountName string
  178. if len(params) > 0 {
  179. if !client.HasRoleCapabs("vhosts") {
  180. hsNotice(rb, client.t("Command restricted"))
  181. return
  182. }
  183. accountName = params[0]
  184. } else {
  185. accountName = client.Account()
  186. if accountName == "" {
  187. hsNotice(rb, client.t("You're not logged into an account"))
  188. return
  189. }
  190. }
  191. account, err := server.accounts.LoadAccount(accountName)
  192. if err != nil {
  193. if err != errAccountDoesNotExist {
  194. server.logger.Warning("internal", "error loading account info", accountName, err.Error())
  195. }
  196. hsNotice(rb, client.t("No such account"))
  197. return
  198. }
  199. if account.VHost.ApprovedVHost != "" {
  200. hsNotice(rb, fmt.Sprintf(client.t("Account %[1]s has vhost: %[2]s"), accountName, account.VHost.ApprovedVHost))
  201. if !account.VHost.Enabled {
  202. hsNotice(rb, fmt.Sprintf(client.t("This vhost is currently disabled, but can be enabled with /HS ON")))
  203. }
  204. } else {
  205. hsNotice(rb, fmt.Sprintf(client.t("Account %s has no vhost"), accountName))
  206. }
  207. if account.VHost.RequestedVHost != "" {
  208. hsNotice(rb, fmt.Sprintf(client.t("A request is pending for vhost: %s"), account.VHost.RequestedVHost))
  209. }
  210. if account.VHost.RejectedVHost != "" {
  211. hsNotice(rb, fmt.Sprintf(client.t("A request was previously made for vhost: %s"), account.VHost.RejectedVHost))
  212. hsNotice(rb, fmt.Sprintf(client.t("It was rejected for reason: %s"), account.VHost.RejectionReason))
  213. }
  214. }
  215. func validateVhost(server *Server, vhost string, oper bool) error {
  216. ac := server.AccountConfig()
  217. if len(vhost) > ac.VHosts.MaxLength {
  218. return errVHostTooLong
  219. }
  220. if !ac.VHosts.ValidRegexp.MatchString(vhost) {
  221. return errVHostBadCharacters
  222. }
  223. return nil
  224. }
  225. func hsSetHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  226. user := params[0]
  227. var vhost string
  228. if command == "set" {
  229. vhost = params[1]
  230. if validateVhost(server, vhost, true) != nil {
  231. hsNotice(rb, client.t("Invalid vhost"))
  232. return
  233. }
  234. }
  235. // else: command == "del", vhost == ""
  236. _, err := server.accounts.VHostSet(user, vhost)
  237. if err != nil {
  238. hsNotice(rb, client.t("An error occurred"))
  239. } else if vhost != "" {
  240. hsNotice(rb, client.t("Successfully set vhost"))
  241. } else {
  242. hsNotice(rb, client.t("Successfully cleared vhost"))
  243. }
  244. }
  245. func hsWaitingHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  246. requests, total := server.accounts.VHostListRequests(10)
  247. hsNotice(rb, fmt.Sprintf(client.t("There are %[1]d pending requests for vhosts (%[2]d displayed)"), total, len(requests)))
  248. for i, request := range requests {
  249. hsNotice(rb, fmt.Sprintf(client.t("%[1]d. User %[2]s requests vhost: %[3]s"), i+1, request.Account, request.RequestedVHost))
  250. }
  251. }
  252. func hsApproveHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  253. user := params[0]
  254. vhostInfo, err := server.accounts.VHostApprove(user)
  255. if err != nil {
  256. hsNotice(rb, client.t("An error occurred"))
  257. } else {
  258. hsNotice(rb, fmt.Sprintf(client.t("Successfully approved vhost request for %s"), user))
  259. chanMsg := fmt.Sprintf("Oper %[1]s approved vhost %[2]s for account %[3]s", client.Nick(), vhostInfo.ApprovedVHost, user)
  260. hsNotifyChannel(server, chanMsg)
  261. for _, client := range server.accounts.AccountToClients(user) {
  262. client.Notice(client.t("Your vhost request was approved by an administrator"))
  263. }
  264. }
  265. }
  266. func hsRejectHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  267. var reason string
  268. user := params[0]
  269. if len(params) > 1 {
  270. reason = params[1]
  271. }
  272. vhostInfo, err := server.accounts.VHostReject(user, reason)
  273. if err != nil {
  274. hsNotice(rb, client.t("An error occurred"))
  275. } else {
  276. hsNotice(rb, fmt.Sprintf(client.t("Successfully rejected vhost request for %s"), user))
  277. chanMsg := fmt.Sprintf("Oper %s rejected vhost %s for account %s, with the reason: %v", client.Nick(), vhostInfo.RejectedVHost, user, reason)
  278. hsNotifyChannel(server, chanMsg)
  279. for _, client := range server.accounts.AccountToClients(user) {
  280. if reason == "" {
  281. client.Notice("Your vhost request was rejected by an administrator")
  282. } else {
  283. client.Notice(fmt.Sprintf(client.t("Your vhost request was rejected by an administrator. The reason given was: %s"), reason))
  284. }
  285. }
  286. }
  287. }