log plugin
[arenopage.git] / arenopage.py
blobf9edfa19608b60dd377b4c8de9f69332e8c52519
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 import socket, re
5 class arenopage(object):
6 def __init__(self, config):
7 try:
8 self.folders, self.connection_data, self.plugins_on_load = config.folders, config.connection_data, config.plugins_auto_load
9 except:
10 print "You have error in config file!"
12 def connect(self):
13 self.irc = socket.socket()
14 connect = self.irc.connect((self.connection_data['host'], self.connection_data['port']))
15 self.query("NICK %s" % self.connection_data['nick'])
16 self.query("USER %s host server : %s" % (self.connection_data['ident'], self.connection_data['name']))
17 if self.connection_data['password']:
18 self.query("PRIVMSG NickServ :IDENTIFY %s" % self.connection_data['password'])
19 for channel in self.connection_data['channels']:
20 self.query("JOIN %s" % channel)
21 while True:
22 self.do_action(self.irc.recv(500).split('\r\n')[0])
23 self.load_plugin()
25 def query(self, query):
26 self.irc.send(query + '\r\n')
28 def do_action(self, data):
29 """
30 Parse data, autoload plugins.
31 """
32 try:
33 regexp = re.compile(':(.*)!n=(.*) ([A-Z]+) :{0,1}(#{0,1}[A-Za-z0-9\-\_\.]*)\s*:{0,1}(.*)')
34 check = regexp.match(data)
35 self.data = {
36 'nick' : check.group(1),
37 'host' : check.group(2),
38 'action' : check.group(3),
39 'to' : check.group(4),
40 'msg' : check.group(5)
42 print self.data
43 for plugin in self.plugins_on_load:
44 self.load_plugin(plugin)
45 except:
46 pass
48 def load_plugin(self, plugin = None):
49 self.plugins = {}
50 try:
51 if not plugin:
52 check = re.compile("^!%s (.*)" % self.connection_data['nick']).match(self.data['msg']).group(1).split()
53 plugin_data = { 'name' : check[0], 'args' : check[1:] }
54 else:
55 plugin_data = { 'name' : plugin, 'args' : '' }
56 if not self.plugins.has_key(plugin_data['name']):
57 try:
58 imported = __import__(plugin_data['name'])
59 self.plugin = imported.plugin()
60 self.plugins[plugin_data['name']] = self.plugin
61 for plugin in self.plugins:
62 getattr(self.plugins[plugin], 'install')(self, plugin_data['args'])
63 except:
64 pass
65 except:
66 pass
68 def send_msg(self, msg, to = None):
69 if not to:
70 for channel in self.connection_data['channels']:
71 self.query("PRIVMSG %s :%s" % (channel, msg))
72 elif isinstance(to, list):
73 for receiver in to:
74 self.query("PRVMSG %s :%s" % (receiver, msg))
75 elif isinstance(to, str):
76 self.query("PRIVMSG %s :%s" % (to, msg))