author | Tero Marttila <terom@paivola.fi> |
Fri, 21 Mar 2008 02:02:52 +0200 | |
changeset 8 | 3d648daf9538 |
parent 6 | 614161f85d9b |
child 9 | 75cc996c6ba1 |
permissions | -rw-r--r-- |
1 | 1 |
from twisted.words.protocols import irc |
2 |
from twisted.internet import protocol |
|
4 | 3 |
from twisted.python import log |
6
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
4 |
import traceback |
1 | 5 |
|
6 |
import buffer |
|
7 |
||
4 | 8 |
HOSTNAME = "irc.marttila.de" |
1 | 9 |
PORT = 6667 |
10 |
NICKNAME = "FixBot" |
|
11 |
USERNAME = "fixme" |
|
4 | 12 |
CHANNEL = "#fixme-test" |
1 | 13 |
|
6
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
14 |
class ReplyException (Exception) : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
15 |
def __init__ (self, reply) : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
16 |
self.reply = reply |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
17 |
|
4 | 18 |
class BotProtocol (irc.IRCClient, object) : |
1 | 19 |
""" |
20 |
Fixme IRC bot |
|
21 |
""" |
|
4 | 22 |
|
23 |
def __init__ (self) : |
|
24 |
self.nickname = NICKNAME |
|
25 |
self.username = USERNAME |
|
1 | 26 |
|
27 |
# housekeeping |
|
28 |
def connectionMade (self) : |
|
29 |
log.msg("Connected") |
|
4 | 30 |
super(BotProtocol, self).connectionMade() |
6
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
31 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
32 |
self.nexus = self.factory.nexus |
1 | 33 |
|
34 |
def connectionLost (self, reason) : |
|
35 |
log.msg("Connection lost: %s" % reason) |
|
4 | 36 |
super(BotProtocol, self).connectionLost(reason) |
1 | 37 |
|
38 |
def signedOn (self) : |
|
4 | 39 |
log.msg("Signed on, joining channel %s" % CHANNEL) |
1 | 40 |
self.join(CHANNEL) |
41 |
||
42 |
def joined (self, channel) : |
|
43 |
log.msg("Joined channel %s" % channel) |
|
4 | 44 |
|
45 |
self.factory.connection = self |
|
1 | 46 |
|
47 |
# our actual functionality |
|
48 |
def send (self, msg) : |
|
4 | 49 |
msg = str(msg) |
50 |
||
51 |
if len(msg) > 480 : |
|
52 |
log.msg("truncating: %s" % msg) |
|
53 |
msg = msg[:480] + "..." |
|
54 |
||
55 |
msg = msg.replace("\n", "\\n").replace("\r", "\\r").replace("\0", "\\0") |
|
56 |
||
57 |
self.msg(CHANNEL, msg) |
|
1 | 58 |
|
59 |
def sendEvent (self, event) : |
|
4 | 60 |
self.send("[%s.%s] %s" % (event.module.name, event.type, event.msg)) |
1 | 61 |
|
62 |
def moduleConnected (self, module, addr) : |
|
4 | 63 |
self.send("{modules.%s} connected from %s:%d, version %s" % (module.name, addr.host, addr.port, module.version)) |
1 | 64 |
|
4 | 65 |
def moduleDisconnected (self, module, reason) : |
66 |
self.send("{modules.%s} disconnected: %s" % (module.name, reason)) |
|
6
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
67 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
68 |
class _noDefault : pass |
1 | 69 |
|
6
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
70 |
def _lookupCommand (self, command, default=_noDefault) : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
71 |
if '.' in command : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
72 |
raise ReplyException("No support for module commands yet :P") |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
73 |
else : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
74 |
method = getattr(self, "cmd_%s" % command, None) |
1 | 75 |
|
6
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
76 |
if method : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
77 |
return method |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
78 |
elif default is self._noDefault : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
79 |
raise ReplyException("No such command '%s'. See `help commands'" % command) |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
80 |
else : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
81 |
return default |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
82 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
83 |
def privmsg (self, user, channel, message) : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
84 |
if message.lower().startswith(self.nickname.lower()) : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
85 |
me, command = message.split(":", 1) |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
86 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
87 |
args = command.strip().split() |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
88 |
command = args.pop(0) |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
89 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
90 |
try : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
91 |
method = self._lookupCommand(command) |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
92 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
93 |
reply = method(*args) |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
94 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
95 |
if reply : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
96 |
self.send(reply) |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
97 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
98 |
except ReplyException, e : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
99 |
self.send(e.reply) |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
100 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
101 |
except Exception, e : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
102 |
self.send("Error: %s: %s" % (e.__class__.__name__, e)) |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
103 |
traceback.print_exc() |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
104 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
105 |
def cmd_help (self, cmd="help") : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
106 |
"""help <command|module> - Display help about the given command or module""" |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
107 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
108 |
method = self._lookupCommand(cmd, None) |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
109 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
110 |
if method : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
111 |
return method.__doc__ |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
112 |
else : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
113 |
try : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
114 |
module, addr = self.nexus.getModuleInfo(cmd) |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
115 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
116 |
return "%s is version %d from %s:%d. Events: %s. See `commands %s' for a list of commands" % (module.name, module.version, addr.host, addr.port, ', '.join(module.event_types), module.name) |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
117 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
118 |
except KeyError : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
119 |
raise ReplyException("No command/module called `%s'. See `help commands'" % cmd) |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
120 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
121 |
def cmd_commands (self, module=None) : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
122 |
"""commands [<module>] - Show primary commands, or commands in the given module (see `help modules')""" |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
123 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
124 |
if module : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
125 |
raise ReplyException("No support for module commands yet :P") |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
126 |
else : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
127 |
return "Commands: %s" % ', '.join( |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
128 |
attr_name.split('_', 1)[1] |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
129 |
for attr_name in BotProtocol.__dict__.iterkeys() |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
130 |
if attr_name.startswith("cmd_") |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
131 |
) |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
132 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
133 |
def cmd_modules (self) : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
134 |
"""modules - Show a list of connected modules""" |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
135 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
136 |
return "Modules: %s" % ', '.join( |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
137 |
module.name |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
138 |
for module in self.nexus.getModules() |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
139 |
) |