capscan.lua

Sat, 18 Sep 2010 18:19:37 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 18 Sep 2010 18:19:37 +0100
changeset 0
d17a1b659852
child 1
ce892ac8bec2
permissions
-rw-r--r--

Initial commit

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

mercurial