Tue, 18 Aug 2015 15:53:48 +0200
plugins.issues: Add plugin for issue-tracker
112
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 | local http = require("net.http"); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 | local json = require"util.json"; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 | local bare_jid = require "util.jid".bare; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 | local current_url = "http://xkcd.com/info.0.json"; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 | local numbered_url = "http://xkcd.com/%d/info.0.json"; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 | local formatted_url = "http://xkcd.com/%d/"; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 | local strips = {}; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 | local latest, last_update; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 | function riddim.plugins.xkcd2(bot) |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 | bot:hook("commands/xkcd", function(command) |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 | local url = current_url; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 | local q = command.param; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 | local strip; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 | if q then |
122
f3a458479f9c
plugins.xkcd2: Allow negative numbers in pattern
Kim Alvefur <zash@zash.se>
parents:
115
diff
changeset
|
20 | local t, num = q:match("^([#\"]?)(%-?%d+)\"?$"); |
112
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 | if t ~= '"' then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 | num = tonumber(num); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 | if num and num < 0 then |
123
ce671935f504
plugins.xkcd2: Use table.maxn as there may be holes in the list
Kim Alvefur <zash@zash.se>
parents:
122
diff
changeset
|
24 | num = table.maxn(strips) + num; |
112
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 | strip = strips[num or q:lower()]; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 | if strip == "" or strip == 404 then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 | strip = nil |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 | if not strip and num then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 | url = numbered_url:format(num) or url; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 | elseif os.difftime(os.time(), last_update) <= 3 * 60 * 60 then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 | strip = latest; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 | if q and not strip then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 | local pat = q:lower():gsub("[-()%[]", "%%%0") |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 | :gsub("%%(%b[])",function(s) return (#s > 2 and "" or "%") .. s end); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 | local results = {}; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 | for i, strip in ipairs(strips) do |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 | if strip:lower():match(pat) then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 | results[#results+1] = i; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 | if #results == 0 then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 | return "Sorry, I couldn't find a match"; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 | elseif #results == 1 then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 | strip = results[1]; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 | else |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 | -- We have more than one match |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 | local ret, title = "Multiple matches:"; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 | for i, num in ipairs(results) do |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 | title = strips[num]; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 | ret = string.format("%s %s (%d)%s", ret, title, num, ((i < #results) and ",") or ""); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 | if i > 5 then ret = ret .. " " .. (#results - 5) .. " more"; break; end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 | return ret; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 | if strip then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 | local t, n; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 | if type(strip) == "number" then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 | t, n = strips[strip], strip; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 | else |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 | t, n = strip, strips[strip:lower()]; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 | return ("%s, "..formatted_url.." "):format(t, n); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 | http.request(url, nil, function (data, code) |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 | if code == 200 then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 | data = json.decode(data); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 | if not data then return end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 | local n, t = tonumber(data.num), data.safe_title; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 | strips[n], strips[t:lower()] = t, n; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 | if not q then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 | latest = n; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 | last_update = os.time(); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 | command:reply(("%s, "..formatted_url.." "):format(t, n)); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 | elseif code == 404 then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 | command:reply("Strip not found"); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 | end); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 | return true; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 | end); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 | local admin = bot.config.admin; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 | bot:hook("commands/xkcdlist", function(command) |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 | local actor = bare_jid(command.sender.real_jid or command.sender.jid); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 | if actor ~= admin then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 | return "I shall not"; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 | local get_next; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 | local function handle_reply(data, code) |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 | if code == 200 then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 | data = json.decode(data); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 | if not data then return end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 | local n, t = tonumber(data.num), data.safe_title; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 | strips[n], strips[t:lower()] = t, n; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 | if n > 1 then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 | return get_next(n - 1); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 | function get_next(i) |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 | if strips[i] then return end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 | local url = i and numbered_url:format(i) or current_url; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 | http.request(url, nil, handle_reply); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 | get_next(tonumber(command.param)); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 | return true; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 | end); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 | local function do_load() |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 | local ok, loaded_strips = pcall(dofile,"xkcd-list.lua"); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 | if ok then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 | strips = loaded_strips; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 | return true; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 | else |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 | return nil, loaded_strips; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 | bot:hook("commands/xkcdsave", function(command) |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 | local actor = bare_jid(command.sender.real_jid or command.sender.jid); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 | if actor ~= admin then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 | return "I shall not"; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 | local dump = "return "..require"util.serialization".serialize(strips); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 | local f, err = io.open("xkcd-list.lua~", "w"); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 | if f then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 | f:write(dump); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 | f:close(); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 | os.rename("xkcd-list.lua~", "xkcd-list.lua"); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 | return "Saved list of strips"; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 | return err; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 | end); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
152 | bot:hook("commands/xkcdload", function(command) |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 | local actor = bare_jid(command.sender.real_jid or command.sender.jid); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 | if actor ~= admin then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 | return "I shall not"; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 | local ok, err = do_load() |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 | if not ok then return err end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 | return "List of strips loaded" |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 | end); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 | bot:hook("commands/xkcdreset", function(command) |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 | local actor = bare_jid(command.sender.real_jid or command.sender.jid); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 | if actor ~= admin then |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 | return "I shall not"; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 | end |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 | |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 | strips = {}; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 | return "List of strips emptied"; |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 | end); |
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 | |
124
4530954f545a
plugins.xkcd2: Attempt to load list of strips at startup
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
175 | do_load(); |
112
08670c0cbb65
plugins.xkcd2: I accidentally another XKCD plugin from scratch
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 | end |