author | Mikael Berthe <mikael@lilotux.net> |
Sun, 18 Apr 2010 21:47:05 +0200 | |
changeset 34 | ca01a1696cff |
parent 32 | 96bce644394a |
child 66 | d9c00a9fe9d5 |
permissions | -rw-r--r-- |
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 |
||
34
ca01a1696cff
Remove print() in rfc module
Mikael Berthe <mikael@lilotux.net>
parents:
32
diff
changeset
|
19 |
if not fh then return nil, "Cannot open RFC database" end |
31 | 20 |
|
21 |
-- Skip header |
|
22 |
local count = 0 |
|
23 |
for line in fh:lines() do |
|
24 |
count = count + 1 |
|
25 |
if line:match("^%s+RFC INDEX%s*$") and count > 30 then |
|
26 |
break |
|
27 |
end |
|
28 |
end |
|
29 |
||
30 |
-- Skip 2 more lines |
|
31 |
fh:read("*l") |
|
32 |
fh:read("*l") |
|
33 |
||
34 |
local contents = fh:read("*a") |
|
35 |
fh:close() |
|
36 |
||
37 |
for entry in contents:gfind("(%d%d%d%d .-)\n\n") do |
|
38 |
entry = entry:gsub("\n ", " ") |
|
39 |
local n = entry:match("^(%d%d%d%d) ") |
|
40 |
entry = entry:gsub("^(%d%d%d%d) ", "") |
|
41 |
||
42 |
local data = {} |
|
43 |
data.title = entry:match("^(.-)%. ") |
|
44 |
entry = entry:gsub("^(.-)%. ", "") |
|
45 |
||
46 |
data.date = entry:match("%. (.-)%. %(Format:") |
|
47 |
data.obsoletes = entry:match("%(Obsoletes (.-)%)") |
|
48 |
data.obsoleted_by = entry:match("%(Obsoleted by (.-)%)") |
|
49 |
data.updates = entry:match("%(Updates (.-)%)") |
|
50 |
data.updated_by = entry:match("%(Updated by (.-)%)") |
|
51 |
data.also = entry:match("%(Also (.-)%)") |
|
52 |
data.status = entry:match("%(Status: (.-)%)") |
|
53 |
||
54 |
if data.title then |
|
55 |
rfcdb[n] = data |
|
56 |
end |
|
57 |
end |
|
34
ca01a1696cff
Remove print() in rfc module
Mikael Berthe <mikael@lilotux.net>
parents:
32
diff
changeset
|
58 |
return true |
31 | 59 |
end |
60 |
||
61 |
local function rfc_lookup_string (rfcstring) |
|
62 |
rfcstring = rfcstring:lower() |
|
63 |
local r = "" |
|
64 |
for id, data in pairs(rfcdb) do |
|
65 |
if data.title:lower():find(rfcstring) then |
|
66 |
r = r.."RFC"..id..": "..data.title.."\n" |
|
67 |
end |
|
68 |
end |
|
69 |
r = r:gsub("\n$", "") |
|
70 |
if r ~= "" then return r; end |
|
71 |
return nil, "Sorry, I couldn't find any relevant RFC" |
|
72 |
end |
|
73 |
||
74 |
local function rfc_lookup_number (rfcnum) |
|
75 |
rfcnum = tostring(rfcnum) |
|
76 |
rfcnum = string.rep("0", 4 - rfcnum:len()) .. rfcnum |
|
77 |
||
78 |
local data = rfcdb[rfcnum] |
|
79 |
if not data then return nil, "Sorry, RFC not found" end |
|
80 |
||
81 |
local r = "RFC"..rfcnum .. " is " .. data.title .. "\n" |
|
82 |
if data.status then |
|
83 |
r = r .. "Status:\t" .. data.status:lower() .. "\n" |
|
84 |
end |
|
85 |
if data.obsoletes then |
|
86 |
r = r .. "Obsoletes:\t" .. data.obsoletes .. "\n" |
|
87 |
end |
|
88 |
if data.obsoleted_by then |
|
89 |
r = r .. "Obsoleted by:\t" .. data.obsoleted_by .. "\n" |
|
90 |
end |
|
91 |
if data.updates then |
|
92 |
r = r .. "Updates:\t" .. data.updates .. "\n" |
|
93 |
end |
|
94 |
if data.updated_by then |
|
95 |
r = r .. "Updated by:\t" .. data.updated_by .. "\n" |
|
96 |
end |
|
97 |
if data.also then |
|
98 |
r = r .. "Also:\t" .. data.also .. "\n" |
|
99 |
end |
|
32 | 100 |
r = r .. "URL:\t" .. urlbase..rfcnum |
31 | 101 |
return r |
102 |
end |
|
103 |
||
104 |
function rfc.cmd (rfcnum) |
|
105 |
local empty = true |
|
106 |
for i,v in pairs(rfcdb) do empty = false; break end |
|
34
ca01a1696cff
Remove print() in rfc module
Mikael Berthe <mikael@lilotux.net>
parents:
32
diff
changeset
|
107 |
if empty then |
ca01a1696cff
Remove print() in rfc module
Mikael Berthe <mikael@lilotux.net>
parents:
32
diff
changeset
|
108 |
local r, err = parse_file(indexfname) |
ca01a1696cff
Remove print() in rfc module
Mikael Berthe <mikael@lilotux.net>
parents:
32
diff
changeset
|
109 |
if not r then return nil, err end |
ca01a1696cff
Remove print() in rfc module
Mikael Berthe <mikael@lilotux.net>
parents:
32
diff
changeset
|
110 |
end |
31 | 111 |
|
112 |
if (rfcnum) then |
|
113 |
rfcnum = rfcnum:gsub("^RFC[%s%-]*", "") |
|
114 |
rfcnum = rfcnum:gsub("^rfc[%s%-]*", "") |
|
115 |
rfcnum = rfcnum:gsub("[!%?%s:]+$", "") |
|
116 |
end |
|
117 |
||
118 |
if not rfcnum or rfcnum == "" then |
|
119 |
return nil, "What RFC do you want?" |
|
120 |
end |
|
121 |
||
122 |
local n = tonumber(rfcnum) |
|
123 |
if not n then return rfc_lookup_string(rfcnum) end |
|
124 |
||
125 |
return rfc_lookup_number(n) |
|
126 |
end |
|
127 |
||
128 |
mcbot_register_command("rfc", rfc) |