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.

chanserv.go 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. // Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "fmt"
  6. "regexp"
  7. "sort"
  8. "strings"
  9. "time"
  10. "github.com/goshuirc/irc-go/ircfmt"
  11. "github.com/oragono/oragono/irc/modes"
  12. "github.com/oragono/oragono/irc/sno"
  13. "github.com/oragono/oragono/irc/utils"
  14. )
  15. const chanservHelp = `ChanServ lets you register and manage channels.`
  16. func chanregEnabled(config *Config) bool {
  17. return config.Channels.Registration.Enabled
  18. }
  19. var (
  20. chanservCommands = map[string]*serviceCommand{
  21. "op": {
  22. handler: csOpHandler,
  23. help: `Syntax: $bOP #channel [nickname]$b
  24. OP makes the given nickname, or yourself, a channel admin. You can only use
  25. this command if you're the founder of the channel.`,
  26. helpShort: `$bOP$b makes the given user (or yourself) a channel admin.`,
  27. authRequired: true,
  28. enabled: chanregEnabled,
  29. minParams: 1,
  30. },
  31. "deop": {
  32. handler: csDeopHandler,
  33. help: `Syntax: $bDEOP #channel [nickname]$b
  34. DEOP removes the given nickname, or yourself, the channel admin. You can only use
  35. this command if you're the founder of the channel.`,
  36. helpShort: `$bDEOP$b removes the given user (or yourself) from a channel admin.`,
  37. enabled: chanregEnabled,
  38. minParams: 1,
  39. },
  40. "register": {
  41. handler: csRegisterHandler,
  42. help: `Syntax: $bREGISTER #channel$b
  43. REGISTER lets you own the given channel. If you rejoin this channel, you'll be
  44. given admin privs on it. Modes set on the channel and the topic will also be
  45. remembered.`,
  46. helpShort: `$bREGISTER$b lets you own a given channel.`,
  47. authRequired: true,
  48. enabled: chanregEnabled,
  49. minParams: 1,
  50. },
  51. "unregister": {
  52. handler: csUnregisterHandler,
  53. help: `Syntax: $bUNREGISTER #channel [code]$b
  54. UNREGISTER deletes a channel registration, allowing someone else to claim it.
  55. To prevent accidental unregistrations, a verification code is required;
  56. invoking the command without a code will display the necessary code.`,
  57. helpShort: `$bUNREGISTER$b deletes a channel registration.`,
  58. enabled: chanregEnabled,
  59. minParams: 1,
  60. },
  61. "drop": {
  62. aliasOf: "unregister",
  63. },
  64. "amode": {
  65. handler: csAmodeHandler,
  66. help: `Syntax: $bAMODE #channel [mode change] [account]$b
  67. AMODE lists or modifies persistent mode settings that affect channel members.
  68. For example, $bAMODE #channel +o dan$b grants the the holder of the "dan"
  69. account the +o operator mode every time they join #channel. To list current
  70. accounts and modes, use $bAMODE #channel$b. Note that users are always
  71. referenced by their registered account names, not their nicknames.`,
  72. helpShort: `$bAMODE$b modifies persistent mode settings for channel members.`,
  73. enabled: chanregEnabled,
  74. minParams: 1,
  75. },
  76. "clear": {
  77. handler: csClearHandler,
  78. help: `Syntax: $bCLEAR #channel target$b
  79. CLEAR removes users or settings from a channel. Specifically:
  80. $bCLEAR #channel users$b kicks all users except for you.
  81. $bCLEAR #channel access$b resets all stored bans, invites, ban exceptions,
  82. and persistent user-mode grants made with CS AMODE.`,
  83. helpShort: `$bCLEAR$b removes users or settings from a channel.`,
  84. enabled: chanregEnabled,
  85. minParams: 2,
  86. },
  87. "transfer": {
  88. handler: csTransferHandler,
  89. help: `Syntax: $bTRANSFER [accept] #channel user [code]$b
  90. TRANSFER transfers ownership of a channel from one user to another.
  91. To prevent accidental transfers, a verification code is required. For
  92. example, $bTRANSFER #channel alice$b displays the required confirmation
  93. code, then $bTRANSFER #channel alice 2930242125$b initiates the transfer.
  94. Unless you are an IRC operator with the correct permissions, alice must
  95. then accept the transfer, which she can do with $bTRANSFER accept #channel$b.
  96. To cancel a pending transfer, transfer the channel to yourself.`,
  97. helpShort: `$bTRANSFER$b transfers ownership of a channel to another user.`,
  98. enabled: chanregEnabled,
  99. minParams: 2,
  100. },
  101. "purge": {
  102. handler: csPurgeHandler,
  103. help: `Syntax: $bPURGE #channel [reason]$b
  104. PURGE blacklists a channel from the server, making it impossible to join
  105. or otherwise interact with the channel. If the channel currently has members,
  106. they will be kicked from it. PURGE may also be applied preemptively to
  107. channels that do not currently have members.`,
  108. helpShort: `$bPURGE$b blacklists a channel from the server.`,
  109. capabs: []string{"chanreg"},
  110. minParams: 1,
  111. maxParams: 2,
  112. unsplitFinalParam: true,
  113. },
  114. "unpurge": {
  115. handler: csUnpurgeHandler,
  116. help: `Syntax: $bUNPURGE #channel$b
  117. UNPURGE removes any blacklisting of a channel that was previously
  118. set using PURGE.`,
  119. helpShort: `$bUNPURGE$b undoes a previous PURGE command.`,
  120. capabs: []string{"chanreg"},
  121. minParams: 1,
  122. },
  123. "list": {
  124. handler: csListHandler,
  125. help: `Syntax: $bLIST [regex]$b
  126. LIST returns the list of registered channels, which match the given regex.
  127. If no regex is provided, all registered channels are returned.`,
  128. helpShort: `$bLIST$b searches the list of registered channels.`,
  129. capabs: []string{"chanreg"},
  130. minParams: 0,
  131. },
  132. "info": {
  133. handler: csInfoHandler,
  134. help: `Syntax: $INFO #channel$b
  135. INFO displays info about a registered channel.`,
  136. helpShort: `$bINFO$b displays info about a registered channel.`,
  137. enabled: chanregEnabled,
  138. minParams: 1,
  139. },
  140. "get": {
  141. handler: csGetHandler,
  142. help: `Syntax: $bGET #channel <setting>$b
  143. GET queries the current values of the channel settings. For more information
  144. on the settings and their possible values, see HELP SET.`,
  145. helpShort: `$bGET$b queries the current values of a channel's settings`,
  146. enabled: chanregEnabled,
  147. minParams: 2,
  148. },
  149. "set": {
  150. handler: csSetHandler,
  151. helpShort: `$bSET$b modifies a channel's settings`,
  152. // these are broken out as separate strings so they can be translated separately
  153. helpStrings: []string{
  154. `Syntax $bSET #channel <setting> <value>$b
  155. SET modifies a channel's settings. The following settings are available:`,
  156. `$bHISTORY$b
  157. 'history' lets you control how channel history is stored. Your options are:
  158. 1. 'off' [no history]
  159. 2. 'ephemeral' [a limited amount of temporary history, not stored on disk]
  160. 3. 'on' [history stored in a permanent database, if available]
  161. 4. 'default' [use the server default]`,
  162. },
  163. enabled: chanregEnabled,
  164. minParams: 3,
  165. },
  166. }
  167. )
  168. func csAmodeHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  169. channelName := params[0]
  170. channel := server.channels.Get(channelName)
  171. if channel == nil {
  172. service.Notice(rb, client.t("Channel does not exist"))
  173. return
  174. } else if channel.Founder() == "" {
  175. service.Notice(rb, client.t("Channel is not registered"))
  176. return
  177. }
  178. modeChanges, unknown := modes.ParseChannelModeChanges(params[1:]...)
  179. var change modes.ModeChange
  180. if len(modeChanges) > 1 || len(unknown) > 0 {
  181. service.Notice(rb, client.t("Invalid mode change"))
  182. return
  183. } else if len(modeChanges) == 1 {
  184. change = modeChanges[0]
  185. } else {
  186. change = modes.ModeChange{Op: modes.List}
  187. }
  188. // normalize and validate the account argument
  189. accountIsValid := false
  190. change.Arg, _ = CasefoldName(change.Arg)
  191. switch change.Op {
  192. case modes.List:
  193. accountIsValid = true
  194. case modes.Add:
  195. // if we're adding a mode, the account must exist
  196. if change.Arg != "" {
  197. _, err := server.accounts.LoadAccount(change.Arg)
  198. accountIsValid = (err == nil)
  199. }
  200. case modes.Remove:
  201. // allow removal of accounts that may have been deleted
  202. accountIsValid = (change.Arg != "")
  203. }
  204. if !accountIsValid {
  205. service.Notice(rb, client.t("Account does not exist"))
  206. return
  207. }
  208. affectedModes, err := channel.ProcessAccountToUmodeChange(client, change)
  209. if err == errInsufficientPrivs {
  210. service.Notice(rb, client.t("Insufficient privileges"))
  211. return
  212. } else if err != nil {
  213. service.Notice(rb, client.t("Internal error"))
  214. return
  215. }
  216. switch change.Op {
  217. case modes.List:
  218. // sort the persistent modes in descending order of priority
  219. sort.Slice(affectedModes, func(i, j int) bool {
  220. return umodeGreaterThan(affectedModes[i].Mode, affectedModes[j].Mode)
  221. })
  222. service.Notice(rb, fmt.Sprintf(client.t("Channel %[1]s has %[2]d persistent modes set"), channelName, len(affectedModes)))
  223. for _, modeChange := range affectedModes {
  224. service.Notice(rb, fmt.Sprintf(client.t("Account %[1]s receives mode +%[2]s"), modeChange.Arg, string(modeChange.Mode)))
  225. }
  226. case modes.Add, modes.Remove:
  227. if len(affectedModes) > 0 {
  228. service.Notice(rb, fmt.Sprintf(client.t("Successfully set persistent mode %[1]s on %[2]s"), strings.Join([]string{string(change.Op), string(change.Mode)}, ""), change.Arg))
  229. // #729: apply change to current membership
  230. for _, member := range channel.Members() {
  231. if member.Account() == change.Arg {
  232. applied, change := channel.applyModeToMember(client, change, rb)
  233. if applied {
  234. announceCmodeChanges(channel, modes.ModeChanges{change}, server.name, "*", "", rb)
  235. }
  236. }
  237. }
  238. } else {
  239. service.Notice(rb, client.t("No changes were made"))
  240. }
  241. }
  242. }
  243. func csOpHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  244. channelInfo := server.channels.Get(params[0])
  245. if channelInfo == nil {
  246. service.Notice(rb, client.t("Channel does not exist"))
  247. return
  248. }
  249. channelName := channelInfo.Name()
  250. clientAccount := client.Account()
  251. if clientAccount == "" || clientAccount != channelInfo.Founder() {
  252. service.Notice(rb, client.t("Only the channel founder can do this"))
  253. return
  254. }
  255. var target *Client
  256. if len(params) > 1 {
  257. target = server.clients.Get(params[1])
  258. if target == nil {
  259. service.Notice(rb, client.t("Could not find given client"))
  260. return
  261. }
  262. } else {
  263. target = client
  264. }
  265. // give them privs
  266. givenMode := modes.ChannelOperator
  267. if clientAccount == target.Account() {
  268. givenMode = modes.ChannelFounder
  269. }
  270. applied, change := channelInfo.applyModeToMember(client,
  271. modes.ModeChange{Mode: givenMode,
  272. Op: modes.Add,
  273. Arg: target.NickCasefolded(),
  274. },
  275. rb)
  276. if applied {
  277. announceCmodeChanges(channelInfo, modes.ModeChanges{change}, server.name, "*", "", rb)
  278. }
  279. service.Notice(rb, client.t("Successfully granted operator privileges"))
  280. tnick := target.Nick()
  281. server.logger.Info("services", fmt.Sprintf("Client %s op'd [%s] in channel %s", client.Nick(), tnick, channelName))
  282. server.snomasks.Send(sno.LocalChannels, fmt.Sprintf(ircfmt.Unescape("Client $c[grey][$r%s$c[grey]] CS OP'd $c[grey][$r%s$c[grey]] in channel $c[grey][$r%s$c[grey]]"), client.NickMaskString(), tnick, channelName))
  283. }
  284. func csDeopHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  285. channel := server.channels.Get(params[0])
  286. if channel == nil {
  287. service.Notice(rb, client.t("Channel does not exist"))
  288. return
  289. }
  290. if !channel.hasClient(client) {
  291. service.Notice(rb, client.t("You're not on that channel"))
  292. return
  293. }
  294. var target *Client
  295. if len(params) > 1 {
  296. target = server.clients.Get(params[1])
  297. if target == nil {
  298. service.Notice(rb, client.t("Could not find given client"))
  299. return
  300. }
  301. } else {
  302. target = client
  303. }
  304. present, cumodes := channel.ClientStatus(target)
  305. if !present || len(cumodes) == 0 {
  306. service.Notice(rb, client.t("Target has no privileges to remove"))
  307. return
  308. }
  309. tnick := target.Nick()
  310. modeChanges := make(modes.ModeChanges, len(cumodes))
  311. for i, mode := range cumodes {
  312. modeChanges[i] = modes.ModeChange{
  313. Mode: mode,
  314. Op: modes.Remove,
  315. Arg: tnick,
  316. }
  317. }
  318. // use the user's own permissions for the check, then announce
  319. // the changes as coming from chanserv
  320. applied := channel.ApplyChannelModeChanges(client, false, modeChanges, rb)
  321. details := client.Details()
  322. announceCmodeChanges(channel, applied, details.nickMask, details.accountName, details.account, rb)
  323. if len(applied) == 0 {
  324. return
  325. }
  326. service.Notice(rb, client.t("Successfully removed operator privileges"))
  327. }
  328. func csRegisterHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  329. if server.Config().Channels.Registration.OperatorOnly && !client.HasRoleCapabs("chanreg") {
  330. service.Notice(rb, client.t("Channel registration is restricted to server operators"))
  331. return
  332. }
  333. channelName := params[0]
  334. channelInfo := server.channels.Get(channelName)
  335. if channelInfo == nil {
  336. service.Notice(rb, client.t("No such channel"))
  337. return
  338. }
  339. if !channelInfo.ClientIsAtLeast(client, modes.ChannelOperator) {
  340. service.Notice(rb, client.t("You must be an oper on the channel to register it"))
  341. return
  342. }
  343. account := client.Account()
  344. if !checkChanLimit(service, client, rb) {
  345. return
  346. }
  347. // this provides the synchronization that allows exactly one registration of the channel:
  348. err := server.channels.SetRegistered(channelName, account)
  349. if err != nil {
  350. service.Notice(rb, err.Error())
  351. return
  352. }
  353. service.Notice(rb, fmt.Sprintf(client.t("Channel %s successfully registered"), channelName))
  354. server.logger.Info("services", fmt.Sprintf("Client %s registered channel %s", client.Nick(), channelName))
  355. server.snomasks.Send(sno.LocalChannels, fmt.Sprintf(ircfmt.Unescape("Channel registered $c[grey][$r%s$c[grey]] by $c[grey][$r%s$c[grey]]"), channelName, client.nickMaskString))
  356. // give them founder privs
  357. applied, change := channelInfo.applyModeToMember(client,
  358. modes.ModeChange{
  359. Mode: modes.ChannelFounder,
  360. Op: modes.Add,
  361. Arg: client.NickCasefolded(),
  362. },
  363. rb)
  364. if applied {
  365. announceCmodeChanges(channelInfo, modes.ModeChanges{change}, service.prefix, "*", "", rb)
  366. }
  367. }
  368. // check whether a client has already registered too many channels
  369. func checkChanLimit(service *ircService, client *Client, rb *ResponseBuffer) (ok bool) {
  370. account := client.Account()
  371. channelsAlreadyRegistered := client.server.accounts.ChannelsForAccount(account)
  372. ok = len(channelsAlreadyRegistered) < client.server.Config().Channels.Registration.MaxChannelsPerAccount || client.HasRoleCapabs("chanreg")
  373. if !ok {
  374. service.Notice(rb, client.t("You have already registered the maximum number of channels; try dropping some with /CS UNREGISTER"))
  375. }
  376. return
  377. }
  378. func csPrivsCheck(service *ircService, channel RegisteredChannel, client *Client, rb *ResponseBuffer) (success bool) {
  379. founder := channel.Founder
  380. if founder == "" {
  381. service.Notice(rb, client.t("That channel is not registered"))
  382. return false
  383. }
  384. if client.HasRoleCapabs("chanreg") {
  385. return true
  386. }
  387. if founder != client.Account() {
  388. service.Notice(rb, client.t("Insufficient privileges"))
  389. return false
  390. }
  391. return true
  392. }
  393. func csUnregisterHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  394. channelName := params[0]
  395. var verificationCode string
  396. if len(params) > 1 {
  397. verificationCode = params[1]
  398. }
  399. channel := server.channels.Get(channelName)
  400. if channel == nil {
  401. service.Notice(rb, client.t("No such channel"))
  402. return
  403. }
  404. info := channel.ExportRegistration(0)
  405. channelKey := info.NameCasefolded
  406. if !csPrivsCheck(service, info, client, rb) {
  407. return
  408. }
  409. expectedCode := utils.ConfirmationCode(info.Name, info.RegisteredAt)
  410. if expectedCode != verificationCode {
  411. service.Notice(rb, ircfmt.Unescape(client.t("$bWarning: unregistering this channel will remove all stored channel attributes.$b")))
  412. service.Notice(rb, fmt.Sprintf(client.t("To confirm, run this command: %s"), fmt.Sprintf("/CS UNREGISTER %s %s", channelKey, expectedCode)))
  413. return
  414. }
  415. server.channels.SetUnregistered(channelKey, info.Founder)
  416. service.Notice(rb, fmt.Sprintf(client.t("Channel %s is now unregistered"), channelKey))
  417. }
  418. func csClearHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  419. channel := server.channels.Get(params[0])
  420. if channel == nil {
  421. service.Notice(rb, client.t("Channel does not exist"))
  422. return
  423. }
  424. if !csPrivsCheck(service, channel.ExportRegistration(0), client, rb) {
  425. return
  426. }
  427. switch strings.ToLower(params[1]) {
  428. case "access":
  429. channel.resetAccess()
  430. service.Notice(rb, client.t("Successfully reset channel access"))
  431. case "users":
  432. for _, target := range channel.Members() {
  433. if target != client {
  434. channel.Kick(client, target, "Cleared by ChanServ", rb, true)
  435. }
  436. }
  437. default:
  438. service.Notice(rb, client.t("Invalid parameters"))
  439. }
  440. }
  441. func csTransferHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  442. if strings.ToLower(params[0]) == "accept" {
  443. processTransferAccept(service, client, params[1], rb)
  444. return
  445. }
  446. chname := params[0]
  447. channel := server.channels.Get(chname)
  448. if channel == nil {
  449. service.Notice(rb, client.t("Channel does not exist"))
  450. return
  451. }
  452. regInfo := channel.ExportRegistration(0)
  453. chname = regInfo.Name
  454. account := client.Account()
  455. isFounder := account != "" && account == regInfo.Founder
  456. hasPrivs := client.HasRoleCapabs("chanreg")
  457. if !(isFounder || hasPrivs) {
  458. service.Notice(rb, client.t("Insufficient privileges"))
  459. return
  460. }
  461. target := params[1]
  462. targetAccount, err := server.accounts.LoadAccount(params[1])
  463. if err != nil {
  464. service.Notice(rb, client.t("Account does not exist"))
  465. return
  466. }
  467. if targetAccount.NameCasefolded != account {
  468. expectedCode := utils.ConfirmationCode(regInfo.Name, regInfo.RegisteredAt)
  469. codeValidated := 2 < len(params) && params[2] == expectedCode
  470. if !codeValidated {
  471. service.Notice(rb, ircfmt.Unescape(client.t("$bWarning: you are about to transfer control of your channel to another user.$b")))
  472. service.Notice(rb, fmt.Sprintf(client.t("To confirm your channel transfer, type: /CS TRANSFER %[1]s %[2]s %[3]s"), chname, target, expectedCode))
  473. return
  474. }
  475. }
  476. status, err := channel.Transfer(client, target, hasPrivs)
  477. if err == nil {
  478. switch status {
  479. case channelTransferComplete:
  480. service.Notice(rb, fmt.Sprintf(client.t("Successfully transferred channel %[1]s to account %[2]s"), chname, target))
  481. case channelTransferPending:
  482. sendTransferPendingNotice(service, server, target, chname)
  483. service.Notice(rb, fmt.Sprintf(client.t("Transfer of channel %[1]s to account %[2]s succeeded, pending acceptance"), chname, target))
  484. case channelTransferCancelled:
  485. service.Notice(rb, fmt.Sprintf(client.t("Cancelled pending transfer of channel %s"), chname))
  486. }
  487. } else {
  488. service.Notice(rb, client.t("Could not transfer channel"))
  489. }
  490. }
  491. func sendTransferPendingNotice(service *ircService, server *Server, account, chname string) {
  492. clients := server.accounts.AccountToClients(account)
  493. if len(clients) == 0 {
  494. return
  495. }
  496. var client *Client
  497. for _, candidate := range clients {
  498. client = candidate
  499. if candidate.NickCasefolded() == candidate.Account() {
  500. break // prefer the login where the nick is the account
  501. }
  502. }
  503. client.Send(nil, service.prefix, "NOTICE", client.Nick(), fmt.Sprintf(client.t("You have been offered ownership of channel %[1]s. To accept, /CS TRANSFER ACCEPT %[1]s"), chname))
  504. }
  505. func processTransferAccept(service *ircService, client *Client, chname string, rb *ResponseBuffer) {
  506. channel := client.server.channels.Get(chname)
  507. if channel == nil {
  508. service.Notice(rb, client.t("Channel does not exist"))
  509. return
  510. }
  511. if !checkChanLimit(service, client, rb) {
  512. return
  513. }
  514. switch channel.AcceptTransfer(client) {
  515. case nil:
  516. service.Notice(rb, fmt.Sprintf(client.t("Successfully accepted ownership of channel %s"), channel.Name()))
  517. case errChannelTransferNotOffered:
  518. service.Notice(rb, fmt.Sprintf(client.t("You weren't offered ownership of channel %s"), channel.Name()))
  519. default:
  520. service.Notice(rb, fmt.Sprintf(client.t("Could not accept ownership of channel %s"), channel.Name()))
  521. }
  522. }
  523. func csPurgeHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  524. oper := client.Oper()
  525. if oper == nil {
  526. return // should be impossible because you need oper capabs for this
  527. }
  528. chname := params[0]
  529. var reason string
  530. if 1 < len(params) {
  531. reason = params[1]
  532. }
  533. purgeRecord := ChannelPurgeRecord{
  534. Oper: oper.Name,
  535. PurgedAt: time.Now().UTC(),
  536. Reason: reason,
  537. }
  538. switch server.channels.Purge(chname, purgeRecord) {
  539. case nil:
  540. channel := server.channels.Get(chname)
  541. if channel != nil { // channel need not exist to be purged
  542. for _, target := range channel.Members() {
  543. channel.Kick(client, target, "Cleared by ChanServ", rb, true)
  544. }
  545. }
  546. service.Notice(rb, fmt.Sprintf(client.t("Successfully purged channel %s from the server"), chname))
  547. case errInvalidChannelName:
  548. service.Notice(rb, fmt.Sprintf(client.t("Can't purge invalid channel %s"), chname))
  549. default:
  550. service.Notice(rb, client.t("An error occurred"))
  551. }
  552. }
  553. func csUnpurgeHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  554. chname := params[0]
  555. switch server.channels.Unpurge(chname) {
  556. case nil:
  557. service.Notice(rb, fmt.Sprintf(client.t("Successfully unpurged channel %s from the server"), chname))
  558. case errNoSuchChannel:
  559. service.Notice(rb, fmt.Sprintf(client.t("Channel %s wasn't previously purged from the server"), chname))
  560. default:
  561. service.Notice(rb, client.t("An error occurred"))
  562. }
  563. }
  564. func csListHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  565. if !client.HasRoleCapabs("chanreg") {
  566. service.Notice(rb, client.t("Insufficient privileges"))
  567. return
  568. }
  569. var searchRegex *regexp.Regexp
  570. if len(params) > 0 {
  571. var err error
  572. searchRegex, err = regexp.Compile(params[0])
  573. if err != nil {
  574. service.Notice(rb, client.t("Invalid regex"))
  575. return
  576. }
  577. }
  578. service.Notice(rb, ircfmt.Unescape(client.t("*** $bChanServ LIST$b ***")))
  579. channels := server.channelRegistry.AllChannels()
  580. for _, channel := range channels {
  581. if searchRegex == nil || searchRegex.MatchString(channel) {
  582. service.Notice(rb, fmt.Sprintf(" %s", channel))
  583. }
  584. }
  585. service.Notice(rb, ircfmt.Unescape(client.t("*** $bEnd of ChanServ LIST$b ***")))
  586. }
  587. func csInfoHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  588. chname, err := CasefoldChannel(params[0])
  589. if err != nil {
  590. service.Notice(rb, client.t("Invalid channel name"))
  591. return
  592. }
  593. // purge status
  594. if client.HasRoleCapabs("chanreg") {
  595. purgeRecord, err := server.channelRegistry.LoadPurgeRecord(chname)
  596. if err == nil {
  597. service.Notice(rb, fmt.Sprintf(client.t("Channel %s was purged by the server operators and cannot be used"), chname))
  598. service.Notice(rb, fmt.Sprintf(client.t("Purged by operator: %s"), purgeRecord.Oper))
  599. service.Notice(rb, fmt.Sprintf(client.t("Purged at: %s"), purgeRecord.PurgedAt.Format(time.RFC1123)))
  600. if purgeRecord.Reason != "" {
  601. service.Notice(rb, fmt.Sprintf(client.t("Purge reason: %s"), purgeRecord.Reason))
  602. }
  603. }
  604. } else {
  605. if server.channels.IsPurged(chname) {
  606. service.Notice(rb, fmt.Sprintf(client.t("Channel %s was purged by the server operators and cannot be used"), chname))
  607. }
  608. }
  609. var chinfo RegisteredChannel
  610. channel := server.channels.Get(params[0])
  611. if channel != nil {
  612. chinfo = channel.ExportRegistration(0)
  613. } else {
  614. chinfo, err = server.channelRegistry.LoadChannel(chname)
  615. if err != nil && !(err == errNoSuchChannel || err == errFeatureDisabled) {
  616. service.Notice(rb, client.t("An error occurred"))
  617. return
  618. }
  619. }
  620. // channel exists but is unregistered, or doesn't exist:
  621. if chinfo.Founder == "" {
  622. service.Notice(rb, fmt.Sprintf(client.t("Channel %s is not registered"), chname))
  623. return
  624. }
  625. service.Notice(rb, fmt.Sprintf(client.t("Channel %s is registered"), chinfo.Name))
  626. service.Notice(rb, fmt.Sprintf(client.t("Founder: %s"), chinfo.Founder))
  627. service.Notice(rb, fmt.Sprintf(client.t("Registered at: %s"), chinfo.RegisteredAt.Format(time.RFC1123)))
  628. }
  629. func displayChannelSetting(service *ircService, settingName string, settings ChannelSettings, client *Client, rb *ResponseBuffer) {
  630. config := client.server.Config()
  631. switch strings.ToLower(settingName) {
  632. case "history":
  633. effectiveValue := historyEnabled(config.History.Persistent.RegisteredChannels, settings.History)
  634. service.Notice(rb, fmt.Sprintf(client.t("The stored channel history setting is: %s"), historyStatusToString(settings.History)))
  635. service.Notice(rb, fmt.Sprintf(client.t("Given current server settings, the channel history setting is: %s"), historyStatusToString(effectiveValue)))
  636. default:
  637. service.Notice(rb, client.t("Invalid params"))
  638. }
  639. }
  640. func csGetHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  641. chname, setting := params[0], params[1]
  642. channel := server.channels.Get(chname)
  643. if channel == nil {
  644. service.Notice(rb, client.t("No such channel"))
  645. return
  646. }
  647. info := channel.ExportRegistration(IncludeSettings)
  648. if !csPrivsCheck(service, info, client, rb) {
  649. return
  650. }
  651. displayChannelSetting(service, setting, info.Settings, client, rb)
  652. }
  653. func csSetHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  654. chname, setting, value := params[0], params[1], params[2]
  655. channel := server.channels.Get(chname)
  656. if channel == nil {
  657. service.Notice(rb, client.t("No such channel"))
  658. return
  659. }
  660. info := channel.ExportRegistration(IncludeSettings)
  661. settings := info.Settings
  662. if !csPrivsCheck(service, info, client, rb) {
  663. return
  664. }
  665. var err error
  666. switch strings.ToLower(setting) {
  667. case "history":
  668. settings.History, err = historyStatusFromString(value)
  669. if err != nil {
  670. err = errInvalidParams
  671. break
  672. }
  673. channel.SetSettings(settings)
  674. channel.resizeHistory(server.Config())
  675. }
  676. switch err {
  677. case nil:
  678. service.Notice(rb, client.t("Successfully changed the channel settings"))
  679. displayChannelSetting(service, setting, settings, client, rb)
  680. case errInvalidParams:
  681. service.Notice(rb, client.t("Invalid parameters"))
  682. default:
  683. server.logger.Error("internal", "CS SET error:", err.Error())
  684. service.Notice(rb, client.t("An error occurred"))
  685. }
  686. }