3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 20:09:41 +01:00
ergo/irc/roleplay.go

122 lines
3.5 KiB
Go
Raw Normal View History

2017-03-27 14:15:02 +02:00
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
2016-11-01 14:56:21 +01:00
// released under the MIT license
package irc
import (
"fmt"
2017-06-15 18:14:19 +02:00
"github.com/goshuirc/irc-go/ircmsg"
"github.com/oragono/oragono/irc/caps"
2016-11-01 14:56:21 +01:00
)
const (
2016-11-01 15:42:25 +01:00
npcNickMask = "*%s*!%s@npc.fakeuser.invalid"
2016-11-01 14:56:21 +01:00
sceneNickMask = "=Scene=!%s@npc.fakeuser.invalid"
)
// SCENE <target> <text to be sent>
func sceneHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
target := msg.Params[0]
message := msg.Params[1]
sourceString := fmt.Sprintf(sceneNickMask, client.nick)
sendRoleplayMessage(server, client, sourceString, target, false, message)
return false
}
// NPC <target> <sourcenick> <text to be sent>
2016-11-01 14:56:21 +01:00
func npcHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
target := msg.Params[0]
fakeSource := msg.Params[1]
message := msg.Params[2]
2016-11-01 15:42:25 +01:00
_, err := CasefoldName(fakeSource)
if err != nil {
client.Send(nil, client.server.name, ERR_CANNOTSENDRP, target, "Fake source must be a valid nickname")
return false
}
2016-11-01 14:56:21 +01:00
sourceString := fmt.Sprintf(npcNickMask, fakeSource, client.nick)
sendRoleplayMessage(server, client, sourceString, target, false, message)
return false
}
// NPCA <target> <sourcenick> <text to be sent>
2016-11-01 14:56:21 +01:00
func npcaHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
target := msg.Params[0]
fakeSource := msg.Params[1]
message := msg.Params[2]
sourceString := fmt.Sprintf(npcNickMask, fakeSource, client.nick)
2016-11-01 15:42:25 +01:00
_, err := CasefoldName(fakeSource)
if err != nil {
client.Send(nil, client.server.name, ERR_CANNOTSENDRP, target, "Fake source must be a valid nickname")
return false
}
2016-11-01 14:56:21 +01:00
sendRoleplayMessage(server, client, sourceString, target, true, message)
return false
}
func sendRoleplayMessage(server *Server, client *Client, source string, targetString string, isAction bool, message string) {
if isAction {
message = fmt.Sprintf("\x01ACTION %s (%s)\x01", message, client.nick)
} else {
message = fmt.Sprintf("%s (%s)", message, client.nick)
}
target, cerr := CasefoldChannel(targetString)
if cerr == nil {
channel := server.channels.Get(target)
if channel == nil {
client.Send(nil, server.name, ERR_NOSUCHCHANNEL, client.nick, targetString, "No such channel")
return
}
if !channel.CanSpeak(client) {
client.Send(nil, client.server.name, ERR_CANNOTSENDTOCHAN, channel.name, "Cannot send to channel")
return
}
2016-11-01 15:42:25 +01:00
if !channel.flags[ChanRoleplaying] {
client.Send(nil, client.server.name, ERR_CANNOTSENDRP, channel.name, "Channel doesn't have roleplaying mode available")
return
}
2017-01-10 17:09:08 +01:00
channel.membersMutex.RLock()
2016-11-01 14:56:21 +01:00
for member := range channel.members {
if member == client && !client.capabilities.Has(caps.EchoMessage) {
2016-11-01 14:56:21 +01:00
continue
}
member.Send(nil, source, "PRIVMSG", channel.name, message)
}
2017-01-10 17:09:08 +01:00
channel.membersMutex.RUnlock()
2016-11-01 14:56:21 +01:00
} else {
target, err := CasefoldName(targetString)
user := server.clients.Get(target)
if err != nil || user == nil {
client.Send(nil, server.name, ERR_NOSUCHNICK, client.nick, target, "No such nick")
2016-11-01 14:56:21 +01:00
return
}
2016-11-01 15:42:25 +01:00
if !user.flags[UserRoleplaying] {
client.Send(nil, client.server.name, ERR_CANNOTSENDRP, user.nick, "User doesn't have roleplaying mode enabled")
return
}
2016-11-01 14:56:21 +01:00
user.Send(nil, source, "PRIVMSG", user.nick, message)
if client.capabilities.Has(caps.EchoMessage) {
2016-11-01 14:56:21 +01:00
client.Send(nil, source, "PRIVMSG", user.nick, message)
}
if user.flags[Away] {
//TODO(dan): possibly implement cooldown of away notifications to users
client.Send(nil, server.name, RPL_AWAY, user.nick, user.awayMessage)
}
}
}