31
|
1 |
#! /usr/bin/env lua |
|
2 |
|
|
3 |
-- RFC index parser |
|
4 |
-- Mikael BERTHE, 2010-04-17 |
|
5 |
|
|
6 |
-- TXT index database, retrieved from |
|
7 |
-- <ftp://ftp.rfc-editor.org/in-notes/rfc-index.txt> |
|
8 |
local indexfname = "/home/mikael/.mcabber/lua/mcbot/rfc-index.txt" |
|
9 |
|
32
|
10 |
local urlbase = "http://tools.ietf.org/html/rfc" |
|
11 |
|
31
|
12 |
local rfc = { ["desc"] = "Search RFC database" } |
|
13 |
|
|
14 |
local rfcdb = {} |
|
15 |
|
|
16 |
local function parse_file (fname) |
|
17 |
local fh = io.open(fname) |
|
18 |
|
|
19 |
if not fh then |
|
20 |
print("Cannot open RFC database") |
|
21 |
return 255 |
|
22 |
end |
|
23 |
|
|
24 |
-- Skip header |
|
25 |
local count = 0 |
|
26 |
for line in fh:lines() do |
|
27 |
count = count + 1 |
|
28 |
if line:match("^%s+RFC INDEX%s*$") and count > 30 then |
|
29 |
break |
|
30 |
end |
|
31 |
end |
|
32 |
|
|
33 |
-- Skip 2 more lines |
|
34 |
fh:read("*l") |
|
35 |
fh:read("*l") |
|
36 |
|
|
37 |
local contents = fh:read("*a") |
|
38 |
fh:close() |
|
39 |
|
|
40 |
for entry in contents:gfind("(%d%d%d%d .-)\n\n") do |
|
41 |
entry = entry:gsub("\n ", " ") |
|
42 |
local n = entry:match("^(%d%d%d%d) ") |
|
43 |
entry = entry:gsub("^(%d%d%d%d) ", "") |
|
44 |
|
|
45 |
local data = {} |
|
46 |
data.title = entry:match("^(.-)%. ") |
|
47 |
entry = entry:gsub("^(.-)%. ", "") |
|
48 |
|
|
49 |
data.date = entry:match("%. (.-)%. %(Format:") |
|
50 |
data.obsoletes = entry:match("%(Obsoletes (.-)%)") |
|
51 |
data.obsoleted_by = entry:match("%(Obsoleted by (.-)%)") |
|
52 |
data.updates = entry:match("%(Updates (.-)%)") |
|
53 |
data.updated_by = entry:match("%(Updated by (.-)%)") |
|
54 |
data.also = entry:match("%(Also (.-)%)") |
|
55 |
data.status = entry:match("%(Status: (.-)%)") |
|
56 |
|
|
57 |
if data.title then |
|
58 |
rfcdb[n] = data |
|
59 |
end |
|
60 |
end |
|
61 |
end |
|
62 |
|
|
63 |
local function rfc_lookup_string (rfcstring) |
|
64 |
rfcstring = rfcstring:lower() |
|
65 |
local r = "" |
|
66 |
for id, data in pairs(rfcdb) do |
|
67 |
if data.title:lower():find(rfcstring) then |
|
68 |
r = r.."RFC"..id..": "..data.title.."\n" |
|
69 |
end |
|
70 |
end |
|
71 |
r = r:gsub("\n$", "") |
|
72 |
if r ~= "" then return r; end |
|
73 |
return nil, "Sorry, I couldn't find any relevant RFC" |
|
74 |
end |
|
75 |
|
|
76 |
local function rfc_lookup_number (rfcnum) |
|
77 |
rfcnum = tostring(rfcnum) |
|
78 |
rfcnum = string.rep("0", 4 - rfcnum:len()) .. rfcnum |
|
79 |
|
|
80 |
local data = rfcdb[rfcnum] |
|
81 |
if not data then return nil, "Sorry, RFC not found" end |
|
82 |
|
|
83 |
local r = "RFC"..rfcnum .. " is " .. data.title .. "\n" |
|
84 |
if data.status then |
|
85 |
r = r .. "Status:\t" .. data.status:lower() .. "\n" |
|
86 |
end |
|
87 |
if data.obsoletes then |
|
88 |
r = r .. "Obsoletes:\t" .. data.obsoletes .. "\n" |
|
89 |
end |
|
90 |
if data.obsoleted_by then |
|
91 |
r = r .. "Obsoleted by:\t" .. data.obsoleted_by .. "\n" |
|
92 |
end |
|
93 |
if data.updates then |
|
94 |
r = r .. "Updates:\t" .. data.updates .. "\n" |
|
95 |
end |
|
96 |
if data.updated_by then |
|
97 |
r = r .. "Updated by:\t" .. data.updated_by .. "\n" |
|
98 |
end |
|
99 |
if data.also then |
|
100 |
r = r .. "Also:\t" .. data.also .. "\n" |
|
101 |
end |
32
|
102 |
r = r .. "URL:\t" .. urlbase..rfcnum |
31
|
103 |
return r |
|
104 |
end |
|
105 |
|
|
106 |
function rfc.cmd (rfcnum) |
|
107 |
local empty = true |
|
108 |
for i,v in pairs(rfcdb) do empty = false; break end |
|
109 |
if empty then parse_file(indexfname) end |
|
110 |
|
|
111 |
if (rfcnum) then |
|
112 |
rfcnum = rfcnum:gsub("^RFC[%s%-]*", "") |
|
113 |
rfcnum = rfcnum:gsub("^rfc[%s%-]*", "") |
|
114 |
rfcnum = rfcnum:gsub("[!%?%s:]+$", "") |
|
115 |
end |
|
116 |
|
|
117 |
if not rfcnum or rfcnum == "" then |
|
118 |
return nil, "What RFC do you want?" |
|
119 |
end |
|
120 |
|
|
121 |
local n = tonumber(rfcnum) |
|
122 |
if not n then return rfc_lookup_string(rfcnum) end |
|
123 |
|
|
124 |
return rfc_lookup_number(n) |
|
125 |
end |
|
126 |
|
|
127 |
mcbot_register_command("rfc", rfc) |