-
Notifications
You must be signed in to change notification settings - Fork 4
/
context.go
184 lines (152 loc) · 3.69 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"context"
"log"
"strings"
"github.com/lrstanley/girc"
)
type Message interface {
IsAddressed() bool
IsAdmin() bool
Reply(string)
Valid() bool
IsPrivate() bool
GetCommand() string
GetSource() string
GetArgs() []string
}
type Server interface {
Join(string) bool
Nick(string) bool
Action(string, string) bool
Mode(string, string, string) bool
Kick(string, string, string) bool
Topic(string, string) bool
Oper(string, string) bool
}
type System interface {
GetLLM() LLM
GetToolRegistry() *ToolRegistry
GetSessionStore() SessionStore
}
type ChatContextInterface interface {
context.Context
GetSession() Session
GetConfig() *Configuration
GetSystem() System
Message
Server
}
type ChatContext struct {
context.Context
Sys System
Session Session
Config *Configuration
client *girc.Client
event *girc.Event
args []string
}
var _ ChatContextInterface = (*ChatContext)(nil)
func NewChatContext(parentctx context.Context, config *Configuration, system System, ircclient *girc.Client, e *girc.Event) (ChatContextInterface, context.CancelFunc) {
timedctx, cancel := context.WithTimeout(parentctx, config.API.Timeout)
ctx := ChatContext{
Context: timedctx,
Config: config,
Sys: system,
client: ircclient,
event: e,
args: strings.Fields(e.Last()),
}
if ctx.IsAddressed() {
ctx.args = ctx.args[1:]
}
if e.Source == nil {
e.Source = &girc.Source{
Name: config.Server.Channel,
}
}
key := e.Params[0]
if !girc.IsValidChannel(key) {
key = e.Source.Name
}
ctx.Session = ctx.Sys.GetSessionStore().Get(key)
return ctx, cancel
}
func (c ChatContext) GetSystem() System {
return c.Sys
}
func (c ChatContext) GetConfig() *Configuration {
return c.Config
}
func (c ChatContext) Oper(channel, nick string) bool {
c.client.Cmd.Oper(channel, nick)
return true
}
func (c ChatContext) Mode(channel, target, mode string) bool {
c.client.Cmd.Mode(channel, target, mode)
return true
}
func (c ChatContext) Kick(channel, nick, reason string) bool {
c.client.Cmd.Kick(channel, nick, reason)
return true
}
func (c ChatContext) Topic(channel, topic string) bool {
c.client.Cmd.Topic(channel, topic)
return true
}
func (s ChatContext) IsAddressed() bool {
return strings.HasPrefix(s.event.Last(), s.client.GetNick())
}
func (c ChatContext) Nick(nickname string) bool {
c.client.Cmd.Nick(nickname)
return true
}
func (c ChatContext) Join(channel string) bool {
c.client.Cmd.Join(channel)
return true
}
func (c ChatContext) GetArgs() []string {
return c.args
}
func (c ChatContext) GetSession() Session {
return c.Session
}
func (c ChatContext) GetClient() *girc.Client {
return c.client
}
func (c ChatContext) GetSource() string {
return c.event.Source.Name
}
func (c ChatContext) IsAdmin() bool {
hostmask := c.event.Source.String()
log.Println("checking hostmask:", hostmask)
// XXX: if no admins are configured, all hostmasks are admins
if len(c.Config.Bot.Admins) == 0 {
log.Println("all hostmasks are admin, please configure admins")
return true
}
for _, user := range c.Config.Bot.Admins {
if user == hostmask {
log.Println(hostmask, "is admin")
return true
}
}
return false
}
func (c ChatContext) Reply(message string) {
c.client.Cmd.Reply(*c.event, message)
}
func (c ChatContext) Action(target string, message string) bool {
c.client.Cmd.Action(target, message)
return true
}
// checks if the message is valid for processing
func (c ChatContext) Valid() bool {
return (c.IsAddressed() || !c.Config.Bot.Addressed || c.IsPrivate()) && len(c.args) > 0
}
func (c ChatContext) IsPrivate() bool {
return !strings.HasPrefix(c.event.Params[0], "#")
}
func (c ChatContext) GetCommand() string {
return strings.ToLower(c.args[0])
}