diff options
Diffstat (limited to 'wmd')
| -rw-r--r-- | wmd/actions/__init__.py | 2 | ||||
| -rw-r--r-- | wmd/actions/nickserv.py | 21 | ||||
| -rw-r--r-- | wmd/irc.py | 58 | ||||
| -rw-r--r-- | wmd/parser.py | 12 |
4 files changed, 57 insertions, 36 deletions
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 @@ | |||
| 1 | class 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 @@ | |||
| 1 | from wmd.actions import Action | ||
| 2 | |||
| 3 | import settings | ||
| 4 | |||
| 5 | class 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 | ||
| @@ -1,7 +1,5 @@ | |||
| 1 | import socket | 1 | import socket |
| 2 | 2 | ||
| 3 | from actions.ActionMap import * | ||
| 4 | from passiveactions.PassiveActionMap import * | ||
| 5 | from wmd import parser | 3 | from wmd import parser |
| 6 | 4 | ||
| 7 | import settings | 5 | import 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 @@ | |||
| 1 | class ircparse(object): | 1 | class 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 | |||
| 49 | class configparse(object): | ||
| 50 | def __init__(self, filename): | ||
| 51 | self.filename = filename | ||
| 52 | |||
| 53 | |||