3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 18:52:41 +01:00
Also upgrade RENAME to latest draft specification
This commit is contained in:
Shivaram Lingamneni 2019-03-11 07:03:51 -04:00
parent acd9eeeb15
commit 713df3a34d
3 changed files with 50 additions and 52 deletions

View File

@ -28,15 +28,15 @@ var (
errCallbackFailed = errors.New("Account verification could not be sent") errCallbackFailed = errors.New("Account verification could not be sent")
errCertfpAlreadyExists = errors.New(`An account already exists for your certificate fingerprint`) errCertfpAlreadyExists = errors.New(`An account already exists for your certificate fingerprint`)
errChannelAlreadyRegistered = errors.New("Channel is already registered") errChannelAlreadyRegistered = errors.New("Channel is already registered")
errChannelNameInUse = errors.New("Channel name in use") errChannelNameInUse = errors.New(`Channel name in use`)
errInvalidChannelName = errors.New("Invalid channel name") errInvalidChannelName = errors.New(`Invalid channel name`)
errMonitorLimitExceeded = errors.New("Monitor limit exceeded") errMonitorLimitExceeded = errors.New("Monitor limit exceeded")
errNickMissing = errors.New("nick missing") errNickMissing = errors.New("nick missing")
errNicknameInUse = errors.New("nickname in use") errNicknameInUse = errors.New("nickname in use")
errNicknameReserved = errors.New("nickname is reserved") errNicknameReserved = errors.New("nickname is reserved")
errNoExistingBan = errors.New("Ban does not exist") errNoExistingBan = errors.New("Ban does not exist")
errNoSuchChannel = errors.New("No such channel") errNoSuchChannel = errors.New(`No such channel`)
errRenamePrivsNeeded = errors.New("Only chanops can rename channels") errRenamePrivsNeeded = errors.New(`Only chanops can rename channels`)
errInsufficientPrivs = errors.New("Insufficient privileges") errInsufficientPrivs = errors.New("Insufficient privileges")
errSaslFail = errors.New("SASL failed") errSaslFail = errors.New("SASL failed")
errResumeTokenAlreadySet = errors.New("Client was already assigned a resume token") errResumeTokenAlreadySet = errors.New("Client was already assigned a resume token")

View File

@ -2208,65 +2208,43 @@ func rehashHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Re
// RENAME <oldchan> <newchan> [<reason>] // RENAME <oldchan> <newchan> [<reason>]
func renameHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *ResponseBuffer) (result bool) { func renameHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *ResponseBuffer) (result bool) {
result = false result = false
oldName, newName := msg.Params[0], msg.Params[1]
errorResponse := func(err error, name string) { if newName == "" {
// TODO: send correct error codes, e.g., ERR_CANNOTRENAME, ERR_CHANNAMEINUSE newName = "<empty>" // intentionally invalid channel name, will error as expected
var code string
switch err {
case errNoSuchChannel:
code = ERR_NOSUCHCHANNEL
case errRenamePrivsNeeded:
code = ERR_CHANOPRIVSNEEDED
case errInvalidChannelName:
code = ERR_UNKNOWNERROR
case errChannelNameInUse:
code = ERR_UNKNOWNERROR
default:
code = ERR_UNKNOWNERROR
} }
rb.Add(nil, server.name, code, client.Nick(), "RENAME", name, err.Error()) var reason string
}
oldName := strings.TrimSpace(msg.Params[0])
newName := strings.TrimSpace(msg.Params[1])
if oldName == "" || newName == "" {
errorResponse(errInvalidChannelName, "<empty>")
return
}
casefoldedOldName, err := CasefoldChannel(oldName)
if err != nil {
errorResponse(errInvalidChannelName, oldName)
return
}
reason := "No reason"
if 2 < len(msg.Params) { if 2 < len(msg.Params) {
reason = msg.Params[2] reason = msg.Params[2]
} }
channel := server.channels.Get(oldName) channel := server.channels.Get(oldName)
if channel == nil { if channel == nil {
errorResponse(errNoSuchChannel, oldName) rb.Add(nil, server.name, ERR_NOSUCHCHANNEL, client.Nick(), oldName, client.t("No such channel"))
return return false
} }
//TODO(dan): allow IRCops to do this? casefoldedOldName := channel.NameCasefolded()
if !channel.ClientIsAtLeast(client, modes.Operator) { if !(channel.ClientIsAtLeast(client, modes.Operator) || client.HasRoleCapabs("chanreg")) {
errorResponse(errRenamePrivsNeeded, oldName) rb.Add(nil, server.name, ERR_CHANOPRIVSNEEDED, client.Nick(), oldName, client.t("You're not a channel operator"))
return return false
} }
founder := channel.Founder() founder := channel.Founder()
if founder != "" && founder != client.Account() { if founder != "" && founder != client.Account() {
//TODO(dan): Change this to ERR_CANNOTRENAME rb.Add(nil, server.name, ERR_CANNOTRENAME, client.Nick(), oldName, newName, client.t("Only channel founders can change registered channels"))
rb.Add(nil, server.name, ERR_UNKNOWNERROR, client.nick, "RENAME", oldName, client.t("Only channel founders can change registered channels"))
return false return false
} }
// perform the channel rename // perform the channel rename
err = server.channels.Rename(oldName, newName) err := server.channels.Rename(oldName, newName)
if err == errInvalidChannelName {
rb.Add(nil, server.name, ERR_NOSUCHCHANNEL, client.Nick(), newName, client.t(err.Error()))
} else if err == errChannelNameInUse {
rb.Add(nil, server.name, ERR_CHANNAMEINUSE, client.Nick(), newName, client.t(err.Error()))
} else if err != nil {
rb.Add(nil, server.name, ERR_CANNOTRENAME, client.Nick(), oldName, newName, client.t("Cannot rename channel"))
}
if err != nil { if err != nil {
errorResponse(err, newName) return false
return
} }
// rename succeeded, persist it // rename succeeded, persist it
@ -2274,15 +2252,33 @@ func renameHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Re
// send RENAME messages // send RENAME messages
for _, mcl := range channel.Members() { for _, mcl := range channel.Members() {
if mcl.capabilities.Has(caps.Rename) { targetRb := rb
mcl.Send(nil, client.nickMaskString, "RENAME", oldName, newName, reason) if mcl != client {
} else { targetRb = NewResponseBuffer(mcl)
mcl.Send(nil, mcl.nickMaskString, "PART", oldName, fmt.Sprintf(mcl.t("Channel renamed: %s"), reason))
if mcl.capabilities.Has(caps.ExtendedJoin) {
mcl.Send(nil, mcl.nickMaskString, "JOIN", newName, mcl.AccountName(), mcl.realname)
} else {
mcl.Send(nil, mcl.nickMaskString, "JOIN", newName)
} }
prefix := mcl.NickMaskString()
if mcl.capabilities.Has(caps.Rename) {
if reason != "" {
targetRb.Add(nil, prefix, "RENAME", oldName, newName, reason)
} else {
targetRb.Add(nil, prefix, "RENAME", oldName, newName)
}
} else {
if reason != "" {
targetRb.Add(nil, prefix, "PART", oldName, fmt.Sprintf(mcl.t("Channel renamed: %s"), reason))
} else {
targetRb.Add(nil, prefix, "PART", oldName, fmt.Sprintf(mcl.t("Channel renamed")))
}
if mcl.capabilities.Has(caps.ExtendedJoin) {
targetRb.Add(nil, prefix, "JOIN", newName, mcl.AccountName(), mcl.Realname())
} else {
targetRb.Add(nil, prefix, "JOIN", newName)
}
channel.SendTopic(mcl, targetRb, false)
channel.Names(mcl, targetRb)
}
if mcl != client {
targetRb.Send(false)
} }
} }

View File

@ -165,6 +165,8 @@ const (
RPL_WHOISSECURE = "671" RPL_WHOISSECURE = "671"
RPL_YOURLANGUAGESARE = "687" RPL_YOURLANGUAGESARE = "687"
RPL_WHOISLANGUAGE = "690" RPL_WHOISLANGUAGE = "690"
ERR_CHANNAMEINUSE = "692"
ERR_CANNOTRENAME = "693"
RPL_HELPSTART = "704" RPL_HELPSTART = "704"
RPL_HELPTXT = "705" RPL_HELPTXT = "705"
RPL_ENDOFHELP = "706" RPL_ENDOFHELP = "706"