capscan.lua

Tue, 21 Sep 2010 10:22:30 +0100

author
will@willthompson.co.uk
date
Tue, 21 Sep 2010 10:22:30 +0100
changeset 5
db500386b9c4
parent 4
9a4a18d11166
permissions
-rw-r--r--

Correctly close <title> tag in report.html

0
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 require "verse"
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 require "verse.client"
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local array = require "array";
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local calculate_hash = require "caps".calculate_hash;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- Configurable:
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local jid, pass = arg[1], nil;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 --- -- -- -- ---
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local escape_table = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local function escape(str) return (string.gsub(str, "['&<>\"]", escape_table)); end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local xmlns_caps = "http://jabber.org/protocol/caps";
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local xmlns_disco = "http://jabber.org/protocol/disco#info";
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
1
ce892ac8bec2 Warn and exit when no JID is supplied
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
16 if not jid then
ce892ac8bec2 Warn and exit when no JID is supplied
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
17 io.write("Please give a JID as the first argument\n");
ce892ac8bec2 Warn and exit when no JID is supplied
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
18 os.exit(1);
ce892ac8bec2 Warn and exit when no JID is supplied
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
19 end
ce892ac8bec2 Warn and exit when no JID is supplied
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
20
0
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if not pass then
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 io.write("Password (not blanked): ");
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 pass = io.read("*l");
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local conn = verse.new(verse.logger());
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 conn:add_plugin("version");
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 conn:connect_client(jid, pass);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local contacts = {};
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 conn:hook("ready", function ()
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 conn:hook("presence", function (presence)
4
9a4a18d11166 Discard presence of type error/unavailable
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
36 if contacts[presence.attr.from] or presence.attr.type then return; end;
0
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local contact = { jid = presence.attr.from };
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 contacts[contact.jid] = contact;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local caps_tag = presence:get_child("c", xmlns_caps);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if not caps_tag then
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 conn:debug("No caps from %s: %s", presence.attr.from, tostring(presence));
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 return;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 contact.hash_type = caps_tag.attr.hash;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 contact.ver = caps_tag.attr.ver;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 contact.node = caps_tag.attr.node;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 local node_string = contact.node.."#"..contact.ver;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 conn:query_version(contact.jid, function (version)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 contact.version = ("%s%s%s"):format(
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 version.name or "[no name]",
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 version.version and (" "..version.version) or "",
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 version.platform and (" ("..version.platform..")") or ""
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 );
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 conn:send_iq(verse.iq({ to = contact.jid, type = "get" })
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 :tag("query", { xmlns = xmlns_disco, node = node_string }),
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 function (result)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 if result.attr.type == "error" then
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 contact.error = { result:get_error() };
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 return;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 contact.calculated_ver, contact.calculated_S
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 = calculate_hash(result.tags[1]);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 if contact.calculated_ver ~= contact.ver then
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 conn:warn("Invalid caps hash: %s", contact.jid);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 conn:warn("Received: %s Calculated: %s", contact.ver, contact.calculated_ver);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 conn:warn("Received stanza: %s", tostring(result));
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 conn:warn("Calculated S: %s", contact.calculated_S);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 else
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 conn:warn("Valid caps hash: %s", contact.jid);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 end);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 end, 1000);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 conn:send(verse.presence():tag("priority"):text("-1"));
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 verse.loop();
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 --- Write report
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 local report = io.open("report.html", "w+");
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 report:write[[<html>
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 <head>
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 <title>]];
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 report:write("Entity capabilities validity for contacts of ", jid);
5
db500386b9c4 Correctly close <title> tag in report.html
will@willthompson.co.uk
parents: 4
diff changeset
92 report:write[[</title>
db500386b9c4 Correctly close <title> tag in report.html
will@willthompson.co.uk
parents: 4
diff changeset
93 </head>
0
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 <body>
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 ]];
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 local contact_jids = array.collect(keys(contacts)):sort();
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 report:write("<h1>Entity capabilities report for contacts of ", jid, "</h1>\n");
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 report:write("<p>", tostring(#contact_jids), " contacts</p>\n");
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 local function write_section(title, jids, show)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 report:write("\n<h1>", title, "</h1>\n");
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 report:write("<p>", tostring(#jids), " contacts</p>\n");
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 report:write("<ul>\n");
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 for _, jid in ipairs(jids) do
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 local contact = contacts[jid];
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 local client_link = ("&nbsp;<a href='%s'>%s</a>"):format(
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 escape(contacts[jid].node or "#"),
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 escape(contacts[jid].version or "Unknown client")
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 );
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 report:write(" <li>", escape(jid), client_link);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 if show then
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 report:write("\n <ul>\n");
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 for _, field in ipairs(show) do
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 local friendly_field = field:gsub("^.", string.upper):gsub("_", " ");
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 local value = escape(contacts[jid][field] or "");
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 report:write((" "):rep(12), "<li>", friendly_field, ": ", value, "</li>\n");
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 report:write(" </ul>\n");
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 report:write("</li>\n");
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 report:write("</ul>\n");
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 local function no_caps_filter(jid)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 return not contacts[jid].ver;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 local function legacy_caps_filter(jid)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 return contacts[jid].ver and not contacts[jid].hash_type;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 local function valid_caps_filter(jid)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 return not(legacy_caps_filter(jid))
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 and (contacts[jid].ver == contacts[jid].calculated_ver);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 local function invalid_caps_filter(jid)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 return not(legacy_caps_filter(jid)) and not(no_caps_filter(jid)) and not(valid_caps_filter(jid));
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 write_section("Valid caps", array.filter(contact_jids, valid_caps_filter));
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 write_section("Invalid caps", array.filter(contact_jids, invalid_caps_filter), { "ver", "calculated_ver", "calculated_S" });
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 write_section("No caps", array.filter(contact_jids, no_caps_filter));
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 write_section("Legacy caps", array.filter(contact_jids, legacy_caps_filter), { "ver" });
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 report:write("</body></html>");

mercurial