-
Notifications
You must be signed in to change notification settings - Fork 1
/
irc_bnetd.go
286 lines (236 loc) · 7.05 KB
/
irc_bnetd.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (c) 2020-2022, The OneBot Contributors. All rights reserved.
package main
import (
"bufio"
"fmt"
"net"
"strings"
"time"
"github.com/TheDiscordian/onebot/onelib"
)
const (
// NAME is same as filename, minus extension
NAME = "irc_bnetd"
// LONGNAME is what's presented to the user
LONGNAME = "IRC (bnetd)"
// VERSION of the script
VERSION = "v0.0.0"
)
var (
// Nick for the bot to use (keep in mind bnetd requires this nick to correlate to an existing account)
bnetNick string
// Bnet server like "bnet.thedisco.zone:6667"
bnetServer string
// Password (likely required)
bnetPass string
// Channels to automatically join (comma separated)
bnetAutoJoin string
bnetConn net.Conn
)
func loadConfig() {
bnetNick = onelib.GetTextConfig(NAME, "nick")
bnetServer = onelib.GetTextConfig(NAME, "server")
bnetPass = onelib.GetTextConfig(NAME, "pass")
bnetAutoJoin = onelib.GetTextConfig(NAME, "auto_join")
}
// Load connects to BnetProtocol, and sets up listeners. It's required for OneBot.
func Load() onelib.Protocol {
loadConfig()
bnetClient := &BnetProtocol{prefix: onelib.DefaultPrefix}
go handleConnection(bnetClient)
return onelib.Protocol(bnetClient)
}
func handleConnection(c *BnetProtocol) {
for {
var err error
bnetConn, err = net.Dial("tcp", bnetServer)
if err != nil {
onelib.Error.Println("[bnet]", err)
time.Sleep(time.Second * 30)
continue
}
bnetConn.Write([]byte(fmt.Sprintf("USER %s * * :%s\r\n", bnetNick, bnetNick)))
bnetConn.Write([]byte(fmt.Sprintf("NICK %s\r\n", bnetNick)))
r := bufio.NewReader(bnetConn)
for err == nil {
msgStr, err := r.ReadString('\n')
if err != nil {
break
}
msgStr = msgStr[:len(msgStr)-2]
splitMsg := strings.Split(msgStr, " ")
if len(splitMsg) < 2 {
continue
}
if splitMsg[0] == "PING" {
bnetConn.Write([]byte(fmt.Sprintf("PONG %s\r\n", splitMsg[1])))
} else if splitMsg[1] == "001" {
bnetConn.Write([]byte(fmt.Sprintf("PRIVMSG NICKSERV :identify %s\r\n", bnetPass)))
if len(bnetAutoJoin) > 0 {
joinSplit := strings.Split(bnetAutoJoin, ",")
for _, r := range joinSplit {
bnetConn.Write([]byte(fmt.Sprintf("JOIN %s\r\n", r)))
}
}
} else if splitMsg[1] == "PRIVMSG" {
splitMsg[3] = splitMsg[3][1:]
msg := &bnetMessage{text: strings.Join(splitMsg[3:], " ")}
splitMsg[0] = splitMsg[0][1:]
senderNick := strings.Split(splitMsg[0], "!")[0]
var loc *bnetLocation
if splitMsg[2] != bnetNick {
loc = &bnetLocation{displayName: splitMsg[2], uuid: onelib.UUID(splitMsg[2])}
} else {
loc = &bnetLocation{displayName: senderNick, uuid: onelib.UUID(senderNick)} // using NICK so responses get through correctly...
}
sender := &bnetSender{displayName: senderNick, location: loc, uuid: onelib.UUID(splitMsg[0])}
c.recv(msg, sender)
}
//onelib.Debug.Println("[bnet]", msgStr)
}
}
}
// BnetProtocol is the Protocol object used for handling anything BnetProtocol related.
type BnetProtocol struct {
/*
Store useful data here such as connected rooms, admins, nickname, accepted prefixes, etc
*/
prefix string
}
// Name returns the name of the plugin, usually the filename.
func (bp *BnetProtocol) Name() string {
return NAME
}
// LongName returns the display name of the plugin.
func (bp *BnetProtocol) LongName() string {
return LONGNAME
}
// Version returns the version of the plugin, usually in the format of "v0.0.0".
func (bp *BnetProtocol) Version() string {
return VERSION
}
// NewMessage should generate a message object from something
func (bp *BnetProtocol) NewMessage(raw []byte) onelib.Message {
return nil
}
// Send sends a Message object to a location specified by to (usually a location or sender UUID).
func (bp *BnetProtocol) Send(to onelib.UUID, msg onelib.Message) {
bnetSendText(to, msg.Text())
}
// SendText sends text to a location specified by to (usually a location or sender UUID).
func (bp *BnetProtocol) SendText(to onelib.UUID, text string) {
bnetSendText(to, text)
}
// SendFormattedText sends formatted text to a location specified by to (usually a location or sender UUID).
func (bp *BnetProtocol) SendFormattedText(to onelib.UUID, text, formattedText string) {
bnetSendText(to, text)
}
// recv should be called after you've recieved data and built a Message object
func (bp *BnetProtocol) recv(msg onelib.Message, sender onelib.Sender) {
onelib.ProcessMessage([]string{bp.prefix}, msg, sender)
}
// Remove should disconnect any open connections making it so the bot can forget about the protocol cleanly.
func (bp *BnetProtocol) Remove() {
/*
Unload code goes here (disconnects)
*/
}
type bnetMessage struct {
text string
}
func (bm *bnetMessage) Mentioned() bool {
return strings.Contains(bm.text, bnetNick)
}
func (bm *bnetMessage) UUID() onelib.UUID {
onelib.Debug.Printf("[%s] Message UUIDs not supported.\n", NAME)
return onelib.UUID("")
}
func (bm *bnetMessage) Reaction() *onelib.Emoji {
onelib.Debug.Printf("[%s] Reactions not supported.\n", NAME)
return nil
}
func (bm *bnetMessage) Text() string {
return bm.text
}
func (bm *bnetMessage) FormattedText() string {
return bm.text
}
func (bm *bnetMessage) StripPrefix(prefix string) onelib.Message {
if len(bm.text) > len(prefix) {
prefix = prefix + " "
}
return onelib.Message(&bnetMessage{text: strings.Replace(bm.text, prefix, "", 1)})
}
func (bm *bnetMessage) Raw() []byte {
return []byte(bm.text)
}
func bnetSendText(to onelib.UUID, text string) {
lines := strings.Split(text, "\n")
for _, msg := range lines {
_, err := bnetConn.Write([]byte(fmt.Sprintf("PRIVMSG %s :%s\r\n", string(to), msg)))
if err != nil {
onelib.Error.Println(err)
}
time.Sleep(200 * time.Millisecond)
}
}
type bnetSender struct {
displayName string
location *bnetLocation
uuid onelib.UUID
}
func (bs *bnetSender) Self() bool {
return bs.uuid == onelib.UUID(bnetNick)
}
func (bs *bnetSender) DisplayName() string {
return bs.displayName
}
func (bs *bnetSender) Username() string {
return bs.displayName
}
func (bs *bnetSender) UUID() onelib.UUID {
return bs.uuid
}
func (bs *bnetSender) Location() onelib.Location {
return bs.location
}
func (bs *bnetSender) Protocol() string {
return NAME
}
func (bs *bnetSender) Send(msg onelib.Message) {
bnetSendText(bs.uuid, msg.Text())
}
func (bs *bnetSender) SendText(text string) {
bnetSendText(bs.uuid, text)
}
func (bs *bnetSender) SendFormattedText(text, formattedText string) {
bnetSendText(bs.uuid, text)
}
type bnetLocation struct {
displayName string
uuid onelib.UUID
}
func (bl *bnetLocation) DisplayName() string {
return bl.displayName
}
func (bl *bnetLocation) Nickname() string {
return bl.displayName
}
func (bl *bnetLocation) Topic() string {
return ""
}
func (bl *bnetLocation) UUID() onelib.UUID {
return bl.uuid
}
func (bl *bnetLocation) Send(msg onelib.Message) {
bnetSendText(bl.uuid, msg.Text())
}
func (bl *bnetLocation) SendText(text string) {
bnetSendText(bl.uuid, text)
}
func (bl *bnetLocation) SendFormattedText(text, formattedText string) {
bnetSendText(bl.uuid, text)
}
func (bl *bnetLocation) Protocol() string {
return NAME
}