Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

chanserv.go 22KB

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