From 8485ed9fe30afd6f8740dc600713540a72eeace3 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Thu, 21 Apr 2016 11:19:38 -0400 Subject: [PATCH] Fix Whoing a channel and factoid timer --- bot/bot.go | 11 ++++---- bot/interfaces.go | 2 ++ bot/user/users.go | 17 +++++------- irc/irc.go | 4 +++ plugins/fact/factoid.go | 6 +++-- slack/slack.go | 59 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 81 insertions(+), 18 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index 040b381..051532d 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -178,13 +178,12 @@ func (b *bot) AddHandler(name string, h Handler) { } func (b *bot) Who(channel string) []user.User { - out := []user.User{} - for _, u := range b.users { - if u.Name != b.Config().Nick { - out = append(out, u) - } + names := b.conn.Who(channel) + users := []user.User{} + for _, n := range names { + users = append(users, user.New(n)) } - return out + return users } var rootIndex string = ` diff --git a/bot/interfaces.go b/bot/interfaces.go index 0f99231..43362f3 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -31,6 +31,8 @@ type Connector interface { SendMessage(channel, message string) SendAction(channel, message string) Serve() + + Who(string) []string } // Interface used for compatibility with the Plugin interface diff --git a/bot/user/users.go b/bot/user/users.go index 1bd0eaf..ddd9ac9 100644 --- a/bot/user/users.go +++ b/bot/user/users.go @@ -6,15 +6,12 @@ package user // session type User struct { // Current nickname known - Name string - - // LastSeen DateTime - - // Alternative nicknames seen - Alts []string - Parent string - + Name string Admin bool - - //bot *bot +} + +func New(name string) User { + return User{ + Name: name, + } } diff --git a/irc/irc.go b/irc/irc.go index 5bcf70e..62f1ed7 100644 --- a/irc/irc.go +++ b/irc/irc.go @@ -274,3 +274,7 @@ func (i *Irc) buildMessage(inMsg irc.Msg) msg.Message { return msg } + +func (i Irc) Who(channel string) []string { + return []string{} +} diff --git a/plugins/fact/factoid.go b/plugins/fact/factoid.go index bad0063..385dca2 100644 --- a/plugins/fact/factoid.go +++ b/plugins/fact/factoid.go @@ -609,13 +609,14 @@ func (p *Factoid) randomFact() *factoid { // factTimer spits out a fact at a given interval and with given probability func (p *Factoid) factTimer(channel string) { - duration := time.Duration(p.Bot.Config().Factoid.QuoteTime) * time.Minute + duration := time.Duration(p.Bot.Config().Factoid.QuoteTime) * time.Second myLastMsg := time.Now() for { - time.Sleep(time.Duration(5) * time.Second) + time.Sleep(time.Duration(5) * time.Second) // why 5? lastmsg, err := p.Bot.LastMessage(channel) if err != nil { + // Probably no previous message to time off of continue } @@ -627,6 +628,7 @@ func (p *Factoid) factTimer(channel string) { if success && tdelta > duration && earlier { fact := p.randomFact() if fact == nil { + log.Println("Didn't find a random fact to say") continue } diff --git a/slack/slack.go b/slack/slack.go index 7f6cc4f..10adcc1 100644 --- a/slack/slack.go +++ b/slack/slack.go @@ -11,8 +11,10 @@ import ( "log" "net/http" "net/url" + "strconv" "strings" "sync/atomic" + "time" "github.com/velour/catbase/bot" "github.com/velour/catbase/bot/msg" @@ -44,6 +46,25 @@ type slackUserInfoResp struct { } `json:"user"` } +type slackChannelInfoResp struct { + Ok bool `json:"ok"` + Channel struct { + Id string `json:"id"` + Name string `json:"name"` + + Created int64 `json:"created"` + Creator string `json:"creator"` + + Members []string `json:"members"` + + Topic struct { + Value string `json:"value"` + Creator string `json:"creator"` + LastSet int64 `json:"last_set"` + } `json:"topic"` + } `json:"channel"` +} + type slackMessage struct { Id uint64 `json:"id"` Type string `json:"type"` @@ -51,6 +72,7 @@ type slackMessage struct { Channel string `json:"channel"` Text string `json:"text"` User string `json:"user"` + Ts string `json:"ts"` Error struct { Code uint64 `json:"code"` Msg string `json:"msg"` @@ -150,6 +172,12 @@ func (s *Slack) buildMessage(m slackMessage) msg.Message { u := s.getUser(m.User) + // I think it's horseshit that I have to do this + ts := strings.Split(m.Ts, ".") + sec, _ := strconv.ParseInt(ts[0], 10, 64) + nsec, _ := strconv.ParseInt(ts[1], 10, 64) + tstamp := time.Unix(sec, nsec) + return msg.Message{ User: &user.User{ Name: u, @@ -160,6 +188,7 @@ func (s *Slack) buildMessage(m slackMessage) msg.Message { Command: isCmd, Action: isAction, Host: string(m.Id), + Time: tstamp, } } @@ -212,6 +241,7 @@ func (s *Slack) getUser(id string) string { resp.StatusCode, err) return "UNKNOWN" } + defer resp.Body.Close() var userInfo slackUserInfoResp err = json.NewDecoder(resp.Body).Decode(&userInfo) if err != nil { @@ -221,3 +251,32 @@ func (s *Slack) getUser(id string) string { s.users[id] = userInfo.User.Name return s.users[id] } + +// Who gets usernames out of a channel +func (s *Slack) Who(id string) []string { + log.Println("Who is queried for ", id) + u := s.url + "channels.info" + resp, err := http.PostForm(u, + url.Values{"token": {s.config.Slack.Token}, "channel": {id}}) + if err != nil || resp.StatusCode != 200 { + log.Printf("Error posting user info request: %d %s", + resp.StatusCode, err) + return []string{} + } + defer resp.Body.Close() + var chanInfo slackChannelInfoResp + err = json.NewDecoder(resp.Body).Decode(&chanInfo) + if err != nil || !chanInfo.Ok { + log.Println("Error decoding response: ", err) + return []string{} + } + + log.Printf("%#v", chanInfo.Channel) + + handles := []string{} + for _, member := range chanInfo.Channel.Members { + handles = append(handles, s.getUser(member)) + } + log.Printf("Returning %d handles", len(handles)) + return handles +}