From c208a0e00cbe2821054a2be4314a9b12e85f2150 Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Sat, 15 Oct 2016 01:31:11 +0100 Subject: [PATCH] Add a prefix adapter to implement ! commands and ignoring help in public channels --- adapters/prefix_adapter.go | 67 ++++++++++++++++++++++++++++++++++++++ lysenko.go | 8 ++++- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 adapters/prefix_adapter.go diff --git a/adapters/prefix_adapter.go b/adapters/prefix_adapter.go new file mode 100644 index 0000000..b020cf7 --- /dev/null +++ b/adapters/prefix_adapter.go @@ -0,0 +1,67 @@ +package adapters + +import ( + "context" + "strings" + + "github.com/tcolgate/hugot" +) + +// Intercepts incoming messages. If they begin with a prefix, they are rewritten +// so they appear to be to the bot. For example, "!foo" would become "foo". The +// ToBot member of the Message struct is also set to true. +// +// The overall effect is to allow command handlers to be accessed via a !prefix +// +// You can also block the ubiquitous "help" handler outside of PMs by setting +// PrivateHelpOnly to true. This should really be a separate adapter, but +// overhead... +type PrefixAdapter struct { + hugot.Adapter + Prefix string + PrivateHelpOnly bool + c chan *hugot.Message +} + +func NewPrefixAdapter(around hugot.Adapter, prefix string) *PrefixAdapter { + return &PrefixAdapter{ + Adapter: around, + Prefix: prefix, + c: make(chan *hugot.Message), + } +} + +func (p *PrefixAdapter) Receive() <-chan *hugot.Message { + return p.c +} + +func (p *PrefixAdapter) Run(ctx context.Context) { + for { + select { + case m := <-p.Adapter.Receive(): + if m == nil { + return + } + + m = p.fixup(m) + if !p.isBlacklisted(m) { + p.c <- m + } + case <-ctx.Done(): + return + } + } +} + +func (p *PrefixAdapter) fixup(m *hugot.Message) *hugot.Message { + if !m.ToBot && strings.HasPrefix(m.Text, p.Prefix) { + m.Text = strings.Replace(m.Text, p.Prefix, "", 1) + m.ToBot = true + } + + return m +} + +func (p *PrefixAdapter) isBlacklisted(m *hugot.Message) bool { + return p.PrivateHelpOnly && m.ToBot && strings.HasPrefix(m.Text, "help") && !m.Private +} diff --git a/lysenko.go b/lysenko.go index af58af6..46c671f 100644 --- a/lysenko.go +++ b/lysenko.go @@ -14,6 +14,7 @@ import ( "github.com/tcolgate/hugot" "github.com/tcolgate/hugot/adapters/irc" + "ur.gs/lysenko/adapters" "ur.gs/lysenko/handlers/deadline" "ur.gs/lysenko/handlers/quotedb" ) @@ -53,6 +54,11 @@ func main() { } adapter := irc.New(config, channelList...) + prefix_adapter := adapters.NewPrefixAdapter(adapter, "!") + prefix_adapter.PrivateHelpOnly = true + + ctx := context.Background() + go prefix_adapter.Run(ctx) db, err := quotedb.New(*quotes) if err != nil { @@ -67,5 +73,5 @@ func main() { mux.HandleCommand(deadline.NewCommand("edb.RandQuoteHandler{QuoteDB: db})) mux.HandleCommand(deadline.NewCommand("edb.QuoteHandler{QuoteDB: db})) - hugot.ListenAndServe(context.Background(), mux, adapter) + hugot.ListenAndServe(ctx, mux, prefix_adapter) }