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 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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 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 <ADD | DEL | LIST> #channel [code] [reason]$b
  104. PURGE ADD 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. A purge can be undone with
  108. PURGE DEL. To list purged channels, use PURGE LIST.`,
  109. helpShort: `$bPURGE$b blacklists a channel from the server.`,
  110. capabs: []string{"chanreg"},
  111. minParams: 1,
  112. maxParams: 3,
  113. unsplitFinalParam: true,
  114. },
  115. "list": {
  116. handler: csListHandler,
  117. help: `Syntax: $bLIST [regex]$b
  118. LIST returns the list of registered channels, which match the given regex.
  119. If no regex is provided, all registered channels are returned.`,
  120. helpShort: `$bLIST$b searches the list of registered channels.`,
  121. capabs: []string{"chanreg"},
  122. minParams: 0,
  123. },
  124. "info": {
  125. handler: csInfoHandler,
  126. help: `Syntax: $INFO #channel$b
  127. INFO displays info about a registered channel.`,
  128. helpShort: `$bINFO$b displays info about a registered channel.`,
  129. enabled: chanregEnabled,
  130. minParams: 1,
  131. },
  132. "get": {
  133. handler: csGetHandler,
  134. help: `Syntax: $bGET #channel <setting>$b
  135. GET queries the current values of the channel settings. For more information
  136. on the settings and their possible values, see HELP SET.`,
  137. helpShort: `$bGET$b queries the current values of a channel's settings`,
  138. enabled: chanregEnabled,
  139. minParams: 2,
  140. },
  141. "set": {
  142. handler: csSetHandler,
  143. helpShort: `$bSET$b modifies a channel's settings`,
  144. // these are broken out as separate strings so they can be translated separately
  145. helpStrings: []string{
  146. `Syntax $bSET #channel <setting> <value>$b
  147. SET modifies a channel's settings. The following settings are available:`,
  148. `$bHISTORY$b
  149. 'history' lets you control how channel history is stored. Your options are:
  150. 1. 'off' [no history]
  151. 2. 'ephemeral' [a limited amount of temporary history, not stored on disk]
  152. 3. 'on' [history stored in a permanent database, if available]
  153. 4. 'default' [use the server default]`,
  154. `$bQUERY-CUTOFF$b
  155. 'query-cutoff' lets you restrict how much channel history can be retrieved
  156. by unprivileged users. Your options are:
  157. 1. 'none' [no restrictions]
  158. 2. 'registration-time' [users can view history from after their account was
  159. registered, plus a grace period]
  160. 3. 'join-time' [users can biew history from after they joined the
  161. channel; note that history will be effectively
  162. unavailable to clients that are not always-on]
  163. 4. 'default' [use the server default]`,
  164. },
  165. enabled: chanregEnabled,
  166. minParams: 3,
  167. },
  168. "howtoban": {
  169. handler: csHowToBanHandler,
  170. helpShort: `$bHOWTOBAN$b suggests the best available way of banning a user`,
  171. help: `Syntax: $bHOWTOBAN #channel <nick>
  172. The best way to ban a user from a channel will depend on how they are
  173. connected to the server. $bHOWTOBAN$b suggests a ban command that will
  174. (ideally) prevent the user from returning to the channel.`,
  175. enabled: chanregEnabled,
  176. minParams: 2,
  177. },
  178. }
  179. )
  180. func csAmodeHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  181. channelName := params[0]
  182. channel := server.channels.Get(channelName)
  183. if channel == nil {
  184. service.Notice(rb, client.t("Channel does not exist"))
  185. return
  186. } else if channel.Founder() == "" {
  187. service.Notice(rb, client.t("Channel is not registered"))
  188. return
  189. }
  190. modeChanges, unknown := modes.ParseChannelModeChanges(params[1:]...)
  191. var change modes.ModeChange
  192. if len(modeChanges) > 1 || len(unknown) > 0 {
  193. service.Notice(rb, client.t("Invalid mode change"))
  194. return
  195. } else if len(modeChanges) == 1 {
  196. change = modeChanges[0]
  197. } else {
  198. change = modes.ModeChange{Op: modes.List}
  199. }
  200. // normalize and validate the account argument
  201. accountIsValid := false
  202. change.Arg, _ = CasefoldName(change.Arg)
  203. switch change.Op {
  204. case modes.List:
  205. accountIsValid = true
  206. case modes.Add:
  207. // if we're adding a mode, the account must exist
  208. if change.Arg != "" {
  209. _, err := server.accounts.LoadAccount(change.Arg)
  210. accountIsValid = (err == nil)
  211. }
  212. case modes.Remove:
  213. // allow removal of accounts that may have been deleted
  214. accountIsValid = (change.Arg != "")
  215. }
  216. if !accountIsValid {
  217. service.Notice(rb, client.t("Account does not exist"))
  218. return
  219. }
  220. affectedModes, err := channel.ProcessAccountToUmodeChange(client, change)
  221. if err == errInsufficientPrivs {
  222. service.Notice(rb, client.t("Insufficient privileges"))
  223. return
  224. } else if err != nil {
  225. service.Notice(rb, client.t("Internal error"))
  226. return
  227. }
  228. switch change.Op {
  229. case modes.List:
  230. // sort the persistent modes in descending order of priority
  231. sort.Slice(affectedModes, func(i, j int) bool {
  232. return umodeGreaterThan(affectedModes[i].Mode, affectedModes[j].Mode)
  233. })
  234. service.Notice(rb, fmt.Sprintf(client.t("Channel %[1]s has %[2]d persistent modes set"), channelName, len(affectedModes)))
  235. for _, modeChange := range affectedModes {
  236. service.Notice(rb, fmt.Sprintf(client.t("Account %[1]s receives mode +%[2]s"), modeChange.Arg, string(modeChange.Mode)))
  237. }
  238. case modes.Add, modes.Remove:
  239. if len(affectedModes) > 0 {
  240. 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))
  241. // #729: apply change to current membership
  242. for _, member := range channel.Members() {
  243. if member.Account() == change.Arg {
  244. applied, change := channel.applyModeToMember(client, change, rb)
  245. if applied {
  246. announceCmodeChanges(channel, modes.ModeChanges{change}, server.name, "*", "", rb)
  247. }
  248. }
  249. }
  250. } else {
  251. service.Notice(rb, client.t("No changes were made"))
  252. }
  253. }
  254. }
  255. func csOpHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  256. channelInfo := server.channels.Get(params[0])
  257. if channelInfo == nil {
  258. service.Notice(rb, client.t("Channel does not exist"))
  259. return
  260. }
  261. channelName := channelInfo.Name()
  262. clientAccount := client.Account()
  263. if clientAccount == "" || clientAccount != channelInfo.Founder() {
  264. service.Notice(rb, client.t("Only the channel founder can do this"))
  265. return
  266. }
  267. var target *Client
  268. if len(params) > 1 {
  269. target = server.clients.Get(params[1])
  270. if target == nil {
  271. service.Notice(rb, client.t("Could not find given client"))
  272. return
  273. }
  274. } else {
  275. target = client
  276. }
  277. // give them privs
  278. givenMode := modes.ChannelOperator
  279. if clientAccount == target.Account() {
  280. givenMode = modes.ChannelFounder
  281. }
  282. applied, change := channelInfo.applyModeToMember(client,
  283. modes.ModeChange{Mode: givenMode,
  284. Op: modes.Add,
  285. Arg: target.NickCasefolded(),
  286. },
  287. rb)
  288. if applied {
  289. announceCmodeChanges(channelInfo, modes.ModeChanges{change}, server.name, "*", "", rb)
  290. }
  291. service.Notice(rb, client.t("Successfully granted operator privileges"))
  292. tnick := target.Nick()
  293. server.logger.Info("services", fmt.Sprintf("Client %s op'd [%s] in channel %s", client.Nick(), tnick, channelName))
  294. 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))
  295. }
  296. func csDeopHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  297. channel := server.channels.Get(params[0])
  298. if channel == nil {
  299. service.Notice(rb, client.t("Channel does not exist"))
  300. return
  301. }
  302. if !channel.hasClient(client) {
  303. service.Notice(rb, client.t("You're not on that channel"))
  304. return
  305. }
  306. var target *Client
  307. if len(params) > 1 {
  308. target = server.clients.Get(params[1])
  309. if target == nil {
  310. service.Notice(rb, client.t("Could not find given client"))
  311. return
  312. }
  313. } else {
  314. target = client
  315. }
  316. present, _, cumodes := channel.ClientStatus(target)
  317. if !present || len(cumodes) == 0 {
  318. service.Notice(rb, client.t("Target has no privileges to remove"))
  319. return
  320. }
  321. tnick := target.Nick()
  322. modeChanges := make(modes.ModeChanges, len(cumodes))
  323. for i, mode := range cumodes {
  324. modeChanges[i] = modes.ModeChange{
  325. Mode: mode,
  326. Op: modes.Remove,
  327. Arg: tnick,
  328. }
  329. }
  330. // use the user's own permissions for the check, then announce
  331. // the changes as coming from chanserv
  332. applied := channel.ApplyChannelModeChanges(client, false, modeChanges, rb)
  333. details := client.Details()
  334. announceCmodeChanges(channel, applied, details.nickMask, details.accountName, details.account, rb)
  335. if len(applied) == 0 {
  336. return
  337. }
  338. service.Notice(rb, client.t("Successfully removed operator privileges"))
  339. }
  340. func csRegisterHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  341. if server.Config().Channels.Registration.OperatorOnly && !client.HasRoleCapabs("chanreg") {
  342. service.Notice(rb, client.t("Channel registration is restricted to server operators"))
  343. return
  344. }
  345. channelName := params[0]
  346. channelInfo := server.channels.Get(channelName)
  347. if channelInfo == nil {
  348. service.Notice(rb, client.t("No such channel"))
  349. return
  350. }
  351. if !channelInfo.ClientIsAtLeast(client, modes.ChannelOperator) {
  352. service.Notice(rb, client.t("You must be an oper on the channel to register it"))
  353. return
  354. }
  355. account := client.Account()
  356. if !checkChanLimit(service, client, rb) {
  357. return
  358. }
  359. // this provides the synchronization that allows exactly one registration of the channel:
  360. err := server.channels.SetRegistered(channelName, account)
  361. if err != nil {
  362. service.Notice(rb, err.Error())
  363. return
  364. }
  365. service.Notice(rb, fmt.Sprintf(client.t("Channel %s successfully registered"), channelName))
  366. server.logger.Info("services", fmt.Sprintf("Client %s registered channel %s", client.Nick(), channelName))
  367. 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))
  368. // give them founder privs
  369. applied, change := channelInfo.applyModeToMember(client,
  370. modes.ModeChange{
  371. Mode: modes.ChannelFounder,
  372. Op: modes.Add,
  373. Arg: client.NickCasefolded(),
  374. },
  375. rb)
  376. if applied {
  377. announceCmodeChanges(channelInfo, modes.ModeChanges{change}, service.prefix, "*", "", rb)
  378. }
  379. }
  380. // check whether a client has already registered too many channels
  381. func checkChanLimit(service *ircService, client *Client, rb *ResponseBuffer) (ok bool) {
  382. account := client.Account()
  383. channelsAlreadyRegistered := client.server.accounts.ChannelsForAccount(account)
  384. ok = len(channelsAlreadyRegistered) < client.server.Config().Channels.Registration.MaxChannelsPerAccount || client.HasRoleCapabs("chanreg")
  385. if !ok {
  386. service.Notice(rb, client.t("You have already registered the maximum number of channels; try dropping some with /CS UNREGISTER"))
  387. }
  388. return
  389. }
  390. func csPrivsCheck(service *ircService, channel RegisteredChannel, client *Client, rb *ResponseBuffer) (success bool) {
  391. founder := channel.Founder
  392. if founder == "" {
  393. service.Notice(rb, client.t("That channel is not registered"))
  394. return false
  395. }
  396. if client.HasRoleCapabs("chanreg") {
  397. return true
  398. }
  399. if founder != client.Account() {
  400. service.Notice(rb, client.t("Insufficient privileges"))
  401. return false
  402. }
  403. return true
  404. }
  405. func csUnregisterHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  406. channelName := params[0]
  407. var verificationCode string
  408. if len(params) > 1 {
  409. verificationCode = params[1]
  410. }
  411. channel := server.channels.Get(channelName)
  412. if channel == nil {
  413. service.Notice(rb, client.t("No such channel"))
  414. return
  415. }
  416. info := channel.ExportRegistration(0)
  417. channelKey := info.NameCasefolded
  418. if !csPrivsCheck(service, info, client, rb) {
  419. return
  420. }
  421. expectedCode := utils.ConfirmationCode(info.Name, info.RegisteredAt)
  422. if expectedCode != verificationCode {
  423. service.Notice(rb, ircfmt.Unescape(client.t("$bWarning: unregistering this channel will remove all stored channel attributes.$b")))
  424. service.Notice(rb, fmt.Sprintf(client.t("To confirm, run this command: %s"), fmt.Sprintf("/CS UNREGISTER %s %s", channelKey, expectedCode)))
  425. return
  426. }
  427. server.channels.SetUnregistered(channelKey, info.Founder)
  428. service.Notice(rb, fmt.Sprintf(client.t("Channel %s is now unregistered"), channelKey))
  429. }
  430. func csClearHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  431. channel := server.channels.Get(params[0])
  432. if channel == nil {
  433. service.Notice(rb, client.t("Channel does not exist"))
  434. return
  435. }
  436. if !csPrivsCheck(service, channel.ExportRegistration(0), client, rb) {
  437. return
  438. }
  439. switch strings.ToLower(params[1]) {
  440. case "access":
  441. channel.resetAccess()
  442. service.Notice(rb, client.t("Successfully reset channel access"))
  443. case "users":
  444. for _, target := range channel.Members() {
  445. if target != client {
  446. channel.Kick(client, target, "Cleared by ChanServ", rb, true)
  447. }
  448. }
  449. default:
  450. service.Notice(rb, client.t("Invalid parameters"))
  451. }
  452. }
  453. func csTransferHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  454. if strings.ToLower(params[0]) == "accept" {
  455. processTransferAccept(service, client, params[1], rb)
  456. return
  457. }
  458. chname := params[0]
  459. channel := server.channels.Get(chname)
  460. if channel == nil {
  461. service.Notice(rb, client.t("Channel does not exist"))
  462. return
  463. }
  464. regInfo := channel.ExportRegistration(0)
  465. chname = regInfo.Name
  466. account := client.Account()
  467. isFounder := account != "" && account == regInfo.Founder
  468. var oper *Oper
  469. if !isFounder {
  470. oper = client.Oper()
  471. if !oper.HasRoleCapab("chanreg") {
  472. service.Notice(rb, client.t("Insufficient privileges"))
  473. return
  474. }
  475. }
  476. target := params[1]
  477. targetAccount, err := server.accounts.LoadAccount(params[1])
  478. if err != nil {
  479. service.Notice(rb, client.t("Account does not exist"))
  480. return
  481. }
  482. if targetAccount.NameCasefolded != account {
  483. expectedCode := utils.ConfirmationCode(regInfo.Name, regInfo.RegisteredAt)
  484. codeValidated := 2 < len(params) && params[2] == expectedCode
  485. if !codeValidated {
  486. service.Notice(rb, ircfmt.Unescape(client.t("$bWarning: you are about to transfer control of your channel to another user.$b")))
  487. service.Notice(rb, fmt.Sprintf(client.t("To confirm your channel transfer, type: /CS TRANSFER %[1]s %[2]s %[3]s"), chname, target, expectedCode))
  488. return
  489. }
  490. }
  491. if !isFounder {
  492. message := fmt.Sprintf("Operator %s ran CS TRANSFER on %s to account %s", oper.Name, chname, target)
  493. server.snomasks.Send(sno.LocalOpers, message)
  494. server.logger.Info("opers", message)
  495. }
  496. status, err := channel.Transfer(client, target, oper != nil)
  497. if err == nil {
  498. switch status {
  499. case channelTransferComplete:
  500. service.Notice(rb, fmt.Sprintf(client.t("Successfully transferred channel %[1]s to account %[2]s"), chname, target))
  501. case channelTransferPending:
  502. sendTransferPendingNotice(service, server, target, chname)
  503. service.Notice(rb, fmt.Sprintf(client.t("Transfer of channel %[1]s to account %[2]s succeeded, pending acceptance"), chname, target))
  504. case channelTransferCancelled:
  505. service.Notice(rb, fmt.Sprintf(client.t("Cancelled pending transfer of channel %s"), chname))
  506. }
  507. } else {
  508. service.Notice(rb, client.t("Could not transfer channel"))
  509. }
  510. }
  511. func sendTransferPendingNotice(service *ircService, server *Server, account, chname string) {
  512. clients := server.accounts.AccountToClients(account)
  513. if len(clients) == 0 {
  514. return
  515. }
  516. var client *Client
  517. for _, candidate := range clients {
  518. client = candidate
  519. if candidate.NickCasefolded() == candidate.Account() {
  520. break // prefer the login where the nick is the account
  521. }
  522. }
  523. 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))
  524. }
  525. func processTransferAccept(service *ircService, client *Client, chname string, rb *ResponseBuffer) {
  526. channel := client.server.channels.Get(chname)
  527. if channel == nil {
  528. service.Notice(rb, client.t("Channel does not exist"))
  529. return
  530. }
  531. if !checkChanLimit(service, client, rb) {
  532. return
  533. }
  534. switch channel.AcceptTransfer(client) {
  535. case nil:
  536. service.Notice(rb, fmt.Sprintf(client.t("Successfully accepted ownership of channel %s"), channel.Name()))
  537. case errChannelTransferNotOffered:
  538. service.Notice(rb, fmt.Sprintf(client.t("You weren't offered ownership of channel %s"), channel.Name()))
  539. default:
  540. service.Notice(rb, fmt.Sprintf(client.t("Could not accept ownership of channel %s"), channel.Name()))
  541. }
  542. }
  543. func csPurgeHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  544. oper := client.Oper()
  545. if oper == nil {
  546. return // should be impossible because you need oper capabs for this
  547. }
  548. switch strings.ToLower(params[0]) {
  549. case "add":
  550. csPurgeAddHandler(service, client, params[1:], oper.Name, rb)
  551. case "del", "remove":
  552. csPurgeDelHandler(service, client, params[1:], oper.Name, rb)
  553. case "list":
  554. csPurgeListHandler(service, client, rb)
  555. default:
  556. service.Notice(rb, client.t("Invalid parameters"))
  557. }
  558. }
  559. func csPurgeAddHandler(service *ircService, client *Client, params []string, operName string, rb *ResponseBuffer) {
  560. if len(params) == 0 {
  561. service.Notice(rb, client.t("Invalid parameters"))
  562. return
  563. }
  564. chname := params[0]
  565. params = params[1:]
  566. channel := client.server.channels.Get(chname) // possibly nil
  567. var ctime time.Time
  568. if channel != nil {
  569. chname = channel.Name()
  570. ctime = channel.Ctime()
  571. }
  572. code := utils.ConfirmationCode(chname, ctime)
  573. if len(params) == 0 || params[0] != code {
  574. service.Notice(rb, ircfmt.Unescape(client.t("$bWarning: you are about to empty this channel and remove it from the server.$b")))
  575. service.Notice(rb, fmt.Sprintf(client.t("To confirm, run this command: %s"), fmt.Sprintf("/CS PURGE ADD %s %s", chname, code)))
  576. return
  577. }
  578. params = params[1:]
  579. var reason string
  580. if 1 < len(params) {
  581. reason = params[1]
  582. }
  583. purgeRecord := ChannelPurgeRecord{
  584. Oper: operName,
  585. PurgedAt: time.Now().UTC(),
  586. Reason: reason,
  587. }
  588. switch client.server.channels.Purge(chname, purgeRecord) {
  589. case nil:
  590. if channel != nil { // channel need not exist to be purged
  591. for _, target := range channel.Members() {
  592. channel.Kick(client, target, "Cleared by ChanServ", rb, true)
  593. }
  594. }
  595. service.Notice(rb, fmt.Sprintf(client.t("Successfully purged channel %s from the server"), chname))
  596. case errInvalidChannelName:
  597. service.Notice(rb, fmt.Sprintf(client.t("Can't purge invalid channel %s"), chname))
  598. default:
  599. service.Notice(rb, client.t("An error occurred"))
  600. }
  601. }
  602. func csPurgeDelHandler(service *ircService, client *Client, params []string, operName string, rb *ResponseBuffer) {
  603. if len(params) == 0 {
  604. service.Notice(rb, client.t("Invalid parameters"))
  605. return
  606. }
  607. chname := params[0]
  608. switch client.server.channels.Unpurge(chname) {
  609. case nil:
  610. service.Notice(rb, fmt.Sprintf(client.t("Successfully unpurged channel %s from the server"), chname))
  611. case errNoSuchChannel:
  612. service.Notice(rb, fmt.Sprintf(client.t("Channel %s wasn't previously purged from the server"), chname))
  613. default:
  614. service.Notice(rb, client.t("An error occurred"))
  615. }
  616. }
  617. func csPurgeListHandler(service *ircService, client *Client, rb *ResponseBuffer) {
  618. l := client.server.channels.ListPurged()
  619. service.Notice(rb, fmt.Sprintf(client.t("There are %d purged channel(s)."), len(l)))
  620. for i, c := range l {
  621. service.Notice(rb, fmt.Sprintf("%d: %s", i+1, c))
  622. }
  623. }
  624. func csListHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  625. if !client.HasRoleCapabs("chanreg") {
  626. service.Notice(rb, client.t("Insufficient privileges"))
  627. return
  628. }
  629. var searchRegex *regexp.Regexp
  630. if len(params) > 0 {
  631. var err error
  632. searchRegex, err = regexp.Compile(params[0])
  633. if err != nil {
  634. service.Notice(rb, client.t("Invalid regex"))
  635. return
  636. }
  637. }
  638. service.Notice(rb, ircfmt.Unescape(client.t("*** $bChanServ LIST$b ***")))
  639. channels := server.channelRegistry.AllChannels()
  640. for _, channel := range channels {
  641. if searchRegex == nil || searchRegex.MatchString(channel) {
  642. service.Notice(rb, fmt.Sprintf(" %s", channel))
  643. }
  644. }
  645. service.Notice(rb, ircfmt.Unescape(client.t("*** $bEnd of ChanServ LIST$b ***")))
  646. }
  647. func csInfoHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  648. chname, err := CasefoldChannel(params[0])
  649. if err != nil {
  650. service.Notice(rb, client.t("Invalid channel name"))
  651. return
  652. }
  653. // purge status
  654. if client.HasRoleCapabs("chanreg") {
  655. purgeRecord, err := server.channelRegistry.LoadPurgeRecord(chname)
  656. if err == nil {
  657. service.Notice(rb, fmt.Sprintf(client.t("Channel %s was purged by the server operators and cannot be used"), chname))
  658. service.Notice(rb, fmt.Sprintf(client.t("Purged by operator: %s"), purgeRecord.Oper))
  659. service.Notice(rb, fmt.Sprintf(client.t("Purged at: %s"), purgeRecord.PurgedAt.Format(time.RFC1123)))
  660. if purgeRecord.Reason != "" {
  661. service.Notice(rb, fmt.Sprintf(client.t("Purge reason: %s"), purgeRecord.Reason))
  662. }
  663. }
  664. } else {
  665. if server.channels.IsPurged(chname) {
  666. service.Notice(rb, fmt.Sprintf(client.t("Channel %s was purged by the server operators and cannot be used"), chname))
  667. }
  668. }
  669. var chinfo RegisteredChannel
  670. channel := server.channels.Get(params[0])
  671. if channel != nil {
  672. chinfo = channel.ExportRegistration(0)
  673. } else {
  674. chinfo, err = server.channelRegistry.LoadChannel(chname)
  675. if err != nil && !(err == errNoSuchChannel || err == errFeatureDisabled) {
  676. service.Notice(rb, client.t("An error occurred"))
  677. return
  678. }
  679. }
  680. // channel exists but is unregistered, or doesn't exist:
  681. if chinfo.Founder == "" {
  682. service.Notice(rb, fmt.Sprintf(client.t("Channel %s is not registered"), chname))
  683. return
  684. }
  685. service.Notice(rb, fmt.Sprintf(client.t("Channel %s is registered"), chinfo.Name))
  686. service.Notice(rb, fmt.Sprintf(client.t("Founder: %s"), chinfo.Founder))
  687. service.Notice(rb, fmt.Sprintf(client.t("Registered at: %s"), chinfo.RegisteredAt.Format(time.RFC1123)))
  688. }
  689. func displayChannelSetting(service *ircService, settingName string, settings ChannelSettings, client *Client, rb *ResponseBuffer) {
  690. config := client.server.Config()
  691. switch strings.ToLower(settingName) {
  692. case "history":
  693. effectiveValue := historyEnabled(config.History.Persistent.RegisteredChannels, settings.History)
  694. service.Notice(rb, fmt.Sprintf(client.t("The stored channel history setting is: %s"), historyStatusToString(settings.History)))
  695. service.Notice(rb, fmt.Sprintf(client.t("Given current server settings, the channel history setting is: %s"), historyStatusToString(effectiveValue)))
  696. case "query-cutoff":
  697. effectiveValue := settings.QueryCutoff
  698. if effectiveValue == HistoryCutoffDefault {
  699. effectiveValue = config.History.Restrictions.queryCutoff
  700. }
  701. service.Notice(rb, fmt.Sprintf(client.t("The stored channel history query cutoff setting is: %s"), historyCutoffToString(settings.QueryCutoff)))
  702. service.Notice(rb, fmt.Sprintf(client.t("Given current server settings, the channel history query cutoff setting is: %s"), historyCutoffToString(effectiveValue)))
  703. default:
  704. service.Notice(rb, client.t("Invalid params"))
  705. }
  706. }
  707. func csGetHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  708. chname, setting := params[0], params[1]
  709. channel := server.channels.Get(chname)
  710. if channel == nil {
  711. service.Notice(rb, client.t("No such channel"))
  712. return
  713. }
  714. info := channel.ExportRegistration(IncludeSettings)
  715. if !csPrivsCheck(service, info, client, rb) {
  716. return
  717. }
  718. displayChannelSetting(service, setting, info.Settings, client, rb)
  719. }
  720. func csSetHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  721. chname, setting, value := params[0], params[1], params[2]
  722. channel := server.channels.Get(chname)
  723. if channel == nil {
  724. service.Notice(rb, client.t("No such channel"))
  725. return
  726. }
  727. info := channel.ExportRegistration(IncludeSettings)
  728. settings := info.Settings
  729. if !csPrivsCheck(service, info, client, rb) {
  730. return
  731. }
  732. var err error
  733. switch strings.ToLower(setting) {
  734. case "history":
  735. settings.History, err = historyStatusFromString(value)
  736. if err != nil {
  737. err = errInvalidParams
  738. break
  739. }
  740. channel.SetSettings(settings)
  741. channel.resizeHistory(server.Config())
  742. case "query-cutoff":
  743. settings.QueryCutoff, err = historyCutoffFromString(value)
  744. if err != nil {
  745. err = errInvalidParams
  746. break
  747. }
  748. channel.SetSettings(settings)
  749. }
  750. switch err {
  751. case nil:
  752. service.Notice(rb, client.t("Successfully changed the channel settings"))
  753. displayChannelSetting(service, setting, settings, client, rb)
  754. case errInvalidParams:
  755. service.Notice(rb, client.t("Invalid parameters"))
  756. default:
  757. server.logger.Error("internal", "CS SET error:", err.Error())
  758. service.Notice(rb, client.t("An error occurred"))
  759. }
  760. }
  761. func csHowToBanHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  762. success := false
  763. defer func() {
  764. if success {
  765. service.Notice(rb, client.t("Note that if the user is currently in the channel, you must /KICK them after you ban them"))
  766. }
  767. }()
  768. chname, nick := params[0], params[1]
  769. channel := server.channels.Get(chname)
  770. if channel == nil {
  771. service.Notice(rb, client.t("No such channel"))
  772. return
  773. }
  774. if !(channel.ClientIsAtLeast(client, modes.ChannelOperator) || client.HasRoleCapabs("samode")) {
  775. service.Notice(rb, client.t("Insufficient privileges"))
  776. return
  777. }
  778. var details WhoWas
  779. target := server.clients.Get(nick)
  780. if target == nil {
  781. whowasList := server.whoWas.Find(nick, 1)
  782. if len(whowasList) == 0 {
  783. service.Notice(rb, client.t("No such nick"))
  784. return
  785. }
  786. service.Notice(rb, fmt.Sprintf(client.t("Warning: %s is not currently connected to the server. Using WHOWAS data, which may be inaccurate:"), nick))
  787. details = whowasList[0]
  788. } else {
  789. details = target.Details().WhoWas
  790. }
  791. if details.account != "" {
  792. if channel.getAmode(details.account) != modes.Mode(0) {
  793. service.Notice(rb, fmt.Sprintf(client.t("Warning: account %s currently has a persistent channel privilege granted with CS AMODE. If this mode is not removed, bans will not be respected"), details.accountName))
  794. return
  795. } else if details.account == channel.Founder() {
  796. service.Notice(rb, fmt.Sprintf(client.t("Warning: account %s is the channel founder and cannot be banned"), details.accountName))
  797. return
  798. }
  799. }
  800. config := server.Config()
  801. if !config.Server.Cloaks.EnabledForAlwaysOn {
  802. service.Notice(rb, client.t("Warning: server.ip-cloaking.enabled-for-always-on is disabled. This reduces the precision of channel bans."))
  803. }
  804. if details.account != "" {
  805. if config.Accounts.NickReservation.ForceNickEqualsAccount || target.AlwaysOn() {
  806. service.Notice(rb, fmt.Sprintf(client.t("User %[1]s is authenticated and can be banned by nickname: /MODE %[2]s +b %[3]s!*@*"), details.nick, channel.Name(), details.nick))
  807. success = true
  808. return
  809. }
  810. }
  811. ban := fmt.Sprintf("*!*@%s", strings.ToLower(details.hostname))
  812. banRe, err := utils.CompileGlob(ban, false)
  813. if err != nil {
  814. server.logger.Error("internal", "couldn't compile ban regex", ban, err.Error())
  815. service.Notice(rb, "An error occurred")
  816. return
  817. }
  818. var collateralDamage []string
  819. for _, mcl := range channel.Members() {
  820. if mcl != target && banRe.MatchString(mcl.NickMaskCasefolded()) {
  821. collateralDamage = append(collateralDamage, mcl.Nick())
  822. }
  823. }
  824. service.Notice(rb, fmt.Sprintf(client.t("User %[1]s can be banned by hostname: /MODE %[2]s +b %[3]s"), details.nick, channel.Name(), ban))
  825. success = true
  826. if len(collateralDamage) != 0 {
  827. service.Notice(rb, fmt.Sprintf(client.t("Warning: this ban will affect %d other users:"), len(collateralDamage)))
  828. for _, line := range utils.BuildTokenLines(400, collateralDamage, " ") {
  829. service.Notice(rb, line)
  830. }
  831. }
  832. }