marvin

A simple and modular IRC bot

git clone https://git.8pit.net/marvin.git

 1// This program is free software: you can redistribute it and/or modify
 2// it under the terms of the GNU Affero General Public License as
 3// published by the Free Software Foundation, either version 3 of the
 4// License, or (at your option) any later version.
 5//
 6// This program is distributed in the hope that it will be useful, but
 7// WITHOUT ANY WARRANTY; without even the implied warranty of
 8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 9// Affero General Public License for more details.
10//
11// You should have received a copy of the GNU Affero General Public
12// License along with this program. If not, see <http://www.gnu.org/licenses/>.
13
14package rejoin
15
16import (
17	"github.com/nmeum/marvin/irc"
18	"github.com/nmeum/marvin/modules"
19	"strings"
20	"time"
21)
22
23type Module struct {
24	Timeout string `json:"timeout"`
25}
26
27func Init(moduleSet *modules.ModuleSet) {
28	moduleSet.Register(new(Module))
29}
30
31func (m *Module) Name() string {
32	return "rejoin"
33}
34
35func (m *Module) Help() string {
36	return "Enables automatic reconnection on kick."
37}
38
39func (m *Module) Defaults() {
40	m.Timeout = "0m3s"
41}
42
43func (m *Module) Load(client *irc.Client) error {
44	duration, err := time.ParseDuration(m.Timeout)
45	if err != nil {
46		return err
47	}
48
49	client.CmdHook("kick", func(c *irc.Client, msg irc.Message) error {
50		if msg.Data != client.Nickname {
51			return nil
52		}
53
54		time.Sleep(duration)
55		return c.Write("JOIN %s", strings.Fields(msg.Receiver)[0])
56	})
57
58	return nil
59}