Browse Source

[Contribs] Add simple IRC bot for looking up error codes.

tags/v0.9.4
Stefan Hajnoczi 16 years ago
parent
commit
f43a7e349a
3 changed files with 126 additions and 2 deletions
  1. 3
    0
      contrib/errcode/README
  2. 2
    2
      contrib/errcode/errcode.py
  3. 121
    0
      contrib/errcode/gpxebot.py

+ 3
- 0
contrib/errcode/README View File

@@ -20,6 +20,9 @@ A PHP script is provided as a web interface.  First edit errcode.php to point
20 20
 $ERRCODE_PATH to the errcode.py script.  Then move errcode.php to a location
21 21
 visible from your web server.
22 22
 
23
+[OPTIONAL]
24
+A simple IRC bot is provided.  Edit gpxebot.py to fill in the IRC details.
25
+
23 26
 Usage
24 27
 -----
25 28
 Looking up error codes on the command-line:

+ 2
- 2
contrib/errcode/errcode.py View File

@@ -40,7 +40,7 @@ def lookup_errno_component(defines, component):
40 40
     else:
41 41
         return '0x%x' % component
42 42
 
43
-class Errno(object):
43
+class Errcode(object):
44 44
     def __init__(self, errno):
45 45
         self.pxenv_status = to_pxenv_status(errno)
46 46
         self.uniq = to_uniq(errno)
@@ -74,5 +74,5 @@ if __name__ == '__main__':
74 74
     except ValueError:
75 75
         usage()
76 76
 
77
-    print Errno(errno)
77
+    print Errcode(errno)
78 78
     sys.exit(0)

+ 121
- 0
contrib/errcode/gpxebot.py View File

@@ -0,0 +1,121 @@
1
+#!/usr/bin/env python
2
+# Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
3
+#
4
+# This program is free software; you can redistribute it and/or
5
+# modify it under the terms of the GNU General Public License as
6
+# published by the Free Software Foundation; either version 2 of the
7
+# License, or any later version.
8
+#
9
+# This program is distributed in the hope that it will be useful, but
10
+# WITHOUT ANY WARRANTY; without even the implied warranty of
11
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+# General Public License for more details.
13
+#
14
+# You should have received a copy of the GNU General Public License
15
+# along with this program; if not, write to the Free Software
16
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+import re
18
+import socket
19
+import errcode
20
+
21
+HOST = 'irc.freenode.net'
22
+PORT = 6667
23
+NICK = 'gpxebot'
24
+CHAN = '#etherboot'
25
+NICKSERV_PASSWORD = None
26
+IDENT = 'gpxebot'
27
+REALNAME = 'gPXE bot'
28
+
29
+ERRCODE_RE = re.compile(r'(errcode|Error)\s+((0x)?[0-9a-fA-F]{8})')
30
+
31
+NO_ARGS = -1
32
+
33
+handlers = {}
34
+
35
+def nick_from_mask(mask):
36
+    return (mask.find('!') > -1 and mask.split('!', 1)[0]) or mask
37
+
38
+def autojoin():
39
+    del handlers['376']
40
+    if NICKSERV_PASSWORD:
41
+        pmsg('nickserv', 'identify %s' % NICKSERV_PASSWORD)
42
+    if CHAN:
43
+        cmd('JOIN %s' % CHAN)
44
+
45
+def ping(_, arg):
46
+    cmd('PONG %s' % arg)
47
+
48
+def privmsg(_, target, msg):
49
+    if target == CHAN:
50
+        replyto = target
51
+        if msg.find(NICK) == -1:
52
+            return
53
+    elif target == NICK:
54
+        replyto = nick_from_mask(who)
55
+    m = ERRCODE_RE.search(msg)
56
+    if m:
57
+        try:
58
+            pmsg(replyto, str(errcode.Errcode(int(m.groups()[1], 16))))
59
+        except ValueError:
60
+            pass
61
+    if msg.find('help') > -1:
62
+        pmsg(replyto, 'I look up gPXE error codes.  Message me like this:')
63
+        pmsg(replyto, 'errcode 0x12345678  OR  Error 0x12345678')
64
+
65
+def add_handler(command, handler, nargs):
66
+    handlers[command] = (handler, nargs)
67
+
68
+def cmd(msg):
69
+    sock.sendall('%s\r\n' % msg)
70
+
71
+def pmsg(target, msg):
72
+    cmd('PRIVMSG %s :%s' % (target, msg))
73
+
74
+def dispatch(args):
75
+    command = args[0]
76
+    if command in handlers:
77
+        h = handlers[command]
78
+        if h[1] == NO_ARGS:
79
+            h[0]()
80
+        elif len(args) == h[1]:
81
+            h[0](*args)
82
+
83
+def parse(line):
84
+    if line[0] == ':':
85
+        who, line = line.split(None, 1)
86
+        who = who[1:]
87
+    else:
88
+        who = None
89
+    args = []
90
+    while line and line[0] != ':' and line.find(' ') != -1:
91
+        arg, line = line.split(None, 1)
92
+        args.append(arg)
93
+    if line:
94
+        if line[0] == ':':
95
+            args.append(line[1:])
96
+        else:
97
+            args.append(line)
98
+    return who, args
99
+
100
+add_handler('376', autojoin, NO_ARGS)
101
+add_handler('PING', ping, 2)
102
+add_handler('PRIVMSG', privmsg, 3)
103
+
104
+sock = socket.socket()
105
+sock.connect((HOST, PORT))
106
+cmd('NICK %s' % NICK)
107
+cmd('USER %s none none :%s' % (IDENT, REALNAME))
108
+
109
+rbuf = ''
110
+while True:
111
+    r = sock.recv(4096)
112
+    if not r:
113
+        break
114
+    rbuf += r
115
+
116
+    while rbuf.find('\r\n') != -1:
117
+        line, rbuf = rbuf.split('\r\n', 1)
118
+        if not line:
119
+            continue
120
+        who, args = parse(line)
121
+        dispatch(args)

Loading…
Cancel
Save