summaryrefslogtreecommitdiff
path: root/autoexec.py
blob: 501ec2c40b3ff1783e2df5f82ad5de65092ab3f7 (plain)
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
#!/usr/bin/python
import re
import irc
import fnmatch


def AutoexecReload(old_ax):
    ax = Autoexec()
    for (context, conf) in old_ax.networks.items():
        context.rmAddon(old_ax)
        context.addAddon(ax, **conf.__dict__)
    return ax


class Autoexec(object):

    def __init__(self):
        self.networks = {}
        self._rejoinchannels = {}
            # Saved channels for when a connection is lost

    def onAddonAdd(self, context, label, onconnect=None, onregister=None, autojoin=None, usermodes=None, nsautojoin=None, nsmatch=None, wallet=None, opername=None, opermodes=None, snomasks=None, operexec=None, operjoin=None, autorejoin=True):
        labels = [v.label for v in self.networks.values()]
        if label in labels:
            raise BaseException, "Label already exists"
        if context in self.networks.keys():
            raise BaseException, "Network already exists"
        self.networks[context] = irc.Config(
            self, label=label, onconnect=onconnect, onregister=onregister, autojoin=irc.ChanList(
                autojoin, context=context),
            usermodes=usermodes, nsautojoin=irc.ChanList(nsautojoin, context=context), nsmatch=nsmatch, wallet=wallet,
            opername=opername, opermodes=opermodes, snomasks=snomasks, operexec=operexec, operjoin=irc.ChanList(operjoin, context=context), autorejoin=autorejoin)
        self._rejoinchannels[context] = None
        return self.networks[context]

    def onDisconnect(self, context, expected):
        conf = self.networks[context]
        if conf.autorejoin and not expected and context.identity:
            self._rejoinchannels[context] = irc.ChanList(
                context.identity.channels, context=context)  # Store a *copy* of the list of channels

    def onQuit(self, context, user, quitmsg):
        if user == context.identity and not context._quitexpected:
            # Bot received a QUIT message for itself, and was not expected.
            self.onDisconnect(context, False)

    def onAddonRem(self, context):
        del self.networks[context], self._rejoinchannels[context]

    def onConnect(self, context):
        conf = self.networks[context]
        if conf.onconnect:
            for line in conf.onconnect:
                context.raw(line, origin=self)

    def onRegistered(self, context):
        conf = self.networks[context]
        if conf.onregister:
            for line in conf.onregister:
                context.raw(line, origin=self)
        if conf.usermodes:
            context.raw("MODE %s %s" %
                        (context.identity.nick, conf.usermodes), origin=self)
        if conf.opername and conf.wallet and "%s/opers/%s" % (conf.label, conf.opername) in conf.wallet.keys():
            context.raw("OPER %s %s" %
                        (conf.opername, conf.wallet["%s/opers/%s" % (conf.label, conf.opername)]), origin=self)
        if conf.autojoin:
            conf.autojoin.join(origin=self)
        if conf.autorejoin and self._rejoinchannels[context]:
            rejoin = irc.ChanList([channel for channel in self._rejoinchannels[
                                  context] if channel not in conf.autojoin + conf.nsautojoin + conf.operjoin], context=context)
            if len(rejoin):
                rejoin.join(origin=self)
        self._rejoinchannels[context] = None

    def on381(self, context, line, origin, target, params, extinfo):
        conf = self.networks[context]
        if conf.operexec:
            for line in conf.operexec:
                context.raw(line, origin=self)
        if conf.opermodes:
            context.raw("MODE %s %s" %
                        (context.identity.nick, conf.opermodes), origin=self)
        if conf.snomasks:
            context.raw("MODE %s +s %s" %
                        (context.identity.nick, conf.snomasks), origin=self)
        if conf.operjoin:
            conf.operjoin.join(origin=self)

    def onPrivNotice(self, context, origin, msg):
        conf = self.networks[context]
        if type(origin) == irc.User and origin.nick.lower() == "nickserv":
            if re.match("This nickname is registered( and protected)?", msg) and (not conf.nsmatch or fnmatch.fnmatch("%s!%s@%s" % (origin.nick, origin.username, origin.host), conf.nsmatch)) and conf.wallet and "%s/NickServ/%s" % (conf.label, context.identity.nick.lower()) in conf.wallet.keys():
                origin.msg("identify %s" %
                           conf.wallet["%s/NickServ/%s" % (conf.label, context.identity.nick.lower())])
            if re.match("You are now identified", msg):
                if conf.nsautojoin:
                    conf.nsautojoin.join(origin=self)

    def on900(self, context, line, origin, target, params, extinfo):
        conf = self.networks[context]
        if conf.nsautojoin:
            conf.nsautojoin.join(origin=self)