-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirc.go
145 lines (126 loc) · 2.85 KB
/
irc.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
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
package main
import (
"crypto/tls"
"net"
"go.lindenii.runxiyu.org/lindenii-common/clog"
irc "go.lindenii.runxiyu.org/lindenii-irc"
)
var (
ircSendBuffered chan string
ircSendDirectChan chan errorBack[string]
)
type errorBack[T any] struct {
content T
errorBack chan error
}
func ircBotSession() error {
var err error
var underlyingConn net.Conn
if config.IRC.TLS {
underlyingConn, err = tls.Dial(config.IRC.Net, config.IRC.Addr, nil)
} else {
underlyingConn, err = net.Dial(config.IRC.Net, config.IRC.Addr)
}
if err != nil {
return err
}
defer underlyingConn.Close()
conn := irc.NewConn(underlyingConn)
logAndWriteLn := func(s string) (n int, err error) {
clog.Debug("IRC tx: " + s)
return conn.WriteString(s + "\r\n")
}
_, err = logAndWriteLn("NICK " + config.IRC.Nick)
if err != nil {
return err
}
_, err = logAndWriteLn("USER " + config.IRC.User + " 0 * :" + config.IRC.Gecos)
if err != nil {
return err
}
readLoopError := make(chan error)
writeLoopAbort := make(chan struct{})
go func() {
for {
select {
case <-writeLoopAbort:
return
default:
}
msg, line, err := conn.ReadMessage()
if err != nil {
readLoopError <- err
return
}
clog.Debug("IRC rx: " + line)
switch msg.Command {
case "001":
_, err = logAndWriteLn("JOIN #chat")
if err != nil {
readLoopError <- err
return
}
case "PING":
_, err = logAndWriteLn("PONG :" + msg.Args[0])
if err != nil {
readLoopError <- err
return
}
case "JOIN":
c, ok := msg.Source.(irc.Client)
if !ok {
clog.Error("IRC server told us a non-client is joining a channel...")
}
if c.Nick != config.IRC.Nick {
continue
}
default:
}
}
}()
for {
select {
case err = <-readLoopError:
return err
case line := <-ircSendBuffered:
_, err = logAndWriteLn(line)
if err != nil {
select {
case ircSendBuffered <- line:
default:
clog.Error("unable to requeue IRC message: " + line)
}
writeLoopAbort <- struct{}{}
return err
}
case lineErrorBack := <-ircSendDirectChan:
_, err = logAndWriteLn(lineErrorBack.content)
lineErrorBack.errorBack <- err
if err != nil {
writeLoopAbort <- struct{}{}
return err
}
}
}
}
// ircSendDirect sends an IRC message directly to the connection and bypasses
// the buffering system.
func ircSendDirect(s string) error {
ech := make(chan error, 1)
ircSendDirectChan <- errorBack[string]{
content: s,
errorBack: ech,
}
return <-ech
}
// TODO: Delay and warnings?
func ircBotLoop() {
ircSendBuffered = make(chan string, config.IRC.SendQ)
ircSendDirectChan = make(chan errorBack[string])
for {
err := ircBotSession()
clog.Error("IRC error: " + err.Error())
}
}