convert ID field to ID method

This commit is contained in:
Liam Stanley 2018-12-09 16:23:41 -08:00
parent 3167511d81
commit d173036cbc
8 changed files with 37 additions and 39 deletions

@ -143,13 +143,13 @@ func handleJOIN(c *Client, e Event) {
channel = c.state.lookupChannel(channelName) channel = c.state.lookupChannel(channelName)
} }
user := c.state.lookupUser(e.Source.ID) user := c.state.lookupUser(e.Source.ID())
if user == nil { if user == nil {
if ok := c.state.createUser(e.Source); !ok { if ok := c.state.createUser(e.Source); !ok {
c.state.Unlock() c.state.Unlock()
return return
} }
user = c.state.lookupUser(e.Source.ID) user = c.state.lookupUser(e.Source.ID())
} }
defer c.state.notify(c, UPDATE_STATE) defer c.state.notify(c, UPDATE_STATE)
@ -169,7 +169,7 @@ func handleJOIN(c *Client, e Event) {
} }
c.state.Unlock() c.state.Unlock()
if e.Source.ID == ToRFC1459(c.GetNick()) { if e.Source.ID() == ToRFC1459(c.GetNick()) {
// If it's us, don't just add our user to the list. Run a WHO which // If it's us, don't just add our user to the list. Run a WHO which
// will tell us who exactly is in the entire channel. // will tell us who exactly is in the entire channel.
c.Send(&Event{Command: WHO, Params: []string{channelName, "%tacuhnr,1"}}) c.Send(&Event{Command: WHO, Params: []string{channelName, "%tacuhnr,1"}})
@ -209,7 +209,7 @@ func handlePART(c *Client, e Event) {
defer c.state.notify(c, UPDATE_STATE) defer c.state.notify(c, UPDATE_STATE)
if e.Source.ID == ToRFC1459(c.GetNick()) { if e.Source.ID() == ToRFC1459(c.GetNick()) {
c.state.Lock() c.state.Lock()
c.state.deleteChannel(channel) c.state.deleteChannel(channel)
c.state.Unlock() c.state.Unlock()
@ -217,7 +217,7 @@ func handlePART(c *Client, e Event) {
} }
c.state.Lock() c.state.Lock()
c.state.deleteUser(channel, e.Source.ID) c.state.deleteUser(channel, e.Source.ID())
c.state.Unlock() c.state.Unlock()
} }
@ -327,9 +327,9 @@ func handleNICK(c *Client, e Event) {
c.state.Lock() c.state.Lock()
// renameUser updates the LastActive time automatically. // renameUser updates the LastActive time automatically.
if len(e.Params) == 1 { if len(e.Params) == 1 {
c.state.renameUser(e.Source.ID, e.Params[0]) c.state.renameUser(e.Source.ID(), e.Params[0])
} else if len(e.Trailing) > 0 { } else if len(e.Trailing) > 0 {
c.state.renameUser(e.Source.ID, e.Trailing) c.state.renameUser(e.Source.ID(), e.Trailing)
} }
c.state.Unlock() c.state.Unlock()
c.state.notify(c, UPDATE_STATE) c.state.notify(c, UPDATE_STATE)
@ -341,12 +341,12 @@ func handleQUIT(c *Client, e Event) {
return return
} }
if e.Source.ID == ToRFC1459(c.GetNick()) { if e.Source.ID() == ToRFC1459(c.GetNick()) {
return return
} }
c.state.Lock() c.state.Lock()
c.state.deleteUser("", e.Source.ID) c.state.deleteUser("", e.Source.ID())
c.state.Unlock() c.state.Unlock()
c.state.notify(c, UPDATE_STATE) c.state.notify(c, UPDATE_STATE)
} }
@ -464,22 +464,21 @@ func handleNAMES(c *Client, e Event) {
} else { } else {
s = &Source{ s = &Source{
Name: nick, Name: nick,
ID: ToRFC1459(nick),
} }
if !IsValidNick(s.ID) { if !IsValidNick(s.Name) {
continue continue
} }
} }
c.state.createUser(s) c.state.createUser(s)
user := c.state.lookupUser(s.ID) user := c.state.lookupUser(s.ID())
if user == nil { if user == nil {
continue continue
} }
user.addChannel(channel.Name) user.addChannel(channel.Name)
channel.addUser(s.ID) channel.addUser(s.ID())
// Don't append modes, overwrite them. // Don't append modes, overwrite them.
perms, _ := user.Perms.Lookup(channel.Name) perms, _ := user.Perms.Lookup(channel.Name)
@ -502,7 +501,7 @@ func updateLastActive(c *Client, e Event) {
c.state.Lock() c.state.Lock()
// Update the users last active time, if they exist. // Update the users last active time, if they exist.
user := c.state.lookupUser(e.Source.ID) user := c.state.lookupUser(e.Source.ID())
if user == nil { if user == nil {
c.state.Unlock() c.state.Unlock()
return return

6
cap.go

@ -193,7 +193,7 @@ func handleCHGHOST(c *Client, e Event) {
} }
c.state.Lock() c.state.Lock()
user := c.state.lookupUser(e.Source.ID) user := c.state.lookupUser(e.Source.ID())
if user != nil { if user != nil {
user.Ident = e.Params[0] user.Ident = e.Params[0]
user.Host = e.Params[1] user.Host = e.Params[1]
@ -206,7 +206,7 @@ func handleCHGHOST(c *Client, e Event) {
// when users are no longer away, or when they are away. // when users are no longer away, or when they are away.
func handleAWAY(c *Client, e Event) { func handleAWAY(c *Client, e Event) {
c.state.Lock() c.state.Lock()
user := c.state.lookupUser(e.Source.ID) user := c.state.lookupUser(e.Source.ID())
if user != nil { if user != nil {
user.Extras.Away = e.Trailing user.Extras.Away = e.Trailing
} }
@ -229,7 +229,7 @@ func handleACCOUNT(c *Client, e Event) {
} }
c.state.Lock() c.state.Lock()
user := c.state.lookupUser(e.Source.ID) user := c.state.lookupUser(e.Source.ID())
if user != nil { if user != nil {
user.Extras.Account = account user.Extras.Account = account
} }

@ -25,7 +25,7 @@ func handleTags(c *Client, e Event) {
} }
c.state.Lock() c.state.Lock()
user := c.state.lookupUser(e.Source.ID) user := c.state.lookupUser(e.Source.ID())
if user != nil { if user != nil {
user.Extras.Account = account user.Extras.Account = account
} }

@ -374,7 +374,7 @@ func (c *Client) readLoop(ctx context.Context, errs chan error, wg *sync.WaitGro
// Check if it's an echo-message. // Check if it's an echo-message.
if !c.Config.disableTracking { if !c.Config.disableTracking {
event.Echo = (event.Command == PRIVMSG || event.Command == NOTICE) && event.Echo = (event.Command == PRIVMSG || event.Command == NOTICE) &&
event.Source != nil && event.Source.ID == ToRFC1459(c.GetNick()) event.Source != nil && event.Source.ID() == ToRFC1459(c.GetNick())
} }
c.rx <- event c.rx <- event

18
ctcp.go

@ -155,8 +155,8 @@ func (c *CTCP) call(client *Client, event *CTCPEvent) {
} }
// Send a ERRMSG reply, if we know who sent it. // Send a ERRMSG reply, if we know who sent it.
if event.Source != nil && IsValidNick(event.Source.ID) { if event.Source != nil && IsValidNick(event.Source.ID()) {
client.Cmd.SendCTCPReply(event.Source.ID, CTCP_ERRMSG, "that is an unknown CTCP query") client.Cmd.SendCTCPReply(event.Source.ID(), CTCP_ERRMSG, "that is an unknown CTCP query")
} }
return return
} }
@ -248,7 +248,7 @@ func handleCTCPPing(client *Client, ctcp CTCPEvent) {
if ctcp.Reply { if ctcp.Reply {
return return
} }
client.Cmd.SendCTCPReply(ctcp.Source.ID, CTCP_PING, ctcp.Text) client.Cmd.SendCTCPReply(ctcp.Source.ID(), CTCP_PING, ctcp.Text)
} }
// handleCTCPPong replies with a pong. // handleCTCPPong replies with a pong.
@ -256,7 +256,7 @@ func handleCTCPPong(client *Client, ctcp CTCPEvent) {
if ctcp.Reply { if ctcp.Reply {
return return
} }
client.Cmd.SendCTCPReply(ctcp.Source.ID, CTCP_PONG, "") client.Cmd.SendCTCPReply(ctcp.Source.ID(), CTCP_PONG, "")
} }
// handleCTCPVersion replies with the name of the client, Go version, as well // handleCTCPVersion replies with the name of the client, Go version, as well
@ -264,12 +264,12 @@ func handleCTCPPong(client *Client, ctcp CTCPEvent) {
// arm, etc). // arm, etc).
func handleCTCPVersion(client *Client, ctcp CTCPEvent) { func handleCTCPVersion(client *Client, ctcp CTCPEvent) {
if client.Config.Version != "" { if client.Config.Version != "" {
client.Cmd.SendCTCPReply(ctcp.Source.ID, CTCP_VERSION, client.Config.Version) client.Cmd.SendCTCPReply(ctcp.Source.ID(), CTCP_VERSION, client.Config.Version)
return return
} }
client.Cmd.SendCTCPReplyf( client.Cmd.SendCTCPReplyf(
ctcp.Source.ID, CTCP_VERSION, ctcp.Source.ID(), CTCP_VERSION,
"girc (github.com/lrstanley/girc) using %s (%s, %s)", "girc (github.com/lrstanley/girc) using %s (%s, %s)",
runtime.Version(), runtime.GOOS, runtime.GOARCH, runtime.Version(), runtime.GOOS, runtime.GOARCH,
) )
@ -277,13 +277,13 @@ func handleCTCPVersion(client *Client, ctcp CTCPEvent) {
// handleCTCPSource replies with the public git location of this library. // handleCTCPSource replies with the public git location of this library.
func handleCTCPSource(client *Client, ctcp CTCPEvent) { func handleCTCPSource(client *Client, ctcp CTCPEvent) {
client.Cmd.SendCTCPReply(ctcp.Source.ID, CTCP_SOURCE, "https://github.com/lrstanley/girc") client.Cmd.SendCTCPReply(ctcp.Source.ID(), CTCP_SOURCE, "https://github.com/lrstanley/girc")
} }
// handleCTCPTime replies with a RFC 1123 (Z) formatted version of Go's // handleCTCPTime replies with a RFC 1123 (Z) formatted version of Go's
// local time. // local time.
func handleCTCPTime(client *Client, ctcp CTCPEvent) { func handleCTCPTime(client *Client, ctcp CTCPEvent) {
client.Cmd.SendCTCPReply(ctcp.Source.ID, CTCP_TIME, ":"+time.Now().Format(time.RFC1123Z)) client.Cmd.SendCTCPReply(ctcp.Source.ID(), CTCP_TIME, ":"+time.Now().Format(time.RFC1123Z))
} }
// handleCTCPFinger replies with the realname and idle time of the user. This // handleCTCPFinger replies with the realname and idle time of the user. This
@ -293,5 +293,5 @@ func handleCTCPFinger(client *Client, ctcp CTCPEvent) {
active := client.conn.lastActive active := client.conn.lastActive
client.conn.mu.RUnlock() client.conn.mu.RUnlock()
client.Cmd.SendCTCPReply(ctcp.Source.ID, CTCP_FINGER, fmt.Sprintf("%s -- idle %s", client.Config.Name, time.Since(active))) client.Cmd.SendCTCPReply(ctcp.Source.ID(), CTCP_FINGER, fmt.Sprintf("%s -- idle %s", client.Config.Name, time.Since(active)))
} }

@ -516,9 +516,6 @@ const (
// Source represents the sender of an IRC event, see RFC1459 section 2.3.1. // Source represents the sender of an IRC event, see RFC1459 section 2.3.1.
// <servername> | <nick> [ '!' <user> ] [ '@' <host> ] // <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
type Source struct { type Source struct {
// ID is the nickname, server name, or service name, in it's converted
// and comparable) form.
ID string `json:"id"`
// Name is the nickname, server name, or service name, in its original // Name is the nickname, server name, or service name, in its original
// non-rfc1459 form. // non-rfc1459 form.
Name string `json:"name"` Name string `json:"name"`
@ -529,6 +526,12 @@ type Source struct {
Host string `json:"host"` Host string `json:"host"`
} }
// ID is the nickname, server name, or service name, in it's converted
// and comparable) form.
func (s *Source) ID() string {
return ToRFC1459(s.Name)
}
// Equals compares two Sources for equality. // Equals compares two Sources for equality.
func (s *Source) Equals(ss *Source) bool { func (s *Source) Equals(ss *Source) bool {
if s == nil && ss == nil { if s == nil && ss == nil {
@ -537,7 +540,7 @@ func (s *Source) Equals(ss *Source) bool {
if s != nil && ss == nil || s == nil && ss != nil { if s != nil && ss == nil || s == nil && ss != nil {
return false return false
} }
if s.ID != ss.ID || s.Ident != ss.Ident || s.Host != ss.Host { if s.ID() != ss.ID() || s.Ident != ss.Ident || s.Host != ss.Host {
return false return false
} }
return true return true
@ -550,7 +553,6 @@ func (s *Source) Copy() *Source {
} }
newSource := &Source{ newSource := &Source{
ID: s.ID,
Name: s.Name, Name: s.Name,
Ident: s.Ident, Ident: s.Ident,
Host: s.Host, Host: s.Host,
@ -581,8 +583,6 @@ func ParseSource(raw string) (src *Source) {
src.Name = raw src.Name = raw
} }
src.ID = ToRFC1459(src.Name)
return src return src
} }

@ -11,7 +11,7 @@ import (
func mockEvent() *Event { func mockEvent() *Event {
return &Event{ return &Event{
Source: &Source{ID: "nick", Name: "nick", Ident: "user", Host: "host.com"}, Source: &Source{Name: "nick", Ident: "user", Host: "host.com"},
Command: "PRIVMSG", Command: "PRIVMSG",
Params: []string{"#channel"}, Params: []string{"#channel"},
Trailing: "1 2 3", Trailing: "1 2 3",
@ -47,7 +47,6 @@ func TestParseSource(t *testing.T) {
}}, }},
} }
for _, tt := range tests { for _, tt := range tests {
tt.wantSrc.ID = ToRFC1459(tt.wantSrc.Name)
gotSrc := ParseSource(tt.args.raw) gotSrc := ParseSource(tt.args.raw)
if !reflect.DeepEqual(gotSrc, tt.wantSrc) { if !reflect.DeepEqual(gotSrc, tt.wantSrc) {

@ -420,12 +420,12 @@ func (s *state) lookupUser(name string) *User {
// createUser creates the user in state, if not already done. // createUser creates the user in state, if not already done.
func (s *state) createUser(src *Source) (ok bool) { func (s *state) createUser(src *Source) (ok bool) {
if _, ok := s.users[src.ID]; ok { if _, ok := s.users[src.ID()]; ok {
// User already exists. // User already exists.
return false return false
} }
s.users[src.ID] = &User{ s.users[src.ID()] = &User{
Nick: src.Name, Nick: src.Name,
Host: src.Host, Host: src.Host,
Ident: src.Ident, Ident: src.Ident,