Update to freenode's new capability mechanism
[people/stefanha/gpxebot.git] / cmds.py
diff --git a/cmds.py b/cmds.py
index 0f77a96..82defdf 100644 (file)
--- a/cmds.py
+++ b/cmds.py
@@ -29,17 +29,16 @@ class Who(object):
 def readrc_command(who=None, target=None, replyto=None, words=None):
     '''Source bot command file (readrc)'''
     try:
-        f = open(config.RCFILE).readlines()
+        cmd = []
+        for line in open(config.RCFILE):
+            response = do_command(Who(config.NICK, True), config.NICK, config.NICK, line.split())
+            if isinstance(response, list):
+                cmd += response
+            elif response:
+                cmd.append(response)
+        return cmd
     except IOError:
         return 'PRIVMSG', (replyto, 'unable to open file')
-    cmd = []
-    for line in f:
-        response = do_command(Who(config.NICK, True), config.NICK, config.NICK, line.split())
-        if isinstance(response, list):
-            cmd += response
-        elif response:
-            cmd.append(response)
-    return cmd
 op_commands['readrc'] = readrc_command
 
 def alias_command(who, _, replyto, words):
@@ -79,7 +78,6 @@ ERRCODE_RE = re.compile(r'((?:0x)?[0-9a-fA-F]{8})')
 
 def errcode_command(who, _, replyto, words):
     '''Look up gPXE error code (errcode 0x12345678)'''
-    # errcode 0x12345678
     msg = ' '.join(words)
     m = ERRCODE_RE.search(msg)
     if m:
@@ -90,6 +88,45 @@ def errcode_command(who, _, replyto, words):
 commands['errcode'] = errcode_command
 commands['error'] = errcode_command
 
+def lspci_command(who, _, replyto, words):
+    '''Look up driver for PCI ID (lspci 10ec:8139)'''
+    ids = []
+    if len(words) == 2:
+        ids = words[1].split(':')
+    elif len(words) == 3:
+        ids = words[1:3]
+    if len(ids) != 2:
+        return 'PRIVMSG', (replyto, 'Invalid arguments')
+
+    try:
+        vendor_id, device_id = [int(s, 16) for s in ids]
+    except ValueError:
+        return 'PRIVMSG', (replyto, 'Invalid arguments')
+    pci_id = '%04x,%04x' % (vendor_id, device_id)
+
+    try:
+        for line in open(config.NICFILE):
+            line = line.rstrip('\r\n')
+            if not line or line.startswith('#'):
+                continue
+            fields = line.split('\t')
+
+            if fields[0] == 'family':
+                driver = fields[1].split('/')[-1]
+                continue
+
+            if fields[1] == pci_id:
+                return 'PRIVMSG', (replyto, driver)
+    except IndexError, e:
+        utils.dbg(str(e))
+    except IOError, e:
+        utils.dbg(str(e))
+    return 'PRIVMSG', (replyto, 'No driver found')
+if config.NICFILE:
+    commands['lspci'] = lspci_command
+else:
+    sys.stderr.write('running without lspci support (NICFILE not given)\n')
+
 def help_command(who, _, replyto, msg):
     '''List commands (help)'''
     def do_help(vocabulary):