Add checks after shcmd() calls
authorMikael Berthe <mikael@lilotux.net>
Sat, 24 Apr 2010 10:45:00 +0200
changeset 39 237af42156a1
parent 38 a51ddbce247b
child 40 95ba0699f365
Add checks after shcmd() calls
mcbot/cmds/mcabber_bts.lua
mcbot/cmds/tvcal.lua
mcbot/cmds/xep.lua
mcbot/libs/shcmd.lua
--- a/mcbot/cmds/mcabber_bts.lua	Mon Apr 19 10:20:28 2010 +0200
+++ b/mcbot/cmds/mcabber_bts.lua	Sat Apr 24 10:45:00 2010 +0200
@@ -16,6 +16,7 @@
     issue.id = tostring(num)
     issue.url = btsurl..issue.id.."/"
     local data = shcmd("curl "..issue.url)
+    if not data then return nil end
     local title = data:match("#"..issue.id.."%s*-%s*(.*)%s*&mdash;")
     if not title then return nil end
     issue.title = title:gsub("&quot;", "\""):gsub("&amp;", "&")
--- a/mcbot/cmds/tvcal.lua	Mon Apr 19 10:20:28 2010 +0200
+++ b/mcbot/cmds/tvcal.lua	Sat Apr 24 10:45:00 2010 +0200
@@ -28,6 +28,10 @@
 local function parse_webpage (url)
     local contents = shcmd("curl "..url)
 
+    if not contents then
+        return nil, "Could not fetch calendar, please try again later!"
+    end
+
     local tabregex = '<td id="d_(%d+_%d+_%d%d%d%d)" class="t?o?day"%s*>%s*\n%s*'..
                      '<table>(.-)</table>'
 
--- a/mcbot/cmds/xep.lua	Mon Apr 19 10:20:28 2010 +0200
+++ b/mcbot/cmds/xep.lua	Sat Apr 24 10:45:00 2010 +0200
@@ -29,8 +29,10 @@
     if os.difftime(now, xeps_updated_at) < 43200 then return end
     -- Let's get some fresh data
     local xepdata = shcmd(xeplisturl)
-    xeps_updated_at = now
-    parse_xeps(xepdata)
+    if xepdata then
+        xeps_updated_at = now
+        parse_xeps(xepdata)
+    end
 end
 
 -- Deeply inspired by MattJ's riddim bot
--- a/mcbot/libs/shcmd.lua	Mon Apr 19 10:20:28 2010 +0200
+++ b/mcbot/libs/shcmd.lua	Sat Apr 24 10:45:00 2010 +0200
@@ -6,6 +6,7 @@
     local fh = io.popen(fullcmd)
     result = fh:read("*a")  -- read cmd output
     fh:close()
+    if not result then return nil end
     -- Trim trailing newlines
     local r = result:gsub("\n+$", "")
     return r