Annotate

init.lua @ 500:674daff6c73b

use util.bitcompat from Prosody for bitwise compat layer
author Kim Alvefur <zash@zash.se>
date Fri, 23 Jun 2023 12:26:43 +0200
parent 492:22844ac3be4e
child 505:289c866d7fb0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
17
ec6b0b94826c verse: Include LuaRocks packages if we can
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
2 -- Use LuaRocks if available
ec6b0b94826c verse: Include LuaRocks packages if we can
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
3 pcall(require, "luarocks.require");
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
368
154c2f04d73b init: Restore loading of LuaSec (must be loaded before net.server) (thanks 桜)
Kim Alvefur <zash@zash.se>
parents: 361
diff changeset
5 local socket = require"socket";
154c2f04d73b init: Restore loading of LuaSec (must be loaded before net.server) (thanks 桜)
Kim Alvefur <zash@zash.se>
parents: 361
diff changeset
6
64
a28540d4117a verse: Load LuaSec if possible
Matthew Wild <mwild1@gmail.com>
parents: 60
diff changeset
7 -- Load LuaSec if available
368
154c2f04d73b init: Restore loading of LuaSec (must be loaded before net.server) (thanks 桜)
Kim Alvefur <zash@zash.se>
parents: 361
diff changeset
8 pcall(require, "ssl");
64
a28540d4117a verse: Load LuaSec if possible
Matthew Wild <mwild1@gmail.com>
parents: 60
diff changeset
9
492
22844ac3be4e server: Import one of Prosodys net.server implementations
Kim Alvefur <zash@zash.se>
parents: 490
diff changeset
10 local server = require "verse.server";
490
6b2f31da9610 Update for new Prosody module namespace
Kim Alvefur <zash@zash.se>
parents: 465
diff changeset
11 package.loaded["prosody.net.server"] = server; -- XXX
6b2f31da9610 Update for new Prosody module namespace
Kim Alvefur <zash@zash.se>
parents: 465
diff changeset
12 local events = require "prosody.util.events";
6b2f31da9610 Update for new Prosody module namespace
Kim Alvefur <zash@zash.se>
parents: 465
diff changeset
13 local logger = require "prosody.util.logger";
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
490
6b2f31da9610 Update for new Prosody module namespace
Kim Alvefur <zash@zash.se>
parents: 465
diff changeset
15 local format = require "prosody.util.format".format;
461
fa5c40e5e079 Use util.format for logging
Matthew Wild <mwild1@gmail.com>
parents: 444
diff changeset
16
417
d46a502955c0 verse: Remove use of deprecated module() function
Kim Alvefur <zash@zash.se>
parents: 411
diff changeset
17 local verse = {};
d46a502955c0 verse: Remove use of deprecated module() function
Kim Alvefur <zash@zash.se>
parents: 411
diff changeset
18 verse.server = server;
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local stream = {};
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 stream.__index = stream;
417
d46a502955c0 verse: Remove use of deprecated module() function
Kim Alvefur <zash@zash.se>
parents: 411
diff changeset
22 verse.stream_mt = stream;
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
3
372ddb5900d3 verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
24 verse.plugins = {};
372ddb5900d3 verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
25
257
816b8ccb3082 verse: Add init(...) method to load connection modules (client, component, bosh, etc.)
Matthew Wild <mwild1@gmail.com>
parents: 247
diff changeset
26 function verse.init(...)
816b8ccb3082 verse: Add init(...) method to load connection modules (client, component, bosh, etc.)
Matthew Wild <mwild1@gmail.com>
parents: 247
diff changeset
27 for i=1,select("#", ...) do
385
e6bf19ad5c19 verse: Pass on error from trying to require verse connection module
Kim Alvefur <zash@zash.se>
parents: 382
diff changeset
28 local ok, err = pcall(require, "verse."..select(i,...));
257
816b8ccb3082 verse: Add init(...) method to load connection modules (client, component, bosh, etc.)
Matthew Wild <mwild1@gmail.com>
parents: 247
diff changeset
29 if not ok then
385
e6bf19ad5c19 verse: Pass on error from trying to require verse connection module
Kim Alvefur <zash@zash.se>
parents: 382
diff changeset
30 error("Verse connection module not found: verse."..select(i,...)..err);
257
816b8ccb3082 verse: Add init(...) method to load connection modules (client, component, bosh, etc.)
Matthew Wild <mwild1@gmail.com>
parents: 247
diff changeset
31 end
816b8ccb3082 verse: Add init(...) method to load connection modules (client, component, bosh, etc.)
Matthew Wild <mwild1@gmail.com>
parents: 247
diff changeset
32 end
816b8ccb3082 verse: Add init(...) method to load connection modules (client, component, bosh, etc.)
Matthew Wild <mwild1@gmail.com>
parents: 247
diff changeset
33 return verse;
816b8ccb3082 verse: Add init(...) method to load connection modules (client, component, bosh, etc.)
Matthew Wild <mwild1@gmail.com>
parents: 247
diff changeset
34 end
816b8ccb3082 verse: Add init(...) method to load connection modules (client, component, bosh, etc.)
Matthew Wild <mwild1@gmail.com>
parents: 247
diff changeset
35
816b8ccb3082 verse: Add init(...) method to load connection modules (client, component, bosh, etc.)
Matthew Wild <mwild1@gmail.com>
parents: 247
diff changeset
36
239
65a0d3fcbbad verse: Switch connection ids to be a simple incrementing integer
Matthew Wild <mwild1@gmail.com>
parents: 238
diff changeset
37 local max_id = 0;
65a0d3fcbbad verse: Switch connection ids to be a simple incrementing integer
Matthew Wild <mwild1@gmail.com>
parents: 238
diff changeset
38
44
37396504de5f verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
39 function verse.new(logger, base)
37396504de5f verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
40 local t = setmetatable(base or {}, stream);
239
65a0d3fcbbad verse: Switch connection ids to be a simple incrementing integer
Matthew Wild <mwild1@gmail.com>
parents: 238
diff changeset
41 max_id = max_id + 1;
65a0d3fcbbad verse: Switch connection ids to be a simple incrementing integer
Matthew Wild <mwild1@gmail.com>
parents: 238
diff changeset
42 t.id = tostring(max_id);
238
9b6fca5d9a02 verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
Matthew Wild <mwild1@gmail.com>
parents: 237
diff changeset
43 t.logger = logger or verse.new_logger("stream"..t.id);
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
44 t.events = events.new();
173
3112a9e450ac verse: Make add_plugin only load a plugin once per stream
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
45 t.plugins = {};
247
7c58c16efa3e verse: Remove silly logger line I just noticed at the end of the file
Matthew Wild <mwild1@gmail.com>
parents: 242
diff changeset
46 t.verse = verse;
44
37396504de5f verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
47 return t;
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
490
6b2f31da9610 Update for new Prosody module namespace
Kim Alvefur <zash@zash.se>
parents: 465
diff changeset
50 verse.add_task = require "prosody.util.timer".add_task;
42
6006e6bb1c28 verse: Add verse.add_task(delay, callback) to add timer functions using util.timer
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
51
238
9b6fca5d9a02 verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
Matthew Wild <mwild1@gmail.com>
parents: 237
diff changeset
52 verse.logger = logger.init; -- COMPAT: Deprecated
236
d75a209e57fc verse: verse.logger() -> verse.new_logger()
Matthew Wild <mwild1@gmail.com>
parents: 234
diff changeset
53 verse.new_logger = logger.init;
125
b46921de1e03 verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents: 124
diff changeset
54 verse.log = verse.logger("verse");
b46921de1e03 verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents: 124
diff changeset
55
238
9b6fca5d9a02 verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
Matthew Wild <mwild1@gmail.com>
parents: 237
diff changeset
56 function verse.set_log_handler(log_handler, levels)
9b6fca5d9a02 verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
Matthew Wild <mwild1@gmail.com>
parents: 237
diff changeset
57 levels = levels or { "debug", "info", "warn", "error" };
9b6fca5d9a02 verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
Matthew Wild <mwild1@gmail.com>
parents: 237
diff changeset
58 logger.reset();
279
7a0aa3d055f4 verse: Accept a file object as a log handler, and automatically call :write() on it with a formatted message
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
59 if io.type(log_handler) == "file" then
7a0aa3d055f4 verse: Accept a file object as a log handler, and automatically call :write() on it with a formatted message
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
60 local f = log_handler;
7a0aa3d055f4 verse: Accept a file object as a log handler, and automatically call :write() on it with a formatted message
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
61 function log_handler(name, level, message)
7a0aa3d055f4 verse: Accept a file object as a log handler, and automatically call :write() on it with a formatted message
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
62 f:write(name, "\t", level, "\t", message, "\n");
7a0aa3d055f4 verse: Accept a file object as a log handler, and automatically call :write() on it with a formatted message
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
63 end
241
931bf313c691 verse: Format messages before passing them to custom log handler
Matthew Wild <mwild1@gmail.com>
parents: 240
diff changeset
64 end
240
e27bf761bea6 verse: set_log_handler(): Allow nil to be passed for log_handler to disable logging (the default).
Matthew Wild <mwild1@gmail.com>
parents: 239
diff changeset
65 if log_handler then
279
7a0aa3d055f4 verse: Accept a file object as a log handler, and automatically call :write() on it with a formatted message
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
66 local function _log_handler(name, level, message, ...)
7a0aa3d055f4 verse: Accept a file object as a log handler, and automatically call :write() on it with a formatted message
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
67 return log_handler(name, level, format(message, ...));
7a0aa3d055f4 verse: Accept a file object as a log handler, and automatically call :write() on it with a formatted message
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
68 end
240
e27bf761bea6 verse: set_log_handler(): Allow nil to be passed for log_handler to disable logging (the default).
Matthew Wild <mwild1@gmail.com>
parents: 239
diff changeset
69 for i, level in ipairs(levels) do
241
931bf313c691 verse: Format messages before passing them to custom log handler
Matthew Wild <mwild1@gmail.com>
parents: 240
diff changeset
70 logger.add_level_sink(level, _log_handler);
240
e27bf761bea6 verse: set_log_handler(): Allow nil to be passed for log_handler to disable logging (the default).
Matthew Wild <mwild1@gmail.com>
parents: 239
diff changeset
71 end
142
ea753bc81a33 verse: Add verse.filter_log(levels, logger) to filter a logger for certain levels
Matthew Wild <mwild1@gmail.com>
parents: 141
diff changeset
72 end
ea753bc81a33 verse: Add verse.filter_log(levels, logger) to filter a logger for certain levels
Matthew Wild <mwild1@gmail.com>
parents: 141
diff changeset
73 end
ea753bc81a33 verse: Add verse.filter_log(levels, logger) to filter a logger for certain levels
Matthew Wild <mwild1@gmail.com>
parents: 141
diff changeset
74
417
d46a502955c0 verse: Remove use of deprecated module() function
Kim Alvefur <zash@zash.se>
parents: 411
diff changeset
75 function verse._default_log_handler(name, level, message)
242
ab4773b0ef5e verse: Add default log handler for errors
Matthew Wild <mwild1@gmail.com>
parents: 241
diff changeset
76 return io.stderr:write(name, "\t", level, "\t", message, "\n");
ab4773b0ef5e verse: Add default log handler for errors
Matthew Wild <mwild1@gmail.com>
parents: 241
diff changeset
77 end
417
d46a502955c0 verse: Remove use of deprecated module() function
Kim Alvefur <zash@zash.se>
parents: 411
diff changeset
78 verse.set_log_handler(verse._default_log_handler, { "error" });
242
ab4773b0ef5e verse: Add default log handler for errors
Matthew Wild <mwild1@gmail.com>
parents: 241
diff changeset
79
125
b46921de1e03 verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents: 124
diff changeset
80 local function error_handler(err)
b46921de1e03 verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents: 124
diff changeset
81 verse.log("error", "Error: %s", err);
b46921de1e03 verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents: 124
diff changeset
82 verse.log("error", "Traceback: %s", debug.traceback());
b46921de1e03 verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents: 124
diff changeset
83 end
b46921de1e03 verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents: 124
diff changeset
84
b46921de1e03 verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents: 124
diff changeset
85 function verse.set_error_handler(new_error_handler)
b46921de1e03 verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents: 124
diff changeset
86 error_handler = new_error_handler;
b46921de1e03 verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents: 124
diff changeset
87 end
b46921de1e03 verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents: 124
diff changeset
88
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 function verse.loop()
126
fa3ddadb8364 verse: verse.loop() and new verse.step() use the new error handler to, er, handle errors.
Matthew Wild <mwild1@gmail.com>
parents: 125
diff changeset
90 return xpcall(server.loop, error_handler);
fa3ddadb8364 verse: verse.loop() and new verse.step() use the new error handler to, er, handle errors.
Matthew Wild <mwild1@gmail.com>
parents: 125
diff changeset
91 end
fa3ddadb8364 verse: verse.loop() and new verse.step() use the new error handler to, er, handle errors.
Matthew Wild <mwild1@gmail.com>
parents: 125
diff changeset
92
fa3ddadb8364 verse: verse.loop() and new verse.step() use the new error handler to, er, handle errors.
Matthew Wild <mwild1@gmail.com>
parents: 125
diff changeset
93 function verse.step()
fa3ddadb8364 verse: verse.loop() and new verse.step() use the new error handler to, er, handle errors.
Matthew Wild <mwild1@gmail.com>
parents: 125
diff changeset
94 return xpcall(server.step, error_handler);
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 end
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96
45
50a2e4fb0a16 verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents: 44
diff changeset
97 function verse.quit()
424
eaaaf4495e06 verse: Ensure verse.quit() only quits the loop once
Matthew Wild <mwild1@gmail.com>
parents: 417
diff changeset
98 return server.setquitting("once");
45
50a2e4fb0a16 verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents: 44
diff changeset
99 end
50a2e4fb0a16 verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents: 44
diff changeset
100
463
98fe3ed54639 Update to new tls_builder() API in net.server upstream
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
101 function verse.tls_builder(...)
98fe3ed54639 Update to new tls_builder() API in net.server upstream
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
102 return server.tls_builder(...);
98fe3ed54639 Update to new tls_builder() API in net.server upstream
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
103 end
98fe3ed54639 Update to new tls_builder() API in net.server upstream
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
104
280
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
105 function stream:listen(host, port)
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
106 host = host or "localhost";
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
107 port = port or 0;
417
d46a502955c0 verse: Remove use of deprecated module() function
Kim Alvefur <zash@zash.se>
parents: 411
diff changeset
108 local conn, err = server.addserver(host, port, verse.new_listener(self, "server"), "*a");
280
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
109 if conn then
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
110 self:debug("Bound to %s:%s", host, port);
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
111 self.server = conn;
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
112 end
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
113 return conn, err;
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
114 end
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
115
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
116 function stream:connect(connect_host, connect_port)
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
117 connect_host = connect_host or "localhost";
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
118 connect_port = tonumber(connect_port) or 5222;
411
db462d4feb44 verse: trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 399
diff changeset
119
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
120 -- Create and initiate connection
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
121 local conn = socket.tcp()
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
122 conn:settimeout(0);
382
479b856a910f verse: Enable TCP keepalives
Kim Alvefur <zash@zash.se>
parents: 368
diff changeset
123 conn:setoption("keepalive", true);
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
124 local success, err = conn:connect(connect_host, connect_port);
411
db462d4feb44 verse: trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 399
diff changeset
125
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
126 if not success and err ~= "timeout" then
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
127 self:warn("connect() to %s:%d failed: %s", connect_host, connect_port, err);
135
8adf5fb27ce3 verse: Fire disconnected event even if connection fails instantly
Matthew Wild <mwild1@gmail.com>
parents: 132
diff changeset
128 return self:event("disconnected", { reason = err }) or false, err;
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
129 end
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
130
417
d46a502955c0 verse: Remove use of deprecated module() function
Kim Alvefur <zash@zash.se>
parents: 411
diff changeset
131 local conn = server.wrapclient(conn, connect_host, connect_port, verse.new_listener(self), "*a");
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 if not conn then
124
3659ba3b8dfa verse: Log error when connection fails instantly
Matthew Wild <mwild1@gmail.com>
parents: 98
diff changeset
133 self:warn("connection initialisation failed: %s", err);
135
8adf5fb27ce3 verse: Fire disconnected event even if connection fails instantly
Matthew Wild <mwild1@gmail.com>
parents: 132
diff changeset
134 return self:event("disconnected", { reason = err }) or false, err;
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 end
280
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
136 self:set_conn(conn);
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
137 return true;
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
138 end
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
139
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
140 function stream:set_conn(conn)
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 self.conn = conn;
163
48a47bca9a63 verse: Change to set conn:send in only one place, and fire new events 'outgoing' and 'outgoing-raw'
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
142 self.send = function (stream, data)
48a47bca9a63 verse: Change to set conn:send in only one place, and fire new events 'outgoing' and 'outgoing-raw'
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
143 self:event("outgoing", data);
48a47bca9a63 verse: Change to set conn:send in only one place, and fire new events 'outgoing' and 'outgoing-raw'
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
144 data = tostring(data);
48a47bca9a63 verse: Change to set conn:send in only one place, and fire new events 'outgoing' and 'outgoing-raw'
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
145 self:event("outgoing-raw", data);
48a47bca9a63 verse: Change to set conn:send in only one place, and fire new events 'outgoing' and 'outgoing-raw'
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
146 return conn:write(data);
48a47bca9a63 verse: Change to set conn:send in only one place, and fire new events 'outgoing' and 'outgoing-raw'
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
147 end;
96
f0aec10023aa verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 95
diff changeset
148 end
f0aec10023aa verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 95
diff changeset
149
322
819b35b8fcf6 verse: Add missing 'reason' argument to stream:close()
Kim Alvefur <zash@zash.se>
parents: 281
diff changeset
150 function stream:close(reason)
411
db462d4feb44 verse: trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 399
diff changeset
151 if not self.conn then
132
b38397163737 verse: Log error and return when trying to close a closed connection
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
152 verse.log("error", "Attempt to close disconnected connection - possibly a bug");
b38397163737 verse: Log error and return when trying to close a closed connection
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
153 return;
b38397163737 verse: Log error and return when trying to close a closed connection
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
154 end
96
f0aec10023aa verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 95
diff changeset
155 local on_disconnect = self.conn.disconnect();
465
6707e3a47f71 Add 'shutdown' event for a self-initiated disconnect
Matthew Wild <mwild1@gmail.com>
parents: 464
diff changeset
156 self:event("shutdown");
96
f0aec10023aa verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 95
diff changeset
157 self.conn:close();
330
44d2bd1cee46 Backout 55e3fd7b9731
Kim Alvefur <zash@zash.se>
parents: 328
diff changeset
158 on_disconnect(self.conn, reason);
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
159 end
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
160
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
161 -- Logging functions
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
162 function stream:debug(...)
238
9b6fca5d9a02 verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
Matthew Wild <mwild1@gmail.com>
parents: 237
diff changeset
163 return self.logger("debug", ...);
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
164 end
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
165
258
94b1d9863dd9 verse: Add stream:info() for emitting info-level log message.
Kim Alvefur <zash@zash.se>
parents: 247
diff changeset
166 function stream:info(...)
94b1d9863dd9 verse: Add stream:info() for emitting info-level log message.
Kim Alvefur <zash@zash.se>
parents: 247
diff changeset
167 return self.logger("info", ...);
94b1d9863dd9 verse: Add stream:info() for emitting info-level log message.
Kim Alvefur <zash@zash.se>
parents: 247
diff changeset
168 end
94b1d9863dd9 verse: Add stream:info() for emitting info-level log message.
Kim Alvefur <zash@zash.se>
parents: 247
diff changeset
169
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
170 function stream:warn(...)
238
9b6fca5d9a02 verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
Matthew Wild <mwild1@gmail.com>
parents: 237
diff changeset
171 return self.logger("warn", ...);
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 end
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
174 function stream:error(...)
238
9b6fca5d9a02 verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
Matthew Wild <mwild1@gmail.com>
parents: 237
diff changeset
175 return self.logger("error", ...);
94
b40465267fb5 verse: Add stream:set_loglevels()
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
176 end
b40465267fb5 verse: Add stream:set_loglevels()
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
177
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
178 -- Event handling
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
179 function stream:event(name, ...)
4
0ef21511c7ff Log debug message when firing an event
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
180 self:debug("Firing event: "..tostring(name));
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
181 return self.events.fire_event(name, ...);
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
182 end
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
183
16
13444ae6e3c4 verse: Fix stream:hook() to pass additional parameters to the underlying hook(), so we don't strip priority
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
184 function stream:hook(name, ...)
13444ae6e3c4 verse: Fix stream:hook() to pass additional parameters to the underlying hook(), so we don't strip priority
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
185 return self.events.add_handler(name, ...);
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
186 end
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
187
53
091ff10eb51c verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
188 function stream:unhook(name, handler)
091ff10eb51c verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
189 return self.events.remove_handler(name, handler);
091ff10eb51c verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
190 end
091ff10eb51c verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
191
98
1dccff7df2d5 verse: Add verse.eventable() to give a passed object :event() and :hook()/:unhook() methods and return it again
Matthew Wild <mwild1@gmail.com>
parents: 96
diff changeset
192 function verse.eventable(object)
464
dc05a8d6dbfb Fix whitespace
Matthew Wild <mwild1@gmail.com>
parents: 463
diff changeset
193 object.events = events.new();
dc05a8d6dbfb Fix whitespace
Matthew Wild <mwild1@gmail.com>
parents: 463
diff changeset
194 object.hook, object.unhook = stream.hook, stream.unhook;
dc05a8d6dbfb Fix whitespace
Matthew Wild <mwild1@gmail.com>
parents: 463
diff changeset
195 local fire_event = object.events.fire_event;
dc05a8d6dbfb Fix whitespace
Matthew Wild <mwild1@gmail.com>
parents: 463
diff changeset
196 function object:event(name, ...) return fire_event(name, ...); end
dc05a8d6dbfb Fix whitespace
Matthew Wild <mwild1@gmail.com>
parents: 463
diff changeset
197 return object;
98
1dccff7df2d5 verse: Add verse.eventable() to give a passed object :event() and :hook()/:unhook() methods and return it again
Matthew Wild <mwild1@gmail.com>
parents: 96
diff changeset
198 end
1dccff7df2d5 verse: Add verse.eventable() to give a passed object :event() and :hook()/:unhook() methods and return it again
Matthew Wild <mwild1@gmail.com>
parents: 96
diff changeset
199
3
372ddb5900d3 verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
200 function stream:add_plugin(name)
173
3112a9e450ac verse: Make add_plugin only load a plugin once per stream
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
201 if self.plugins[name] then return true; end
3
372ddb5900d3 verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
202 if require("verse.plugins."..name) then
372ddb5900d3 verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
203 local ok, err = verse.plugins[name](self);
162
2950079d4319 verse: Consider a plugin as failed to load only if it returns false
Matthew Wild <mwild1@gmail.com>
parents: 142
diff changeset
204 if ok ~= false then
3
372ddb5900d3 verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
205 self:debug("Loaded %s plugin", name);
173
3112a9e450ac verse: Make add_plugin only load a plugin once per stream
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
206 self.plugins[name] = true;
3
372ddb5900d3 verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
207 else
372ddb5900d3 verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
208 self:warn("Failed to load %s plugin: %s", name, err);
372ddb5900d3 verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
209 end
372ddb5900d3 verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
210 end
372ddb5900d3 verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
211 return self;
372ddb5900d3 verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
212 end
372ddb5900d3 verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
213
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
214 -- Listener factory
417
d46a502955c0 verse: Remove use of deprecated module() function
Kim Alvefur <zash@zash.se>
parents: 411
diff changeset
215 function verse.new_listener(stream)
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 local conn_listener = {};
411
db462d4feb44 verse: trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 399
diff changeset
217
54
1a2a3d598254 verse: Take advantage of server.lua's new onconnect callback for a more robust "connected" event
Matthew Wild <mwild1@gmail.com>
parents: 53
diff changeset
218 function conn_listener.onconnect(conn)
280
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
219 if stream.server then
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
220 local client = verse.new();
417
d46a502955c0 verse: Remove use of deprecated module() function
Kim Alvefur <zash@zash.se>
parents: 411
diff changeset
221 conn:setlistener(verse.new_listener(client));
280
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
222 client:set_conn(conn);
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
223 stream:event("connected", { client = client });
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
224 else
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
225 stream.connected = true;
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
226 stream:event("connected");
981c8867f1fb verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
Matthew Wild <mwild1@gmail.com>
parents: 279
diff changeset
227 end
54
1a2a3d598254 verse: Take advantage of server.lua's new onconnect callback for a more robust "connected" event
Matthew Wild <mwild1@gmail.com>
parents: 53
diff changeset
228 end
411
db462d4feb44 verse: trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 399
diff changeset
229
20
972066e06f4c verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
230 function conn_listener.onincoming(conn, data)
54
1a2a3d598254 verse: Take advantage of server.lua's new onconnect callback for a more robust "connected" event
Matthew Wild <mwild1@gmail.com>
parents: 53
diff changeset
231 stream:event("incoming-raw", data);
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 end
411
db462d4feb44 verse: trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 399
diff changeset
233
20
972066e06f4c verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
234 function conn_listener.ondisconnect(conn, err)
331
d23bb21668a7 verse: Fix disconnected event for unassociated connections
Kim Alvefur <zash@zash.se>
parents: 330
diff changeset
235 if conn ~= stream.conn then return end
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 stream.connected = false;
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 stream:event("disconnected", { reason = err });
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 end
55
163beb198646 verse: Add "drained" event to signal when send buffer is empty (new server.lua feature)
Matthew Wild <mwild1@gmail.com>
parents: 54
diff changeset
239
163beb198646 verse: Add "drained" event to signal when send buffer is empty (new server.lua feature)
Matthew Wild <mwild1@gmail.com>
parents: 54
diff changeset
240 function conn_listener.ondrain(conn)
163beb198646 verse: Add "drained" event to signal when send buffer is empty (new server.lua feature)
Matthew Wild <mwild1@gmail.com>
parents: 54
diff changeset
241 stream:event("drained");
163beb198646 verse: Add "drained" event to signal when send buffer is empty (new server.lua feature)
Matthew Wild <mwild1@gmail.com>
parents: 54
diff changeset
242 end
411
db462d4feb44 verse: trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 399
diff changeset
243
60
1f47ddab3499 verse: Fire "status" event for connection status changes (notably SSL handshake complete)
Matthew Wild <mwild1@gmail.com>
parents: 59
diff changeset
244 function conn_listener.onstatus(conn, new_status)
1f47ddab3499 verse: Fire "status" event for connection status changes (notably SSL handshake complete)
Matthew Wild <mwild1@gmail.com>
parents: 59
diff changeset
245 stream:event("status", new_status);
1f47ddab3499 verse: Fire "status" event for connection status changes (notably SSL handshake complete)
Matthew Wild <mwild1@gmail.com>
parents: 59
diff changeset
246 end
411
db462d4feb44 verse: trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 399
diff changeset
247
444
12c1be0044c6 client: Send whitespace keeplives
Kim Alvefur <zash@zash.se>
parents: 424
diff changeset
248 function conn_listener.onreadtimeout(conn)
12c1be0044c6 client: Send whitespace keeplives
Kim Alvefur <zash@zash.se>
parents: 424
diff changeset
249 return stream:event("read-timeout");
12c1be0044c6 client: Send whitespace keeplives
Kim Alvefur <zash@zash.se>
parents: 424
diff changeset
250 end
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 return conn_listener;
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 end
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 return verse;