-
+ EF0185BD3B618AC6C82EECA7AEE17E7DFB5F1863330C9BF80BD68880A69BA1F285F4E51AC9E01438481F52541B8AD01C05F5371809925365BAF77ED2FECC5721
main.py
(0 . 0)(1 . 73)
143 #!/usr/bin/env python2
144 import select
145 import psycopg2
146 import psycopg2.extensions
147 from knobs import router
148 import postgres_interfacing
149
150 class main():
151 #LISTEN to postgres_channel for postgres-payload and parse for commands intended for bot, routing it to the proper submodule.
152 #For use with logbot-genesis or logbot-multiple-channels-corrected on a LOCAL postgres server
153
154 def __init__(self):
155 self.my_router = router.router()
156 self.my_interface = postgres_interfacing.postgres_interfacing()
157
158 def parse_message(self, message):
159 #parse irc message for valid command-calling syntax and return extracted command and arguments
160 bot_command_prefix = self.my_interface.bot_command_prefix
161 bot_command_prefix_len = len(bot_command_prefix)
162 if message[:bot_command_prefix_len] in bot_command_prefix:
163 command_start_pos = bot_command_prefix_len + (len(message[bot_command_prefix_len:]) - len(message[bot_command_prefix_len:].lstrip()))
164 if message[command_start_pos:].find(" ") > -1:
165 command_end_pos = command_start_pos + message[command_start_pos:].find(" ")
166 command = message[command_start_pos:command_end_pos]
167 command_arguments = message[command_end_pos:]
168 else:
169 command = message[command_start_pos:]
170 command_arguments = ""
171 return command, command_arguments
172
173 def send_to_command_router(self, target, source, command, command_arguments):
174 #do stuff with valid commands
175 command_function = "cmd_%s" % command
176 try:
177 route_command = getattr(self.my_router, command_function)(target, source, command_arguments)
178 except AttributeError:
179 pass
180
181 def listen_for_notifications(self):
182 #Main Loop ===================
183 #Execute a LISTEN on the 'log_new_message' channel on local postgres server.
184 #If NOTIFY is recieved, feed the payload (which corresponds to an id in the 'log' table) into fetch_irc_message() to return the message.
185 #Parse message and handle as configured in knobs and commands
186 conn = psycopg2.connect(self.my_interface.connection_string)
187 conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
188 cur = conn.cursor()
189 cur.execute(self.my_interface.postgres_listen_string)
190 print "Waiting for notifications on postgres channel '%s'" % self.my_interface.postgres_channel
191 while 1:
192 if select.select([conn],[],[],5) == ([],[],[]):
193 print "Timeout"
194 else:
195 conn.poll()
196 while conn.notifies:
197 notify = conn.notifies.pop(0)
198 print "Got NOTIFY:", notify.pid, notify.channel, notify.payload
199 target, source, message = self.my_interface.fetch_irc_message(notify.payload)
200 print "Message: %s" % message
201 if source not in self.my_interface.ignore_list:
202 if self.parse_message(message):
203 command, command_arguments = self.parse_message(message)
204 print "Attempting to route command '%s' with arguments '%s'..." % (command, command_arguments)
205 self.send_to_command_router(target, source, command, command_arguments)
206 elif self.my_interface.route_all_notifications == True:
207 print "Routing non-command message: '%s'" % message
208 self.my_router.passive(target, source, message)
209 else:
210 print "Ignoring nick '%s'" % source
211
212 if __name__ == "__main__":
213 print "running"
214 my_connection = main()
215 my_connection.listen_for_notifications()