main.lua

Thu, 03 Dec 2020 17:05:27 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 03 Dec 2020 17:05:27 +0000
changeset 0
550f506de75a
permissions
-rw-r--r--

Initial commit

0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Admin credentials
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local jid, password = "user@example.com", "secret"";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 -- Domain that users are on
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local domain = "example.com";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 -- Number of connections to the XMPP server
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local max_workers = 20;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local input_filename = assert(arg[1], "No user file specified");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local verse = require "verse".init("client");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local async = require "util.async";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local promise = require "util.promise";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local function fs_encode(s)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 return (s:gsub("%W", function (c)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 return ("%02x"):format(c:byte());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local function get_user_pep_nodes(c, username)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 return promise.new(function (resolve, reject)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local request = verse.iq({ type = "get", to = username.."@"..domain })
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 :tag("query", { xmlns = "http://jabber.org/protocol/disco#items" });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 c:send_iq(request, function (response)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 if response.attr.type == "error" then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 return reject(response);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local nodes = {};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 for item in response.tags[1]:childtags("item") do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 table.insert(nodes, item.attr.node);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 resolve(nodes);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local function get_user_pep_data(c, username, node)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 return promise.new(function (resolve, reject)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local request = verse.iq({ type = "get", to = username.."@"..domain })
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 :tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" })
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 :tag("items", { node = node });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 c:send_iq(request, function (response)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 if response.attr.type == "error" then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 return reject(response);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 resolve(response
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 :get_child("pubsub", "http://jabber.org/protocol/pubsub")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 :get_child("items")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 );
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 local function connect_clients(n_clients)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 local client_promises = {};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 for i = 1, n_clients do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 local c = verse.new();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 client_promises[i] = promise.new(function (resolve, reject)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 c:hook("ready", function ()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 resolve(c);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 c:hook("disconnected", reject);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 c:connect_client(jid, password);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 return promise.all(client_promises);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 local function run_worker(client, users_file)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 return promise.new(function (resolve, reject)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 local processed = 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local line = users_file:read("*l");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 if not line then resolve(processed); end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 client:onready(function (self)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 processed = processed + 1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 local new_line = users_file:read("*l");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 if new_line then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 self:run(new_line);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 resolve(processed);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 :onerror(reject)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 :run(line);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 connect_clients(max_workers):next(function (connections)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 local clients = {};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 for i, conn_ in ipairs(connections) do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 local conn = conn_;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 clients[i] = async.runner(function (username)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 --print("Getting nodes for "..username.."...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 local nodes, err = async.wait_for(get_user_pep_nodes(conn, username));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 if not nodes then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 print("Error:", err);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 local f = io.open(fs_encode(username)..".xml", "w+");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 f:write("<pep>");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 for _, node_name in ipairs(nodes) do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 local data = async.wait_for(get_user_pep_data(conn, username, node_name));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 f:write(tostring(data));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 f:write("</pep>");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 f:close();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 return clients;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end):next(function (clients)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 local users_file = assert(io.open(input_filename));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 local workers = {};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 for i = 1, #clients do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 workers[i] = run_worker(clients[i], users_file);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 promise.all(workers):next(function (status)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 local total = 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 for i, processed in ipairs(status) do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 print(("Worker %d processed %d users"):format(i, processed));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 total = total + processed;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 print(("Total: %d users processed"):format(total));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 verse.quit();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end, function (err)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 print("Fatal error:", err);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 os.exit(1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 end):catch(function (err)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 print("Initialization error:", err);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 os.exit(1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 print("Starting loop...")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 local start_time = os.time();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 verse.loop()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 print(("All done in %d seconds!"):format(os.time()-start_time));

mercurial