Implement automatic channel joining

Avoid the need for an administrator to join the bot to channels by
implementing a configuration option allowing the passing of channels
the bot should always join to by itself upon startup.

Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
This commit is contained in:
Georg Pfuetzenreuter 2024-09-21 18:40:29 +02:00
parent 1f41d9ac17
commit 923efb3d0e
Signed by: Georg
GPG Key ID: 1ED2F138E7E6FF57
3 changed files with 17 additions and 1 deletions

View File

@ -13,6 +13,8 @@ watbot:
hosts: hosts:
- annoying.example.com - annoying.example.com
channels: # optional, no default channels: # optional, no default
join:
- crantest # channels without a prefix character will be prefixed with "#"
permitted: permitted:
- '#lucy' - '#lucy'

View File

@ -28,6 +28,7 @@ type watConfig struct {
Hosts []string `yaml:"hosts"` Hosts []string `yaml:"hosts"`
} `yaml:"admins"` } `yaml:"admins"`
Channels struct { Channels struct {
Join []string `yaml:"join"`
Permitted []string `yaml:"permitted"` Permitted []string `yaml:"permitted"`
} `yaml:"channels"` } `yaml:"channels"`
Ignores struct { Ignores struct {
@ -94,6 +95,7 @@ func main() {
Name: config.Name, Name: config.Name,
} }
watConfig := wat.WatConfig{ watConfig := wat.WatConfig{
AutoJoinChannels: config.Channels.Join,
PermittedChannels: config.Channels.Permitted, PermittedChannels: config.Channels.Permitted,
IgnoredHosts: config.Ignores.Hosts, IgnoredHosts: config.Ignores.Hosts,
AdminHosts: config.Admins.Hosts, AdminHosts: config.Admins.Hosts,

View File

@ -20,6 +20,7 @@ type WatBot struct {
type WatConfig struct { type WatConfig struct {
AdminHosts []string AdminHosts []string
IgnoredHosts []string IgnoredHosts []string
AutoJoinChannels []string
PermittedChannels []string PermittedChannels []string
} }
@ -36,12 +37,23 @@ func CleanNick(nick string) string {
return string(nick[0]) + "\u200c" + nick[1:] return string(nick[0]) + "\u200c" + nick[1:]
} }
func PrefixChannel(channel string) string {
if channel[0] != '#' {
channel = "#" + channel
}
return channel
}
func (w *WatBot) HandleIrcMsg(c *irc.Client, m *irc.Message) { func (w *WatBot) HandleIrcMsg(c *irc.Client, m *irc.Message) {
switch cmd := m.Command; cmd { switch cmd := m.Command; cmd {
case "PING": case "PING":
w.write("PONG", m.Params[0]) w.write("PONG", m.Params[0])
case "PRIVMSG": case "PRIVMSG":
w.Msg(m) w.Msg(m)
case "001":
for _, channel := range w.c.AutoJoinChannels {
w.write("JOIN", PrefixChannel(channel))
}
} }
} }
@ -68,7 +80,7 @@ func (w *WatBot) CanRespond(m *irc.Message) bool {
// if !strings.Contains(m.Prefix.Host, "") { // if !strings.Contains(m.Prefix.Host, "") {
// return false // return false
// } // }
if !w.Allowed(m.Params[0], w.c.PermittedChannels) { if !w.Allowed(PrefixChannel(m.Params[0]), w.c.PermittedChannels) {
return false return false
} }
return true return true