extra test checks; added ctcp SetBg, added handleCTCPPing to default CTCPs

This commit is contained in:
Liam Stanley 2016-12-26 03:06:15 -05:00
parent f2ae68e9b7
commit caaaf560be
2 changed files with 18 additions and 4 deletions

@ -32,6 +32,8 @@ type Client struct {
// Callbacks is a handler which manages internal and external callbacks.
Callbacks *Caller
// CTCP is a handler which manages internal and external CTCP handlers.
CTCP *CTCP
// tries represents the internal reconnect count to the IRC server.
tries int
@ -109,6 +111,7 @@ func New(config Config) *Client {
quitChan: make(chan struct{}, 1),
stopChan: make(chan struct{}, 1),
Callbacks: newCaller(),
CTCP: newCTCP(),
initTime: time.Now(),
}

19
ctcp.go

@ -18,7 +18,9 @@ type CTCPEvent struct {
}
func decodeCTCP(e *Event) *CTCPEvent {
if len(e.Params) != 1 {
// Must be targetting a user/channel, AND trailing must have
// DELIM+TAG+DELIM minimum (at least 3 chars).
if len(e.Params) != 1 || len(e.Trailing) < 3 {
return nil
}
@ -30,6 +32,7 @@ func decodeCTCP(e *Event) *CTCPEvent {
return nil
}
// Strip delimiters.
text := e.Trailing[1 : len(e.Trailing)-1]
s := strings.IndexByte(text, space)
@ -54,7 +57,7 @@ func decodeCTCP(e *Event) *CTCPEvent {
}
}
return &CTCPEvent{Source: e.Source, Command: text[1:s], Text: text[s+1 : len(text)-1]}
return &CTCPEvent{Source: e.Source, Command: text[0:s], Text: text[s+1 : len(text)]}
}
func encodeCTCP(ctcp *CTCPEvent) (out string) {
@ -90,7 +93,7 @@ func newCTCP() *CTCP {
return &CTCP{handlers: map[string]CTCPHandler{}}
}
func (c *CTCP) Call(event *CTCPEvent, client *Client) {
func (c *CTCP) call(event *CTCPEvent, client *Client) {
c.mu.RLock()
if _, ok := c.handlers[event.Command]; !ok {
c.mu.RUnlock()
@ -124,6 +127,12 @@ func (c *CTCP) Set(cmd string, handler func(client *Client, ctcp *CTCPEvent)) {
c.mu.Unlock()
}
func (c *CTCP) SetBg(cmd string, handler func(client *Client, ctcp *CTCPEvent)) {
c.Set(cmd, func(client *Client, ctcp *CTCPEvent) {
go handler(client, ctcp)
})
}
func (c *CTCP) Clear(cmd string) {
if cmd = c.parseCMD(cmd); cmd == "" {
return
@ -144,7 +153,9 @@ func (c *CTCP) ClearAll() {
// implement a CTCP handler.
type CTCPHandler func(client *Client, ctcp *CTCPEvent)
func (c *CTCP) addDefaultHandlers() {}
func (c *CTCP) addDefaultHandlers() {
c.SetBg(CTCP_PING, handleCTCPPing)
}
func handleCTCPPing(client *Client, ctcp *CTCPEvent) {
client.SendCTCP(ctcp.Source.Name, CTCP_PING, "")