From 5ea3252b9b272fbcc0e5e68341195c35ab79ba9b Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Fri, 26 Nov 2021 00:53:20 -0800 Subject: [PATCH] New: handle RPL_CREATIONTIME --- builtin.go | 29 +++++++++++++++++++++++++++++ codebook.go | 6 ++++++ constants.go | 1 + state.go | 5 ++++- util.go | 2 +- 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/builtin.go b/builtin.go index 549fba0..6351843 100644 --- a/builtin.go +++ b/builtin.go @@ -51,6 +51,9 @@ func (c *Client) registerBuiltins() { c.Handlers.register(true, false, MODE, HandlerFunc(handleMODE)) c.Handlers.register(true, false, RPL_CHANNELMODEIS, HandlerFunc(handleMODE)) + // Channel creation time. + c.Handlers.register(true, false, RPL_CREATIONTIME, HandlerFunc(handleCREATIONTIME)) + // WHO/WHOX responses. c.Handlers.register(true, false, RPL_WHOREPLY, HandlerFunc(handleWHO)) c.Handlers.register(true, false, RPL_WHOSPCRPL, HandlerFunc(handleWHO)) @@ -230,6 +233,32 @@ func handlePART(c *Client, e Event) { c.state.Unlock() } +// handleCREATIONTIME handles incoming TOPIC events and keeps channel tracking info +// updated with the latest channel topic. +func handleCREATIONTIME(c *Client, e Event) { + var created string + var name string + switch len(e.Params) { + case 0, 1, 2: + return + default: + name = e.Params[1] + created = e.Params[2] + break + } + + c.state.Lock() + defer c.state.Unlock() + + channel := c.state.lookupChannel(name) + if channel == nil { + return + } + + channel.Created = created + c.state.notify(c, UPDATE_STATE) +} + // handleTOPIC handles incoming TOPIC events and keeps channel tracking info // updated with the latest channel topic. func handleTOPIC(c *Client, e Event) { diff --git a/codebook.go b/codebook.go index cdb6d42..98ef1be 100644 --- a/codebook.go +++ b/codebook.go @@ -5,12 +5,17 @@ IRCNumToStr takes in a numeric IRC code and returns the relevant girc name. IRCNumToStr accepts a string because that's how we tend to receive the codes. */ func IRCNumToStr(code string) string { + if _, ok := noTranslate[code]; ok { + return code + } if result, ok := IRCCodes[code]; ok { return result } return "" } +var noTranslate = map[string]uint8{"JOIN":1, "INVITE":1, "NOTICE":1, "CAP":1, "MODE":1,"QUIT":1, "PRIVMSG":1} + // IRCCodes is just a map form of our constants for quick lookup purposes. var IRCCodes = map[string]string { "001":"RPL_WELCOME", @@ -36,6 +41,7 @@ var IRCCodes = map[string]string { "323":"RPL_LISTEND", "325":"RPL_UNIQOPIS", "324":"RPL_CHANNELMODEIS", + "329":"RPL_CREATIONTIME", "331":"RPL_NOTOPIC", "332":"RPL_TOPIC", "341":"RPL_INVITING", diff --git a/constants.go b/constants.go index a190ef2..8526be5 100644 --- a/constants.go +++ b/constants.go @@ -152,6 +152,7 @@ const ( RPL_LISTEND = "323" RPL_UNIQOPIS = "325" RPL_CHANNELMODEIS = "324" + RPL_CREATIONTIME = "329" RPL_NOTOPIC = "331" RPL_TOPIC = "332" RPL_INVITING = "341" diff --git a/state.go b/state.go index 89a2617..8d8a8e3 100644 --- a/state.go +++ b/state.go @@ -225,7 +225,10 @@ type Channel struct { Name string `json:"name"` // Topic of the channel. Topic string `json:"topic"` - + // Created is the time/date the channel was created (if available). + // Created time.Time `json:"created"` + // TODO: Figure out if these are all unix timestamps, if so, convert it to time.Time + Created string `json:"created"` // UserList is a sorted list of all users we are currently tracking within // the channel. Each is the nickname, and is rfc1459 compliant. UserList []string `json:"user_list"` diff --git a/util.go b/util.go index 26056cc..14ca8db 100644 --- a/util.go +++ b/util.go @@ -7,5 +7,5 @@ import ( func randSleep() { rand.Seed(time.Now().UnixNano()) - time.Sleep(time.Duration(rand.Intn(25)) * time.Millisecond) + time.Sleep(time.Duration(rand.Intn(5)) * time.Millisecond) }