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

View File

@ -143,13 +143,13 @@ func handleJOIN(c *Client, e Event) {
channel = c.state.lookupChannel(channelName)
}
user := c.state.lookupUser(e.Source.ID)
user := c.state.lookupUser(e.Source.ID())
if user == nil {
if ok := c.state.createUser(e.Source); !ok {
c.state.Unlock()
return
}
user = c.state.lookupUser(e.Source.ID)
user = c.state.lookupUser(e.Source.ID())
}
defer c.state.notify(c, UPDATE_STATE)
@ -169,7 +169,7 @@ func handleJOIN(c *Client, e Event) {
}
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
// will tell us who exactly is in the entire channel.
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)
if e.Source.ID == ToRFC1459(c.GetNick()) {
if e.Source.ID() == ToRFC1459(c.GetNick()) {
c.state.Lock()
c.state.deleteChannel(channel)
c.state.Unlock()
@ -217,7 +217,7 @@ func handlePART(c *Client, e Event) {
}
c.state.Lock()
c.state.deleteUser(channel, e.Source.ID)
c.state.deleteUser(channel, e.Source.ID())
c.state.Unlock()
}
@ -327,9 +327,9 @@ func handleNICK(c *Client, e Event) {
c.state.Lock()
// renameUser updates the LastActive time automatically.
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 {
c.state.renameUser(e.Source.ID, e.Trailing)
c.state.renameUser(e.Source.ID(), e.Trailing)
}
c.state.Unlock()
c.state.notify(c, UPDATE_STATE)
@ -341,12 +341,12 @@ func handleQUIT(c *Client, e Event) {
return
}
if e.Source.ID == ToRFC1459(c.GetNick()) {
if e.Source.ID() == ToRFC1459(c.GetNick()) {
return
}
c.state.Lock()
c.state.deleteUser("", e.Source.ID)
c.state.deleteUser("", e.Source.ID())
c.state.Unlock()
c.state.notify(c, UPDATE_STATE)
}
@ -464,22 +464,21 @@ func handleNAMES(c *Client, e Event) {
} else {
s = &Source{
Name: nick,
ID: ToRFC1459(nick),
}
if !IsValidNick(s.ID) {
if !IsValidNick(s.Name) {
continue
}
}
c.state.createUser(s)
user := c.state.lookupUser(s.ID)
user := c.state.lookupUser(s.ID())
if user == nil {
continue
}
user.addChannel(channel.Name)
channel.addUser(s.ID)
channel.addUser(s.ID())
// Don't append modes, overwrite them.
perms, _ := user.Perms.Lookup(channel.Name)
@ -502,7 +501,7 @@ func updateLastActive(c *Client, e Event) {
c.state.Lock()
// 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 {
c.state.Unlock()
return

6
cap.go
View File

@ -193,7 +193,7 @@ func handleCHGHOST(c *Client, e Event) {
}
c.state.Lock()
user := c.state.lookupUser(e.Source.ID)
user := c.state.lookupUser(e.Source.ID())
if user != nil {
user.Ident = e.Params[0]
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.
func handleAWAY(c *Client, e Event) {
c.state.Lock()
user := c.state.lookupUser(e.Source.ID)
user := c.state.lookupUser(e.Source.ID())
if user != nil {
user.Extras.Away = e.Trailing
}
@ -229,7 +229,7 @@ func handleACCOUNT(c *Client, e Event) {
}
c.state.Lock()
user := c.state.lookupUser(e.Source.ID)
user := c.state.lookupUser(e.Source.ID())
if user != nil {
user.Extras.Account = account
}

View File

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

View File

@ -374,7 +374,7 @@ func (c *Client) readLoop(ctx context.Context, errs chan error, wg *sync.WaitGro
// Check if it's an echo-message.
if !c.Config.disableTracking {
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

18
ctcp.go
View File

@ -155,8 +155,8 @@ func (c *CTCP) call(client *Client, event *CTCPEvent) {
}
// Send a ERRMSG reply, if we know who sent it.
if event.Source != nil && IsValidNick(event.Source.ID) {
client.Cmd.SendCTCPReply(event.Source.ID, CTCP_ERRMSG, "that is an unknown CTCP query")
if event.Source != nil && IsValidNick(event.Source.ID()) {
client.Cmd.SendCTCPReply(event.Source.ID(), CTCP_ERRMSG, "that is an unknown CTCP query")
}
return
}
@ -248,7 +248,7 @@ func handleCTCPPing(client *Client, ctcp CTCPEvent) {
if ctcp.Reply {
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.
@ -256,7 +256,7 @@ func handleCTCPPong(client *Client, ctcp CTCPEvent) {
if ctcp.Reply {
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
@ -264,12 +264,12 @@ func handleCTCPPong(client *Client, ctcp CTCPEvent) {
// arm, etc).
func handleCTCPVersion(client *Client, ctcp CTCPEvent) {
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
}
client.Cmd.SendCTCPReplyf(
ctcp.Source.ID, CTCP_VERSION,
ctcp.Source.ID(), CTCP_VERSION,
"girc (github.com/lrstanley/girc) using %s (%s, %s)",
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.
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
// local time.
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
@ -293,5 +293,5 @@ func handleCTCPFinger(client *Client, ctcp CTCPEvent) {
active := client.conn.lastActive
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)))
}

View File

@ -516,9 +516,6 @@ const (
// Source represents the sender of an IRC event, see RFC1459 section 2.3.1.
// <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
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
// non-rfc1459 form.
Name string `json:"name"`
@ -529,6 +526,12 @@ type Source struct {
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.
func (s *Source) Equals(ss *Source) bool {
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 {
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 true
@ -550,7 +553,6 @@ func (s *Source) Copy() *Source {
}
newSource := &Source{
ID: s.ID,
Name: s.Name,
Ident: s.Ident,
Host: s.Host,
@ -581,8 +583,6 @@ func ParseSource(raw string) (src *Source) {
src.Name = raw
}
src.ID = ToRFC1459(src.Name)
return src
}

View File

@ -11,7 +11,7 @@ import (
func mockEvent() *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",
Params: []string{"#channel"},
Trailing: "1 2 3",
@ -47,7 +47,6 @@ func TestParseSource(t *testing.T) {
}},
}
for _, tt := range tests {
tt.wantSrc.ID = ToRFC1459(tt.wantSrc.Name)
gotSrc := ParseSource(tt.args.raw)
if !reflect.DeepEqual(gotSrc, tt.wantSrc) {

View File

@ -420,12 +420,12 @@ func (s *state) lookupUser(name string) *User {
// createUser creates the user in state, if not already done.
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.
return false
}
s.users[src.ID] = &User{
s.users[src.ID()] = &User{
Nick: src.Name,
Host: src.Host,
Ident: src.Ident,