summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--settings.py9
-rw-r--r--wmd/actions/__init__.py2
-rw-r--r--wmd/actions/nickserv.py21
-rw-r--r--wmd/irc.py58
-rw-r--r--wmd/parser.py12
5 files changed, 65 insertions, 37 deletions
diff --git a/settings.py b/settings.py
index 947b5bb..6591f19 100644
--- a/settings.py
+++ b/settings.py
@@ -4,6 +4,9 @@
4# choose the nickname for you bot. 4# choose the nickname for you bot.
5NICKNAME = 'warmachine' 5NICKNAME = 'warmachine'
6 6
7# (optional) Only for servers that have nickserv.
8NICKSERV_PASSWORD =''
9
7SERVER = 'irc.freenode.org' 10SERVER = 'irc.freenode.org'
8PORT = 6667 11PORT = 6667
9IDENT = 'warmachine' 12IDENT = 'warmachine'
@@ -12,7 +15,11 @@ CHANNELS = ('#warmachine-dev',)
12 15
13ADMINS = ('com4',) 16ADMINS = ('com4',)
14 17
18ACTIONS = (
19 'wmd.actions.nickserv.IdentWithNickserv',
20)
21
15try: 22try:
16 from local_settings.py import * 23 from local_settings import *
17except ImportError: 24except ImportError:
18 pass \ No newline at end of file 25 pass \ No newline at end of file
diff --git a/wmd/actions/__init__.py b/wmd/actions/__init__.py
new file mode 100644
index 0000000..03cde4c
--- /dev/null
+++ b/wmd/actions/__init__.py
@@ -0,0 +1,2 @@
1class Action(object):
2 pass \ No newline at end of file
diff --git a/wmd/actions/nickserv.py b/wmd/actions/nickserv.py
new file mode 100644
index 0000000..fa91d5b
--- /dev/null
+++ b/wmd/actions/nickserv.py
@@ -0,0 +1,21 @@
1from wmd.actions import Action
2
3import settings
4
5class IdentWithNickserv(Action):
6 def __init__(self):
7 self.logged_in = False
8
9 def recv_msg(self, irc, obj_data):
10 username = obj_data.get_username()
11 msg = obj_data.params
12 if username and username.lower() == 'nickserv':
13
14 if 'identify' in msg.lower()\
15 and not self.logged_in:
16 msg = 'identify %s' % (settings.NICKSERV_PASSWORD)
17 print "%s: Logging in..." % self.__class__.__name__
18 irc.privmsg(username, msg)
19 self.logged_in = True
20 elif 'are now identified' in msg.lower():
21 print "%s: Logged in." % (self.__class__.__name__) \ No newline at end of file
diff --git a/wmd/irc.py b/wmd/irc.py
index 98e8245..558712d 100644
--- a/wmd/irc.py
+++ b/wmd/irc.py
@@ -1,7 +1,5 @@
1import socket 1import socket
2 2
3from actions.ActionMap import *
4from passiveactions.PassiveActionMap import *
5from wmd import parser 3from wmd import parser
6 4
7import settings 5import settings
@@ -16,12 +14,9 @@ class IRC(object):
16 self.nick = nick 14 self.nick = nick
17 self.name = name 15 self.name = name
18 self.port = port 16 self.port = port
17 self.actions = []
19 18
20 def locaActions(self): 19 self.load_actions()
21 """
22 Loads the actions internally.
23 """
24 pass
25 20
26 def connect(self): 21 def connect(self):
27 """ 22 """
@@ -37,7 +32,7 @@ class IRC(object):
37 """ 32 """
38 Joins a channel 33 Joins a channel
39 """ 34 """
40 self.send('JOIN ' + chan) 35 self.rawsend('JOIN ' + chan)
41 36
42 def log(self, string): 37 def log(self, string):
43 """ 38 """
@@ -45,12 +40,34 @@ class IRC(object):
45 """ 40 """
46 print 'log: ' + string 41 print 'log: ' + string
47 42
48 def send(self, command): 43 def privmsg(self, dest, msg):
44 """
45 send a privmsg to dest
46 """
47 self.rawsend('PRIVMSG %s :%s' % (dest, msg))
48
49 def rawsend(self, command):
49 """ 50 """
50 Sends commands straight to the irc server. 51 Sends commands straight to the irc server.
51 """ 52 """
52 self.irc.send(command + '\r\n') 53 self.irc.send(command + '\r\n')
53 54
55 def load_actions(self):
56 """
57 Loads the actions internally.
58 """
59 for action in settings.ACTIONS:
60 self.load_action(action)
61
62 def load_action(self, path):
63 """
64 Loads the provided action
65 """
66 module_name, class_name = path.rsplit('.', 1)
67 module = __import__(module_name, globals(), locals(), [class_name], -1)
68 classz = getattr(module, class_name)
69 self.actions.insert(0, classz())
70
54 def __call__(self, *args, **kwargs): 71 def __call__(self, *args, **kwargs):
55 """ 72 """
56 Main Event Loop that parses generic commands 73 Main Event Loop that parses generic commands
@@ -66,26 +83,13 @@ class IRC(object):
66 data = data + self.irc.recv(4096) 83 data = data + self.irc.recv(4096)
67 84
68 for line in data.split('\r\n'): 85 for line in data.split('\r\n'):
69 obj_data = parser.ircparse(line) 86 obj_data = parser.IRCParse(line)
70 #pass to action handlers here... 87 #pass to action handlers here...
71 if (obj_data.prefix == '') and (obj_data.command == '') and (obj_data.params == ''): 88 if (obj_data.prefix == '') and (obj_data.command == '') and (obj_data.params == ''):
72 continue 89 continue
73 90
74 print "!" + obj_data.prefix + "~" + obj_data.command + "~" + obj_data.params 91 print "!" + obj_data.prefix + "~" + obj_data.command + "~" + obj_data.params
75 try: 92 for action in self.actions:
76 for key in passiveactions.keys(): 93 retval = action.recv_msg(self, obj_data)
77 pa = passiveactions[key].getAction(obj_data, user) 94 if retval:
78 if pa: 95 self.rawsend(retval) \ No newline at end of file
79 self.send(pa)
80
81 if obj_data.params.find(self.nick + ':') > -1 or obj_data.params.find(':!') > -1:
82 curuser = obj_data.getUsername()
83 if curuser not in user:
84 continue
85 for key in actions.keys():
86 if obj_data.params.find(key) > -1:
87 self.send(actions[key].getAction(obj_data))
88
89 except Exception,e:
90 print "Action failed"
91 print e
diff --git a/wmd/parser.py b/wmd/parser.py
index cf88f75..24b965c 100644
--- a/wmd/parser.py
+++ b/wmd/parser.py
@@ -1,4 +1,4 @@
1class ircparse(object): 1class IRCParse(object):
2 #http://www.irchelp.org/irchelp/rfc/rfc.html 2 #http://www.irchelp.org/irchelp/rfc/rfc.html
3 3
4 def __init__(self, data): 4 def __init__(self, data):
@@ -11,7 +11,7 @@ class ircparse(object):
11 if data != '': 11 if data != '':
12 self._process_data(data) 12 self._process_data(data)
13 13
14 def getUsername(self): 14 def get_username(self):
15 # Usernames are from 0 to the !... extract it out of we can. 15 # Usernames are from 0 to the !... extract it out of we can.
16 if self.prefix.find('!') > -1: 16 if self.prefix.find('!') > -1:
17 return self.prefix[0:self.prefix.index('!')] 17 return self.prefix[0:self.prefix.index('!')]
@@ -44,10 +44,4 @@ class ircparse(object):
44 if len(data.split(' ')) > (start_at + 1): 44 if len(data.split(' ')) > (start_at + 1):
45 for param in data.split(' ')[(start_at+1):]: 45 for param in data.split(' ')[(start_at+1):]:
46 self.params += param + " " 46 self.params += param + " "
47 self.params.strip() 47 self.params.strip() \ No newline at end of file
48
49class configparse(object):
50 def __init__(self, filename):
51 self.filename = filename
52
53