author | Mikael Berthe <mikael@lilotux.net> |
Tue, 27 Nov 2012 16:26:04 +0100 | |
changeset 66 | d9c00a9fe9d5 |
parent 34 | ca01a1696cff |
child 67 | 5c756a9d7d8c |
permissions | -rw-r--r-- |
31 | 1 |
#! /usr/bin/env lua |
2 |
||
66
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
34
diff
changeset
|
3 |
-- This module is part of the McBot / mcabbot project |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
34
diff
changeset
|
4 |
-- RFC index parser |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
34
diff
changeset
|
5 |
-- |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
34
diff
changeset
|
6 |
-- Copyright (C) 2010-2012 Mikael Berthe <mikael@lilotux.net> |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
34
diff
changeset
|
7 |
-- |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
34
diff
changeset
|
8 |
-- This program is free software; you can redistribute it and/or modify |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
34
diff
changeset
|
9 |
-- it under the terms of the GNU General Public License as published by |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
34
diff
changeset
|
10 |
-- the Free Software Foundation; either version 2 of the License, or (at |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
34
diff
changeset
|
11 |
-- your option) any later version. |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
34
diff
changeset
|
12 |
-- |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
34
diff
changeset
|
13 |
-- Please check the license in the COPYING file at the root of the tree. |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
34
diff
changeset
|
14 |
-- |
31 | 15 |
|
16 |
-- TXT index database, retrieved from |
|
17 |
-- <ftp://ftp.rfc-editor.org/in-notes/rfc-index.txt> |
|
18 |
local indexfname = "/home/mikael/.mcabber/lua/mcbot/rfc-index.txt" |
|
19 |
||
32 | 20 |
local urlbase = "http://tools.ietf.org/html/rfc" |
21 |
||
31 | 22 |
local rfc = { ["desc"] = "Search RFC database" } |
23 |
||
24 |
local rfcdb = {} |
|
25 |
||
26 |
local function parse_file (fname) |
|
27 |
local fh = io.open(fname) |
|
28 |
||
34
ca01a1696cff
Remove print() in rfc module
Mikael Berthe <mikael@lilotux.net>
parents:
32
diff
changeset
|
29 |
if not fh then return nil, "Cannot open RFC database" end |
31 | 30 |
|
31 |
-- Skip header |
|
32 |
local count = 0 |
|
33 |
for line in fh:lines() do |
|
34 |
count = count + 1 |
|
35 |
if line:match("^%s+RFC INDEX%s*$") and count > 30 then |
|
36 |
break |
|
37 |
end |
|
38 |
end |
|
39 |
||
40 |
-- Skip 2 more lines |
|
41 |
fh:read("*l") |
|
42 |
fh:read("*l") |
|
43 |
||
44 |
local contents = fh:read("*a") |
|
45 |
fh:close() |
|
46 |
||
47 |
for entry in contents:gfind("(%d%d%d%d .-)\n\n") do |
|
48 |
entry = entry:gsub("\n ", " ") |
|
49 |
local n = entry:match("^(%d%d%d%d) ") |
|
50 |
entry = entry:gsub("^(%d%d%d%d) ", "") |
|
51 |
||
52 |
local data = {} |
|
53 |
data.title = entry:match("^(.-)%. ") |
|
54 |
entry = entry:gsub("^(.-)%. ", "") |
|
55 |
||
56 |
data.date = entry:match("%. (.-)%. %(Format:") |
|
57 |
data.obsoletes = entry:match("%(Obsoletes (.-)%)") |
|
58 |
data.obsoleted_by = entry:match("%(Obsoleted by (.-)%)") |
|
59 |
data.updates = entry:match("%(Updates (.-)%)") |
|
60 |
data.updated_by = entry:match("%(Updated by (.-)%)") |
|
61 |
data.also = entry:match("%(Also (.-)%)") |
|
62 |
data.status = entry:match("%(Status: (.-)%)") |
|
63 |
||
64 |
if data.title then |
|
65 |
rfcdb[n] = data |
|
66 |
end |
|
67 |
end |
|
34
ca01a1696cff
Remove print() in rfc module
Mikael Berthe <mikael@lilotux.net>
parents:
32
diff
changeset
|
68 |
return true |
31 | 69 |
end |
70 |
||
71 |
local function rfc_lookup_string (rfcstring) |
|
72 |
rfcstring = rfcstring:lower() |
|
73 |
local r = "" |
|
74 |
for id, data in pairs(rfcdb) do |
|
75 |
if data.title:lower():find(rfcstring) then |
|
76 |
r = r.."RFC"..id..": "..data.title.."\n" |
|
77 |
end |
|
78 |
end |
|
79 |
r = r:gsub("\n$", "") |
|
80 |
if r ~= "" then return r; end |
|
81 |
return nil, "Sorry, I couldn't find any relevant RFC" |
|
82 |
end |
|
83 |
||
84 |
local function rfc_lookup_number (rfcnum) |
|
85 |
rfcnum = tostring(rfcnum) |
|
86 |
rfcnum = string.rep("0", 4 - rfcnum:len()) .. rfcnum |
|
87 |
||
88 |
local data = rfcdb[rfcnum] |
|
89 |
if not data then return nil, "Sorry, RFC not found" end |
|
90 |
||
91 |
local r = "RFC"..rfcnum .. " is " .. data.title .. "\n" |
|
92 |
if data.status then |
|
93 |
r = r .. "Status:\t" .. data.status:lower() .. "\n" |
|
94 |
end |
|
95 |
if data.obsoletes then |
|
96 |
r = r .. "Obsoletes:\t" .. data.obsoletes .. "\n" |
|
97 |
end |
|
98 |
if data.obsoleted_by then |
|
99 |
r = r .. "Obsoleted by:\t" .. data.obsoleted_by .. "\n" |
|
100 |
end |
|
101 |
if data.updates then |
|
102 |
r = r .. "Updates:\t" .. data.updates .. "\n" |
|
103 |
end |
|
104 |
if data.updated_by then |
|
105 |
r = r .. "Updated by:\t" .. data.updated_by .. "\n" |
|
106 |
end |
|
107 |
if data.also then |
|
108 |
r = r .. "Also:\t" .. data.also .. "\n" |
|
109 |
end |
|
32 | 110 |
r = r .. "URL:\t" .. urlbase..rfcnum |
31 | 111 |
return r |
112 |
end |
|
113 |
||
114 |
function rfc.cmd (rfcnum) |
|
115 |
local empty = true |
|
116 |
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
|
117 |
if empty then |
ca01a1696cff
Remove print() in rfc module
Mikael Berthe <mikael@lilotux.net>
parents:
32
diff
changeset
|
118 |
local r, err = parse_file(indexfname) |
ca01a1696cff
Remove print() in rfc module
Mikael Berthe <mikael@lilotux.net>
parents:
32
diff
changeset
|
119 |
if not r then return nil, err end |
ca01a1696cff
Remove print() in rfc module
Mikael Berthe <mikael@lilotux.net>
parents:
32
diff
changeset
|
120 |
end |
31 | 121 |
|
122 |
if (rfcnum) then |
|
123 |
rfcnum = rfcnum:gsub("^RFC[%s%-]*", "") |
|
124 |
rfcnum = rfcnum:gsub("^rfc[%s%-]*", "") |
|
125 |
rfcnum = rfcnum:gsub("[!%?%s:]+$", "") |
|
126 |
end |
|
127 |
||
128 |
if not rfcnum or rfcnum == "" then |
|
129 |
return nil, "What RFC do you want?" |
|
130 |
end |
|
131 |
||
132 |
local n = tonumber(rfcnum) |
|
133 |
if not n then return rfc_lookup_string(rfcnum) end |
|
134 |
||
135 |
return rfc_lookup_number(n) |
|
136 |
end |
|
137 |
||
138 |
mcbot_register_command("rfc", rfc) |