support errors for common helper functions like Client.Message()

This commit is contained in:
Liam Stanley 2016-12-04 22:09:03 -05:00
parent d94de12e6b
commit 29612d0d29
2 changed files with 33 additions and 33 deletions

@ -18,9 +18,8 @@
## TODO ## TODO
- [ ] Should Client.Message() an other similar methods support errors? - [ ] should we forcefully check to ensure that the target/events are valid?
- [ ] along with this, should we forcefully check to ensure that the target/events are valid? - [ ] should we not allow methods like `Action()` and `SendRaw()` when not connected?
- [ ] should we not allow methods like `Action()` and `SendRaw()` when not connected?
- [ ] would be cool to track things like `SERVERNAME`, `VERSION`, `UMODES`, `CMODES`, etc. also see `Config.DisableCapTracking`. [e.g. here](https://github.com/Liamraystanley/Code/blob/master/core/triggers.py#L40-L67) - [ ] would be cool to track things like `SERVERNAME`, `VERSION`, `UMODES`, `CMODES`, etc. also see `Config.DisableCapTracking`. [e.g. here](https://github.com/Liamraystanley/Code/blob/master/core/triggers.py#L40-L67)
- [ ] client should support ping tracking (sending `PING`'s to the server) - [ ] client should support ping tracking (sending `PING`'s to the server)
- [ ] with this, we can potentially find lag. `Client.Lag()` would be useful - [ ] with this, we can potentially find lag. `Client.Lag()` would be useful

61
main.go

@ -401,12 +401,12 @@ func (c *Client) GetNick() string {
} }
// SetNick changes the client nickname. // SetNick changes the client nickname.
func (c *Client) SetNick(name string) { func (c *Client) SetNick(name string) error {
c.state.m.Lock() c.state.m.Lock()
defer c.state.m.Unlock() defer c.state.m.Unlock()
c.state.nick = name c.state.nick = name
c.Send(&Event{Command: NICK, Params: []string{name}}) return c.Send(&Event{Command: NICK, Params: []string{name}})
} }
// GetChannels returns the active list of channels that the client // GetChannels returns the active list of channels that the client
@ -427,80 +427,81 @@ func (c *Client) GetChannels() map[string]*Channel {
// Who tells the client to update it's channel/user records. // Who tells the client to update it's channel/user records.
// //
// Does not update internal state if tracking is disabled. // Does not update internal state if tracking is disabled.
func (c *Client) Who(target string) { func (c *Client) Who(target string) error {
c.Send(&Event{Command: WHO, Params: []string{target, "%tcuhn,1"}}) return c.Send(&Event{Command: WHO, Params: []string{target, "%tcuhn,1"}})
} }
// Join attempts to enter an IRC channel with an optional password. // Join attempts to enter an IRC channel with an optional password.
func (c *Client) Join(channel, password string) { func (c *Client) Join(channel, password string) error {
if password != "" { if password != "" {
c.Send(&Event{Command: JOIN, Params: []string{channel, password}}) return c.Send(&Event{Command: JOIN, Params: []string{channel, password}})
return
} }
c.Send(&Event{Command: JOIN, Params: []string{channel}}) return c.Send(&Event{Command: JOIN, Params: []string{channel}})
} }
// Part leaves an IRC channel with an optional leave message. // Part leaves an IRC channel with an optional leave message.
func (c *Client) Part(channel, message string) { func (c *Client) Part(channel, message string) error {
if message != "" { if message != "" {
c.Send(&Event{Command: JOIN, Params: []string{channel}, Trailing: message}) return c.Send(&Event{Command: JOIN, Params: []string{channel}, Trailing: message})
return
} }
c.Send(&Event{Command: JOIN, Params: []string{channel}}) return c.Send(&Event{Command: JOIN, Params: []string{channel}})
} }
// Message sends a PRIVMSG to target (either channel, service, or // Message sends a PRIVMSG to target (either channel, service, or
// user). // user).
func (c *Client) Message(target, message string) { func (c *Client) Message(target, message string) error {
c.Send(&Event{Command: PRIVMSG, Params: []string{target}, Trailing: message}) return c.Send(&Event{Command: PRIVMSG, Params: []string{target}, Trailing: message})
} }
// Messagef sends a formated PRIVMSG to target (either channel, // Messagef sends a formated PRIVMSG to target (either channel,
// service, or user). // service, or user).
func (c *Client) Messagef(target, format string, a ...interface{}) { func (c *Client) Messagef(target, format string, a ...interface{}) error {
c.Message(target, fmt.Sprintf(format, a...)) return c.Message(target, fmt.Sprintf(format, a...))
} }
// Action sends a PRIVMSG ACTION (/me) to target (either channel, // Action sends a PRIVMSG ACTION (/me) to target (either channel,
// service, or user). // service, or user).
func (c *Client) Action(target, message string) { func (c *Client) Action(target, message string) error {
c.Send(&Event{Command: PRIVMSG, Params: []string{target}, Trailing: fmt.Sprintf("\001ACTION %s\001", message)}) return c.Send(&Event{
Command: PRIVMSG,
Params: []string{target},
Trailing: fmt.Sprintf("\001ACTION %s\001", message),
})
} }
// Actionf sends a formated PRIVMSG ACTION (/me) to target (either // Actionf sends a formated PRIVMSG ACTION (/me) to target (either
// channel, service, or user). // channel, service, or user).
func (c *Client) Actionf(target, format string, a ...interface{}) { func (c *Client) Actionf(target, format string, a ...interface{}) error {
c.Action(target, fmt.Sprintf(format, a...)) return c.Action(target, fmt.Sprintf(format, a...))
} }
// Notice sends a NOTICE to target (either channel, service, or user). // Notice sends a NOTICE to target (either channel, service, or user).
func (c *Client) Notice(target, message string) { func (c *Client) Notice(target, message string) error {
c.Send(&Event{Command: NOTICE, Params: []string{target}, Trailing: message}) return c.Send(&Event{Command: NOTICE, Params: []string{target}, Trailing: message})
} }
// Noticef sends a formated NOTICE to target (either channel, service, or user). // Noticef sends a formated NOTICE to target (either channel, service, or user).
func (c *Client) Noticef(target, format string, a ...interface{}) { func (c *Client) Noticef(target, format string, a ...interface{}) error {
c.Notice(target, fmt.Sprintf(format, a...)) return c.Notice(target, fmt.Sprintf(format, a...))
} }
// SendRaw sends a raw string back to the server, without carriage returns or // SendRaw sends a raw string back to the server, without carriage returns or
// newlines. // newlines.
func (c *Client) SendRaw(raw string) { func (c *Client) SendRaw(raw string) error {
e := ParseEvent(raw) e := ParseEvent(raw)
if e == nil { if e == nil {
c.log.Printf("invalid event: %q", raw) return errors.New("invalid event: " + raw)
return
} }
c.Send(e) return c.Send(e)
} }
// SendRawf sends a formated string back to the server, without carriage // SendRawf sends a formated string back to the server, without carriage
// returns or newlines. // returns or newlines.
func (c *Client) SendRawf(format string, a ...interface{}) { func (c *Client) SendRawf(format string, a ...interface{}) error {
c.SendRaw(fmt.Sprintf(format, a...)) return c.SendRaw(fmt.Sprintf(format, a...))
} }
// Whowas sends and waits for a response to a WHOWAS query to the server. // Whowas sends and waits for a response to a WHOWAS query to the server.