Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

chanserv.go 22KB

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