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