all repos — paprika @ 5d2368994a7887c962696739070e16398fc2e8ac

go rewrite of taigabot

plugins/version.go (view raw)

 1package plugins
 2
 3import (
 4	"fmt"
 5	"strings"
 6	"time"
 7
 8	"git.icyphox.sh/paprika/database"
 9	"github.com/dgraph-io/badger/v3"
10	"gopkg.in/irc.v3"
11)
12
13func init() {
14	Register(Version{})
15}
16
17type Version struct{}
18
19func (Version) Triggers() []string {
20	return []string{".v", ".version"}
21}
22
23func (Version) Execute(m *irc.Message) (string, error) {
24	params := strings.SplitN(m.Trailing(), " ", 3)
25	if len(params) != 2 {
26		return fmt.Sprintf("Usage: %s <nick>", params[0]), nil
27	}
28
29	nickTarget := params[1]
30	if likelyInvalidNick(nickTarget) {
31		return fmt.Sprintf("%s does not look like an IRC Nick", nickTarget), nil
32	}
33
34	nickKey := database.ToKey("version", nickTarget)
35	replyTarget := m.Params[0]
36	err := database.DB.SetWithTTL(
37		nickKey,
38		[]byte(replyTarget),
39		2 * time.Minute,
40	)
41	if err != nil {
42		return "", err
43	}
44
45	m.Params[0] = nickTarget
46	return "\x01VERSION\x01", nil
47}
48
49func CTCPReply(m *irc.Message) (string, error) {
50	msg := m.Trailing()
51	if !strings.HasPrefix(msg, "\x01VERSION") {
52		return "", NoReply
53	}
54
55	params := strings.SplitN(msg, " ", 2)
56	if len(params) != 2 {
57		return "", NoReply
58	}
59	if len(params[1]) == 0 {
60		return "", NoReply
61	}
62
63	ver := params[1][:len(params[1])-1]
64	from := m.Name
65
66	newTarget, err := database.DB.Delete(database.ToKey("version", from))
67	if err != nil {
68		return "", err
69	}
70	
71	m.Params[0] = string(newTarget)
72	m.Command = "PRIVMSG"
73	return fmt.Sprintf("%s VERSION: %s", from, ver), nil
74}
75
76func NoSuchUser(m *irc.Message) (string, error) {
77	invalidNick := m.Params[1]
78	newTarget, err := database.DB.Delete(database.ToKey("version", invalidNick))
79	if err == badger.ErrKeyNotFound {
80		return "", NoReply
81	} else if err != nil {
82		return "", err
83	}
84
85	m.Command = "PRIVMSG"
86	m.Params[0] = string(newTarget)
87	m.Params = m.Params[:1]
88	return fmt.Sprintf("No such nickname: %s", invalidNick), nil
89}