Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 176 lines (150 sloc) 5.323 kB
df02500 @richo initial import
richo authored
1 """This module represents the core features a developer needs to write modules
2 for the pyBawt framework"""
3 import re
4 import time
5 import ourgit
6 import os
7 import sys
8 import logging
9
8661ae0 @richo Move all exceptions into lib (prelude to catching config)
richo authored
10 from lib import *
11
df02500 @richo initial import
richo authored
12 VERSION="$Rev: 1252 $".split(" ")[1]
13
14 def get_help(mdl):
15 try:
16 obj = globals()[mdl]
17 except KeyError:
18 return "No help for %s" % mdl
19 if hasattr(obj, '_commands'):
20 return ", ".join(obj._commands) + "\n" + str(obj.__doc__)
21 else:
22 return str(obj.__doc__)
23
24 # CORE
25 #-----
26
27 class BawtM2(object):
28 """I'm a lazy programmer who doesn't write help files"""
29
30 # Match everything by default
31 privmsg_re = "."
32 privmsg_flags = 0
33 topic_re = "."
34 topic_flags = 0
35 mode_re = "."
36 mode_flags = 0
37 join_re = "."
38 join_flags = 0
39 kick_re = "."
40 kick_flags = 0
41 part_re = "."
42 part_flags = 0
43 notice_re = "."
44 notice_flags = 0
45 # For !list
46 _name = "BawtM2"
47 # for is_action
48 action_matcher = re.compile("^\x01ACTION.*\x01$")
49 def __init__(self, parent, channel):
50 self.parent = parent
51 self.channel = channel
52 self.re_data = {'nick': self.parent.nick}# ADD more as neededh
53 self.rehash()
54
55 # Default authentication method is a match of the hostmask against the
56 # Message. I fully expect this to be overridden.
57
58 # Or against a static hash
59 # XXX Potentially allocates stale handles
60 self.auth = self.parent.authenticator.authed
61 self.revoke_auth = self.parent.authenticator.revoke_auth
62 self.on_load()
63
64 def __repr__(self):
65 return self._name
66 def __str__(self):
67 return self._name
68 def require(self, prop):
69 # TODO - Flesh these out
70 """XXX Unratified API"""
71 return self.__getattr__("_require_%s" % prop)()
72 def _require_op(self):
73 return self.channel.modes['op']
74 def rehash(self):
75 """Reconstruct matcher regex"""
76 self.matchers = { "JOIN": re.compile(self.join_re % self.re_data, self.join_flags),
77 "KICK": re.compile(self.kick_re % self.re_data, self.kick_flags),
78 "MODE": re.compile(self.mode_re % self.re_data, self.mode_flags),
79 "PRIVMSG": re.compile(self.privmsg_re % self.re_data, self.privmsg_flags),
80 "PART": re.compile(self.part_re % self.re_data, self.part_flags),
81 "TOPIC": re.compile(self.topic_re % self.re_data, self.topic_flags),
82 "NOTICE": re.compile(self.notice_re % self.re_data, self.notice_flags)
83 }
84
85 # API for adding stuff to this on the fly..
86 self.handlers = { "JOIN": self.handle_join,
87 "KICK": self.handle_kick,
88 "MODE": self.handle_mode,
89 "PRIVMSG": self.handle_privmsg,
90 "PART": self.handle_part,
91 "TOPIC": self.handle_topic,
92 "NOTICE": self.handle_notice
93 }
94 def want(self, msg):
95 try:
96 self.m = self.matchers[msg.event].search(msg.data_segment)
97 if self.m:
98 return True
99 else:
100 return False
101 except KeyError:
102 # We've been asked to handle something we're not aware of.. so we
103 # probably don't want it...
104 return False
105
106 # TODO Delegate to overridable from instance
107
108 def handle(self, msg):
109 # We contruct this list at runtime, which makes the objects mutable,
110 # whereas if it were constructed and stored, we would not be able to
111 # alter them on the fly
112 try:
113 self.handlers[msg.event](msg)
114 except KeyError:
115 logging.fixme("Module %s attached a handler to an event it couldn't handle" % (self._name))
116
117 def handle_privmsg(self, msg):
118 self.noop()
119
120 def handle_kick(self, msg):
121 self.noop()
122
123 def handle_join(self, msg):
124 self.noop()
125
126 def handle_mode(self, msg):
127 self.noop()
128
129 def handle_topic(self, msg):
130 self.noop()
131
132 def handle_part(self, msg):
133 self.noop()
134
135 def handle_notice(self, msg):
136 self.noop()
137
138 def noop(self, *args, **kwargs):
139 pass
140
141 def is_action(self, msg):
142 if self.action_matcher.match(msg.data_segment):
143 return True
144 else:
145 return False
146 def on_load(self, *args, **kwargs):
147 pass
148
149 def _exit():
150 #template
151 sys.exit()
152
153
154 class OurModules(object):
155 data = {'default': []}
156 nick = "dummynick"
157 def __setitem__(self, key, value):
158 if key not in self.data:
159 for i in self.data['default']:
160 value.append(i)
161 self.data[key] = value
162 def __getitem__(self, key):
163 if key in self.data:
164 return self.data[key]
165 else:
166 return self.data['default']
167 def dump(self):
168 dmp = []
169 for i in self.data:
170 dmp.append("%s:" % i)
171 dmp.append("[ %s ]" % (", ".join(repr(j(self, self)) for j in self.data[i])))
172 return dmp
173 def __repr__(self):
174 return "\n".join(dmp)
175
Something went wrong with that request. Please try again.