mirror of
https://github.com/ergochat/ergo.git
synced 2024-11-10 22:19:31 +01:00
commit
2e212e3692
@ -244,11 +244,12 @@ func (channel *Channel) PrivMsg(client *Client, message Text) {
|
|||||||
client.ErrCannotSendToChan(channel)
|
client.ErrCannotSendToChan(channel)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
reply := RplPrivMsg(client, channel, message)
|
||||||
for member := range channel.members {
|
for member := range channel.members {
|
||||||
if member == client {
|
if member == client {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
member.Reply(RplPrivMsg(client, channel, message))
|
member.Reply(reply)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -458,11 +459,12 @@ func (channel *Channel) Notice(client *Client, message Text) {
|
|||||||
client.ErrCannotSendToChan(channel)
|
client.ErrCannotSendToChan(channel)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
reply := RplNotice(client, channel, message)
|
||||||
for member := range channel.members {
|
for member := range channel.members {
|
||||||
if member == client {
|
if member == client {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
member.Reply(RplNotice(client, channel, message))
|
member.Reply(reply)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,10 +62,7 @@ func NewClient(server *Server, conn net.Conn) *Client {
|
|||||||
|
|
||||||
func (client *Client) run() {
|
func (client *Client) run() {
|
||||||
for command := range client.commands {
|
for command := range client.commands {
|
||||||
command.SetClient(client)
|
if checkPass, ok := command.(checkPasswordCommand); ok {
|
||||||
|
|
||||||
checkPass, ok := command.(checkPasswordCommand)
|
|
||||||
if ok {
|
|
||||||
checkPass.LoadPassword(client.server)
|
checkPass.LoadPassword(client.server)
|
||||||
// Block the client thread while handling a potentially expensive
|
// Block the client thread while handling a potentially expensive
|
||||||
// password bcrypt operation. Since the server is single-threaded
|
// password bcrypt operation. Since the server is single-threaded
|
||||||
@ -74,15 +71,13 @@ func (client *Client) run() {
|
|||||||
// completes. This could be a form of DoS if handled naively.
|
// completes. This could be a form of DoS if handled naively.
|
||||||
checkPass.CheckPassword()
|
checkPass.CheckPassword()
|
||||||
}
|
}
|
||||||
|
command.SetClient(client)
|
||||||
client.server.commands <- command
|
client.server.commands <- command
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) connectionTimeout() {
|
func (client *Client) connectionTimeout() {
|
||||||
client.commands <- &QuitCommand{
|
client.commands <- NewQuitCommand("connection timeout")
|
||||||
message: "connection timeout",
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -259,8 +254,8 @@ func (client *Client) Quit(message Text) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
client.Reply(RplError("connection closed"))
|
|
||||||
client.hasQuit = true
|
client.hasQuit = true
|
||||||
|
client.Reply(RplError("quit"))
|
||||||
client.server.whoWas.Append(client)
|
client.server.whoWas.Append(client)
|
||||||
friends := client.Friends()
|
friends := client.Friends()
|
||||||
friends.Remove(client)
|
friends.Remove(client)
|
||||||
|
@ -92,12 +92,13 @@ func (clients *ClientLookupSet) FindAll(userhost Name) (set ClientSet) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var nickname Name
|
var sqlNickname string
|
||||||
err := rows.Scan(&nickname)
|
err := rows.Scan(&sqlNickname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Log.error.Println("ClientLookupSet.FindAll.Scan:", err)
|
Log.error.Println("ClientLookupSet.FindAll.Scan:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
nickname := Name(sqlNickname)
|
||||||
client := clients.Get(nickname)
|
client := clients.Get(nickname)
|
||||||
if client == nil {
|
if client == nil {
|
||||||
Log.error.Println("ClientLookupSet.FindAll: missing client:", nickname)
|
Log.error.Println("ClientLookupSet.FindAll: missing client:", nickname)
|
||||||
|
231
irc/commands.go
231
irc/commands.go
@ -26,37 +26,37 @@ var (
|
|||||||
NotEnoughArgsError = errors.New("not enough arguments")
|
NotEnoughArgsError = errors.New("not enough arguments")
|
||||||
ErrParseCommand = errors.New("failed to parse message")
|
ErrParseCommand = errors.New("failed to parse message")
|
||||||
parseCommandFuncs = map[StringCode]parseCommandFunc{
|
parseCommandFuncs = map[StringCode]parseCommandFunc{
|
||||||
AWAY: NewAwayCommand,
|
AWAY: ParseAwayCommand,
|
||||||
CAP: NewCapCommand,
|
CAP: ParseCapCommand,
|
||||||
DEBUG: NewDebugCommand,
|
DEBUG: ParseDebugCommand,
|
||||||
INVITE: NewInviteCommand,
|
INVITE: ParseInviteCommand,
|
||||||
ISON: NewIsOnCommand,
|
ISON: ParseIsOnCommand,
|
||||||
JOIN: NewJoinCommand,
|
JOIN: ParseJoinCommand,
|
||||||
KICK: NewKickCommand,
|
KICK: ParseKickCommand,
|
||||||
KILL: NewKillCommand,
|
KILL: ParseKillCommand,
|
||||||
LIST: NewListCommand,
|
LIST: ParseListCommand,
|
||||||
MODE: NewModeCommand,
|
MODE: ParseModeCommand,
|
||||||
MOTD: NewMOTDCommand,
|
MOTD: ParseMOTDCommand,
|
||||||
NAMES: NewNamesCommand,
|
NAMES: ParseNamesCommand,
|
||||||
NICK: NewNickCommand,
|
NICK: ParseNickCommand,
|
||||||
NOTICE: NewNoticeCommand,
|
NOTICE: ParseNoticeCommand,
|
||||||
ONICK: NewOperNickCommand,
|
ONICK: ParseOperNickCommand,
|
||||||
OPER: NewOperCommand,
|
OPER: ParseOperCommand,
|
||||||
PART: NewPartCommand,
|
PART: ParsePartCommand,
|
||||||
PASS: NewPassCommand,
|
PASS: ParsePassCommand,
|
||||||
PING: NewPingCommand,
|
PING: ParsePingCommand,
|
||||||
PONG: NewPongCommand,
|
PONG: ParsePongCommand,
|
||||||
PRIVMSG: NewPrivMsgCommand,
|
PRIVMSG: ParsePrivMsgCommand,
|
||||||
PROXY: NewProxyCommand,
|
PROXY: ParseProxyCommand,
|
||||||
QUIT: NewQuitCommand,
|
QUIT: ParseQuitCommand,
|
||||||
THEATER: NewTheaterCommand, // nonstandard
|
THEATER: ParseTheaterCommand, // nonstandard
|
||||||
TIME: NewTimeCommand,
|
TIME: ParseTimeCommand,
|
||||||
TOPIC: NewTopicCommand,
|
TOPIC: ParseTopicCommand,
|
||||||
USER: NewUserCommand,
|
USER: ParseUserCommand,
|
||||||
VERSION: NewVersionCommand,
|
VERSION: ParseVersionCommand,
|
||||||
WHO: NewWhoCommand,
|
WHO: ParseWhoCommand,
|
||||||
WHOIS: NewWhoisCommand,
|
WHOIS: ParseWhoisCommand,
|
||||||
WHOWAS: NewWhoWasCommand,
|
WHOWAS: ParseWhoWasCommand,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -85,7 +85,7 @@ func ParseCommand(line string) (cmd Command, err error) {
|
|||||||
code, args := ParseLine(line)
|
code, args := ParseLine(line)
|
||||||
constructor := parseCommandFuncs[code]
|
constructor := parseCommandFuncs[code]
|
||||||
if constructor == nil {
|
if constructor == nil {
|
||||||
cmd = NewUnknownCommand(args)
|
cmd = ParseUnknownCommand(args)
|
||||||
} else {
|
} else {
|
||||||
cmd, err = constructor(args)
|
cmd, err = constructor(args)
|
||||||
}
|
}
|
||||||
@ -135,11 +135,7 @@ type UnknownCommand struct {
|
|||||||
args []string
|
args []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *UnknownCommand) String() string {
|
func ParseUnknownCommand(args []string) *UnknownCommand {
|
||||||
return fmt.Sprintf("UNKNOWN(command=%s, args=%s)", cmd.Code(), cmd.args)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewUnknownCommand(args []string) *UnknownCommand {
|
|
||||||
return &UnknownCommand{
|
return &UnknownCommand{
|
||||||
args: args,
|
args: args,
|
||||||
}
|
}
|
||||||
@ -153,11 +149,7 @@ type PingCommand struct {
|
|||||||
server2 Name
|
server2 Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *PingCommand) String() string {
|
func ParsePingCommand(args []string) (Command, error) {
|
||||||
return fmt.Sprintf("PING(server=%s, server2=%s)", cmd.server, cmd.server2)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPingCommand(args []string) (Command, error) {
|
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -178,11 +170,7 @@ type PongCommand struct {
|
|||||||
server2 Name
|
server2 Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *PongCommand) String() string {
|
func ParsePongCommand(args []string) (Command, error) {
|
||||||
return fmt.Sprintf("PONG(server1=%s, server2=%s)", cmd.server1, cmd.server2)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPongCommand(args []string) (Command, error) {
|
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -204,10 +192,6 @@ type PassCommand struct {
|
|||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *PassCommand) String() string {
|
|
||||||
return fmt.Sprintf("PASS(password=%s)", cmd.password)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cmd *PassCommand) LoadPassword(server *Server) {
|
func (cmd *PassCommand) LoadPassword(server *Server) {
|
||||||
cmd.hash = server.password
|
cmd.hash = server.password
|
||||||
}
|
}
|
||||||
@ -219,7 +203,7 @@ func (cmd *PassCommand) CheckPassword() {
|
|||||||
cmd.err = ComparePassword(cmd.hash, cmd.password)
|
cmd.err = ComparePassword(cmd.hash, cmd.password)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPassCommand(args []string) (Command, error) {
|
func ParsePassCommand(args []string) (Command, error) {
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -230,7 +214,7 @@ func NewPassCommand(args []string) (Command, error) {
|
|||||||
|
|
||||||
// NICK <nickname>
|
// NICK <nickname>
|
||||||
|
|
||||||
func NewNickCommand(args []string) (Command, error) {
|
func ParseNickCommand(args []string) (Command, error) {
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -252,11 +236,6 @@ type RFC1459UserCommand struct {
|
|||||||
servername Name
|
servername Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *RFC1459UserCommand) String() string {
|
|
||||||
return fmt.Sprintf("USER(username=%s, hostname=%s, servername=%s, realname=%s)",
|
|
||||||
cmd.username, cmd.hostname, cmd.servername, cmd.realname)
|
|
||||||
}
|
|
||||||
|
|
||||||
// USER <user> <mode> <unused> <realname>
|
// USER <user> <mode> <unused> <realname>
|
||||||
type RFC2812UserCommand struct {
|
type RFC2812UserCommand struct {
|
||||||
UserCommand
|
UserCommand
|
||||||
@ -264,11 +243,6 @@ type RFC2812UserCommand struct {
|
|||||||
unused string
|
unused string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *RFC2812UserCommand) String() string {
|
|
||||||
return fmt.Sprintf("USER(username=%s, mode=%d, unused=%s, realname=%s)",
|
|
||||||
cmd.username, cmd.mode, cmd.unused, cmd.realname)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cmd *RFC2812UserCommand) Flags() []UserMode {
|
func (cmd *RFC2812UserCommand) Flags() []UserMode {
|
||||||
flags := make([]UserMode, 0)
|
flags := make([]UserMode, 0)
|
||||||
if (cmd.mode & 4) == 4 {
|
if (cmd.mode & 4) == 4 {
|
||||||
@ -280,7 +254,7 @@ func (cmd *RFC2812UserCommand) Flags() []UserMode {
|
|||||||
return flags
|
return flags
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserCommand(args []string) (Command, error) {
|
func ParseUserCommand(args []string) (Command, error) {
|
||||||
if len(args) != 4 {
|
if len(args) != 4 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -311,11 +285,15 @@ type QuitCommand struct {
|
|||||||
message Text
|
message Text
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *QuitCommand) String() string {
|
func NewQuitCommand(message Text) *QuitCommand {
|
||||||
return fmt.Sprintf("QUIT(message=%s)", cmd.message)
|
cmd := &QuitCommand{
|
||||||
|
message: message,
|
||||||
|
}
|
||||||
|
cmd.code = QUIT
|
||||||
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewQuitCommand(args []string) (Command, error) {
|
func ParseQuitCommand(args []string) (Command, error) {
|
||||||
msg := &QuitCommand{}
|
msg := &QuitCommand{}
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
msg.message = NewText(args[0])
|
msg.message = NewText(args[0])
|
||||||
@ -331,11 +309,7 @@ type JoinCommand struct {
|
|||||||
zero bool
|
zero bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *JoinCommand) String() string {
|
func ParseJoinCommand(args []string) (Command, error) {
|
||||||
return fmt.Sprintf("JOIN(channels=%s, zero=%t)", cmd.channels, cmd.zero)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewJoinCommand(args []string) (Command, error) {
|
|
||||||
msg := &JoinCommand{
|
msg := &JoinCommand{
|
||||||
channels: make(map[Name]Text),
|
channels: make(map[Name]Text),
|
||||||
}
|
}
|
||||||
@ -378,11 +352,7 @@ func (cmd *PartCommand) Message() Text {
|
|||||||
return cmd.message
|
return cmd.message
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *PartCommand) String() string {
|
func ParsePartCommand(args []string) (Command, error) {
|
||||||
return fmt.Sprintf("PART(channels=%s, message=%s)", cmd.channels, cmd.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPartCommand(args []string) (Command, error) {
|
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -403,11 +373,7 @@ type PrivMsgCommand struct {
|
|||||||
message Text
|
message Text
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *PrivMsgCommand) String() string {
|
func ParsePrivMsgCommand(args []string) (Command, error) {
|
||||||
return fmt.Sprintf("PRIVMSG(target=%s, message=%s)", cmd.target, cmd.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPrivMsgCommand(args []string) (Command, error) {
|
|
||||||
if len(args) < 2 {
|
if len(args) < 2 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -426,11 +392,7 @@ type TopicCommand struct {
|
|||||||
topic Text
|
topic Text
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *TopicCommand) String() string {
|
func ParseTopicCommand(args []string) (Command, error) {
|
||||||
return fmt.Sprintf("TOPIC(channel=%s, topic=%s)", cmd.channel, cmd.topic)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTopicCommand(args []string) (Command, error) {
|
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -480,7 +442,7 @@ type ModeCommand struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MODE <nickname> *( ( "+" / "-" ) *( "i" / "w" / "o" / "O" / "r" ) )
|
// MODE <nickname> *( ( "+" / "-" ) *( "i" / "w" / "o" / "O" / "r" ) )
|
||||||
func NewUserModeCommand(nickname Name, args []string) (Command, error) {
|
func ParseUserModeCommand(nickname Name, args []string) (Command, error) {
|
||||||
cmd := &ModeCommand{
|
cmd := &ModeCommand{
|
||||||
nickname: nickname,
|
nickname: nickname,
|
||||||
changes: make(ModeChanges, 0),
|
changes: make(ModeChanges, 0),
|
||||||
@ -506,10 +468,6 @@ func NewUserModeCommand(nickname Name, args []string) (Command, error) {
|
|||||||
return cmd, nil
|
return cmd, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *ModeCommand) String() string {
|
|
||||||
return fmt.Sprintf("MODE(nickname=%s, changes=%s)", cmd.nickname, cmd.changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
type ChannelModeChange struct {
|
type ChannelModeChange struct {
|
||||||
mode ChannelMode
|
mode ChannelMode
|
||||||
op ModeOp
|
op ModeOp
|
||||||
@ -557,7 +515,7 @@ type ChannelModeCommand struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MODE <channel> *( ( "-" / "+" ) *<modes> *<modeparams> )
|
// MODE <channel> *( ( "-" / "+" ) *<modes> *<modeparams> )
|
||||||
func NewChannelModeCommand(channel Name, args []string) (Command, error) {
|
func ParseChannelModeCommand(channel Name, args []string) (Command, error) {
|
||||||
cmd := &ChannelModeCommand{
|
cmd := &ChannelModeCommand{
|
||||||
channel: channel,
|
channel: channel,
|
||||||
changes: make(ChannelModeChanges, 0),
|
changes: make(ChannelModeChanges, 0),
|
||||||
@ -599,20 +557,16 @@ func NewChannelModeCommand(channel Name, args []string) (Command, error) {
|
|||||||
return cmd, nil
|
return cmd, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg *ChannelModeCommand) String() string {
|
func ParseModeCommand(args []string) (Command, error) {
|
||||||
return fmt.Sprintf("MODE(channel=%s, changes=%s)", msg.channel, msg.changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewModeCommand(args []string) (Command, error) {
|
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
|
|
||||||
name := NewName(args[0])
|
name := NewName(args[0])
|
||||||
if name.IsChannel() {
|
if name.IsChannel() {
|
||||||
return NewChannelModeCommand(name, args[1:])
|
return ParseChannelModeCommand(name, args[1:])
|
||||||
} else {
|
} else {
|
||||||
return NewUserModeCommand(name, args[1:])
|
return ParseUserModeCommand(name, args[1:])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -623,7 +577,7 @@ type WhoisCommand struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WHOIS [ <target> ] <mask> *( "," <mask> )
|
// WHOIS [ <target> ] <mask> *( "," <mask> )
|
||||||
func NewWhoisCommand(args []string) (Command, error) {
|
func ParseWhoisCommand(args []string) (Command, error) {
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -644,10 +598,6 @@ func NewWhoisCommand(args []string) (Command, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg *WhoisCommand) String() string {
|
|
||||||
return fmt.Sprintf("WHOIS(target=%s, masks=%s)", msg.target, msg.masks)
|
|
||||||
}
|
|
||||||
|
|
||||||
type WhoCommand struct {
|
type WhoCommand struct {
|
||||||
BaseCommand
|
BaseCommand
|
||||||
mask Name
|
mask Name
|
||||||
@ -655,7 +605,7 @@ type WhoCommand struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WHO [ <mask> [ "o" ] ]
|
// WHO [ <mask> [ "o" ] ]
|
||||||
func NewWhoCommand(args []string) (Command, error) {
|
func ParseWhoCommand(args []string) (Command, error) {
|
||||||
cmd := &WhoCommand{}
|
cmd := &WhoCommand{}
|
||||||
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
@ -669,25 +619,17 @@ func NewWhoCommand(args []string) (Command, error) {
|
|||||||
return cmd, nil
|
return cmd, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg *WhoCommand) String() string {
|
|
||||||
return fmt.Sprintf("WHO(mask=%s, operatorOnly=%t)", msg.mask, msg.operatorOnly)
|
|
||||||
}
|
|
||||||
|
|
||||||
type OperCommand struct {
|
type OperCommand struct {
|
||||||
PassCommand
|
PassCommand
|
||||||
name Name
|
name Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg *OperCommand) String() string {
|
|
||||||
return fmt.Sprintf("OPER(name=%s, password=%s)", msg.name, msg.password)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (msg *OperCommand) LoadPassword(server *Server) {
|
func (msg *OperCommand) LoadPassword(server *Server) {
|
||||||
msg.hash = server.operators[msg.name]
|
msg.hash = server.operators[msg.name]
|
||||||
}
|
}
|
||||||
|
|
||||||
// OPER <name> <password>
|
// OPER <name> <password>
|
||||||
func NewOperCommand(args []string) (Command, error) {
|
func ParseOperCommand(args []string) (Command, error) {
|
||||||
if len(args) < 2 {
|
if len(args) < 2 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -705,12 +647,7 @@ type CapCommand struct {
|
|||||||
capabilities CapabilitySet
|
capabilities CapabilitySet
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg *CapCommand) String() string {
|
func ParseCapCommand(args []string) (Command, error) {
|
||||||
return fmt.Sprintf("CAP(subCommand=%s, capabilities=%s)",
|
|
||||||
msg.subCommand, msg.capabilities)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCapCommand(args []string) (Command, error) {
|
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -740,11 +677,15 @@ type ProxyCommand struct {
|
|||||||
hostname Name // looked up in socket thread
|
hostname Name // looked up in socket thread
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg *ProxyCommand) String() string {
|
func NewProxyCommand(hostname Name) *ProxyCommand {
|
||||||
return fmt.Sprintf("PROXY(sourceIP=%s, sourcePort=%s)", msg.sourceIP, msg.sourcePort)
|
cmd := &ProxyCommand{
|
||||||
|
hostname: hostname,
|
||||||
|
}
|
||||||
|
cmd.code = PROXY
|
||||||
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProxyCommand(args []string) (Command, error) {
|
func ParseProxyCommand(args []string) (Command, error) {
|
||||||
if len(args) < 5 {
|
if len(args) < 5 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -764,11 +705,7 @@ type AwayCommand struct {
|
|||||||
away bool
|
away bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg *AwayCommand) String() string {
|
func ParseAwayCommand(args []string) (Command, error) {
|
||||||
return fmt.Sprintf("AWAY(%s)", msg.text)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewAwayCommand(args []string) (Command, error) {
|
|
||||||
cmd := &AwayCommand{}
|
cmd := &AwayCommand{}
|
||||||
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
@ -784,11 +721,7 @@ type IsOnCommand struct {
|
|||||||
nicks []Name
|
nicks []Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg *IsOnCommand) String() string {
|
func ParseIsOnCommand(args []string) (Command, error) {
|
||||||
return fmt.Sprintf("ISON(nicks=%s)", msg.nicks)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewIsOnCommand(args []string) (Command, error) {
|
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -803,7 +736,7 @@ type MOTDCommand struct {
|
|||||||
target Name
|
target Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMOTDCommand(args []string) (Command, error) {
|
func ParseMOTDCommand(args []string) (Command, error) {
|
||||||
cmd := &MOTDCommand{}
|
cmd := &MOTDCommand{}
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
cmd.target = NewName(args[0])
|
cmd.target = NewName(args[0])
|
||||||
@ -817,11 +750,7 @@ type NoticeCommand struct {
|
|||||||
message Text
|
message Text
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *NoticeCommand) String() string {
|
func ParseNoticeCommand(args []string) (Command, error) {
|
||||||
return fmt.Sprintf("NOTICE(target=%s, message=%s)", cmd.target, cmd.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewNoticeCommand(args []string) (Command, error) {
|
|
||||||
if len(args) < 2 {
|
if len(args) < 2 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -844,7 +773,7 @@ func (msg *KickCommand) Comment() Text {
|
|||||||
return msg.comment
|
return msg.comment
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewKickCommand(args []string) (Command, error) {
|
func ParseKickCommand(args []string) (Command, error) {
|
||||||
if len(args) < 2 {
|
if len(args) < 2 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -875,7 +804,7 @@ type ListCommand struct {
|
|||||||
target Name
|
target Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewListCommand(args []string) (Command, error) {
|
func ParseListCommand(args []string) (Command, error) {
|
||||||
cmd := &ListCommand{}
|
cmd := &ListCommand{}
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
cmd.channels = NewNames(strings.Split(args[0], ","))
|
cmd.channels = NewNames(strings.Split(args[0], ","))
|
||||||
@ -892,7 +821,7 @@ type NamesCommand struct {
|
|||||||
target Name
|
target Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNamesCommand(args []string) (Command, error) {
|
func ParseNamesCommand(args []string) (Command, error) {
|
||||||
cmd := &NamesCommand{}
|
cmd := &NamesCommand{}
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
cmd.channels = NewNames(strings.Split(args[0], ","))
|
cmd.channels = NewNames(strings.Split(args[0], ","))
|
||||||
@ -908,7 +837,7 @@ type DebugCommand struct {
|
|||||||
subCommand Name
|
subCommand Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDebugCommand(args []string) (Command, error) {
|
func ParseDebugCommand(args []string) (Command, error) {
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -923,7 +852,7 @@ type VersionCommand struct {
|
|||||||
target Name
|
target Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVersionCommand(args []string) (Command, error) {
|
func ParseVersionCommand(args []string) (Command, error) {
|
||||||
cmd := &VersionCommand{}
|
cmd := &VersionCommand{}
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
cmd.target = NewName(args[0])
|
cmd.target = NewName(args[0])
|
||||||
@ -937,7 +866,7 @@ type InviteCommand struct {
|
|||||||
channel Name
|
channel Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewInviteCommand(args []string) (Command, error) {
|
func ParseInviteCommand(args []string) (Command, error) {
|
||||||
if len(args) < 2 {
|
if len(args) < 2 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -948,7 +877,7 @@ func NewInviteCommand(args []string) (Command, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTheaterCommand(args []string) (Command, error) {
|
func ParseTheaterCommand(args []string) (Command, error) {
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
} else if upperSubCmd := strings.ToUpper(args[0]); upperSubCmd == "IDENTIFY" && len(args) == 3 {
|
} else if upperSubCmd := strings.ToUpper(args[0]); upperSubCmd == "IDENTIFY" && len(args) == 3 {
|
||||||
@ -978,7 +907,7 @@ type TimeCommand struct {
|
|||||||
target Name
|
target Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTimeCommand(args []string) (Command, error) {
|
func ParseTimeCommand(args []string) (Command, error) {
|
||||||
cmd := &TimeCommand{}
|
cmd := &TimeCommand{}
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
cmd.target = NewName(args[0])
|
cmd.target = NewName(args[0])
|
||||||
@ -992,7 +921,7 @@ type KillCommand struct {
|
|||||||
comment Text
|
comment Text
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewKillCommand(args []string) (Command, error) {
|
func ParseKillCommand(args []string) (Command, error) {
|
||||||
if len(args) < 2 {
|
if len(args) < 2 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -1009,7 +938,7 @@ type WhoWasCommand struct {
|
|||||||
target Name
|
target Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWhoWasCommand(args []string) (Command, error) {
|
func ParseWhoWasCommand(args []string) (Command, error) {
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
@ -1025,7 +954,7 @@ func NewWhoWasCommand(args []string) (Command, error) {
|
|||||||
return cmd, nil
|
return cmd, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOperNickCommand(args []string) (Command, error) {
|
func ParseOperNickCommand(args []string) (Command, error) {
|
||||||
if len(args) < 2 {
|
if len(args) < 2 {
|
||||||
return nil, NotEnoughArgsError
|
return nil, NotEnoughArgsError
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,10 @@
|
|||||||
package irc
|
package irc
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
type NickCommand struct {
|
type NickCommand struct {
|
||||||
BaseCommand
|
BaseCommand
|
||||||
nickname Name
|
nickname Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *NickCommand) String() string {
|
|
||||||
return fmt.Sprintf("NICK(nickname=%s)", m.nickname)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *NickCommand) HandleRegServer(s *Server) {
|
func (m *NickCommand) HandleRegServer(s *Server) {
|
||||||
client := m.Client()
|
client := m.Client()
|
||||||
if !client.authorized {
|
if !client.authorized {
|
||||||
@ -89,11 +81,20 @@ func (msg *OperNickCommand) HandleServer(server *Server) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if msg.nick == client.nick {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
target := server.clients.Get(msg.target)
|
target := server.clients.Get(msg.target)
|
||||||
if target == nil {
|
if target == nil {
|
||||||
client.ErrNoSuchNick(msg.target)
|
client.ErrNoSuchNick(msg.target)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if server.clients.Get(msg.nick) != nil {
|
||||||
|
client.ErrNickNameInUse(msg.nick)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
target.ChangeNickname(msg.nick)
|
target.ChangeNickname(msg.nick)
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ func (target *Client) MultilineReply(names []string, code NumericCode, format st
|
|||||||
for to < len(names) {
|
for to < len(names) {
|
||||||
if (from < (to - 1)) && tooLong(names[from:to]) {
|
if (from < (to - 1)) && tooLong(names[from:to]) {
|
||||||
target.NumericReply(code, format, argsAndNames(names[from:to-1])...)
|
target.NumericReply(code, format, argsAndNames(names[from:to-1])...)
|
||||||
from, to = to-1, to
|
from = to - 1
|
||||||
} else {
|
} else {
|
||||||
to += 1
|
to += 1
|
||||||
}
|
}
|
||||||
|
@ -121,7 +121,7 @@ func (server *Server) loadChannels() {
|
|||||||
|
|
||||||
func (server *Server) processCommand(cmd Command) {
|
func (server *Server) processCommand(cmd Command) {
|
||||||
client := cmd.Client()
|
client := cmd.Client()
|
||||||
Log.debug.Printf("%s → %s %s", client, server, cmd)
|
Log.debug.Printf("%s → %+v", client, cmd)
|
||||||
|
|
||||||
if !client.registered {
|
if !client.registered {
|
||||||
regCmd, ok := cmd.(RegServerCommand)
|
regCmd, ok := cmd.(RegServerCommand)
|
||||||
@ -654,9 +654,11 @@ func (msg *DebugCommand) HandleServer(server *Server) {
|
|||||||
server.Reply(client, "OK")
|
server.Reply(client, "OK")
|
||||||
|
|
||||||
case "GCSTATS":
|
case "GCSTATS":
|
||||||
stats := &debug.GCStats{
|
stats := debug.GCStats{
|
||||||
|
Pause: make([]time.Duration, 10),
|
||||||
PauseQuantiles: make([]time.Duration, 5),
|
PauseQuantiles: make([]time.Duration, 5),
|
||||||
}
|
}
|
||||||
|
debug.ReadGCStats(&stats)
|
||||||
server.Reply(client, "last GC: %s", stats.LastGC.Format(time.RFC1123))
|
server.Reply(client, "last GC: %s", stats.LastGC.Format(time.RFC1123))
|
||||||
server.Reply(client, "num GC: %d", stats.NumGC)
|
server.Reply(client, "num GC: %d", stats.NumGC)
|
||||||
server.Reply(client, "pause total: %s", stats.PauseTotal)
|
server.Reply(client, "pause total: %s", stats.PauseTotal)
|
||||||
@ -671,14 +673,15 @@ func (msg *DebugCommand) HandleServer(server *Server) {
|
|||||||
server.Reply(client, "num goroutines: %d", count)
|
server.Reply(client, "num goroutines: %d", count)
|
||||||
|
|
||||||
case "PROFILEHEAP":
|
case "PROFILEHEAP":
|
||||||
file, err := os.Create("ergonomadic.heap.prof")
|
profFile := "ergonomadic-heap.prof"
|
||||||
|
file, err := os.Create(profFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error: %s", err)
|
log.Printf("error: %s", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
pprof.Lookup("heap").WriteTo(file, 0)
|
pprof.Lookup("heap").WriteTo(file, 0)
|
||||||
server.Reply(client, "written to ergonomadic-heap.prof")
|
server.Reply(client, "written to %s", profFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -15,14 +14,12 @@ const (
|
|||||||
|
|
||||||
type Socket struct {
|
type Socket struct {
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
reader *bufio.Reader
|
|
||||||
writer *bufio.Writer
|
writer *bufio.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSocket(conn net.Conn, commands chan<- Command) *Socket {
|
func NewSocket(conn net.Conn, commands chan<- Command) *Socket {
|
||||||
socket := &Socket{
|
socket := &Socket{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
reader: bufio.NewReader(conn),
|
|
||||||
writer: bufio.NewWriter(conn),
|
writer: bufio.NewWriter(conn),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,16 +38,11 @@ func (socket *Socket) Close() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (socket *Socket) readLines(commands chan<- Command) {
|
func (socket *Socket) readLines(commands chan<- Command) {
|
||||||
commands <- &ProxyCommand{
|
commands <- NewProxyCommand(AddrLookupHostname(socket.conn.RemoteAddr()))
|
||||||
hostname: AddrLookupHostname(socket.conn.RemoteAddr()),
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
scanner := bufio.NewScanner(socket.conn)
|
||||||
line, err := socket.reader.ReadString('\n')
|
for scanner.Scan() {
|
||||||
if socket.isError(err, R) {
|
line := scanner.Text()
|
||||||
break
|
|
||||||
}
|
|
||||||
line = strings.TrimRight(line, CRLF)
|
|
||||||
if len(line) == 0 {
|
if len(line) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -64,9 +56,13 @@ func (socket *Socket) readLines(commands chan<- Command) {
|
|||||||
commands <- msg
|
commands <- msg
|
||||||
}
|
}
|
||||||
|
|
||||||
commands <- &QuitCommand{
|
if err := scanner.Err(); err != nil {
|
||||||
message: "connection closed",
|
Log.debug.Printf("%s error: %s", socket, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
commands <- NewQuitCommand("connection closed")
|
||||||
|
|
||||||
|
close(commands)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (socket *Socket) Write(line string) (err error) {
|
func (socket *Socket) Write(line string) (err error) {
|
||||||
|
@ -1,9 +1,5 @@
|
|||||||
package irc
|
package irc
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
type TheaterClient Name
|
type TheaterClient Name
|
||||||
|
|
||||||
func (c TheaterClient) Id() Name {
|
func (c TheaterClient) Id() Name {
|
||||||
@ -29,10 +25,6 @@ func (m *TheaterIdentifyCommand) LoadPassword(s *Server) {
|
|||||||
m.hash = s.theaters[m.channel]
|
m.hash = s.theaters[m.channel]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *TheaterIdentifyCommand) String() string {
|
|
||||||
return fmt.Sprintf("THEATER_IDENTIFY(channel=%s)", cmd.channel)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TheaterIdentifyCommand) HandleServer(s *Server) {
|
func (m *TheaterIdentifyCommand) HandleServer(s *Server) {
|
||||||
client := m.Client()
|
client := m.Client()
|
||||||
if !m.channel.IsChannel() {
|
if !m.channel.IsChannel() {
|
||||||
@ -66,10 +58,6 @@ type TheaterPrivMsgCommand struct {
|
|||||||
message Text
|
message Text
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *TheaterPrivMsgCommand) String() string {
|
|
||||||
return fmt.Sprintf("THEATER_PRIVMSG(channel=%s, asNick=%s, message=%s)", cmd.channel, cmd.asNick, cmd.message)
|
|
||||||
|
|
||||||
}
|
|
||||||
func (m *TheaterPrivMsgCommand) HandleServer(s *Server) {
|
func (m *TheaterPrivMsgCommand) HandleServer(s *Server) {
|
||||||
client := m.Client()
|
client := m.Client()
|
||||||
|
|
||||||
@ -89,8 +77,9 @@ func (m *TheaterPrivMsgCommand) HandleServer(s *Server) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reply := RplPrivMsg(TheaterClient(m.asNick), channel, m.message)
|
||||||
for member := range channel.members {
|
for member := range channel.members {
|
||||||
member.Reply(RplPrivMsg(TheaterClient(m.asNick), channel, m.message))
|
member.Reply(reply)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,10 +90,6 @@ type TheaterActionCommand struct {
|
|||||||
action CTCPText
|
action CTCPText
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *TheaterActionCommand) String() string {
|
|
||||||
return fmt.Sprintf("THEATER_ACTION(channel=%s, asNick=%s, action=%s)", cmd.channel, cmd.asNick, cmd.action)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TheaterActionCommand) HandleServer(s *Server) {
|
func (m *TheaterActionCommand) HandleServer(s *Server) {
|
||||||
client := m.Client()
|
client := m.Client()
|
||||||
|
|
||||||
@ -124,7 +109,8 @@ func (m *TheaterActionCommand) HandleServer(s *Server) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reply := RplCTCPAction(TheaterClient(m.asNick), channel, m.action)
|
||||||
for member := range channel.members {
|
for member := range channel.members {
|
||||||
member.Reply(RplCTCPAction(TheaterClient(m.asNick), channel, m.action))
|
member.Reply(reply)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user