all repos — paprika @ 8f9d436522849283119c92ce4ef8a66c944254c2

go rewrite of taigabot

plugins/plugins.go (view raw)

 1package plugins
 2
 3import (
 4	"errors"
 5	"strings"
 6
 7	"gopkg.in/irc.v3"
 8)
 9
10type Plugin interface {
11	Triggers() []string
12	Execute(m *irc.Message) (string, error)
13}
14
15var Plugins = make(map[string]Plugin)
16
17func Register(p Plugin) {
18	for _, t := range p.Triggers() {
19		Plugins[t] = p
20	}
21}
22
23// This Error is used to signal a NAK so other plugins
24// can attempt to parse the result
25// This is useful for broad prefix matching or future regexp
26// functions, other special errors may need defining
27// to determin priority or to "concatenate" output.
28var NoReply = errors.New("No Reply")
29// This error indicates we are sending a NOTICE instead of a PRIVMSG
30var IsNotice = errors.New("Is Notice")
31// This means the string(s) we are returning are raw IRC commands
32// that need to be written verbatim.
33var IsRaw = errors.New("Is Raw")
34
35// Due to racey nature of the handler in main.go being invoked as a goroutine,
36// it's hard to have race free way of building correct state of the IRC world.
37// This is a temporary (lol) hack to check if the PRIVMSG target looks like a
38// IRC nickname. We assume that any IRC nickname target would be us, unless
39// the IRCd we are on is neurotic
40//
41// Normally one would use the ISUPPORT details to learn what prefixes are used
42// on the network's valid channel names.
43//
44// E.G. ISUPPORT ... CHANTYPES=# ... where # would be the only valid channel name
45// allowed on the IRCd.
46func unlikelyDirectMessage(target string) bool {
47	if len(target) < 1 {
48		panic("Conformity Error, IRCd sent us a PRIVMSG with an empty target and message.")
49	}
50
51	sym := target[0] // we only care about the byte (ASCII)
52	// Is one of: !"#$%&'()*+,_./
53	// or one of: ;<=>?@
54	// If your IRCd uses symbols outside of this range,
55	// god help us.
56	//
57	// ALSO NOTE: RFC1459 defines a "nick" as
58	// <nick> ::= <letter> { <letter> | <number> | <special> }
59	// But I have seen some networks that allow special/number as the first letter.
60	return sym > 32 /* SPACE */ && sym < 48 /* 0 */ ||
61	       sym > 58 /* : */ && sym < 65 /* A */ ||
62	       sym == 126 /* ~ */
63}
64
65// Checks for triggers in a message and executes its
66// corresponding plugin, returning the response/error.
67func ProcessTrigger(m *irc.Message) (string, error) {
68	if !unlikelyDirectMessage(m.Params[0]) {
69		m.Params[0] = m.Name
70	}
71
72	for trigger, plugin := range Plugins {
73		if strings.HasPrefix(m.Trailing(), trigger) {
74			response, err := plugin.Execute(m)
75			if !errors.Is(err, NoReply) {
76				return response, err
77			}
78		}
79	}
80	return "", NoReply // No plugin matched, so we need to Ignore.
81}