implement Source.Copy(), and rewrite Event.Copy()

This commit is contained in:
Liam Stanley 2017-04-18 06:20:33 -04:00
parent 22e4b8c8a4
commit 9cb3f3c522
2 changed files with 28 additions and 3 deletions

View File

@ -78,7 +78,9 @@ func handleConnect(c *Client, e Event) {
// the one we supplied during connection, but some networks will rename
// users on connect.
if len(e.Params) > 0 {
c.state.mu.Lock()
c.state.nick = e.Params[0]
c.state.mu.Unlock()
}
time.Sleep(2 * time.Second)

View File

@ -125,17 +125,25 @@ func ParseEvent(raw string) (e *Event) {
// functions/handlers edit the event without causing potential issues with
// other handlers.
func (e *Event) Copy() *Event {
newEvent := &Event{}
if e == nil {
return nil
}
*newEvent = *e
newEvent := &Event{
Command: e.Command,
Trailing: e.Trailing,
EmptyTrailing: e.EmptyTrailing,
Sensitive: e.Sensitive,
}
// Copy Source field, as it's a pointer and needs to be dereferenced.
if e.Source != nil {
*newEvent.Source = *e.Source
newEvent.Source = e.Source.Copy()
}
// Copy Params in order to dereference as well.
if e.Params != nil {
newEvent.Params = make([]string, len(e.Params))
copy(newEvent.Params, e.Params)
}
@ -422,6 +430,21 @@ type Source struct {
Host string
}
// Copy returns a deep copy of Source.
func (s *Source) Copy() *Source {
if s == nil {
return nil
}
newSource := &Source{
Name: s.Name,
Ident: s.Ident,
Host: s.Host,
}
return newSource
}
// ParseSource takes a string and attempts to create a Source struct.
func ParseSource(raw string) (src *Source) {
src = new(Source)