annotate mod_ircd/mod_ircd_broke.lua @ 455:52f2188ec47d

mod_default_vcard: Sets initial vCard from data enterd on registration
author Kim Alvefur <zash@zash.se>
date Sat, 15 Oct 2011 13:43:37 +0200
parents 21cb01de89a8
children bbea8081c865
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
455
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1 package.preload['verse'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2 package.preload['util.encodings'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3 local function not_impl()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4 error("Function not implemented");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
6
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
7 local mime = require "mime";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
8
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
9 module "encodings"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
10
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
11 stringprep = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
12 base64 = { encode = mime.b64, decode = not_impl }; --mime.unb64 is buggy with \0
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
13
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
14 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
15 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
16 package.preload['util.hashes'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
17 local sha1 = require "util.sha1";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
18
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
19 return { sha1 = sha1.sha1 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
20 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
21 package.preload['util.logger'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
22 local select, tostring = select, tostring;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
23 local io_write = function(...) return io.stderr:write(...) end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
24 module "logger"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
25
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
26 local function format(format, ...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
27 local n, maxn = 0, #arg;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
28 return (format:gsub("%%(.)", function (c) if c ~= "%" and n <= maxn then n = n + 1; return tostring(arg[n]); end end));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
29 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
30
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
31 local function format(format, ...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
32 local n, maxn = 0, select('#', ...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
33 local arg = { ... };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
34 return (format:gsub("%%(.)", function (c) if n <= maxn then n = n + 1; return tostring(arg[n]); end end));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
35 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
36
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
37 function init(name)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
38 return function (level, message, ...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
39 io_write(level, "\t", format(message, ...), "\n");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
40 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
41 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
42
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
43 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
44 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
45 package.preload['util.sha1'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
46 -------------------------------------------------
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
47 --- *** SHA-1 algorithm for Lua *** ---
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
48 -------------------------------------------------
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
49 --- Author: Martin Huesser ---
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
50 --- Date: 2008-06-16 ---
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
51 --- License: You may use this code in your ---
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
52 --- projects as long as this header ---
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
53 --- stays intact. ---
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
54 -------------------------------------------------
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
55
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
56 local strlen = string.len
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
57 local strchar = string.char
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
58 local strbyte = string.byte
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
59 local strsub = string.sub
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
60 local floor = math.floor
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
61 local bit = require "bit"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
62 local bnot = bit.bnot
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
63 local band = bit.band
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
64 local bor = bit.bor
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
65 local bxor = bit.bxor
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
66 local shl = bit.lshift
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
67 local shr = bit.rshift
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
68 local h0, h1, h2, h3, h4
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
69
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
70 -------------------------------------------------
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
71
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
72 local function LeftRotate(val, nr)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
73 return shl(val, nr) + shr(val, 32 - nr)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
74 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
75
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
76 -------------------------------------------------
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
77
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
78 local function ToHex(num)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
79 local i, d
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
80 local str = ""
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
81 for i = 1, 8 do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
82 d = band(num, 15)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
83 if (d < 10) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
84 str = strchar(d + 48) .. str
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
85 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
86 str = strchar(d + 87) .. str
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
87 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
88 num = floor(num / 16)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
89 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
90 return str
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
91 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
92
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
93 -------------------------------------------------
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
94
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
95 local function PreProcess(str)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
96 local bitlen, i
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
97 local str2 = ""
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
98 bitlen = strlen(str) * 8
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
99 str = str .. strchar(128)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
100 i = 56 - band(strlen(str), 63)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
101 if (i < 0) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
102 i = i + 64
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
103 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
104 for i = 1, i do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
105 str = str .. strchar(0)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
106 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
107 for i = 1, 8 do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
108 str2 = strchar(band(bitlen, 255)) .. str2
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
109 bitlen = floor(bitlen / 256)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
110 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
111 return str .. str2
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
112 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
113
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
114 -------------------------------------------------
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
115
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
116 local function MainLoop(str)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
117 local a, b, c, d, e, f, k, t
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
118 local i, j
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
119 local w = {}
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
120 while (str ~= "") do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
121 for i = 0, 15 do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
122 w[i] = 0
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
123 for j = 1, 4 do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
124 w[i] = w[i] * 256 + strbyte(str, i * 4 + j)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
125 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
126 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
127 for i = 16, 79 do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
128 w[i] = LeftRotate(bxor(bxor(w[i - 3], w[i - 8]), bxor(w[i - 14], w[i - 16])), 1)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
129 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
130 a = h0
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
131 b = h1
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
132 c = h2
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
133 d = h3
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
134 e = h4
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
135 for i = 0, 79 do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
136 if (i < 20) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
137 f = bor(band(b, c), band(bnot(b), d))
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
138 k = 1518500249
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
139 elseif (i < 40) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
140 f = bxor(bxor(b, c), d)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
141 k = 1859775393
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
142 elseif (i < 60) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
143 f = bor(bor(band(b, c), band(b, d)), band(c, d))
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
144 k = 2400959708
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
145 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
146 f = bxor(bxor(b, c), d)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
147 k = 3395469782
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
148 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
149 t = LeftRotate(a, 5) + f + e + k + w[i]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
150 e = d
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
151 d = c
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
152 c = LeftRotate(b, 30)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
153 b = a
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
154 a = t
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
155 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
156 h0 = band(h0 + a, 4294967295)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
157 h1 = band(h1 + b, 4294967295)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
158 h2 = band(h2 + c, 4294967295)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
159 h3 = band(h3 + d, 4294967295)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
160 h4 = band(h4 + e, 4294967295)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
161 str = strsub(str, 65)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
162 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
163 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
164
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
165 -------------------------------------------------
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
166
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
167 local function sha1(str, hexres)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
168 str = PreProcess(str)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
169 h0 = 1732584193
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
170 h1 = 4023233417
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
171 h2 = 2562383102
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
172 h3 = 0271733878
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
173 h4 = 3285377520
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
174 MainLoop(str)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
175 local hex = ToHex(h0)..ToHex(h1)..ToHex(h2)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
176 ..ToHex(h3)..ToHex(h4);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
177 if hexres then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
178 return hex;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
179 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
180 return (hex:gsub("..", function (byte)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
181 return string.char(tonumber(byte, 16));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
182 end));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
183 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
184 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
185
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
186 _G.sha1 = {sha1 = sha1};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
187 return _G.sha1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
188
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
189 -------------------------------------------------
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
190 -------------------------------------------------
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
191 -------------------------------------------------
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
192 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
193 package.preload['lib.adhoc'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
194 -- Copyright (C) 2009-2010 Florian Zeitz
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
195 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
196 -- This file is MIT/X11 licensed. Please see the
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
197 -- COPYING file in the source package for more information.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
198 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
199
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
200 local st, uuid = require "util.stanza", require "util.uuid";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
201
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
202 local xmlns_cmd = "http://jabber.org/protocol/commands";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
203
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
204 local states = {}
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
205
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
206 local _M = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
207
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
208 function _cmdtag(desc, status, sessionid, action)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
209 local cmd = st.stanza("command", { xmlns = xmlns_cmd, node = desc.node, status = status });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
210 if sessionid then cmd.attr.sessionid = sessionid; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
211 if action then cmd.attr.action = action; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
212
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
213 return cmd;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
214 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
215
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
216 function _M.new(name, node, handler, permission)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
217 return { name = name, node = node, handler = handler, cmdtag = _cmdtag, permission = (permission or "user") };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
218 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
219
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
220 function _M.handle_cmd(command, origin, stanza)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
221 local sessionid = stanza.tags[1].attr.sessionid or uuid.generate();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
222 local dataIn = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
223 dataIn.to = stanza.attr.to;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
224 dataIn.from = stanza.attr.from;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
225 dataIn.action = stanza.tags[1].attr.action or "execute";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
226 dataIn.form = stanza.tags[1]:child_with_ns("jabber:x:data");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
227
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
228 local data, state = command:handler(dataIn, states[sessionid]);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
229 states[sessionid] = state;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
230 local stanza = st.reply(stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
231 if data.status == "completed" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
232 states[sessionid] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
233 cmdtag = command:cmdtag("completed", sessionid);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
234 elseif data.status == "canceled" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
235 states[sessionid] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
236 cmdtag = command:cmdtag("canceled", sessionid);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
237 elseif data.status == "error" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
238 states[sessionid] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
239 stanza = st.error_reply(stanza, data.error.type, data.error.condition, data.error.message);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
240 origin.send(stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
241 return true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
242 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
243 cmdtag = command:cmdtag("executing", sessionid);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
244 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
245
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
246 for name, content in pairs(data) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
247 if name == "info" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
248 cmdtag:tag("note", {type="info"}):text(content):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
249 elseif name == "warn" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
250 cmdtag:tag("note", {type="warn"}):text(content):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
251 elseif name == "error" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
252 cmdtag:tag("note", {type="error"}):text(content.message):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
253 elseif name =="actions" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
254 local actions = st.stanza("actions");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
255 for _, action in ipairs(content) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
256 if (action == "prev") or (action == "next") or (action == "complete") then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
257 actions:tag(action):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
258 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
259 module:log("error", 'Command "'..command.name..
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
260 '" at node "'..command.node..'" provided an invalid action "'..action..'"');
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
261 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
262 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
263 cmdtag:add_child(actions);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
264 elseif name == "form" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
265 cmdtag:add_child((content.layout or content):form(content.values));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
266 elseif name == "result" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
267 cmdtag:add_child((content.layout or content):form(content.values, "result"));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
268 elseif name == "other" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
269 cmdtag:add_child(content);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
270 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
271 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
272 stanza:add_child(cmdtag);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
273 origin.send(stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
274
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
275 return true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
276 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
277
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
278 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
279 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
280 package.preload['util.stanza'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
281 -- Prosody IM
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
282 -- Copyright (C) 2008-2010 Matthew Wild
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
283 -- Copyright (C) 2008-2010 Waqas Hussain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
284 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
285 -- This project is MIT/X11 licensed. Please see the
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
286 -- COPYING file in the source package for more information.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
287 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
288
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
289
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
290 local t_insert = table.insert;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
291 local t_concat = table.concat;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
292 local t_remove = table.remove;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
293 local t_concat = table.concat;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
294 local s_format = string.format;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
295 local s_match = string.match;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
296 local tostring = tostring;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
297 local setmetatable = setmetatable;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
298 local getmetatable = getmetatable;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
299 local pairs = pairs;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
300 local ipairs = ipairs;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
301 local type = type;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
302 local next = next;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
303 local print = print;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
304 local unpack = unpack;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
305 local s_gsub = string.gsub;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
306 local s_char = string.char;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
307 local s_find = string.find;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
308 local os = os;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
309
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
310 local do_pretty_printing = not os.getenv("WINDIR");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
311 local getstyle, getstring;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
312 if do_pretty_printing then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
313 local ok, termcolours = pcall(require, "util.termcolours");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
314 if ok then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
315 getstyle, getstring = termcolours.getstyle, termcolours.getstring;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
316 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
317 do_pretty_printing = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
318 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
319 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
320
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
321 local xmlns_stanzas = "urn:ietf:params:xml:ns:xmpp-stanzas";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
322
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
323 module "stanza"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
324
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
325 stanza_mt = { __type = "stanza" };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
326 stanza_mt.__index = stanza_mt;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
327 local stanza_mt = stanza_mt;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
328
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
329 function stanza(name, attr)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
330 local stanza = { name = name, attr = attr or {}, tags = {} };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
331 return setmetatable(stanza, stanza_mt);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
332 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
333 local stanza = stanza;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
334
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
335 function stanza_mt:query(xmlns)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
336 return self:tag("query", { xmlns = xmlns });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
337 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
338
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
339 function stanza_mt:body(text, attr)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
340 return self:tag("body", attr):text(text);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
341 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
342
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
343 function stanza_mt:tag(name, attrs)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
344 local s = stanza(name, attrs);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
345 local last_add = self.last_add;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
346 if not last_add then last_add = {}; self.last_add = last_add; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
347 (last_add[#last_add] or self):add_direct_child(s);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
348 t_insert(last_add, s);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
349 return self;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
350 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
351
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
352 function stanza_mt:text(text)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
353 local last_add = self.last_add;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
354 (last_add and last_add[#last_add] or self):add_direct_child(text);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
355 return self;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
356 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
357
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
358 function stanza_mt:up()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
359 local last_add = self.last_add;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
360 if last_add then t_remove(last_add); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
361 return self;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
362 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
363
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
364 function stanza_mt:reset()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
365 self.last_add = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
366 return self;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
367 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
368
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
369 function stanza_mt:add_direct_child(child)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
370 if type(child) == "table" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
371 t_insert(self.tags, child);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
372 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
373 t_insert(self, child);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
374 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
375
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
376 function stanza_mt:add_child(child)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
377 local last_add = self.last_add;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
378 (last_add and last_add[#last_add] or self):add_direct_child(child);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
379 return self;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
380 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
381
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
382 function stanza_mt:get_child(name, xmlns)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
383 for _, child in ipairs(self.tags) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
384 if (not name or child.name == name)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
385 and ((not xmlns and self.attr.xmlns == child.attr.xmlns)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
386 or child.attr.xmlns == xmlns) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
387
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
388 return child;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
389 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
390 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
391 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
392
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
393 function stanza_mt:get_child_text(name, xmlns)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
394 local tag = self:get_child(name, xmlns);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
395 if tag then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
396 return tag:get_text();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
397 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
398 return nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
399 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
400
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
401 function stanza_mt:child_with_name(name)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
402 for _, child in ipairs(self.tags) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
403 if child.name == name then return child; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
404 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
405 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
406
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
407 function stanza_mt:child_with_ns(ns)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
408 for _, child in ipairs(self.tags) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
409 if child.attr.xmlns == ns then return child; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
410 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
411 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
412
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
413 function stanza_mt:children()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
414 local i = 0;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
415 return function (a)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
416 i = i + 1
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
417 return a[i];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
418 end, self, i;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
419 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
420
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
421 function stanza_mt:childtags(name, xmlns)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
422 xmlns = xmlns or self.attr.xmlns;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
423 local tags = self.tags;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
424 local start_i, max_i = 1, #tags;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
425 return function ()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
426 for i = start_i, max_i do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
427 local v = tags[i];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
428 if (not name or v.name == name)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
429 and (not xmlns or xmlns == v.attr.xmlns) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
430 start_i = i+1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
431 return v;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
432 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
433 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
434 end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
435 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
436
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
437 function stanza_mt:maptags(callback)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
438 local tags, curr_tag = self.tags, 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
439 local n_children, n_tags = #self, #tags;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
440
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
441 local i = 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
442 while curr_tag <= n_tags do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
443 if self[i] == tags[curr_tag] then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
444 local ret = callback(self[i]);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
445 if ret == nil then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
446 t_remove(self, i);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
447 t_remove(tags, curr_tag);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
448 n_children = n_children - 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
449 n_tags = n_tags - 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
450 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
451 self[i] = ret;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
452 tags[i] = ret;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
453 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
454 i = i + 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
455 curr_tag = curr_tag + 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
456 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
457 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
458 return self;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
459 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
460
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
461 local xml_escape
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
462 do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
463 local escape_table = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
464 function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
465 _M.xml_escape = xml_escape;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
466 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
467
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
468 local function _dostring(t, buf, self, xml_escape, parentns)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
469 local nsid = 0;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
470 local name = t.name
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
471 t_insert(buf, "<"..name);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
472 for k, v in pairs(t.attr) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
473 if s_find(k, "\1", 1, true) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
474 local ns, attrk = s_match(k, "^([^\1]*)\1?(.*)$");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
475 nsid = nsid + 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
476 t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
477 elseif not(k == "xmlns" and v == parentns) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
478 t_insert(buf, " "..k.."='"..xml_escape(v).."'");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
479 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
480 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
481 local len = #t;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
482 if len == 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
483 t_insert(buf, "/>");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
484 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
485 t_insert(buf, ">");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
486 for n=1,len do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
487 local child = t[n];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
488 if child.name then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
489 self(child, buf, self, xml_escape, t.attr.xmlns);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
490 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
491 t_insert(buf, xml_escape(child));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
492 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
493 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
494 t_insert(buf, "</"..name..">");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
495 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
496 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
497 function stanza_mt.__tostring(t)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
498 local buf = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
499 _dostring(t, buf, _dostring, xml_escape, nil);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
500 return t_concat(buf);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
501 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
502
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
503 function stanza_mt.top_tag(t)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
504 local attr_string = "";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
505 if t.attr then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
506 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(" %s='%s'", k, xml_escape(tostring(v))); end end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
507 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
508 return s_format("<%s%s>", t.name, attr_string);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
509 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
510
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
511 function stanza_mt.get_text(t)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
512 if #t.tags == 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
513 return t_concat(t);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
514 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
515 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
516
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
517 function stanza_mt.get_error(stanza)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
518 local type, condition, text;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
519
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
520 local error_tag = stanza:get_child("error");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
521 if not error_tag then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
522 return nil, nil, nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
523 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
524 type = error_tag.attr.type;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
525
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
526 for child in error_tag:childtags() do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
527 if child.attr.xmlns == xmlns_stanzas then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
528 if not text and child.name == "text" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
529 text = child:get_text();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
530 elseif not condition then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
531 condition = child.name;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
532 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
533 if condition and text then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
534 break;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
535 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
536 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
537 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
538 return type, condition or "undefined-condition", text;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
539 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
540
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
541 function stanza_mt.__add(s1, s2)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
542 return s1:add_direct_child(s2);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
543 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
544
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
545
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
546 do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
547 local id = 0;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
548 function new_id()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
549 id = id + 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
550 return "lx"..id;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
551 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
552 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
553
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
554 function preserialize(stanza)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
555 local s = { name = stanza.name, attr = stanza.attr };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
556 for _, child in ipairs(stanza) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
557 if type(child) == "table" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
558 t_insert(s, preserialize(child));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
559 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
560 t_insert(s, child);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
561 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
562 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
563 return s;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
564 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
565
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
566 function deserialize(stanza)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
567 -- Set metatable
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
568 if stanza then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
569 local attr = stanza.attr;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
570 for i=1,#attr do attr[i] = nil; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
571 local attrx = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
572 for att in pairs(attr) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
573 if s_find(att, "|", 1, true) and not s_find(att, "\1", 1, true) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
574 local ns,na = s_match(att, "^([^|]+)|(.+)$");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
575 attrx[ns.."\1"..na] = attr[att];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
576 attr[att] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
577 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
578 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
579 for a,v in pairs(attrx) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
580 attr[a] = v;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
581 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
582 setmetatable(stanza, stanza_mt);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
583 for _, child in ipairs(stanza) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
584 if type(child) == "table" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
585 deserialize(child);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
586 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
587 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
588 if not stanza.tags then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
589 -- Rebuild tags
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
590 local tags = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
591 for _, child in ipairs(stanza) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
592 if type(child) == "table" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
593 t_insert(tags, child);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
594 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
595 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
596 stanza.tags = tags;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
597 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
598 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
599
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
600 return stanza;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
601 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
602
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
603 local function _clone(stanza)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
604 local attr, tags = {}, {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
605 for k,v in pairs(stanza.attr) do attr[k] = v; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
606 local new = { name = stanza.name, attr = attr, tags = tags };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
607 for i=1,#stanza do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
608 local child = stanza[i];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
609 if child.name then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
610 child = _clone(child);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
611 t_insert(tags, child);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
612 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
613 t_insert(new, child);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
614 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
615 return setmetatable(new, stanza_mt);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
616 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
617 clone = _clone;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
618
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
619 function message(attr, body)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
620 if not body then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
621 return stanza("message", attr);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
622 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
623 return stanza("message", attr):tag("body"):text(body):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
624 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
625 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
626 function iq(attr)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
627 if attr and not attr.id then attr.id = new_id(); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
628 return stanza("iq", attr or { id = new_id() });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
629 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
630
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
631 function reply(orig)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
632 return stanza(orig.name, orig.attr and { to = orig.attr.from, from = orig.attr.to, id = orig.attr.id, type = ((orig.name == "iq" and "result") or orig.attr.type) });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
633 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
634
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
635 do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
636 local xmpp_stanzas_attr = { xmlns = xmlns_stanzas };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
637 function error_reply(orig, type, condition, message)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
638 local t = reply(orig);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
639 t.attr.type = "error";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
640 t:tag("error", {type = type}) --COMPAT: Some day xmlns:stanzas goes here
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
641 :tag(condition, xmpp_stanzas_attr):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
642 if (message) then t:tag("text", xmpp_stanzas_attr):text(message):up(); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
643 return t; -- stanza ready for adding app-specific errors
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
644 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
645 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
646
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
647 function presence(attr)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
648 return stanza("presence", attr);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
649 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
650
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
651 if do_pretty_printing then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
652 local style_attrk = getstyle("yellow");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
653 local style_attrv = getstyle("red");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
654 local style_tagname = getstyle("red");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
655 local style_punc = getstyle("magenta");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
656
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
657 local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
658 local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
659 --local tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">").."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
660 local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
661 function stanza_mt.pretty_print(t)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
662 local children_text = "";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
663 for n, child in ipairs(t) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
664 if type(child) == "string" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
665 children_text = children_text .. xml_escape(child);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
666 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
667 children_text = children_text .. child:pretty_print();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
668 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
669 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
670
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
671 local attr_string = "";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
672 if t.attr then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
673 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
674 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
675 return s_format(tag_format, t.name, attr_string, children_text, t.name);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
676 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
677
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
678 function stanza_mt.pretty_top_tag(t)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
679 local attr_string = "";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
680 if t.attr then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
681 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
682 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
683 return s_format(top_tag_format, t.name, attr_string);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
684 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
685 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
686 -- Sorry, fresh out of colours for you guys ;)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
687 stanza_mt.pretty_print = stanza_mt.__tostring;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
688 stanza_mt.pretty_top_tag = stanza_mt.top_tag;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
689 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
690
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
691 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
692 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
693 package.preload['util.timer'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
694 -- Prosody IM
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
695 -- Copyright (C) 2008-2010 Matthew Wild
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
696 -- Copyright (C) 2008-2010 Waqas Hussain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
697 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
698 -- This project is MIT/X11 licensed. Please see the
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
699 -- COPYING file in the source package for more information.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
700 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
701
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
702
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
703 local ns_addtimer = require "net.server".addtimer;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
704 local event = require "net.server".event;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
705 local event_base = require "net.server".event_base;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
706
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
707 local math_min = math.min
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
708 local math_huge = math.huge
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
709 local get_time = require "socket".gettime;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
710 local t_insert = table.insert;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
711 local t_remove = table.remove;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
712 local ipairs, pairs = ipairs, pairs;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
713 local type = type;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
714
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
715 local data = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
716 local new_data = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
717
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
718 module "timer"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
719
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
720 local _add_task;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
721 if not event then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
722 function _add_task(delay, func)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
723 local current_time = get_time();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
724 delay = delay + current_time;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
725 if delay >= current_time then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
726 t_insert(new_data, {delay, func});
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
727 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
728 func();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
729 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
730 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
731
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
732 ns_addtimer(function()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
733 local current_time = get_time();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
734 if #new_data > 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
735 for _, d in pairs(new_data) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
736 t_insert(data, d);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
737 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
738 new_data = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
739 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
740
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
741 local next_time = math_huge;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
742 for i, d in pairs(data) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
743 local t, func = d[1], d[2];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
744 if t <= current_time then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
745 data[i] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
746 local r = func(current_time);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
747 if type(r) == "number" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
748 _add_task(r, func);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
749 next_time = math_min(next_time, r);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
750 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
751 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
752 next_time = math_min(next_time, t - current_time);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
753 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
754 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
755 return next_time;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
756 end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
757 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
758 local EVENT_LEAVE = (event.core and event.core.LEAVE) or -1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
759 function _add_task(delay, func)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
760 local event_handle;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
761 event_handle = event_base:addevent(nil, 0, function ()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
762 local ret = func();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
763 if ret then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
764 return 0, ret;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
765 elseif event_handle then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
766 return EVENT_LEAVE;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
767 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
768 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
769 , delay);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
770 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
771 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
772
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
773 add_task = _add_task;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
774
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
775 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
776 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
777 package.preload['util.termcolours'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
778 -- Prosody IM
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
779 -- Copyright (C) 2008-2010 Matthew Wild
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
780 -- Copyright (C) 2008-2010 Waqas Hussain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
781 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
782 -- This project is MIT/X11 licensed. Please see the
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
783 -- COPYING file in the source package for more information.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
784 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
785
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
786
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
787 local t_concat, t_insert = table.concat, table.insert;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
788 local char, format = string.char, string.format;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
789 local ipairs = ipairs;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
790 local io_write = io.write;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
791
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
792 local windows;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
793 if os.getenv("WINDIR") then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
794 windows = require "util.windows";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
795 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
796 local orig_color = windows and windows.get_consolecolor and windows.get_consolecolor();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
797
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
798 module "termcolours"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
799
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
800 local stylemap = {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
801 reset = 0; bright = 1, dim = 2, underscore = 4, blink = 5, reverse = 7, hidden = 8;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
802 black = 30; red = 31; green = 32; yellow = 33; blue = 34; magenta = 35; cyan = 36; white = 37;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
803 ["black background"] = 40; ["red background"] = 41; ["green background"] = 42; ["yellow background"] = 43; ["blue background"] = 44; ["magenta background"] = 45; ["cyan background"] = 46; ["white background"] = 47;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
804 bold = 1, dark = 2, underline = 4, underlined = 4, normal = 0;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
805 }
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
806
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
807 local winstylemap = {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
808 ["0"] = orig_color, -- reset
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
809 ["1"] = 7+8, -- bold
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
810 ["1;33"] = 2+4+8, -- bold yellow
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
811 ["1;31"] = 4+8 -- bold red
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
812 }
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
813
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
814 local fmt_string = char(0x1B).."[%sm%s"..char(0x1B).."[0m";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
815 function getstring(style, text)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
816 if style then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
817 return format(fmt_string, style, text);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
818 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
819 return text;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
820 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
821 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
822
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
823 function getstyle(...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
824 local styles, result = { ... }, {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
825 for i, style in ipairs(styles) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
826 style = stylemap[style];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
827 if style then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
828 t_insert(result, style);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
829 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
830 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
831 return t_concat(result, ";");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
832 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
833
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
834 local last = "0";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
835 function setstyle(style)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
836 style = style or "0";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
837 if style ~= last then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
838 io_write("\27["..style.."m");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
839 last = style;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
840 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
841 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
842
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
843 if windows then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
844 function setstyle(style)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
845 style = style or "0";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
846 if style ~= last then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
847 windows.set_consolecolor(winstylemap[style] or orig_color);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
848 last = style;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
849 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
850 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
851 if not orig_color then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
852 function setstyle(style) end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
853 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
854 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
855
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
856 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
857 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
858 package.preload['util.uuid'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
859 -- Prosody IM
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
860 -- Copyright (C) 2008-2010 Matthew Wild
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
861 -- Copyright (C) 2008-2010 Waqas Hussain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
862 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
863 -- This project is MIT/X11 licensed. Please see the
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
864 -- COPYING file in the source package for more information.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
865 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
866
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
867
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
868 local m_random = math.random;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
869 local tostring = tostring;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
870 local os_time = os.time;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
871 local os_clock = os.clock;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
872 local sha1 = require "util.hashes".sha1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
873
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
874 module "uuid"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
875
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
876 local last_uniq_time = 0;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
877 local function uniq_time()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
878 local new_uniq_time = os_time();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
879 if last_uniq_time >= new_uniq_time then new_uniq_time = last_uniq_time + 1; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
880 last_uniq_time = new_uniq_time;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
881 return new_uniq_time;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
882 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
883
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
884 local function new_random(x)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
885 return sha1(x..os_clock()..tostring({}), true);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
886 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
887
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
888 local buffer = new_random(uniq_time());
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
889 local function _seed(x)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
890 buffer = new_random(buffer..x);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
891 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
892 local function get_nibbles(n)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
893 if #buffer < n then _seed(uniq_time()); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
894 local r = buffer:sub(0, n);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
895 buffer = buffer:sub(n+1);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
896 return r;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
897 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
898 local function get_twobits()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
899 return ("%x"):format(get_nibbles(1):byte() % 4 + 8);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
900 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
901
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
902 function generate()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
903 -- generate RFC 4122 complaint UUIDs (version 4 - random)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
904 return get_nibbles(8).."-"..get_nibbles(4).."-4"..get_nibbles(3).."-"..(get_twobits())..get_nibbles(3).."-"..get_nibbles(12);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
905 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
906 seed = _seed;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
907
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
908 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
909 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
910 package.preload['net.dns'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
911 -- Prosody IM
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
912 -- This file is included with Prosody IM. It has modifications,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
913 -- which are hereby placed in the public domain.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
914
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
915
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
916 -- todo: quick (default) header generation
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
917 -- todo: nxdomain, error handling
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
918 -- todo: cache results of encodeName
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
919
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
920
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
921 -- reference: http://tools.ietf.org/html/rfc1035
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
922 -- reference: http://tools.ietf.org/html/rfc1876 (LOC)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
923
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
924
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
925 local socket = require "socket";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
926 local timer = require "util.timer";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
927
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
928 local _, windows = pcall(require, "util.windows");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
929 local is_windows = (_ and windows) or os.getenv("WINDIR");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
930
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
931 local coroutine, io, math, string, table =
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
932 coroutine, io, math, string, table;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
933
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
934 local ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack, select, type=
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
935 ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack, select, type;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
936
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
937 local ztact = { -- public domain 20080404 lua@ztact.com
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
938 get = function(parent, ...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
939 local len = select('#', ...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
940 for i=1,len do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
941 parent = parent[select(i, ...)];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
942 if parent == nil then break; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
943 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
944 return parent;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
945 end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
946 set = function(parent, ...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
947 local len = select('#', ...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
948 local key, value = select(len-1, ...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
949 local cutpoint, cutkey;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
950
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
951 for i=1,len-2 do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
952 local key = select (i, ...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
953 local child = parent[key]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
954
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
955 if value == nil then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
956 if child == nil then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
957 return;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
958 elseif next(child, next(child)) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
959 cutpoint = nil; cutkey = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
960 elseif cutpoint == nil then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
961 cutpoint = parent; cutkey = key;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
962 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
963 elseif child == nil then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
964 child = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
965 parent[key] = child;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
966 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
967 parent = child
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
968 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
969
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
970 if value == nil and cutpoint then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
971 cutpoint[cutkey] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
972 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
973 parent[key] = value;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
974 return value;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
975 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
976 end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
977 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
978 local get, set = ztact.get, ztact.set;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
979
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
980 local default_timeout = 15;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
981
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
982 -------------------------------------------------- module dns
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
983 module('dns')
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
984 local dns = _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
985
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
986
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
987 -- dns type & class codes ------------------------------ dns type & class codes
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
988
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
989
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
990 local append = table.insert
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
991
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
992
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
993 local function highbyte(i) -- - - - - - - - - - - - - - - - - - - highbyte
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
994 return (i-(i%0x100))/0x100;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
995 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
996
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
997
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
998 local function augment (t) -- - - - - - - - - - - - - - - - - - - - augment
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
999 local a = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1000 for i,s in pairs(t) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1001 a[i] = s;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1002 a[s] = s;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1003 a[string.lower(s)] = s;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1004 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1005 return a;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1006 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1007
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1008
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1009 local function encode (t) -- - - - - - - - - - - - - - - - - - - - - encode
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1010 local code = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1011 for i,s in pairs(t) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1012 local word = string.char(highbyte(i), i%0x100);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1013 code[i] = word;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1014 code[s] = word;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1015 code[string.lower(s)] = word;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1016 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1017 return code;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1018 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1019
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1020
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1021 dns.types = {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1022 'A', 'NS', 'MD', 'MF', 'CNAME', 'SOA', 'MB', 'MG', 'MR', 'NULL', 'WKS',
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1023 'PTR', 'HINFO', 'MINFO', 'MX', 'TXT',
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1024 [ 28] = 'AAAA', [ 29] = 'LOC', [ 33] = 'SRV',
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1025 [252] = 'AXFR', [253] = 'MAILB', [254] = 'MAILA', [255] = '*' };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1026
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1027
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1028 dns.classes = { 'IN', 'CS', 'CH', 'HS', [255] = '*' };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1029
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1030
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1031 dns.type = augment (dns.types);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1032 dns.class = augment (dns.classes);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1033 dns.typecode = encode (dns.types);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1034 dns.classcode = encode (dns.classes);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1035
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1036
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1037
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1038 local function standardize(qname, qtype, qclass) -- - - - - - - standardize
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1039 if string.byte(qname, -1) ~= 0x2E then qname = qname..'.'; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1040 qname = string.lower(qname);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1041 return qname, dns.type[qtype or 'A'], dns.class[qclass or 'IN'];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1042 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1043
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1044
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1045 local function prune(rrs, time, soft) -- - - - - - - - - - - - - - - prune
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1046 time = time or socket.gettime();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1047 for i,rr in pairs(rrs) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1048 if rr.tod then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1049 -- rr.tod = rr.tod - 50 -- accelerated decripitude
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1050 rr.ttl = math.floor(rr.tod - time);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1051 if rr.ttl <= 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1052 table.remove(rrs, i);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1053 return prune(rrs, time, soft); -- Re-iterate
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1054 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1055 elseif soft == 'soft' then -- What is this? I forget!
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1056 assert(rr.ttl == 0);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1057 rrs[i] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1058 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1059 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1060 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1061
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1062
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1063 -- metatables & co. ------------------------------------------ metatables & co.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1064
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1065
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1066 local resolver = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1067 resolver.__index = resolver;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1068
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1069 resolver.timeout = default_timeout;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1070
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1071 local function default_rr_tostring(rr)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1072 local rr_val = rr.type and rr[rr.type:lower()];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1073 if type(rr_val) ~= "string" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1074 return "<UNKNOWN RDATA TYPE>";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1075 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1076 return rr_val;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1077 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1078
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1079 local special_tostrings = {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1080 LOC = resolver.LOC_tostring;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1081 MX = function (rr)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1082 return string.format('%2i %s', rr.pref, rr.mx);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1083 end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1084 SRV = function (rr)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1085 local s = rr.srv;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1086 return string.format('%5d %5d %5d %s', s.priority, s.weight, s.port, s.target);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1087 end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1088 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1089
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1090 local rr_metatable = {}; -- - - - - - - - - - - - - - - - - - - rr_metatable
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1091 function rr_metatable.__tostring(rr)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1092 local rr_string = (special_tostrings[rr.type] or default_rr_tostring)(rr);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1093 return string.format('%2s %-5s %6i %-28s %s', rr.class, rr.type, rr.ttl, rr.name, rr_string);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1094 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1095
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1096
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1097 local rrs_metatable = {}; -- - - - - - - - - - - - - - - - - - rrs_metatable
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1098 function rrs_metatable.__tostring(rrs)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1099 local t = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1100 for i,rr in pairs(rrs) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1101 append(t, tostring(rr)..'\n');
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1102 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1103 return table.concat(t);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1104 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1105
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1106
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1107 local cache_metatable = {}; -- - - - - - - - - - - - - - - - cache_metatable
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1108 function cache_metatable.__tostring(cache)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1109 local time = socket.gettime();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1110 local t = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1111 for class,types in pairs(cache) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1112 for type,names in pairs(types) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1113 for name,rrs in pairs(names) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1114 prune(rrs, time);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1115 append(t, tostring(rrs));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1116 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1117 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1118 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1119 return table.concat(t);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1120 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1121
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1122
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1123 function resolver:new() -- - - - - - - - - - - - - - - - - - - - - resolver
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1124 local r = { active = {}, cache = {}, unsorted = {} };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1125 setmetatable(r, resolver);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1126 setmetatable(r.cache, cache_metatable);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1127 setmetatable(r.unsorted, { __mode = 'kv' });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1128 return r;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1129 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1130
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1131
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1132 -- packet layer -------------------------------------------------- packet layer
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1133
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1134
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1135 function dns.random(...) -- - - - - - - - - - - - - - - - - - - dns.random
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1136 math.randomseed(math.floor(10000*socket.gettime()));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1137 dns.random = math.random;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1138 return dns.random(...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1139 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1140
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1141
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1142 local function encodeHeader(o) -- - - - - - - - - - - - - - - encodeHeader
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1143 o = o or {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1144 o.id = o.id or dns.random(0, 0xffff); -- 16b (random) id
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1145
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1146 o.rd = o.rd or 1; -- 1b 1 recursion desired
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1147 o.tc = o.tc or 0; -- 1b 1 truncated response
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1148 o.aa = o.aa or 0; -- 1b 1 authoritative response
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1149 o.opcode = o.opcode or 0; -- 4b 0 query
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1150 -- 1 inverse query
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1151 -- 2 server status request
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1152 -- 3-15 reserved
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1153 o.qr = o.qr or 0; -- 1b 0 query, 1 response
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1154
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1155 o.rcode = o.rcode or 0; -- 4b 0 no error
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1156 -- 1 format error
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1157 -- 2 server failure
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1158 -- 3 name error
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1159 -- 4 not implemented
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1160 -- 5 refused
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1161 -- 6-15 reserved
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1162 o.z = o.z or 0; -- 3b 0 resvered
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1163 o.ra = o.ra or 0; -- 1b 1 recursion available
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1164
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1165 o.qdcount = o.qdcount or 1; -- 16b number of question RRs
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1166 o.ancount = o.ancount or 0; -- 16b number of answers RRs
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1167 o.nscount = o.nscount or 0; -- 16b number of nameservers RRs
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1168 o.arcount = o.arcount or 0; -- 16b number of additional RRs
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1169
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1170 -- string.char() rounds, so prevent roundup with -0.4999
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1171 local header = string.char(
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1172 highbyte(o.id), o.id %0x100,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1173 o.rd + 2*o.tc + 4*o.aa + 8*o.opcode + 128*o.qr,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1174 o.rcode + 16*o.z + 128*o.ra,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1175 highbyte(o.qdcount), o.qdcount %0x100,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1176 highbyte(o.ancount), o.ancount %0x100,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1177 highbyte(o.nscount), o.nscount %0x100,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1178 highbyte(o.arcount), o.arcount %0x100
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1179 );
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1180
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1181 return header, o.id;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1182 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1183
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1184
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1185 local function encodeName(name) -- - - - - - - - - - - - - - - - encodeName
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1186 local t = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1187 for part in string.gmatch(name, '[^.]+') do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1188 append(t, string.char(string.len(part)));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1189 append(t, part);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1190 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1191 append(t, string.char(0));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1192 return table.concat(t);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1193 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1194
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1195
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1196 local function encodeQuestion(qname, qtype, qclass) -- - - - encodeQuestion
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1197 qname = encodeName(qname);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1198 qtype = dns.typecode[qtype or 'a'];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1199 qclass = dns.classcode[qclass or 'in'];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1200 return qname..qtype..qclass;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1201 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1202
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1203
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1204 function resolver:byte(len) -- - - - - - - - - - - - - - - - - - - - - byte
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1205 len = len or 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1206 local offset = self.offset;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1207 local last = offset + len - 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1208 if last > #self.packet then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1209 error(string.format('out of bounds: %i>%i', last, #self.packet));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1210 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1211 self.offset = offset + len;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1212 return string.byte(self.packet, offset, last);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1213 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1214
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1215
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1216 function resolver:word() -- - - - - - - - - - - - - - - - - - - - - - word
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1217 local b1, b2 = self:byte(2);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1218 return 0x100*b1 + b2;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1219 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1220
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1221
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1222 function resolver:dword () -- - - - - - - - - - - - - - - - - - - - - dword
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1223 local b1, b2, b3, b4 = self:byte(4);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1224 --print('dword', b1, b2, b3, b4);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1225 return 0x1000000*b1 + 0x10000*b2 + 0x100*b3 + b4;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1226 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1227
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1228
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1229 function resolver:sub(len) -- - - - - - - - - - - - - - - - - - - - - - sub
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1230 len = len or 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1231 local s = string.sub(self.packet, self.offset, self.offset + len - 1);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1232 self.offset = self.offset + len;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1233 return s;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1234 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1235
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1236
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1237 function resolver:header(force) -- - - - - - - - - - - - - - - - - - header
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1238 local id = self:word();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1239 --print(string.format(':header id %x', id));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1240 if not self.active[id] and not force then return nil; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1241
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1242 local h = { id = id };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1243
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1244 local b1, b2 = self:byte(2);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1245
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1246 h.rd = b1 %2;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1247 h.tc = b1 /2%2;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1248 h.aa = b1 /4%2;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1249 h.opcode = b1 /8%16;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1250 h.qr = b1 /128;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1251
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1252 h.rcode = b2 %16;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1253 h.z = b2 /16%8;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1254 h.ra = b2 /128;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1255
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1256 h.qdcount = self:word();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1257 h.ancount = self:word();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1258 h.nscount = self:word();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1259 h.arcount = self:word();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1260
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1261 for k,v in pairs(h) do h[k] = v-v%1; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1262
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1263 return h;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1264 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1265
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1266
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1267 function resolver:name() -- - - - - - - - - - - - - - - - - - - - - - name
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1268 local remember, pointers = nil, 0;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1269 local len = self:byte();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1270 local n = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1271 while len > 0 do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1272 if len >= 0xc0 then -- name is "compressed"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1273 pointers = pointers + 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1274 if pointers >= 20 then error('dns error: 20 pointers'); end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1275 local offset = ((len-0xc0)*0x100) + self:byte();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1276 remember = remember or self.offset;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1277 self.offset = offset + 1; -- +1 for lua
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1278 else -- name is not compressed
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1279 append(n, self:sub(len)..'.');
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1280 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1281 len = self:byte();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1282 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1283 self.offset = remember or self.offset;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1284 return table.concat(n);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1285 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1286
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1287
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1288 function resolver:question() -- - - - - - - - - - - - - - - - - - question
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1289 local q = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1290 q.name = self:name();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1291 q.type = dns.type[self:word()];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1292 q.class = dns.class[self:word()];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1293 return q;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1294 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1295
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1296
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1297 function resolver:A(rr) -- - - - - - - - - - - - - - - - - - - - - - - - A
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1298 local b1, b2, b3, b4 = self:byte(4);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1299 rr.a = string.format('%i.%i.%i.%i', b1, b2, b3, b4);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1300 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1301
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1302 function resolver:AAAA(rr)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1303 local addr = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1304 for i = 1, rr.rdlength, 2 do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1305 local b1, b2 = self:byte(2);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1306 table.insert(addr, ("%02x%02x"):format(b1, b2));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1307 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1308 rr.aaaa = table.concat(addr, ":");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1309 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1310
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1311 function resolver:CNAME(rr) -- - - - - - - - - - - - - - - - - - - - CNAME
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1312 rr.cname = self:name();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1313 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1314
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1315
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1316 function resolver:MX(rr) -- - - - - - - - - - - - - - - - - - - - - - - MX
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1317 rr.pref = self:word();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1318 rr.mx = self:name();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1319 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1320
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1321
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1322 function resolver:LOC_nibble_power() -- - - - - - - - - - LOC_nibble_power
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1323 local b = self:byte();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1324 --print('nibbles', ((b-(b%0x10))/0x10), (b%0x10));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1325 return ((b-(b%0x10))/0x10) * (10^(b%0x10));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1326 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1327
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1328
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1329 function resolver:LOC(rr) -- - - - - - - - - - - - - - - - - - - - - - LOC
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1330 rr.version = self:byte();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1331 if rr.version == 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1332 rr.loc = rr.loc or {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1333 rr.loc.size = self:LOC_nibble_power();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1334 rr.loc.horiz_pre = self:LOC_nibble_power();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1335 rr.loc.vert_pre = self:LOC_nibble_power();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1336 rr.loc.latitude = self:dword();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1337 rr.loc.longitude = self:dword();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1338 rr.loc.altitude = self:dword();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1339 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1340 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1341
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1342
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1343 local function LOC_tostring_degrees(f, pos, neg) -- - - - - - - - - - - - -
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1344 f = f - 0x80000000;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1345 if f < 0 then pos = neg; f = -f; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1346 local deg, min, msec;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1347 msec = f%60000;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1348 f = (f-msec)/60000;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1349 min = f%60;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1350 deg = (f-min)/60;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1351 return string.format('%3d %2d %2.3f %s', deg, min, msec/1000, pos);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1352 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1353
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1354
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1355 function resolver.LOC_tostring(rr) -- - - - - - - - - - - - - LOC_tostring
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1356 local t = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1357
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1358 --[[
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1359 for k,name in pairs { 'size', 'horiz_pre', 'vert_pre', 'latitude', 'longitude', 'altitude' } do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1360 append(t, string.format('%4s%-10s: %12.0f\n', '', name, rr.loc[name]));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1361 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1362 --]]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1363
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1364 append(t, string.format(
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1365 '%s %s %.2fm %.2fm %.2fm %.2fm',
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1366 LOC_tostring_degrees (rr.loc.latitude, 'N', 'S'),
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1367 LOC_tostring_degrees (rr.loc.longitude, 'E', 'W'),
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1368 (rr.loc.altitude - 10000000) / 100,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1369 rr.loc.size / 100,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1370 rr.loc.horiz_pre / 100,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1371 rr.loc.vert_pre / 100
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1372 ));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1373
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1374 return table.concat(t);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1375 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1376
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1377
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1378 function resolver:NS(rr) -- - - - - - - - - - - - - - - - - - - - - - - NS
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1379 rr.ns = self:name();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1380 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1381
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1382
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1383 function resolver:SOA(rr) -- - - - - - - - - - - - - - - - - - - - - - SOA
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1384 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1385
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1386
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1387 function resolver:SRV(rr) -- - - - - - - - - - - - - - - - - - - - - - SRV
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1388 rr.srv = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1389 rr.srv.priority = self:word();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1390 rr.srv.weight = self:word();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1391 rr.srv.port = self:word();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1392 rr.srv.target = self:name();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1393 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1394
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1395 function resolver:PTR(rr)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1396 rr.ptr = self:name();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1397 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1398
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1399 function resolver:TXT(rr) -- - - - - - - - - - - - - - - - - - - - - - TXT
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1400 rr.txt = self:sub (self:byte());
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1401 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1402
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1403
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1404 function resolver:rr() -- - - - - - - - - - - - - - - - - - - - - - - - rr
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1405 local rr = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1406 setmetatable(rr, rr_metatable);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1407 rr.name = self:name(self);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1408 rr.type = dns.type[self:word()] or rr.type;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1409 rr.class = dns.class[self:word()] or rr.class;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1410 rr.ttl = 0x10000*self:word() + self:word();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1411 rr.rdlength = self:word();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1412
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1413 if rr.ttl <= 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1414 rr.tod = self.time + 30;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1415 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1416 rr.tod = self.time + rr.ttl;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1417 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1418
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1419 local remember = self.offset;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1420 local rr_parser = self[dns.type[rr.type]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1421 if rr_parser then rr_parser(self, rr); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1422 self.offset = remember;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1423 rr.rdata = self:sub(rr.rdlength);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1424 return rr;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1425 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1426
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1427
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1428 function resolver:rrs (count) -- - - - - - - - - - - - - - - - - - - - - rrs
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1429 local rrs = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1430 for i = 1,count do append(rrs, self:rr()); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1431 return rrs;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1432 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1433
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1434
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1435 function resolver:decode(packet, force) -- - - - - - - - - - - - - - decode
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1436 self.packet, self.offset = packet, 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1437 local header = self:header(force);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1438 if not header then return nil; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1439 local response = { header = header };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1440
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1441 response.question = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1442 local offset = self.offset;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1443 for i = 1,response.header.qdcount do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1444 append(response.question, self:question());
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1445 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1446 response.question.raw = string.sub(self.packet, offset, self.offset - 1);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1447
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1448 if not force then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1449 if not self.active[response.header.id] or not self.active[response.header.id][response.question.raw] then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1450 return nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1451 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1452 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1453
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1454 response.answer = self:rrs(response.header.ancount);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1455 response.authority = self:rrs(response.header.nscount);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1456 response.additional = self:rrs(response.header.arcount);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1457
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1458 return response;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1459 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1460
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1461
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1462 -- socket layer -------------------------------------------------- socket layer
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1463
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1464
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1465 resolver.delays = { 1, 3 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1466
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1467
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1468 function resolver:addnameserver(address) -- - - - - - - - - - addnameserver
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1469 self.server = self.server or {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1470 append(self.server, address);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1471 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1472
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1473
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1474 function resolver:setnameserver(address) -- - - - - - - - - - setnameserver
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1475 self.server = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1476 self:addnameserver(address);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1477 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1478
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1479
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1480 function resolver:adddefaultnameservers() -- - - - - adddefaultnameservers
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1481 if is_windows then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1482 if windows and windows.get_nameservers then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1483 for _, server in ipairs(windows.get_nameservers()) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1484 self:addnameserver(server);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1485 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1486 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1487 if not self.server or #self.server == 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1488 -- TODO log warning about no nameservers, adding opendns servers as fallback
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1489 self:addnameserver("208.67.222.222");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1490 self:addnameserver("208.67.220.220");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1491 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1492 else -- posix
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1493 local resolv_conf = io.open("/etc/resolv.conf");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1494 if resolv_conf then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1495 for line in resolv_conf:lines() do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1496 line = line:gsub("#.*$", "")
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1497 :match('^%s*nameserver%s+(.*)%s*$');
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1498 if line then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1499 line:gsub("%f[%d.](%d+%.%d+%.%d+%.%d+)%f[^%d.]", function (address)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1500 self:addnameserver(address)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1501 end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1502 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1503 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1504 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1505 if not self.server or #self.server == 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1506 -- TODO log warning about no nameservers, adding localhost as the default nameserver
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1507 self:addnameserver("127.0.0.1");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1508 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1509 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1510 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1511
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1512
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1513 function resolver:getsocket(servernum) -- - - - - - - - - - - - - getsocket
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1514 self.socket = self.socket or {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1515 self.socketset = self.socketset or {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1516
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1517 local sock = self.socket[servernum];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1518 if sock then return sock; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1519
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1520 local err;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1521 sock, err = socket.udp();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1522 if not sock then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1523 return nil, err;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1524 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1525 if self.socket_wrapper then sock = self.socket_wrapper(sock, self); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1526 sock:settimeout(0);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1527 -- todo: attempt to use a random port, fallback to 0
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1528 sock:setsockname('*', 0);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1529 sock:setpeername(self.server[servernum], 53);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1530 self.socket[servernum] = sock;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1531 self.socketset[sock] = servernum;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1532 return sock;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1533 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1534
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1535 function resolver:voidsocket(sock)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1536 if self.socket[sock] then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1537 self.socketset[self.socket[sock]] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1538 self.socket[sock] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1539 elseif self.socketset[sock] then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1540 self.socket[self.socketset[sock]] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1541 self.socketset[sock] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1542 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1543 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1544
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1545 function resolver:socket_wrapper_set(func) -- - - - - - - socket_wrapper_set
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1546 self.socket_wrapper = func;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1547 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1548
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1549
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1550 function resolver:closeall () -- - - - - - - - - - - - - - - - - - closeall
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1551 for i,sock in ipairs(self.socket) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1552 self.socket[i] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1553 self.socketset[sock] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1554 sock:close();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1555 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1556 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1557
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1558
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1559 function resolver:remember(rr, type) -- - - - - - - - - - - - - - remember
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1560 --print ('remember', type, rr.class, rr.type, rr.name)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1561 local qname, qtype, qclass = standardize(rr.name, rr.type, rr.class);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1562
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1563 if type ~= '*' then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1564 type = qtype;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1565 local all = get(self.cache, qclass, '*', qname);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1566 --print('remember all', all);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1567 if all then append(all, rr); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1568 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1569
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1570 self.cache = self.cache or setmetatable({}, cache_metatable);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1571 local rrs = get(self.cache, qclass, type, qname) or
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1572 set(self.cache, qclass, type, qname, setmetatable({}, rrs_metatable));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1573 append(rrs, rr);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1574
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1575 if type == 'MX' then self.unsorted[rrs] = true; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1576 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1577
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1578
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1579 local function comp_mx(a, b) -- - - - - - - - - - - - - - - - - - - comp_mx
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1580 return (a.pref == b.pref) and (a.mx < b.mx) or (a.pref < b.pref);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1581 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1582
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1583
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1584 function resolver:peek (qname, qtype, qclass) -- - - - - - - - - - - - peek
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1585 qname, qtype, qclass = standardize(qname, qtype, qclass);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1586 local rrs = get(self.cache, qclass, qtype, qname);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1587 if not rrs then return nil; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1588 if prune(rrs, socket.gettime()) and qtype == '*' or not next(rrs) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1589 set(self.cache, qclass, qtype, qname, nil);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1590 return nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1591 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1592 if self.unsorted[rrs] then table.sort (rrs, comp_mx); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1593 return rrs;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1594 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1595
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1596
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1597 function resolver:purge(soft) -- - - - - - - - - - - - - - - - - - - purge
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1598 if soft == 'soft' then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1599 self.time = socket.gettime();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1600 for class,types in pairs(self.cache or {}) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1601 for type,names in pairs(types) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1602 for name,rrs in pairs(names) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1603 prune(rrs, self.time, 'soft')
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1604 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1605 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1606 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1607 else self.cache = {}; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1608 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1609
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1610
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1611 function resolver:query(qname, qtype, qclass) -- - - - - - - - - - -- query
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1612 qname, qtype, qclass = standardize(qname, qtype, qclass)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1613
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1614 if not self.server then self:adddefaultnameservers(); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1615
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1616 local question = encodeQuestion(qname, qtype, qclass);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1617 local peek = self:peek (qname, qtype, qclass);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1618 if peek then return peek; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1619
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1620 local header, id = encodeHeader();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1621 --print ('query id', id, qclass, qtype, qname)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1622 local o = {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1623 packet = header..question,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1624 server = self.best_server,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1625 delay = 1,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1626 retry = socket.gettime() + self.delays[1]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1627 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1628
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1629 -- remember the query
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1630 self.active[id] = self.active[id] or {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1631 self.active[id][question] = o;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1632
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1633 -- remember which coroutine wants the answer
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1634 local co = coroutine.running();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1635 if co then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1636 set(self.wanted, qclass, qtype, qname, co, true);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1637 --set(self.yielded, co, qclass, qtype, qname, true);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1638 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1639
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1640 local conn, err = self:getsocket(o.server)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1641 if not conn then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1642 return nil, err;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1643 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1644 conn:send (o.packet)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1645
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1646 if timer and self.timeout then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1647 local num_servers = #self.server;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1648 local i = 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1649 timer.add_task(self.timeout, function ()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1650 if get(self.wanted, qclass, qtype, qname, co) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1651 if i < num_servers then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1652 i = i + 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1653 self:servfail(conn);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1654 o.server = self.best_server;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1655 conn, err = self:getsocket(o.server);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1656 if conn then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1657 conn:send(o.packet);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1658 return self.timeout;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1659 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1660 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1661 -- Tried everything, failed
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1662 self:cancel(qclass, qtype, qname, co, true);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1663 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1664 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1665 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1666 return true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1667 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1668
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1669 function resolver:servfail(sock)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1670 -- Resend all queries for this server
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1671
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1672 local num = self.socketset[sock]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1673
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1674 -- Socket is dead now
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1675 self:voidsocket(sock);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1676
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1677 -- Find all requests to the down server, and retry on the next server
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1678 self.time = socket.gettime();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1679 for id,queries in pairs(self.active) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1680 for question,o in pairs(queries) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1681 if o.server == num then -- This request was to the broken server
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1682 o.server = o.server + 1 -- Use next server
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1683 if o.server > #self.server then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1684 o.server = 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1685 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1686
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1687 o.retries = (o.retries or 0) + 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1688 if o.retries >= #self.server then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1689 --print('timeout');
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1690 queries[question] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1691 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1692 local _a = self:getsocket(o.server);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1693 if _a then _a:send(o.packet); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1694 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1695 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1696 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1697 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1698
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1699 if num == self.best_server then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1700 self.best_server = self.best_server + 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1701 if self.best_server > #self.server then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1702 -- Exhausted all servers, try first again
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1703 self.best_server = 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1704 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1705 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1706 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1707
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1708 function resolver:settimeout(seconds)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1709 self.timeout = seconds;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1710 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1711
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1712 function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1713 --print('receive'); print(self.socket);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1714 self.time = socket.gettime();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1715 rset = rset or self.socket;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1716
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1717 local response;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1718 for i,sock in pairs(rset) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1719
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1720 if self.socketset[sock] then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1721 local packet = sock:receive();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1722 if packet then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1723 response = self:decode(packet);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1724 if response and self.active[response.header.id]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1725 and self.active[response.header.id][response.question.raw] then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1726 --print('received response');
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1727 --self.print(response);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1728
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1729 for j,rr in pairs(response.answer) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1730 if rr.name:sub(-#response.question[1].name, -1) == response.question[1].name then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1731 self:remember(rr, response.question[1].type)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1732 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1733 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1734
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1735 -- retire the query
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1736 local queries = self.active[response.header.id];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1737 queries[response.question.raw] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1738
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1739 if not next(queries) then self.active[response.header.id] = nil; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1740 if not next(self.active) then self:closeall(); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1741
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1742 -- was the query on the wanted list?
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1743 local q = response.question[1];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1744 local cos = get(self.wanted, q.class, q.type, q.name);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1745 if cos then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1746 for co in pairs(cos) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1747 set(self.yielded, co, q.class, q.type, q.name, nil);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1748 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1749 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1750 set(self.wanted, q.class, q.type, q.name, nil);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1751 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1752 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1753 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1754 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1755 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1756
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1757 return response;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1758 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1759
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1760
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1761 function resolver:feed(sock, packet, force)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1762 --print('receive'); print(self.socket);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1763 self.time = socket.gettime();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1764
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1765 local response = self:decode(packet, force);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1766 if response and self.active[response.header.id]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1767 and self.active[response.header.id][response.question.raw] then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1768 --print('received response');
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1769 --self.print(response);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1770
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1771 for j,rr in pairs(response.answer) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1772 self:remember(rr, response.question[1].type);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1773 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1774
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1775 -- retire the query
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1776 local queries = self.active[response.header.id];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1777 queries[response.question.raw] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1778 if not next(queries) then self.active[response.header.id] = nil; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1779 if not next(self.active) then self:closeall(); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1780
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1781 -- was the query on the wanted list?
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1782 local q = response.question[1];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1783 if q then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1784 local cos = get(self.wanted, q.class, q.type, q.name);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1785 if cos then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1786 for co in pairs(cos) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1787 set(self.yielded, co, q.class, q.type, q.name, nil);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1788 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1789 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1790 set(self.wanted, q.class, q.type, q.name, nil);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1791 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1792 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1793 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1794
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1795 return response;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1796 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1797
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1798 function resolver:cancel(qclass, qtype, qname, co, call_handler)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1799 local cos = get(self.wanted, qclass, qtype, qname);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1800 if cos then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1801 if call_handler then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1802 coroutine.resume(co);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1803 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1804 cos[co] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1805 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1806 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1807
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1808 function resolver:pulse() -- - - - - - - - - - - - - - - - - - - - - pulse
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1809 --print(':pulse');
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1810 while self:receive() do end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1811 if not next(self.active) then return nil; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1812
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1813 self.time = socket.gettime();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1814 for id,queries in pairs(self.active) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1815 for question,o in pairs(queries) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1816 if self.time >= o.retry then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1817
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1818 o.server = o.server + 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1819 if o.server > #self.server then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1820 o.server = 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1821 o.delay = o.delay + 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1822 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1823
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1824 if o.delay > #self.delays then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1825 --print('timeout');
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1826 queries[question] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1827 if not next(queries) then self.active[id] = nil; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1828 if not next(self.active) then return nil; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1829 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1830 --print('retry', o.server, o.delay);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1831 local _a = self.socket[o.server];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1832 if _a then _a:send(o.packet); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1833 o.retry = self.time + self.delays[o.delay];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1834 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1835 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1836 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1837 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1838
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1839 if next(self.active) then return true; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1840 return nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1841 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1842
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1843
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1844 function resolver:lookup(qname, qtype, qclass) -- - - - - - - - - - lookup
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1845 self:query (qname, qtype, qclass)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1846 while self:pulse() do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1847 local recvt = {}
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1848 for i, s in ipairs(self.socket) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1849 recvt[i] = s
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1850 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1851 socket.select(recvt, nil, 4)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1852 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1853 --print(self.cache);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1854 return self:peek(qname, qtype, qclass);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1855 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1856
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1857 function resolver:lookupex(handler, qname, qtype, qclass) -- - - - - - - - - - lookup
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1858 return self:peek(qname, qtype, qclass) or self:query(qname, qtype, qclass);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1859 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1860
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1861 function resolver:tohostname(ip)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1862 return dns.lookup(ip:gsub("(%d+)%.(%d+)%.(%d+)%.(%d+)", "%4.%3.%2.%1.in-addr.arpa."), "PTR");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1863 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1864
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1865 --print ---------------------------------------------------------------- print
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1866
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1867
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1868 local hints = { -- - - - - - - - - - - - - - - - - - - - - - - - - - - hints
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1869 qr = { [0]='query', 'response' },
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1870 opcode = { [0]='query', 'inverse query', 'server status request' },
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1871 aa = { [0]='non-authoritative', 'authoritative' },
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1872 tc = { [0]='complete', 'truncated' },
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1873 rd = { [0]='recursion not desired', 'recursion desired' },
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1874 ra = { [0]='recursion not available', 'recursion available' },
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1875 z = { [0]='(reserved)' },
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1876 rcode = { [0]='no error', 'format error', 'server failure', 'name error', 'not implemented' },
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1877
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1878 type = dns.type,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1879 class = dns.class
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1880 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1881
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1882
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1883 local function hint(p, s) -- - - - - - - - - - - - - - - - - - - - - - hint
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1884 return (hints[s] and hints[s][p[s]]) or '';
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1885 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1886
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1887
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1888 function resolver.print(response) -- - - - - - - - - - - - - resolver.print
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1889 for s,s in pairs { 'id', 'qr', 'opcode', 'aa', 'tc', 'rd', 'ra', 'z',
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1890 'rcode', 'qdcount', 'ancount', 'nscount', 'arcount' } do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1891 print( string.format('%-30s', 'header.'..s), response.header[s], hint(response.header, s) );
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1892 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1893
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1894 for i,question in ipairs(response.question) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1895 print(string.format ('question[%i].name ', i), question.name);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1896 print(string.format ('question[%i].type ', i), question.type);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1897 print(string.format ('question[%i].class ', i), question.class);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1898 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1899
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1900 local common = { name=1, type=1, class=1, ttl=1, rdlength=1, rdata=1 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1901 local tmp;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1902 for s,s in pairs({'answer', 'authority', 'additional'}) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1903 for i,rr in pairs(response[s]) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1904 for j,t in pairs({ 'name', 'type', 'class', 'ttl', 'rdlength' }) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1905 tmp = string.format('%s[%i].%s', s, i, t);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1906 print(string.format('%-30s', tmp), rr[t], hint(rr, t));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1907 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1908 for j,t in pairs(rr) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1909 if not common[j] then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1910 tmp = string.format('%s[%i].%s', s, i, j);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1911 print(string.format('%-30s %s', tostring(tmp), tostring(t)));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1912 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1913 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1914 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1915 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1916 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1917
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1918
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1919 -- module api ------------------------------------------------------ module api
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1920
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1921
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1922 function dns.resolver () -- - - - - - - - - - - - - - - - - - - - - resolver
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1923 -- this function seems to be redundant with resolver.new ()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1924
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1925 local r = { active = {}, cache = {}, unsorted = {}, wanted = {}, yielded = {}, best_server = 1 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1926 setmetatable (r, resolver);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1927 setmetatable (r.cache, cache_metatable);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1928 setmetatable (r.unsorted, { __mode = 'kv' });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1929 return r;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1930 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1931
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1932 local _resolver = dns.resolver();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1933 dns._resolver = _resolver;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1934
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1935 function dns.lookup(...) -- - - - - - - - - - - - - - - - - - - - - lookup
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1936 return _resolver:lookup(...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1937 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1938
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1939 function dns.tohostname(...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1940 return _resolver:tohostname(...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1941 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1942
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1943 function dns.purge(...) -- - - - - - - - - - - - - - - - - - - - - - purge
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1944 return _resolver:purge(...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1945 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1946
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1947 function dns.peek(...) -- - - - - - - - - - - - - - - - - - - - - - - peek
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1948 return _resolver:peek(...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1949 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1950
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1951 function dns.query(...) -- - - - - - - - - - - - - - - - - - - - - - query
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1952 return _resolver:query(...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1953 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1954
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1955 function dns.feed(...) -- - - - - - - - - - - - - - - - - - - - - - - feed
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1956 return _resolver:feed(...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1957 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1958
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1959 function dns.cancel(...) -- - - - - - - - - - - - - - - - - - - - - - cancel
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1960 return _resolver:cancel(...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1961 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1962
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1963 function dns.settimeout(...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1964 return _resolver:settimeout(...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1965 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1966
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1967 function dns.socket_wrapper_set(...) -- - - - - - - - - socket_wrapper_set
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1968 return _resolver:socket_wrapper_set(...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1969 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1970
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1971 return dns;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1972 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1973 package.preload['net.adns'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1974 -- Prosody IM
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1975 -- Copyright (C) 2008-2010 Matthew Wild
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1976 -- Copyright (C) 2008-2010 Waqas Hussain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1977 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1978 -- This project is MIT/X11 licensed. Please see the
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1979 -- COPYING file in the source package for more information.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1980 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1981
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1982 local server = require "net.server";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1983 local dns = require "net.dns";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1984
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1985 local log = require "util.logger".init("adns");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1986
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1987 local t_insert, t_remove = table.insert, table.remove;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1988 local coroutine, tostring, pcall = coroutine, tostring, pcall;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1989
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1990 local function dummy_send(sock, data, i, j) return (j-i)+1; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1991
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1992 module "adns"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1993
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1994 function lookup(handler, qname, qtype, qclass)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1995 return coroutine.wrap(function (peek)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1996 if peek then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1997 log("debug", "Records for %s already cached, using those...", qname);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1998 handler(peek);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
1999 return;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2000 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2001 log("debug", "Records for %s not in cache, sending query (%s)...", qname, tostring(coroutine.running()));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2002 local ok, err = dns.query(qname, qtype, qclass);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2003 if ok then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2004 coroutine.yield({ qclass or "IN", qtype or "A", qname, coroutine.running()}); -- Wait for reply
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2005 log("debug", "Reply for %s (%s)", qname, tostring(coroutine.running()));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2006 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2007 if ok then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2008 ok, err = pcall(handler, dns.peek(qname, qtype, qclass));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2009 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2010 log("error", "Error sending DNS query: %s", err);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2011 ok, err = pcall(handler, nil, err);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2012 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2013 if not ok then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2014 log("error", "Error in DNS response handler: %s", tostring(err));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2015 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2016 end)(dns.peek(qname, qtype, qclass));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2017 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2018
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2019 function cancel(handle, call_handler, reason)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2020 log("warn", "Cancelling DNS lookup for %s", tostring(handle[3]));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2021 dns.cancel(handle[1], handle[2], handle[3], handle[4], call_handler);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2022 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2023
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2024 function new_async_socket(sock, resolver)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2025 local peername = "<unknown>";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2026 local listener = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2027 local handler = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2028 function listener.onincoming(conn, data)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2029 if data then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2030 dns.feed(handler, data);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2031 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2032 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2033 function listener.ondisconnect(conn, err)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2034 if err then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2035 log("warn", "DNS socket for %s disconnected: %s", peername, err);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2036 local servers = resolver.server;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2037 if resolver.socketset[conn] == resolver.best_server and resolver.best_server == #servers then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2038 log("error", "Exhausted all %d configured DNS servers, next lookup will try %s again", #servers, servers[1]);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2039 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2040
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2041 resolver:servfail(conn); -- Let the magic commence
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2042 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2043 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2044 handler = server.wrapclient(sock, "dns", 53, listener);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2045 if not handler then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2046 log("warn", "handler is nil");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2047 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2048
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2049 handler.settimeout = function () end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2050 handler.setsockname = function (_, ...) return sock:setsockname(...); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2051 handler.setpeername = function (_, ...) peername = (...); local ret = sock:setpeername(...); _:set_send(dummy_send); return ret; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2052 handler.connect = function (_, ...) return sock:connect(...) end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2053 --handler.send = function (_, data) _:write(data); return _.sendbuffer and _.sendbuffer(); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2054 handler.send = function (_, data)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2055 local getpeername = sock.getpeername;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2056 log("debug", "Sending DNS query to %s", (getpeername and getpeername(sock)) or "<unconnected>");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2057 return sock:send(data);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2058 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2059 return handler;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2060 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2061
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2062 dns.socket_wrapper_set(new_async_socket);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2063
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2064 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2065 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2066 package.preload['net.server'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2067 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2068 -- server.lua by blastbeat of the luadch project
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2069 -- Re-used here under the MIT/X Consortium License
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2070 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2071 -- Modifications (C) 2008-2010 Matthew Wild, Waqas Hussain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2072 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2073
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2074 -- // wrapping luadch stuff // --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2075
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2076 local use = function( what )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2077 return _G[ what ]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2078 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2079 local clean = function( tbl )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2080 for i, k in pairs( tbl ) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2081 tbl[ i ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2082 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2083 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2084
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2085 local log, table_concat = require ("util.logger").init("socket"), table.concat;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2086 local out_put = function (...) return log("debug", table_concat{...}); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2087 local out_error = function (...) return log("warn", table_concat{...}); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2088 local mem_free = collectgarbage
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2089
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2090 ----------------------------------// DECLARATION //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2091
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2092 --// constants //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2093
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2094 local STAT_UNIT = 1 -- byte
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2095
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2096 --// lua functions //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2097
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2098 local type = use "type"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2099 local pairs = use "pairs"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2100 local ipairs = use "ipairs"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2101 local tonumber = use "tonumber"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2102 local tostring = use "tostring"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2103 local collectgarbage = use "collectgarbage"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2104
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2105 --// lua libs //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2106
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2107 local os = use "os"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2108 local table = use "table"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2109 local string = use "string"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2110 local coroutine = use "coroutine"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2111
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2112 --// lua lib methods //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2113
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2114 local os_difftime = os.difftime
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2115 local math_min = math.min
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2116 local math_huge = math.huge
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2117 local table_concat = table.concat
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2118 local table_remove = table.remove
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2119 local string_len = string.len
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2120 local string_sub = string.sub
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2121 local coroutine_wrap = coroutine.wrap
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2122 local coroutine_yield = coroutine.yield
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2123
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2124 --// extern libs //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2125
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2126 local luasec = use "ssl"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2127 local luasocket = use "socket" or require "socket"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2128 local luasocket_gettime = luasocket.gettime
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2129
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2130 --// extern lib methods //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2131
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2132 local ssl_wrap = ( luasec and luasec.wrap )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2133 local socket_bind = luasocket.bind
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2134 local socket_sleep = luasocket.sleep
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2135 local socket_select = luasocket.select
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2136 local ssl_newcontext = ( luasec and luasec.newcontext )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2137
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2138 --// functions //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2139
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2140 local id
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2141 local loop
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2142 local stats
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2143 local idfalse
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2144 local addtimer
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2145 local closeall
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2146 local addsocket
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2147 local addserver
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2148 local getserver
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2149 local wrapserver
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2150 local getsettings
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2151 local closesocket
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2152 local removesocket
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2153 local removeserver
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2154 local changetimeout
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2155 local wrapconnection
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2156 local changesettings
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2157
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2158 --// tables //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2159
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2160 local _server
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2161 local _readlist
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2162 local _timerlist
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2163 local _sendlist
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2164 local _socketlist
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2165 local _closelist
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2166 local _readtimes
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2167 local _writetimes
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2168
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2169 --// simple data types //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2170
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2171 local _
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2172 local _readlistlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2173 local _sendlistlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2174 local _timerlistlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2175
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2176 local _sendtraffic
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2177 local _readtraffic
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2178
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2179 local _selecttimeout
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2180 local _sleeptime
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2181
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2182 local _starttime
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2183 local _currenttime
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2184
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2185 local _maxsendlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2186 local _maxreadlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2187
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2188 local _checkinterval
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2189 local _sendtimeout
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2190 local _readtimeout
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2191
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2192 local _cleanqueue
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2193
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2194 local _timer
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2195
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2196 local _maxclientsperserver
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2197
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2198 local _maxsslhandshake
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2199
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2200 ----------------------------------// DEFINITION //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2201
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2202 _server = { } -- key = port, value = table; list of listening servers
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2203 _readlist = { } -- array with sockets to read from
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2204 _sendlist = { } -- arrary with sockets to write to
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2205 _timerlist = { } -- array of timer functions
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2206 _socketlist = { } -- key = socket, value = wrapped socket (handlers)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2207 _readtimes = { } -- key = handler, value = timestamp of last data reading
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2208 _writetimes = { } -- key = handler, value = timestamp of last data writing/sending
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2209 _closelist = { } -- handlers to close
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2210
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2211 _readlistlen = 0 -- length of readlist
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2212 _sendlistlen = 0 -- length of sendlist
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2213 _timerlistlen = 0 -- lenght of timerlist
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2214
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2215 _sendtraffic = 0 -- some stats
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2216 _readtraffic = 0
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2217
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2218 _selecttimeout = 1 -- timeout of socket.select
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2219 _sleeptime = 0 -- time to wait at the end of every loop
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2220
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2221 _maxsendlen = 51000 * 1024 -- max len of send buffer
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2222 _maxreadlen = 25000 * 1024 -- max len of read buffer
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2223
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2224 _checkinterval = 1200000 -- interval in secs to check idle clients
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2225 _sendtimeout = 60000 -- allowed send idle time in secs
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2226 _readtimeout = 6 * 60 * 60 -- allowed read idle time in secs
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2227
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2228 _cleanqueue = false -- clean bufferqueue after using
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2229
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2230 _maxclientsperserver = 1000
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2231
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2232 _maxsslhandshake = 30 -- max handshake round-trips
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2233
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2234 ----------------------------------// PRIVATE //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2235
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2236 wrapserver = function( listeners, socket, ip, serverport, pattern, sslctx, maxconnections ) -- this function wraps a server
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2237
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2238 maxconnections = maxconnections or _maxclientsperserver
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2239
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2240 local connections = 0
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2241
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2242 local dispatch, disconnect = listeners.onconnect or listeners.onincoming, listeners.ondisconnect
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2243
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2244 local accept = socket.accept
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2245
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2246 --// public methods of the object //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2247
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2248 local handler = { }
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2249
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2250 handler.shutdown = function( ) end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2251
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2252 handler.ssl = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2253 return sslctx ~= nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2254 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2255 handler.sslctx = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2256 return sslctx
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2257 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2258 handler.remove = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2259 connections = connections - 1
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2260 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2261 handler.close = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2262 for _, handler in pairs( _socketlist ) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2263 if handler.serverport == serverport then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2264 handler.disconnect( handler, "server closed" )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2265 handler:close( true )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2266 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2267 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2268 socket:close( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2269 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2270 _readlistlen = removesocket( _readlist, socket, _readlistlen )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2271 _socketlist[ socket ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2272 handler = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2273 socket = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2274 --mem_free( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2275 out_put "server.lua: closed server handler and removed sockets from list"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2276 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2277 handler.ip = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2278 return ip
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2279 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2280 handler.serverport = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2281 return serverport
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2282 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2283 handler.socket = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2284 return socket
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2285 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2286 handler.readbuffer = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2287 if connections > maxconnections then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2288 out_put( "server.lua: refused new client connection: server full" )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2289 return false
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2290 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2291 local client, err = accept( socket ) -- try to accept
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2292 if client then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2293 local ip, clientport = client:getpeername( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2294 client:settimeout( 0 )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2295 local handler, client, err = wrapconnection( handler, listeners, client, ip, serverport, clientport, pattern, sslctx ) -- wrap new client socket
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2296 if err then -- error while wrapping ssl socket
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2297 return false
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2298 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2299 connections = connections + 1
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2300 out_put( "server.lua: accepted new client connection from ", tostring(ip), ":", tostring(clientport), " to ", tostring(serverport))
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2301 return dispatch( handler )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2302 elseif err then -- maybe timeout or something else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2303 out_put( "server.lua: error with new client connection: ", tostring(err) )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2304 return false
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2305 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2306 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2307 return handler
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2308 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2309
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2310 wrapconnection = function( server, listeners, socket, ip, serverport, clientport, pattern, sslctx ) -- this function wraps a client to a handler object
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2311
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2312 socket:settimeout( 0 )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2313
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2314 --// local import of socket methods //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2315
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2316 local send
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2317 local receive
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2318 local shutdown
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2319
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2320 --// private closures of the object //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2321
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2322 local ssl
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2323
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2324 local dispatch = listeners.onincoming
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2325 local status = listeners.onstatus
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2326 local disconnect = listeners.ondisconnect
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2327 local drain = listeners.ondrain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2328
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2329 local bufferqueue = { } -- buffer array
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2330 local bufferqueuelen = 0 -- end of buffer array
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2331
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2332 local toclose
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2333 local fatalerror
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2334 local needtls
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2335
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2336 local bufferlen = 0
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2337
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2338 local noread = false
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2339 local nosend = false
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2340
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2341 local sendtraffic, readtraffic = 0, 0
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2342
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2343 local maxsendlen = _maxsendlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2344 local maxreadlen = _maxreadlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2345
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2346 --// public methods of the object //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2347
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2348 local handler = bufferqueue -- saves a table ^_^
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2349
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2350 handler.dispatch = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2351 return dispatch
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2352 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2353 handler.disconnect = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2354 return disconnect
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2355 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2356 handler.setlistener = function( self, listeners )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2357 dispatch = listeners.onincoming
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2358 disconnect = listeners.ondisconnect
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2359 status = listeners.onstatus
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2360 drain = listeners.ondrain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2361 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2362 handler.getstats = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2363 return readtraffic, sendtraffic
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2364 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2365 handler.ssl = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2366 return ssl
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2367 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2368 handler.sslctx = function ( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2369 return sslctx
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2370 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2371 handler.send = function( _, data, i, j )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2372 return send( socket, data, i, j )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2373 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2374 handler.receive = function( pattern, prefix )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2375 return receive( socket, pattern, prefix )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2376 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2377 handler.shutdown = function( pattern )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2378 return shutdown( socket, pattern )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2379 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2380 handler.setoption = function (self, option, value)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2381 if socket.setoption then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2382 return socket:setoption(option, value);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2383 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2384 return false, "setoption not implemented";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2385 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2386 handler.close = function( self, forced )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2387 if not handler then return true; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2388 _readlistlen = removesocket( _readlist, socket, _readlistlen )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2389 _readtimes[ handler ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2390 if bufferqueuelen ~= 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2391 if not ( forced or fatalerror ) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2392 handler.sendbuffer( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2393 if bufferqueuelen ~= 0 then -- try again...
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2394 if handler then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2395 handler.write = nil -- ... but no further writing allowed
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2396 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2397 toclose = true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2398 return false
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2399 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2400 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2401 send( socket, table_concat( bufferqueue, "", 1, bufferqueuelen ), 1, bufferlen ) -- forced send
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2402 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2403 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2404 if socket then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2405 _ = shutdown and shutdown( socket )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2406 socket:close( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2407 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2408 _socketlist[ socket ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2409 socket = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2410 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2411 out_put "server.lua: socket already closed"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2412 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2413 if handler then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2414 _writetimes[ handler ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2415 _closelist[ handler ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2416 handler = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2417 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2418 if server then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2419 server.remove( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2420 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2421 out_put "server.lua: closed client handler and removed socket from list"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2422 return true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2423 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2424 handler.ip = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2425 return ip
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2426 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2427 handler.serverport = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2428 return serverport
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2429 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2430 handler.clientport = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2431 return clientport
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2432 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2433 local write = function( self, data )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2434 bufferlen = bufferlen + string_len( data )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2435 if bufferlen > maxsendlen then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2436 _closelist[ handler ] = "send buffer exceeded" -- cannot close the client at the moment, have to wait to the end of the cycle
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2437 handler.write = idfalse -- dont write anymore
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2438 return false
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2439 elseif socket and not _sendlist[ socket ] then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2440 _sendlistlen = addsocket(_sendlist, socket, _sendlistlen)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2441 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2442 bufferqueuelen = bufferqueuelen + 1
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2443 bufferqueue[ bufferqueuelen ] = data
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2444 if handler then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2445 _writetimes[ handler ] = _writetimes[ handler ] or _currenttime
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2446 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2447 return true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2448 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2449 handler.write = write
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2450 handler.bufferqueue = function( self )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2451 return bufferqueue
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2452 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2453 handler.socket = function( self )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2454 return socket
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2455 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2456 handler.set_mode = function( self, new )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2457 pattern = new or pattern
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2458 return pattern
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2459 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2460 handler.set_send = function ( self, newsend )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2461 send = newsend or send
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2462 return send
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2463 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2464 handler.bufferlen = function( self, readlen, sendlen )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2465 maxsendlen = sendlen or maxsendlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2466 maxreadlen = readlen or maxreadlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2467 return bufferlen, maxreadlen, maxsendlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2468 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2469 --TODO: Deprecate
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2470 handler.lock_read = function (self, switch)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2471 if switch == true then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2472 local tmp = _readlistlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2473 _readlistlen = removesocket( _readlist, socket, _readlistlen )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2474 _readtimes[ handler ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2475 if _readlistlen ~= tmp then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2476 noread = true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2477 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2478 elseif switch == false then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2479 if noread then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2480 noread = false
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2481 _readlistlen = addsocket(_readlist, socket, _readlistlen)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2482 _readtimes[ handler ] = _currenttime
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2483 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2484 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2485 return noread
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2486 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2487 handler.pause = function (self)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2488 return self:lock_read(true);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2489 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2490 handler.resume = function (self)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2491 return self:lock_read(false);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2492 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2493 handler.lock = function( self, switch )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2494 handler.lock_read (switch)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2495 if switch == true then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2496 handler.write = idfalse
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2497 local tmp = _sendlistlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2498 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2499 _writetimes[ handler ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2500 if _sendlistlen ~= tmp then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2501 nosend = true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2502 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2503 elseif switch == false then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2504 handler.write = write
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2505 if nosend then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2506 nosend = false
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2507 write( "" )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2508 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2509 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2510 return noread, nosend
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2511 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2512 local _readbuffer = function( ) -- this function reads data
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2513 local buffer, err, part = receive( socket, pattern ) -- receive buffer with "pattern"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2514 if not err or (err == "wantread" or err == "timeout") then -- received something
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2515 local buffer = buffer or part or ""
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2516 local len = string_len( buffer )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2517 if len > maxreadlen then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2518 disconnect( handler, "receive buffer exceeded" )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2519 handler:close( true )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2520 return false
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2521 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2522 local count = len * STAT_UNIT
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2523 readtraffic = readtraffic + count
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2524 _readtraffic = _readtraffic + count
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2525 _readtimes[ handler ] = _currenttime
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2526 --out_put( "server.lua: read data '", buffer:gsub("[^%w%p ]", "."), "', error: ", err )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2527 return dispatch( handler, buffer, err )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2528 else -- connections was closed or fatal error
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2529 out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " read error: ", tostring(err) )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2530 fatalerror = true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2531 disconnect( handler, err )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2532 _ = handler and handler:close( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2533 return false
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2534 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2535 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2536 local _sendbuffer = function( ) -- this function sends data
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2537 local succ, err, byte, buffer, count;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2538 local count;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2539 if socket then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2540 buffer = table_concat( bufferqueue, "", 1, bufferqueuelen )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2541 succ, err, byte = send( socket, buffer, 1, bufferlen )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2542 count = ( succ or byte or 0 ) * STAT_UNIT
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2543 sendtraffic = sendtraffic + count
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2544 _sendtraffic = _sendtraffic + count
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2545 _ = _cleanqueue and clean( bufferqueue )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2546 --out_put( "server.lua: sended '", buffer, "', bytes: ", tostring(succ), ", error: ", tostring(err), ", part: ", tostring(byte), ", to: ", tostring(ip), ":", tostring(clientport) )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2547 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2548 succ, err, count = false, "closed", 0;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2549 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2550 if succ then -- sending succesful
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2551 bufferqueuelen = 0
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2552 bufferlen = 0
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2553 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) -- delete socket from writelist
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2554 _writetimes[ handler ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2555 if drain then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2556 drain(handler)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2557 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2558 _ = needtls and handler:starttls(nil)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2559 _ = toclose and handler:close( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2560 return true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2561 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2562 buffer = string_sub( buffer, byte + 1, bufferlen ) -- new buffer
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2563 bufferqueue[ 1 ] = buffer -- insert new buffer in queue
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2564 bufferqueuelen = 1
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2565 bufferlen = bufferlen - byte
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2566 _writetimes[ handler ] = _currenttime
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2567 return true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2568 else -- connection was closed during sending or fatal error
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2569 out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " write error: ", tostring(err) )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2570 fatalerror = true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2571 disconnect( handler, err )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2572 _ = handler and handler:close( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2573 return false
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2574 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2575 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2576
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2577 -- Set the sslctx
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2578 local handshake;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2579 function handler.set_sslctx(self, new_sslctx)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2580 ssl = true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2581 sslctx = new_sslctx;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2582 local wrote
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2583 local read
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2584 handshake = coroutine_wrap( function( client ) -- create handshake coroutine
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2585 local err
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2586 for i = 1, _maxsslhandshake do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2587 _sendlistlen = ( wrote and removesocket( _sendlist, client, _sendlistlen ) ) or _sendlistlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2588 _readlistlen = ( read and removesocket( _readlist, client, _readlistlen ) ) or _readlistlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2589 read, wrote = nil, nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2590 _, err = client:dohandshake( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2591 if not err then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2592 out_put( "server.lua: ssl handshake done" )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2593 handler.readbuffer = _readbuffer -- when handshake is done, replace the handshake function with regular functions
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2594 handler.sendbuffer = _sendbuffer
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2595 _ = status and status( handler, "ssl-handshake-complete" )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2596 _readlistlen = addsocket(_readlist, client, _readlistlen)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2597 return true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2598 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2599 if err == "wantwrite" and not wrote then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2600 _sendlistlen = addsocket(_sendlist, client, _sendlistlen)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2601 wrote = true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2602 elseif err == "wantread" and not read then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2603 _readlistlen = addsocket(_readlist, client, _readlistlen)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2604 read = true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2605 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2606 out_put( "server.lua: ssl handshake error: ", tostring(err) )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2607 break;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2608 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2609 --coroutine_yield( handler, nil, err ) -- handshake not finished
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2610 coroutine_yield( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2611 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2612 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2613 disconnect( handler, "ssl handshake failed" )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2614 _ = handler and handler:close( true ) -- forced disconnect
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2615 return false -- handshake failed
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2616 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2617 )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2618 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2619 if luasec then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2620 if sslctx then -- ssl?
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2621 handler:set_sslctx(sslctx);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2622 out_put("server.lua: ", "starting ssl handshake")
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2623 local err
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2624 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2625 if err then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2626 out_put( "server.lua: ssl error: ", tostring(err) )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2627 --mem_free( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2628 return nil, nil, err -- fatal error
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2629 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2630 socket:settimeout( 0 )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2631 handler.readbuffer = handshake
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2632 handler.sendbuffer = handshake
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2633 handshake( socket ) -- do handshake
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2634 if not socket then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2635 return nil, nil, "ssl handshake failed";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2636 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2637 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2638 local sslctx;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2639 handler.starttls = function( self, _sslctx)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2640 if _sslctx then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2641 sslctx = _sslctx;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2642 handler:set_sslctx(sslctx);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2643 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2644 if bufferqueuelen > 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2645 out_put "server.lua: we need to do tls, but delaying until send buffer empty"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2646 needtls = true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2647 return
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2648 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2649 out_put( "server.lua: attempting to start tls on " .. tostring( socket ) )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2650 local oldsocket, err = socket
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2651 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2652 --out_put( "server.lua: sslwrapped socket is " .. tostring( socket ) )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2653 if err then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2654 out_put( "server.lua: error while starting tls on client: ", tostring(err) )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2655 return nil, err -- fatal error
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2656 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2657
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2658 socket:settimeout( 0 )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2659
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2660 -- add the new socket to our system
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2661
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2662 send = socket.send
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2663 receive = socket.receive
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2664 shutdown = id
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2665
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2666 _socketlist[ socket ] = handler
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2667 _readlistlen = addsocket(_readlist, socket, _readlistlen)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2668
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2669 -- remove traces of the old socket
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2670
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2671 _readlistlen = removesocket( _readlist, oldsocket, _readlistlen )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2672 _sendlistlen = removesocket( _sendlist, oldsocket, _sendlistlen )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2673 _socketlist[ oldsocket ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2674
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2675 handler.starttls = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2676 needtls = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2677
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2678 -- Secure now
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2679 ssl = true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2680
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2681 handler.readbuffer = handshake
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2682 handler.sendbuffer = handshake
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2683 handshake( socket ) -- do handshake
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2684 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2685 handler.readbuffer = _readbuffer
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2686 handler.sendbuffer = _sendbuffer
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2687 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2688 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2689 handler.readbuffer = _readbuffer
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2690 handler.sendbuffer = _sendbuffer
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2691 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2692 send = socket.send
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2693 receive = socket.receive
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2694 shutdown = ( ssl and id ) or socket.shutdown
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2695
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2696 _socketlist[ socket ] = handler
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2697 _readlistlen = addsocket(_readlist, socket, _readlistlen)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2698 return handler, socket
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2699 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2700
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2701 id = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2702 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2703
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2704 idfalse = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2705 return false
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2706 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2707
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2708 addsocket = function( list, socket, len )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2709 if not list[ socket ] then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2710 len = len + 1
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2711 list[ len ] = socket
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2712 list[ socket ] = len
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2713 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2714 return len;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2715 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2716
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2717 removesocket = function( list, socket, len ) -- this function removes sockets from a list ( copied from copas )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2718 local pos = list[ socket ]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2719 if pos then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2720 list[ socket ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2721 local last = list[ len ]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2722 list[ len ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2723 if last ~= socket then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2724 list[ last ] = pos
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2725 list[ pos ] = last
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2726 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2727 return len - 1
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2728 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2729 return len
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2730 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2731
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2732 closesocket = function( socket )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2733 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2734 _readlistlen = removesocket( _readlist, socket, _readlistlen )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2735 _socketlist[ socket ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2736 socket:close( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2737 --mem_free( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2738 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2739
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2740 local function link(sender, receiver, buffersize)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2741 local sender_locked;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2742 local _sendbuffer = receiver.sendbuffer;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2743 function receiver.sendbuffer()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2744 _sendbuffer();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2745 if sender_locked and receiver.bufferlen() < buffersize then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2746 sender:lock_read(false); -- Unlock now
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2747 sender_locked = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2748 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2749 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2750
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2751 local _readbuffer = sender.readbuffer;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2752 function sender.readbuffer()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2753 _readbuffer();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2754 if not sender_locked and receiver.bufferlen() >= buffersize then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2755 sender_locked = true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2756 sender:lock_read(true);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2757 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2758 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2759 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2760
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2761 ----------------------------------// PUBLIC //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2762
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2763 addserver = function( addr, port, listeners, pattern, sslctx ) -- this function provides a way for other scripts to reg a server
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2764 local err
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2765 if type( listeners ) ~= "table" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2766 err = "invalid listener table"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2767 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2768 if type( port ) ~= "number" or not ( port >= 0 and port <= 65535 ) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2769 err = "invalid port"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2770 elseif _server[ addr..":"..port ] then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2771 err = "listeners on '[" .. addr .. "]:" .. port .. "' already exist"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2772 elseif sslctx and not luasec then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2773 err = "luasec not found"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2774 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2775 if err then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2776 out_error( "server.lua, [", addr, "]:", port, ": ", err )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2777 return nil, err
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2778 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2779 addr = addr or "*"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2780 local server, err = socket_bind( addr, port )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2781 if err then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2782 out_error( "server.lua, [", addr, "]:", port, ": ", err )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2783 return nil, err
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2784 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2785 local handler, err = wrapserver( listeners, server, addr, port, pattern, sslctx, _maxclientsperserver ) -- wrap new server socket
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2786 if not handler then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2787 server:close( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2788 return nil, err
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2789 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2790 server:settimeout( 0 )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2791 _readlistlen = addsocket(_readlist, server, _readlistlen)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2792 _server[ addr..":"..port ] = handler
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2793 _socketlist[ server ] = handler
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2794 out_put( "server.lua: new "..(sslctx and "ssl " or "").."server listener on '[", addr, "]:", port, "'" )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2795 return handler
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2796 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2797
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2798 getserver = function ( addr, port )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2799 return _server[ addr..":"..port ];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2800 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2801
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2802 removeserver = function( addr, port )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2803 local handler = _server[ addr..":"..port ]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2804 if not handler then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2805 return nil, "no server found on '[" .. addr .. "]:" .. tostring( port ) .. "'"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2806 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2807 handler:close( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2808 _server[ addr..":"..port ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2809 return true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2810 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2811
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2812 closeall = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2813 for _, handler in pairs( _socketlist ) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2814 handler:close( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2815 _socketlist[ _ ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2816 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2817 _readlistlen = 0
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2818 _sendlistlen = 0
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2819 _timerlistlen = 0
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2820 _server = { }
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2821 _readlist = { }
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2822 _sendlist = { }
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2823 _timerlist = { }
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2824 _socketlist = { }
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2825 --mem_free( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2826 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2827
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2828 getsettings = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2829 return _selecttimeout, _sleeptime, _maxsendlen, _maxreadlen, _checkinterval, _sendtimeout, _readtimeout, _cleanqueue, _maxclientsperserver, _maxsslhandshake
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2830 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2831
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2832 changesettings = function( new )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2833 if type( new ) ~= "table" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2834 return nil, "invalid settings table"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2835 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2836 _selecttimeout = tonumber( new.timeout ) or _selecttimeout
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2837 _sleeptime = tonumber( new.sleeptime ) or _sleeptime
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2838 _maxsendlen = tonumber( new.maxsendlen ) or _maxsendlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2839 _maxreadlen = tonumber( new.maxreadlen ) or _maxreadlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2840 _checkinterval = tonumber( new.checkinterval ) or _checkinterval
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2841 _sendtimeout = tonumber( new.sendtimeout ) or _sendtimeout
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2842 _readtimeout = tonumber( new.readtimeout ) or _readtimeout
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2843 _cleanqueue = new.cleanqueue
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2844 _maxclientsperserver = new._maxclientsperserver or _maxclientsperserver
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2845 _maxsslhandshake = new._maxsslhandshake or _maxsslhandshake
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2846 return true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2847 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2848
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2849 addtimer = function( listener )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2850 if type( listener ) ~= "function" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2851 return nil, "invalid listener function"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2852 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2853 _timerlistlen = _timerlistlen + 1
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2854 _timerlist[ _timerlistlen ] = listener
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2855 return true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2856 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2857
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2858 stats = function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2859 return _readtraffic, _sendtraffic, _readlistlen, _sendlistlen, _timerlistlen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2860 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2861
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2862 local quitting;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2863
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2864 setquitting = function (quit)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2865 quitting = not not quit;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2866 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2867
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2868 loop = function(once) -- this is the main loop of the program
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2869 if quitting then return "quitting"; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2870 if once then quitting = "once"; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2871 local next_timer_time = math_huge;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2872 repeat
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2873 local read, write, err = socket_select( _readlist, _sendlist, math_min(_selecttimeout, next_timer_time) )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2874 for i, socket in ipairs( write ) do -- send data waiting in writequeues
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2875 local handler = _socketlist[ socket ]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2876 if handler then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2877 handler.sendbuffer( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2878 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2879 closesocket( socket )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2880 out_put "server.lua: found no handler and closed socket (writelist)" -- this should not happen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2881 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2882 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2883 for i, socket in ipairs( read ) do -- receive data
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2884 local handler = _socketlist[ socket ]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2885 if handler then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2886 handler.readbuffer( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2887 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2888 closesocket( socket )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2889 out_put "server.lua: found no handler and closed socket (readlist)" -- this can happen
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2890 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2891 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2892 for handler, err in pairs( _closelist ) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2893 handler.disconnect( )( handler, err )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2894 handler:close( true ) -- forced disconnect
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2895 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2896 clean( _closelist )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2897 _currenttime = luasocket_gettime( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2898 if _currenttime - _timer >= math_min(next_timer_time, 1) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2899 next_timer_time = math_huge;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2900 for i = 1, _timerlistlen do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2901 local t = _timerlist[ i ]( _currenttime ) -- fire timers
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2902 if t then next_timer_time = math_min(next_timer_time, t); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2903 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2904 _timer = _currenttime
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2905 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2906 next_timer_time = next_timer_time - (_currenttime - _timer);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2907 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2908 socket_sleep( _sleeptime ) -- wait some time
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2909 --collectgarbage( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2910 until quitting;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2911 if once and quitting == "once" then quitting = nil; return; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2912 return "quitting"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2913 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2914
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2915 step = function ()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2916 return loop(true);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2917 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2918
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2919 local function get_backend()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2920 return "select";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2921 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2922
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2923 --// EXPERIMENTAL //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2924
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2925 local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2926 local handler = wrapconnection( nil, listeners, socket, ip, serverport, "clientport", pattern, sslctx )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2927 _socketlist[ socket ] = handler
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2928 _sendlistlen = addsocket(_sendlist, socket, _sendlistlen)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2929 if listeners.onconnect then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2930 -- When socket is writeable, call onconnect
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2931 local _sendbuffer = handler.sendbuffer;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2932 handler.sendbuffer = function ()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2933 handler.sendbuffer = _sendbuffer;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2934 listeners.onconnect(handler);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2935 -- If there was data with the incoming packet, handle it now.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2936 if #handler:bufferqueue() > 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2937 return _sendbuffer();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2938 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2939 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2940 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2941 return handler, socket
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2942 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2943
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2944 local addclient = function( address, port, listeners, pattern, sslctx )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2945 local client, err = luasocket.tcp( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2946 if err then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2947 return nil, err
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2948 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2949 client:settimeout( 0 )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2950 _, err = client:connect( address, port )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2951 if err then -- try again
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2952 local handler = wrapclient( client, address, port, listeners )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2953 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2954 wrapconnection( nil, listeners, client, address, port, "clientport", pattern, sslctx )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2955 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2956 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2957
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2958 --// EXPERIMENTAL //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2959
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2960 ----------------------------------// BEGIN //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2961
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2962 use "setmetatable" ( _socketlist, { __mode = "k" } )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2963 use "setmetatable" ( _readtimes, { __mode = "k" } )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2964 use "setmetatable" ( _writetimes, { __mode = "k" } )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2965
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2966 _timer = luasocket_gettime( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2967 _starttime = luasocket_gettime( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2968
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2969 addtimer( function( )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2970 local difftime = os_difftime( _currenttime - _starttime )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2971 if difftime > _checkinterval then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2972 _starttime = _currenttime
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2973 for handler, timestamp in pairs( _writetimes ) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2974 if os_difftime( _currenttime - timestamp ) > _sendtimeout then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2975 --_writetimes[ handler ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2976 handler.disconnect( )( handler, "send timeout" )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2977 handler:close( true ) -- forced disconnect
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2978 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2979 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2980 for handler, timestamp in pairs( _readtimes ) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2981 if os_difftime( _currenttime - timestamp ) > _readtimeout then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2982 --_readtimes[ handler ] = nil
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2983 handler.disconnect( )( handler, "read timeout" )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2984 handler:close( ) -- forced disconnect?
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2985 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2986 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2987 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2988 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2989 )
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2990
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2991 local function setlogger(new_logger)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2992 local old_logger = log;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2993 if new_logger then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2994 log = new_logger;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2995 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2996 return old_logger;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2997 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2998
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
2999 ----------------------------------// PUBLIC INTERFACE //--
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3000
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3001 return {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3002
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3003 addclient = addclient,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3004 wrapclient = wrapclient,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3005
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3006 loop = loop,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3007 link = link,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3008 step = step,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3009 stats = stats,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3010 closeall = closeall,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3011 addtimer = addtimer,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3012 addserver = addserver,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3013 getserver = getserver,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3014 setlogger = setlogger,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3015 getsettings = getsettings,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3016 setquitting = setquitting,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3017 removeserver = removeserver,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3018 get_backend = get_backend,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3019 changesettings = changesettings,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3020 }
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3021 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3022 package.preload['util.xmppstream'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3023 -- Prosody IM
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3024 -- Copyright (C) 2008-2010 Matthew Wild
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3025 -- Copyright (C) 2008-2010 Waqas Hussain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3026 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3027 -- This project is MIT/X11 licensed. Please see the
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3028 -- COPYING file in the source package for more information.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3029 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3030
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3031
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3032 local lxp = require "lxp";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3033 local st = require "util.stanza";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3034 local stanza_mt = st.stanza_mt;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3035
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3036 local tostring = tostring;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3037 local t_insert = table.insert;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3038 local t_concat = table.concat;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3039 local t_remove = table.remove;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3040 local setmetatable = setmetatable;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3041
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3042 local default_log = require "util.logger".init("xmppstream");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3043
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3044 -- COMPAT: w/LuaExpat 1.1.0
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3045 local lxp_supports_doctype = pcall(lxp.new, { StartDoctypeDecl = false });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3046
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3047 if not lxp_supports_doctype then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3048 default_log("warn", "The version of LuaExpat on your system leaves Prosody "
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3049 .."vulnerable to denial-of-service attacks. You should upgrade to "
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3050 .."LuaExpat 1.1.1 or higher as soon as possible. See "
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3051 .."http://prosody.im/doc/depends#luaexpat for more information.");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3052 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3053
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3054 local error = error;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3055
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3056 module "xmppstream"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3057
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3058 local new_parser = lxp.new;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3059
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3060 local ns_prefixes = {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3061 ["http://www.w3.org/XML/1998/namespace"] = "xml";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3062 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3063
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3064 local xmlns_streams = "http://etherx.jabber.org/streams";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3065
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3066 local ns_separator = "\1";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3067 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3068
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3069 _M.ns_separator = ns_separator;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3070 _M.ns_pattern = ns_pattern;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3071
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3072 function new_sax_handlers(session, stream_callbacks)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3073 local xml_handlers = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3074
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3075 local log = session.log or default_log;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3076
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3077 local cb_streamopened = stream_callbacks.streamopened;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3078 local cb_streamclosed = stream_callbacks.streamclosed;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3079 local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3080 local cb_handlestanza = stream_callbacks.handlestanza;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3081
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3082 local stream_ns = stream_callbacks.stream_ns or xmlns_streams;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3083 local stream_tag = stream_callbacks.stream_tag or "stream";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3084 if stream_ns ~= "" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3085 stream_tag = stream_ns..ns_separator..stream_tag;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3086 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3087 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3088
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3089 local stream_default_ns = stream_callbacks.default_ns;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3090
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3091 local stack = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3092 local chardata, stanza = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3093 local non_streamns_depth = 0;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3094 function xml_handlers:StartElement(tagname, attr)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3095 if stanza and #chardata > 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3096 -- We have some character data in the buffer
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3097 t_insert(stanza, t_concat(chardata));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3098 chardata = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3099 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3100 local curr_ns,name = tagname:match(ns_pattern);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3101 if name == "" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3102 curr_ns, name = "", curr_ns;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3103 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3104
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3105 if curr_ns ~= stream_default_ns or non_streamns_depth > 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3106 attr.xmlns = curr_ns;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3107 non_streamns_depth = non_streamns_depth + 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3108 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3109
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3110 -- FIXME !!!!!
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3111 for i=1,#attr do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3112 local k = attr[i];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3113 attr[i] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3114 local ns, nm = k:match(ns_pattern);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3115 if nm ~= "" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3116 ns = ns_prefixes[ns];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3117 if ns then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3118 attr[ns..":"..nm] = attr[k];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3119 attr[k] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3120 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3121 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3122 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3123
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3124 if not stanza then --if we are not currently inside a stanza
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3125 if session.notopen then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3126 if tagname == stream_tag then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3127 non_streamns_depth = 0;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3128 if cb_streamopened then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3129 cb_streamopened(session, attr);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3130 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3131 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3132 -- Garbage before stream?
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3133 cb_error(session, "no-stream");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3134 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3135 return;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3136 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3137 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3138 cb_error(session, "invalid-top-level-element");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3139 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3140
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3141 stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3142 else -- we are inside a stanza, so add a tag
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3143 t_insert(stack, stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3144 local oldstanza = stanza;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3145 stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3146 t_insert(oldstanza, stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3147 t_insert(oldstanza.tags, stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3148 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3149 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3150 function xml_handlers:CharacterData(data)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3151 if stanza then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3152 t_insert(chardata, data);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3153 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3154 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3155 function xml_handlers:EndElement(tagname)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3156 if non_streamns_depth > 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3157 non_streamns_depth = non_streamns_depth - 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3158 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3159 if stanza then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3160 if #chardata > 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3161 -- We have some character data in the buffer
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3162 t_insert(stanza, t_concat(chardata));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3163 chardata = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3164 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3165 -- Complete stanza
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3166 if #stack == 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3167 if tagname ~= stream_error_tag then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3168 cb_handlestanza(session, stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3169 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3170 cb_error(session, "stream-error", stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3171 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3172 stanza = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3173 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3174 stanza = t_remove(stack);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3175 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3176 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3177 if tagname == stream_tag then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3178 if cb_streamclosed then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3179 cb_streamclosed(session);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3180 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3181 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3182 local curr_ns,name = tagname:match(ns_pattern);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3183 if name == "" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3184 curr_ns, name = "", curr_ns;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3185 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3186 cb_error(session, "parse-error", "unexpected-element-close", name);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3187 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3188 stanza, chardata = nil, {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3189 stack = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3190 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3191 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3192
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3193 local function restricted_handler(parser)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3194 cb_error(session, "parse-error", "restricted-xml", "Restricted XML, see RFC 6120 section 11.1.");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3195 if not parser.stop or not parser:stop() then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3196 error("Failed to abort parsing");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3197 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3198 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3199
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3200 if lxp_supports_doctype then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3201 xml_handlers.StartDoctypeDecl = restricted_handler;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3202 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3203 xml_handlers.Comment = restricted_handler;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3204 xml_handlers.ProcessingInstruction = restricted_handler;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3205
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3206 local function reset()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3207 stanza, chardata = nil, {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3208 stack = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3209 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3210
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3211 local function set_session(stream, new_session)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3212 session = new_session;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3213 log = new_session.log or default_log;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3214 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3215
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3216 return xml_handlers, { reset = reset, set_session = set_session };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3217 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3218
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3219 function new(session, stream_callbacks)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3220 local handlers, meta = new_sax_handlers(session, stream_callbacks);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3221 local parser = new_parser(handlers, ns_separator);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3222 local parse = parser.parse;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3223
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3224 return {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3225 reset = function ()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3226 parser = new_parser(handlers, ns_separator);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3227 parse = parser.parse;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3228 meta.reset();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3229 end,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3230 feed = function (self, data)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3231 return parse(parser, data);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3232 end,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3233 set_session = meta.set_session;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3234 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3235 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3236
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3237 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3238 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3239 package.preload['util.jid'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3240 -- Prosody IM
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3241 -- Copyright (C) 2008-2010 Matthew Wild
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3242 -- Copyright (C) 2008-2010 Waqas Hussain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3243 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3244 -- This project is MIT/X11 licensed. Please see the
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3245 -- COPYING file in the source package for more information.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3246 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3247
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3248
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3249
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3250 local match = string.match;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3251 local nodeprep = require "util.encodings".stringprep.nodeprep;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3252 local nameprep = require "util.encodings".stringprep.nameprep;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3253 local resourceprep = require "util.encodings".stringprep.resourceprep;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3254
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3255 module "jid"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3256
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3257 local function _split(jid)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3258 if not jid then return; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3259 local node, nodepos = match(jid, "^([^@/]+)@()");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3260 local host, hostpos = match(jid, "^([^@/]+)()", nodepos)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3261 if node and not host then return nil, nil, nil; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3262 local resource = match(jid, "^/(.+)$", hostpos);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3263 if (not host) or ((not resource) and #jid >= hostpos) then return nil, nil, nil; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3264 return node, host, resource;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3265 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3266 split = _split;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3267
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3268 function bare(jid)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3269 local node, host = _split(jid);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3270 if node and host then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3271 return node.."@"..host;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3272 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3273 return host;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3274 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3275
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3276 local function _prepped_split(jid)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3277 local node, host, resource = _split(jid);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3278 if host then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3279 host = nameprep(host);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3280 if not host then return; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3281 if node then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3282 node = nodeprep(node);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3283 if not node then return; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3284 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3285 if resource then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3286 resource = resourceprep(resource);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3287 if not resource then return; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3288 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3289 return node, host, resource;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3290 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3291 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3292 prepped_split = _prepped_split;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3293
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3294 function prep(jid)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3295 local node, host, resource = _prepped_split(jid);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3296 if host then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3297 if node then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3298 host = node .. "@" .. host;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3299 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3300 if resource then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3301 host = host .. "/" .. resource;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3302 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3303 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3304 return host;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3305 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3306
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3307 function join(node, host, resource)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3308 if node and host and resource then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3309 return node.."@"..host.."/"..resource;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3310 elseif node and host then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3311 return node.."@"..host;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3312 elseif host and resource then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3313 return host.."/"..resource;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3314 elseif host then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3315 return host;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3316 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3317 return nil; -- Invalid JID
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3318 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3319
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3320 function compare(jid, acl)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3321 -- compare jid to single acl rule
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3322 -- TODO compare to table of rules?
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3323 local jid_node, jid_host, jid_resource = _split(jid);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3324 local acl_node, acl_host, acl_resource = _split(acl);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3325 if ((acl_node ~= nil and acl_node == jid_node) or acl_node == nil) and
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3326 ((acl_host ~= nil and acl_host == jid_host) or acl_host == nil) and
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3327 ((acl_resource ~= nil and acl_resource == jid_resource) or acl_resource == nil) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3328 return true
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3329 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3330 return false
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3331 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3332
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3333 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3334 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3335 package.preload['util.events'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3336 -- Prosody IM
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3337 -- Copyright (C) 2008-2010 Matthew Wild
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3338 -- Copyright (C) 2008-2010 Waqas Hussain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3339 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3340 -- This project is MIT/X11 licensed. Please see the
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3341 -- COPYING file in the source package for more information.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3342 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3343
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3344
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3345 local pairs = pairs;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3346 local t_insert = table.insert;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3347 local t_sort = table.sort;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3348 local setmetatable = setmetatable;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3349 local next = next;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3350
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3351 module "events"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3352
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3353 function new()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3354 local handlers = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3355 local event_map = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3356 local function _rebuild_index(handlers, event)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3357 local _handlers = event_map[event];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3358 if not _handlers or next(_handlers) == nil then return; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3359 local index = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3360 for handler in pairs(_handlers) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3361 t_insert(index, handler);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3362 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3363 t_sort(index, function(a, b) return _handlers[a] > _handlers[b]; end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3364 handlers[event] = index;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3365 return index;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3366 end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3367 setmetatable(handlers, { __index = _rebuild_index });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3368 local function add_handler(event, handler, priority)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3369 local map = event_map[event];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3370 if map then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3371 map[handler] = priority or 0;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3372 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3373 map = {[handler] = priority or 0};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3374 event_map[event] = map;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3375 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3376 handlers[event] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3377 end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3378 local function remove_handler(event, handler)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3379 local map = event_map[event];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3380 if map then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3381 map[handler] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3382 handlers[event] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3383 if next(map) == nil then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3384 event_map[event] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3385 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3386 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3387 end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3388 local function add_handlers(handlers)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3389 for event, handler in pairs(handlers) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3390 add_handler(event, handler);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3391 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3392 end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3393 local function remove_handlers(handlers)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3394 for event, handler in pairs(handlers) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3395 remove_handler(event, handler);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3396 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3397 end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3398 local function fire_event(event, ...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3399 local h = handlers[event];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3400 if h then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3401 for i=1,#h do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3402 local ret = h[i](...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3403 if ret ~= nil then return ret; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3404 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3405 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3406 end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3407 return {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3408 add_handler = add_handler;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3409 remove_handler = remove_handler;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3410 add_handlers = add_handlers;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3411 remove_handlers = remove_handlers;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3412 fire_event = fire_event;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3413 _handlers = handlers;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3414 _event_map = event_map;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3415 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3416 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3417
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3418 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3419 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3420 package.preload['util.dataforms'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3421 -- Prosody IM
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3422 -- Copyright (C) 2008-2010 Matthew Wild
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3423 -- Copyright (C) 2008-2010 Waqas Hussain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3424 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3425 -- This project is MIT/X11 licensed. Please see the
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3426 -- COPYING file in the source package for more information.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3427 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3428
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3429 local setmetatable = setmetatable;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3430 local pairs, ipairs = pairs, ipairs;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3431 local tostring, type = tostring, type;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3432 local t_concat = table.concat;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3433 local st = require "util.stanza";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3434
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3435 module "dataforms"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3436
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3437 local xmlns_forms = 'jabber:x:data';
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3438
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3439 local form_t = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3440 local form_mt = { __index = form_t };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3441
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3442 function new(layout)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3443 return setmetatable(layout, form_mt);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3444 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3445
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3446 function form_t.form(layout, data, formtype)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3447 local form = st.stanza("x", { xmlns = xmlns_forms, type = formtype or "form" });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3448 if layout.title then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3449 form:tag("title"):text(layout.title):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3450 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3451 if layout.instructions then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3452 form:tag("instructions"):text(layout.instructions):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3453 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3454 for n, field in ipairs(layout) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3455 local field_type = field.type or "text-single";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3456 -- Add field tag
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3457 form:tag("field", { type = field_type, var = field.name, label = field.label });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3458
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3459 local value = (data and data[field.name]) or field.value;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3460
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3461 if value then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3462 -- Add value, depending on type
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3463 if field_type == "hidden" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3464 if type(value) == "table" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3465 -- Assume an XML snippet
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3466 form:tag("value")
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3467 :add_child(value)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3468 :up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3469 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3470 form:tag("value"):text(tostring(value)):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3471 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3472 elseif field_type == "boolean" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3473 form:tag("value"):text((value and "1") or "0"):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3474 elseif field_type == "fixed" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3475
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3476 elseif field_type == "jid-multi" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3477 for _, jid in ipairs(value) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3478 form:tag("value"):text(jid):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3479 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3480 elseif field_type == "jid-single" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3481 form:tag("value"):text(value):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3482 elseif field_type == "text-single" or field_type == "text-private" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3483 form:tag("value"):text(value):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3484 elseif field_type == "text-multi" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3485 -- Split into multiple <value> tags, one for each line
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3486 for line in value:gmatch("([^\r\n]+)\r?\n*") do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3487 form:tag("value"):text(line):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3488 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3489 elseif field_type == "list-single" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3490 local has_default = false;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3491 if type(value) == "string" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3492 form:tag("value"):text(value):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3493 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3494 for _, val in ipairs(value) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3495 if type(val) == "table" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3496 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3497 if val.default and (not has_default) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3498 form:tag("value"):text(val.value):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3499 has_default = true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3500 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3501 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3502 form:tag("option", { label= val }):tag("value"):text(tostring(val)):up():up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3503 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3504 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3505 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3506 elseif field_type == "list-multi" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3507 for _, val in ipairs(value) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3508 if type(val) == "table" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3509 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3510 if val.default then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3511 form:tag("value"):text(val.value):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3512 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3513 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3514 form:tag("option", { label= val }):tag("value"):text(tostring(val)):up():up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3515 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3516 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3517 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3518 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3519
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3520 if field.required then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3521 form:tag("required"):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3522 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3523
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3524 -- Jump back up to list of fields
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3525 form:up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3526 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3527 return form;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3528 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3529
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3530 local field_readers = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3531
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3532 function form_t.data(layout, stanza)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3533 local data = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3534
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3535 for field_tag in stanza:childtags() do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3536 local field_type;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3537 for n, field in ipairs(layout) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3538 if field.name == field_tag.attr.var then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3539 field_type = field.type;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3540 break;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3541 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3542 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3543
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3544 local reader = field_readers[field_type];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3545 if reader then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3546 data[field_tag.attr.var] = reader(field_tag);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3547 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3548
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3549 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3550 return data;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3551 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3552
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3553 field_readers["text-single"] =
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3554 function (field_tag)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3555 local value = field_tag:child_with_name("value");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3556 if value then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3557 return value[1];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3558 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3559 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3560
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3561 field_readers["text-private"] =
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3562 field_readers["text-single"];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3563
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3564 field_readers["jid-single"] =
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3565 field_readers["text-single"];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3566
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3567 field_readers["jid-multi"] =
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3568 function (field_tag)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3569 local result = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3570 for value_tag in field_tag:childtags() do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3571 if value_tag.name == "value" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3572 result[#result+1] = value_tag[1];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3573 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3574 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3575 return result;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3576 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3577
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3578 field_readers["text-multi"] =
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3579 function (field_tag)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3580 local result = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3581 for value_tag in field_tag:childtags() do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3582 if value_tag.name == "value" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3583 result[#result+1] = value_tag[1];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3584 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3585 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3586 return t_concat(result, "\n");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3587 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3588
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3589 field_readers["list-single"] =
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3590 field_readers["text-single"];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3591
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3592 field_readers["list-multi"] =
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3593 function (field_tag)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3594 local result = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3595 for value_tag in field_tag:childtags() do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3596 if value_tag.name == "value" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3597 result[#result+1] = value_tag[1];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3598 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3599 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3600 return result;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3601 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3602
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3603 field_readers["boolean"] =
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3604 function (field_tag)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3605 local value = field_tag:child_with_name("value");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3606 if value then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3607 if value[1] == "1" or value[1] == "true" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3608 return true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3609 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3610 return false;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3611 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3612 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3613 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3614
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3615 field_readers["hidden"] =
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3616 function (field_tag)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3617 local value = field_tag:child_with_name("value");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3618 if value then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3619 return value[1];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3620 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3621 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3622
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3623 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3624
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3625
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3626 --[=[
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3627
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3628 Layout:
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3629 {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3630
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3631 title = "MUC Configuration",
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3632 instructions = [[Use this form to configure options for this MUC room.]],
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3633
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3634 { name = "FORM_TYPE", type = "hidden", required = true };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3635 { name = "field-name", type = "field-type", required = false };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3636 }
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3637
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3638
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3639 --]=]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3640 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3641 package.preload['util.serialization'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3642 -- Prosody IM
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3643 -- Copyright (C) 2008-2010 Matthew Wild
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3644 -- Copyright (C) 2008-2010 Waqas Hussain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3645 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3646 -- This project is MIT/X11 licensed. Please see the
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3647 -- COPYING file in the source package for more information.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3648 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3649
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3650 local string_rep = string.rep;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3651 local type = type;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3652 local tostring = tostring;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3653 local t_insert = table.insert;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3654 local t_concat = table.concat;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3655 local error = error;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3656 local pairs = pairs;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3657 local next = next;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3658
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3659 local loadstring = loadstring;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3660 local setfenv = setfenv;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3661 local pcall = pcall;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3662
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3663 local debug_traceback = debug.traceback;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3664 local log = require "util.logger".init("serialization");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3665 module "serialization"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3666
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3667 local indent = function(i)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3668 return string_rep("\t", i);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3669 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3670 local function basicSerialize (o)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3671 if type(o) == "number" or type(o) == "boolean" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3672 -- no need to check for NaN, as that's not a valid table index
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3673 if o == 1/0 then return "(1/0)";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3674 elseif o == -1/0 then return "(-1/0)";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3675 else return tostring(o); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3676 else -- assume it is a string -- FIXME make sure it's a string. throw an error otherwise.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3677 return (("%q"):format(tostring(o)):gsub("\\\n", "\\n"));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3678 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3679 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3680 local function _simplesave(o, ind, t, func)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3681 if type(o) == "number" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3682 if o ~= o then func(t, "(0/0)");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3683 elseif o == 1/0 then func(t, "(1/0)");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3684 elseif o == -1/0 then func(t, "(-1/0)");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3685 else func(t, tostring(o)); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3686 elseif type(o) == "string" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3687 func(t, (("%q"):format(o):gsub("\\\n", "\\n")));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3688 elseif type(o) == "table" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3689 if next(o) ~= nil then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3690 func(t, "{\n");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3691 for k,v in pairs(o) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3692 func(t, indent(ind));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3693 func(t, "[");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3694 func(t, basicSerialize(k));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3695 func(t, "] = ");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3696 if ind == 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3697 _simplesave(v, 0, t, func);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3698 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3699 _simplesave(v, ind+1, t, func);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3700 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3701 func(t, ";\n");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3702 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3703 func(t, indent(ind-1));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3704 func(t, "}");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3705 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3706 func(t, "{}");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3707 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3708 elseif type(o) == "boolean" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3709 func(t, (o and "true" or "false"));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3710 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3711 log("error", "cannot serialize a %s: %s", type(o), debug_traceback())
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3712 func(t, "nil");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3713 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3714 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3715
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3716 function append(t, o)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3717 _simplesave(o, 1, t, t.write or t_insert);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3718 return t;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3719 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3720
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3721 function serialize(o)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3722 return t_concat(append({}, o));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3723 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3724
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3725 function deserialize(str)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3726 if type(str) ~= "string" then return nil; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3727 str = "return "..str;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3728 local f, err = loadstring(str, "@data");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3729 if not f then return nil, err; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3730 setfenv(f, {});
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3731 local success, ret = pcall(f);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3732 if not success then return nil, ret; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3733 return ret;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3734 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3735
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3736 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3737 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3738 package.preload['verse.plugins.presence'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3739 function verse.plugins.presence(stream)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3740 stream.last_presence = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3741
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3742 stream:hook("presence-out", function (presence)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3743 if not presence.attr.to then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3744 stream.last_presence = presence; -- Cache non-directed presence
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3745 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3746 end, 1);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3747
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3748 function stream:resend_presence()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3749 if last_presence then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3750 stream:send(last_presence);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3751 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3752 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3753
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3754 function stream:set_status(opts)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3755 local p = verse.presence();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3756 if type(opts) == "table" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3757 if opts.show then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3758 p:tag("show"):text(opts.show):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3759 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3760 if opts.prio then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3761 p:tag("priority"):text(tostring(opts.prio)):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3762 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3763 if opts.msg then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3764 p:tag("status"):text(opts.msg):up();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3765 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3766 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3767 -- TODO maybe use opts as prio if it's a int,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3768 -- or as show or status if it's a string?
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3769
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3770 stream:send(p);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3771 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3772 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3773 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3774 package.preload['verse.plugins.groupchat'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3775 local events = require "events";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3776
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3777 local room_mt = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3778 room_mt.__index = room_mt;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3779
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3780 local xmlns_delay = "urn:xmpp:delay";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3781 local xmlns_muc = "http://jabber.org/protocol/muc";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3782
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3783 function verse.plugins.groupchat(stream)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3784 stream:add_plugin("presence")
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3785 stream.rooms = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3786
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3787 stream:hook("stanza", function (stanza)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3788 local room_jid = jid.bare(stanza.attr.from);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3789 if not room_jid then return end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3790 local room = stream.rooms[room_jid]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3791 if not room and stanza.attr.to and room_jid then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3792 room = stream.rooms[stanza.attr.to.." "..room_jid]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3793 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3794 if room and room.opts.source and stanza.attr.to ~= room.opts.source then return end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3795 if room then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3796 local nick = select(3, jid.split(stanza.attr.from));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3797 local body = stanza:get_child("body");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3798 local delay = stanza:get_child("delay", xmlns_delay);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3799 local event = {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3800 room_jid = room_jid;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3801 room = room;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3802 sender = room.occupants[nick];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3803 nick = nick;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3804 body = (body and body:get_text()) or nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3805 stanza = stanza;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3806 delay = (delay and delay.attr.stamp);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3807 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3808 local ret = room:event(stanza.name, event);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3809 return ret or (stanza.name == "message") or nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3810 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3811 end, 500);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3812
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3813 function stream:join_room(jid, nick, opts)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3814 if not nick then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3815 return false, "no nickname supplied"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3816 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3817 opts = opts or {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3818 local room = setmetatable({
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3819 stream = stream, jid = jid, nick = nick,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3820 subject = nil,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3821 occupants = {},
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3822 opts = opts,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3823 events = events.new()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3824 }, room_mt);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3825 if opts.source then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3826 self.rooms[opts.source.." "..jid] = room;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3827 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3828 self.rooms[jid] = room;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3829 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3830 local occupants = room.occupants;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3831 room:hook("presence", function (presence)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3832 local nick = presence.nick or nick;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3833 if not occupants[nick] and presence.stanza.attr.type ~= "unavailable" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3834 occupants[nick] = {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3835 nick = nick;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3836 jid = presence.stanza.attr.from;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3837 presence = presence.stanza;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3838 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3839 local x = presence.stanza:get_child("x", xmlns_muc .. "#user");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3840 if x then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3841 local x_item = x:get_child("item");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3842 if x_item and x_item.attr then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3843 occupants[nick].real_jid = x_item.attr.jid;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3844 occupants[nick].affiliation = x_item.attr.affiliation;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3845 occupants[nick].role = x_item.attr.role;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3846 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3847 --TODO Check for status 100?
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3848 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3849 if nick == room.nick then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3850 room.stream:event("groupchat/joined", room);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3851 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3852 room:event("occupant-joined", occupants[nick]);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3853 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3854 elseif occupants[nick] and presence.stanza.attr.type == "unavailable" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3855 if nick == room.nick then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3856 room.stream:event("groupchat/left", room);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3857 if room.opts.source then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3858 self.rooms[room.opts.source.." "..jid] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3859 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3860 self.rooms[jid] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3861 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3862 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3863 occupants[nick].presence = presence.stanza;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3864 room:event("occupant-left", occupants[nick]);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3865 occupants[nick] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3866 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3867 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3868 end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3869 room:hook("message", function(msg)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3870 local subject = msg.stanza:get_child_text("subject");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3871 if not subject then return end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3872 subject = #subject > 0 and subject or nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3873 if subject ~= room.subject then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3874 local old_subject = room.subject;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3875 room.subject = subject;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3876 return self:event("subject-changed", { from = old_subject, to = subject, by = msg.sender });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3877 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3878 end, 2000);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3879 local join_st = verse.presence():tag("x",{xmlns = xmlns_muc}):reset();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3880 self:event("pre-groupchat/joining", join_st);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3881 room:send(join_st)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3882 self:event("groupchat/joining", room);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3883 return room;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3884 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3885
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3886 stream:hook("presence-out", function(presence)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3887 if not presence.attr.to then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3888 for _, room in pairs(stream.rooms) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3889 room:send(presence);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3890 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3891 presence.attr.to = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3892 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3893 end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3894 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3895
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3896 function room_mt:send(stanza)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3897 if stanza.name == "message" and not stanza.attr.type then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3898 stanza.attr.type = "groupchat";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3899 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3900 if stanza.name == "presence" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3901 stanza.attr.to = self.jid .."/"..self.nick;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3902 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3903 if stanza.attr.type == "groupchat" or not stanza.attr.to then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3904 stanza.attr.to = self.jid;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3905 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3906 if self.opts.source then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3907 stanza.attr.from = self.opts.source
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3908 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3909 self.stream:send(stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3910 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3911
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3912 function room_mt:send_message(text)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3913 self:send(verse.message():tag("body"):text(text));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3914 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3915
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3916 function room_mt:set_subject(text)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3917 self:send(verse.message():tag("subject"):text(text));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3918 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3919
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3920 function room_mt:leave(message)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3921 self.stream:event("groupchat/leaving", self);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3922 self:send(verse.presence({type="unavailable"}));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3923 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3924
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3925 function room_mt:admin_set(nick, what, value, reason)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3926 self:send(verse.iq({type="set"})
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3927 :query(xmlns_muc .. "#admin")
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3928 :tag("item", {nick = nick, [what] = value})
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3929 :tag("reason"):text(reason or ""));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3930 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3931
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3932 function room_mt:set_role(nick, role, reason)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3933 self:admin_set(nick, "role", role, reason);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3934 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3935
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3936 function room_mt:set_affiliation(nick, affiliation, reason)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3937 self:admin_set(nick, "affiliation", affiliation, reason);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3938 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3939
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3940 function room_mt:kick(nick, reason)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3941 self:set_role(nick, "none", reason);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3942 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3943
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3944 function room_mt:ban(nick, reason)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3945 self:set_affiliation(nick, "outcast", reason);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3946 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3947
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3948 function room_mt:event(name, arg)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3949 self.stream:debug("Firing room event: %s", name);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3950 return self.events.fire_event(name, arg);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3951 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3952
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3953 function room_mt:hook(name, callback, priority)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3954 return self.events.add_handler(name, callback, priority);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3955 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3956 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3957 package.preload['net.httpclient_listener'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3958 -- Prosody IM
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3959 -- Copyright (C) 2008-2010 Matthew Wild
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3960 -- Copyright (C) 2008-2010 Waqas Hussain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3961 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3962 -- This project is MIT/X11 licensed. Please see the
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3963 -- COPYING file in the source package for more information.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3964 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3965
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3966 local log = require "util.logger".init("httpclient_listener");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3967
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3968 local connlisteners_register = require "net.connlisteners".register;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3969
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3970 local requests = {}; -- Open requests
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3971 local buffers = {}; -- Buffers of partial lines
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3972
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3973 local httpclient = { default_port = 80, default_mode = "*a" };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3974
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3975 function httpclient.onincoming(conn, data)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3976 local request = requests[conn];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3977
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3978 if not request then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3979 log("warn", "Received response from connection %s with no request attached!", tostring(conn));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3980 return;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3981 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3982
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3983 if data and request.reader then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3984 request:reader(data);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3985 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3986 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3987
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3988 function httpclient.ondisconnect(conn, err)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3989 local request = requests[conn];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3990 if request and err ~= "closed" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3991 request:reader(nil);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3992 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3993 requests[conn] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3994 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3995
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3996 function httpclient.register_request(conn, req)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3997 log("debug", "Attaching request %s to connection %s", tostring(req.id or req), tostring(conn));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3998 requests[conn] = req;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
3999 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4000
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4001 connlisteners_register("httpclient", httpclient);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4002 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4003 package.preload['net.connlisteners'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4004 -- Prosody IM
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4005 -- Copyright (C) 2008-2010 Matthew Wild
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4006 -- Copyright (C) 2008-2010 Waqas Hussain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4007 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4008 -- This project is MIT/X11 licensed. Please see the
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4009 -- COPYING file in the source package for more information.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4010 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4011
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4012
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4013
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4014 local listeners_dir = (CFG_SOURCEDIR or ".").."/net/";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4015 local server = require "net.server";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4016 local log = require "util.logger".init("connlisteners");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4017 local tostring = tostring;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4018 local type = type
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4019 local ipairs = ipairs
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4020
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4021 local dofile, xpcall, error =
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4022 dofile, xpcall, error
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4023
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4024 local debug_traceback = debug.traceback;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4025
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4026 module "connlisteners"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4027
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4028 local listeners = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4029
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4030 function register(name, listener)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4031 if listeners[name] and listeners[name] ~= listener then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4032 log("debug", "Listener %s is already registered, not registering any more", name);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4033 return false;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4034 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4035 listeners[name] = listener;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4036 log("debug", "Registered connection listener %s", name);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4037 return true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4038 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4039
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4040 function deregister(name)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4041 listeners[name] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4042 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4043
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4044 function get(name)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4045 local h = listeners[name];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4046 if not h then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4047 local ok, ret = xpcall(function() dofile(listeners_dir..name:gsub("[^%w%-]", "_").."_listener.lua") end, debug_traceback);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4048 if not ok then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4049 log("error", "Error while loading listener '%s': %s", tostring(name), tostring(ret));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4050 return nil, ret;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4051 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4052 h = listeners[name];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4053 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4054 return h;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4055 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4056
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4057 function start(name, udata)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4058 local h, err = get(name);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4059 if not h then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4060 error("No such connection module: "..name.. (err and (" ("..err..")") or ""), 0);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4061 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4062
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4063 local interfaces = (udata and udata.interface) or h.default_interface or "*";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4064 if type(interfaces) == "string" then interfaces = {interfaces}; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4065 local port = (udata and udata.port) or h.default_port or error("Can't start listener "..name.." because no port was specified, and it has no default port", 0);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4066 local mode = (udata and udata.mode) or h.default_mode or 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4067 local ssl = (udata and udata.ssl) or nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4068 local autossl = udata and udata.type == "ssl";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4069
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4070 if autossl and not ssl then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4071 return nil, "no ssl context";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4072 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4073
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4074 ok, err = true, {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4075 for _, interface in ipairs(interfaces) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4076 local handler
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4077 handler, err[interface] = server.addserver(interface, port, h, mode, autossl and ssl or nil);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4078 ok = ok and handler;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4079 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4080
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4081 return ok, err;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4082 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4083
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4084 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4085 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4086 package.preload['util.httpstream'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4087
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4088 local coroutine = coroutine;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4089 local tonumber = tonumber;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4090
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4091 local deadroutine = coroutine.create(function() end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4092 coroutine.resume(deadroutine);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4093
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4094 module("httpstream")
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4095
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4096 local function parser(success_cb, parser_type, options_cb)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4097 local data = coroutine.yield();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4098 local function readline()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4099 local pos = data:find("\r\n", nil, true);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4100 while not pos do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4101 data = data..coroutine.yield();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4102 pos = data:find("\r\n", nil, true);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4103 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4104 local r = data:sub(1, pos-1);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4105 data = data:sub(pos+2);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4106 return r;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4107 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4108 local function readlength(n)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4109 while #data < n do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4110 data = data..coroutine.yield();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4111 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4112 local r = data:sub(1, n);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4113 data = data:sub(n + 1);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4114 return r;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4115 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4116 local function readheaders()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4117 local headers = {}; -- read headers
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4118 while true do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4119 local line = readline();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4120 if line == "" then break; end -- headers done
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4121 local key, val = line:match("^([^%s:]+): *(.*)$");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4122 if not key then coroutine.yield("invalid-header-line"); end -- TODO handle multi-line and invalid headers
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4123 key = key:lower();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4124 headers[key] = headers[key] and headers[key]..","..val or val;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4125 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4126 return headers;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4127 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4128
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4129 if not parser_type or parser_type == "server" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4130 while true do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4131 -- read status line
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4132 local status_line = readline();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4133 local method, path, httpversion = status_line:match("^(%S+)%s+(%S+)%s+HTTP/(%S+)$");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4134 if not method then coroutine.yield("invalid-status-line"); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4135 path = path:gsub("^//+", "/"); -- TODO parse url more
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4136 local headers = readheaders();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4137
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4138 -- read body
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4139 local len = tonumber(headers["content-length"]);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4140 len = len or 0; -- TODO check for invalid len
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4141 local body = readlength(len);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4142
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4143 success_cb({
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4144 method = method;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4145 path = path;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4146 httpversion = httpversion;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4147 headers = headers;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4148 body = body;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4149 });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4150 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4151 elseif parser_type == "client" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4152 while true do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4153 -- read status line
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4154 local status_line = readline();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4155 local httpversion, status_code, reason_phrase = status_line:match("^HTTP/(%S+)%s+(%d%d%d)%s+(.*)$");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4156 status_code = tonumber(status_code);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4157 if not status_code then coroutine.yield("invalid-status-line"); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4158 local headers = readheaders();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4159
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4160 -- read body
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4161 local have_body = not
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4162 ( (options_cb and options_cb().method == "HEAD")
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4163 or (status_code == 204 or status_code == 304 or status_code == 301)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4164 or (status_code >= 100 and status_code < 200) );
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4165
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4166 local body;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4167 if have_body then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4168 local len = tonumber(headers["content-length"]);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4169 if headers["transfer-encoding"] == "chunked" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4170 body = "";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4171 while true do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4172 local chunk_size = readline():match("^%x+");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4173 if not chunk_size then coroutine.yield("invalid-chunk-size"); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4174 chunk_size = tonumber(chunk_size, 16)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4175 if chunk_size == 0 then break; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4176 body = body..readlength(chunk_size);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4177 if readline() ~= "" then coroutine.yield("invalid-chunk-ending"); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4178 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4179 local trailers = readheaders();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4180 elseif len then -- TODO check for invalid len
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4181 body = readlength(len);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4182 else -- read to end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4183 repeat
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4184 local newdata = coroutine.yield();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4185 data = data..newdata;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4186 until newdata == "";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4187 body, data = data, "";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4188 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4189 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4190
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4191 success_cb({
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4192 code = status_code;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4193 httpversion = httpversion;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4194 headers = headers;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4195 body = body;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4196 -- COMPAT the properties below are deprecated
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4197 responseversion = httpversion;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4198 responseheaders = headers;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4199 });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4200 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4201 else coroutine.yield("unknown-parser-type"); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4202 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4203
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4204 function new(success_cb, error_cb, parser_type, options_cb)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4205 local co = coroutine.create(parser);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4206 coroutine.resume(co, success_cb, parser_type, options_cb)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4207 return {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4208 feed = function(self, data)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4209 if not data then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4210 if parser_type == "client" then coroutine.resume(co, ""); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4211 co = deadroutine;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4212 return error_cb();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4213 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4214 local success, result = coroutine.resume(co, data);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4215 if result then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4216 co = deadroutine;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4217 return error_cb(result);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4218 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4219 end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4220 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4221 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4222
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4223 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4224 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4225 package.preload['net.http'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4226 -- Prosody IM
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4227 -- Copyright (C) 2008-2010 Matthew Wild
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4228 -- Copyright (C) 2008-2010 Waqas Hussain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4229 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4230 -- This project is MIT/X11 licensed. Please see the
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4231 -- COPYING file in the source package for more information.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4232 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4233
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4234
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4235 local socket = require "socket"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4236 local mime = require "mime"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4237 local url = require "socket.url"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4238 local httpstream_new = require "util.httpstream".new;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4239
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4240 local server = require "net.server"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4241
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4242 local connlisteners_get = require "net.connlisteners".get;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4243 local listener = connlisteners_get("httpclient") or error("No httpclient listener!");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4244
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4245 local t_insert, t_concat = table.insert, table.concat;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4246 local pairs, ipairs = pairs, ipairs;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4247 local tonumber, tostring, xpcall, select, debug_traceback, char, format =
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4248 tonumber, tostring, xpcall, select, debug.traceback, string.char, string.format;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4249
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4250 local log = require "util.logger".init("http");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4251
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4252 module "http"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4253
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4254 function urlencode(s) return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4255 function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return char(tonumber(c,16)); end)); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4256
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4257 local function _formencodepart(s)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4258 return s and (s:gsub("%W", function (c)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4259 if c ~= " " then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4260 return format("%%%02x", c:byte());
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4261 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4262 return "+";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4263 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4264 end));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4265 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4266 function formencode(form)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4267 local result = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4268 for _, field in ipairs(form) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4269 t_insert(result, _formencodepart(field.name).."=".._formencodepart(field.value));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4270 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4271 return t_concat(result, "&");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4272 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4273
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4274 local function request_reader(request, data, startpos)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4275 if not request.parser then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4276 local function success_cb(r)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4277 if request.callback then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4278 for k,v in pairs(r) do request[k] = v; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4279 request.callback(r.body, r.code, request);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4280 request.callback = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4281 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4282 destroy_request(request);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4283 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4284 local function error_cb(r)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4285 if request.callback then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4286 request.callback(r or "connection-closed", 0, request);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4287 request.callback = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4288 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4289 destroy_request(request);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4290 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4291 local function options_cb()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4292 return request;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4293 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4294 request.parser = httpstream_new(success_cb, error_cb, "client", options_cb);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4295 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4296 request.parser:feed(data);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4297 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4298
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4299 local function handleerr(err) log("error", "Traceback[http]: %s: %s", tostring(err), debug_traceback()); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4300 function request(u, ex, callback)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4301 local req = url.parse(u);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4302
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4303 if not (req and req.host) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4304 callback(nil, 0, req);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4305 return nil, "invalid-url";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4306 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4307
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4308 if not req.path then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4309 req.path = "/";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4310 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4311
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4312 local custom_headers, body;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4313 local default_headers = { ["Host"] = req.host, ["User-Agent"] = "Prosody XMPP Server" }
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4314
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4315
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4316 if req.userinfo then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4317 default_headers["Authorization"] = "Basic "..mime.b64(req.userinfo);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4318 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4319
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4320 if ex then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4321 custom_headers = ex.headers;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4322 req.onlystatus = ex.onlystatus;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4323 body = ex.body;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4324 if body then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4325 req.method = "POST ";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4326 default_headers["Content-Length"] = tostring(#body);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4327 default_headers["Content-Type"] = "application/x-www-form-urlencoded";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4328 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4329 if ex.method then req.method = ex.method; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4330 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4331
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4332 req.handler, req.conn = server.wrapclient(socket.tcp(), req.host, req.port or 80, listener, "*a");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4333 req.write = function (...) return req.handler:write(...); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4334 req.conn:settimeout(0);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4335 local ok, err = req.conn:connect(req.host, req.port or 80);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4336 if not ok and err ~= "timeout" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4337 callback(nil, 0, req);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4338 return nil, err;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4339 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4340
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4341 local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4342
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4343 if req.query then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4344 t_insert(request_line, 4, "?");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4345 t_insert(request_line, 5, req.query);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4346 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4347
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4348 req.write(t_concat(request_line));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4349 local t = { [2] = ": ", [4] = "\r\n" };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4350 if custom_headers then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4351 for k, v in pairs(custom_headers) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4352 t[1], t[3] = k, v;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4353 req.write(t_concat(t));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4354 default_headers[k] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4355 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4356 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4357
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4358 for k, v in pairs(default_headers) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4359 t[1], t[3] = k, v;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4360 req.write(t_concat(t));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4361 default_headers[k] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4362 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4363 req.write("\r\n");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4364
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4365 if body then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4366 req.write(body);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4367 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4368
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4369 req.callback = function (content, code, request) log("debug", "Calling callback, status %s", code or "---"); return select(2, xpcall(function () return callback(content, code, request) end, handleerr)); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4370 req.reader = request_reader;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4371 req.state = "status";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4372
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4373 listener.register_request(req.handler, req);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4374
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4375 return req;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4376 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4377
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4378 function destroy_request(request)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4379 if request.conn then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4380 request.conn = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4381 request.handler:close()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4382 listener.ondisconnect(request.handler, "closed");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4383 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4384 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4385
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4386 _M.urlencode = urlencode;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4387
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4388 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4389 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4390 package.preload['verse.bosh'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4391
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4392 local new_xmpp_stream = require "util.xmppstream".new;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4393 local st = require "util.stanza";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4394 require "net.httpclient_listener"; -- Required for net.http to work
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4395 local http = require "net.http";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4396
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4397 local stream_mt = setmetatable({}, { __index = verse.stream_mt });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4398 stream_mt.__index = stream_mt;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4399
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4400 local xmlns_stream = "http://etherx.jabber.org/streams";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4401 local xmlns_bosh = "http://jabber.org/protocol/httpbind";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4402
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4403 local reconnect_timeout = 5;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4404
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4405 function verse.new_bosh(logger, url)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4406 local stream = {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4407 bosh_conn_pool = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4408 bosh_waiting_requests = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4409 bosh_rid = math.random(1,999999);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4410 bosh_outgoing_buffer = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4411 bosh_url = url;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4412 conn = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4413 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4414 function stream:reopen()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4415 self.bosh_need_restart = true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4416 self:flush();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4417 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4418 local conn = verse.new(logger, stream);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4419 return setmetatable(conn, stream_mt);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4420 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4421
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4422 function stream_mt:connect()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4423 self:_send_session_request();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4424 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4425
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4426 function stream_mt:send(data)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4427 self:debug("Putting into BOSH send buffer: %s", tostring(data));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4428 self.bosh_outgoing_buffer[#self.bosh_outgoing_buffer+1] = st.clone(data);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4429 self:flush(); --TODO: Optimize by doing this on next tick (give a chance for data to buffer)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4430 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4431
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4432 function stream_mt:flush()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4433 if self.connected
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4434 and #self.bosh_waiting_requests < self.bosh_max_requests
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4435 and (#self.bosh_waiting_requests == 0
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4436 or #self.bosh_outgoing_buffer > 0
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4437 or self.bosh_need_restart) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4438 self:debug("Flushing...");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4439 local payload = self:_make_body();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4440 local buffer = self.bosh_outgoing_buffer;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4441 for i, stanza in ipairs(buffer) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4442 payload:add_child(stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4443 buffer[i] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4444 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4445 self:_make_request(payload);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4446 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4447 self:debug("Decided not to flush.");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4448 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4449 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4450
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4451 function stream_mt:_make_request(payload)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4452 local request, err = http.request(self.bosh_url, { body = tostring(payload) }, function (response, code, request)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4453 if code ~= 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4454 self.inactive_since = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4455 return self:_handle_response(response, code, request);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4456 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4457
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4458 -- Connection issues, we need to retry this request
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4459 local time = os.time();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4460 if not self.inactive_since then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4461 self.inactive_since = time; -- So we know when it is time to give up
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4462 elseif time - self.inactive_since > self.bosh_max_inactivity then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4463 return self:_disconnected();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4464 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4465 self:debug("%d seconds left to reconnect, retrying in %d seconds...",
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4466 self.bosh_max_inactivity - (time - self.inactive_since), reconnect_timeout);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4467 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4468
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4469 -- Set up reconnect timer
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4470 timer.add_task(reconnect_timeout, function ()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4471 self:debug("Retrying request...");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4472 -- Remove old request
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4473 for i, waiting_request in ipairs(self.bosh_waiting_requests) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4474 if waiting_request == request then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4475 table.remove(self.bosh_waiting_requests, i);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4476 break;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4477 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4478 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4479 self:_make_request(payload);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4480 end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4481 end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4482 if request then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4483 table.insert(self.bosh_waiting_requests, request);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4484 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4485 self:warn("Request failed instantly: %s", err);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4486 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4487 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4488
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4489 function stream_mt:_disconnected()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4490 self.connected = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4491 self:event("disconnected");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4492 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4493
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4494 function stream_mt:_send_session_request()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4495 local body = self:_make_body();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4496
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4497 -- XEP-0124
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4498 body.attr.hold = "1";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4499 body.attr.wait = "60";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4500 body.attr["xml:lang"] = "en";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4501 body.attr.ver = "1.6";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4502
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4503 -- XEP-0206
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4504 body.attr.from = self.jid;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4505 body.attr.to = self.host;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4506 body.attr.secure = 'true';
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4507
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4508 http.request(self.bosh_url, { body = tostring(body) }, function (response, code)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4509 if code == 0 then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4510 -- Failed to connect
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4511 return self:_disconnected();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4512 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4513 -- Handle session creation response
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4514 local payload = self:_parse_response(response)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4515 if not payload then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4516 self:warn("Invalid session creation response");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4517 self:_disconnected();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4518 return;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4519 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4520 self.bosh_sid = payload.attr.sid; -- Session id
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4521 self.bosh_wait = tonumber(payload.attr.wait); -- How long the server may hold connections for
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4522 self.bosh_hold = tonumber(payload.attr.hold); -- How many connections the server may hold
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4523 self.bosh_max_inactivity = tonumber(payload.attr.inactivity); -- Max amount of time with no connections
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4524 self.bosh_max_requests = tonumber(payload.attr.requests) or self.bosh_hold; -- Max simultaneous requests we can make
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4525 self.connected = true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4526 self:event("connected");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4527 self:_handle_response_payload(payload);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4528 end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4529 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4530
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4531 function stream_mt:_handle_response(response, code, request)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4532 if self.bosh_waiting_requests[1] ~= request then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4533 self:warn("Server replied to request that wasn't the oldest");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4534 for i, waiting_request in ipairs(self.bosh_waiting_requests) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4535 if waiting_request == request then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4536 self.bosh_waiting_requests[i] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4537 break;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4538 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4539 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4540 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4541 table.remove(self.bosh_waiting_requests, 1);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4542 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4543 local payload = self:_parse_response(response);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4544 if payload then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4545 self:_handle_response_payload(payload);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4546 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4547 self:flush();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4548 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4549
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4550 function stream_mt:_handle_response_payload(payload)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4551 for stanza in payload:childtags() do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4552 if stanza.attr.xmlns == xmlns_stream then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4553 self:event("stream-"..stanza.name, stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4554 elseif stanza.attr.xmlns then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4555 self:event("stream/"..stanza.attr.xmlns, stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4556 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4557 self:event("stanza", stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4558 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4559 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4560 if payload.attr.type == "terminate" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4561 self:_disconnected({reason = payload.attr.condition});
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4562 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4563 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4564
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4565 local stream_callbacks = {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4566 stream_ns = "http://jabber.org/protocol/httpbind", stream_tag = "body",
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4567 default_ns = "jabber:client",
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4568 streamopened = function (session, attr) session.notopen = nil; session.payload = verse.stanza("body", attr); return true; end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4569 handlestanza = function (session, stanza) session.payload:add_child(stanza); end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4570 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4571 function stream_mt:_parse_response(response)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4572 self:debug("Parsing response: %s", response);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4573 if response == nil then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4574 self:debug("%s", debug.traceback());
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4575 self:_disconnected();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4576 return;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4577 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4578 local session = { notopen = true, log = self.log };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4579 local stream = new_xmpp_stream(session, stream_callbacks);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4580 stream:feed(response);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4581 return session.payload;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4582 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4583
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4584 function stream_mt:_make_body()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4585 self.bosh_rid = self.bosh_rid + 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4586 local body = verse.stanza("body", {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4587 xmlns = xmlns_bosh;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4588 content = "text/xml; charset=utf-8";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4589 sid = self.bosh_sid;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4590 rid = self.bosh_rid;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4591 });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4592 if self.bosh_need_restart then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4593 self.bosh_need_restart = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4594 body.attr.restart = 'true';
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4595 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4596 return body;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4597 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4598 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4599 package.preload['bit'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4600 -- Prosody IM
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4601 -- Copyright (C) 2008-2010 Matthew Wild
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4602 -- Copyright (C) 2008-2010 Waqas Hussain
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4603 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4604 -- This project is MIT/X11 licensed. Please see the
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4605 -- COPYING file in the source package for more information.
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4606 --
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4607
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4608
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4609 local type = type;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4610 local tonumber = tonumber;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4611 local setmetatable = setmetatable;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4612 local error = error;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4613 local tostring = tostring;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4614 local print = print;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4615
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4616 local xor_map = {[0]=0;[1]=1;[2]=2;[3]=3;[4]=4;[5]=5;[6]=6;[7]=7;[8]=8;[9]=9;[10]=10;[11]=11;[12]=12;[13]=13;[14]=14;[15]=15;[16]=1;[17]=0;[18]=3;[19]=2;[20]=5;[21]=4;[22]=7;[23]=6;[24]=9;[25]=8;[26]=11;[27]=10;[28]=13;[29]=12;[30]=15;[31]=14;[32]=2;[33]=3;[34]=0;[35]=1;[36]=6;[37]=7;[38]=4;[39]=5;[40]=10;[41]=11;[42]=8;[43]=9;[44]=14;[45]=15;[46]=12;[47]=13;[48]=3;[49]=2;[50]=1;[51]=0;[52]=7;[53]=6;[54]=5;[55]=4;[56]=11;[57]=10;[58]=9;[59]=8;[60]=15;[61]=14;[62]=13;[63]=12;[64]=4;[65]=5;[66]=6;[67]=7;[68]=0;[69]=1;[70]=2;[71]=3;[72]=12;[73]=13;[74]=14;[75]=15;[76]=8;[77]=9;[78]=10;[79]=11;[80]=5;[81]=4;[82]=7;[83]=6;[84]=1;[85]=0;[86]=3;[87]=2;[88]=13;[89]=12;[90]=15;[91]=14;[92]=9;[93]=8;[94]=11;[95]=10;[96]=6;[97]=7;[98]=4;[99]=5;[100]=2;[101]=3;[102]=0;[103]=1;[104]=14;[105]=15;[106]=12;[107]=13;[108]=10;[109]=11;[110]=8;[111]=9;[112]=7;[113]=6;[114]=5;[115]=4;[116]=3;[117]=2;[118]=1;[119]=0;[120]=15;[121]=14;[122]=13;[123]=12;[124]=11;[125]=10;[126]=9;[127]=8;[128]=8;[129]=9;[130]=10;[131]=11;[132]=12;[133]=13;[134]=14;[135]=15;[136]=0;[137]=1;[138]=2;[139]=3;[140]=4;[141]=5;[142]=6;[143]=7;[144]=9;[145]=8;[146]=11;[147]=10;[148]=13;[149]=12;[150]=15;[151]=14;[152]=1;[153]=0;[154]=3;[155]=2;[156]=5;[157]=4;[158]=7;[159]=6;[160]=10;[161]=11;[162]=8;[163]=9;[164]=14;[165]=15;[166]=12;[167]=13;[168]=2;[169]=3;[170]=0;[171]=1;[172]=6;[173]=7;[174]=4;[175]=5;[176]=11;[177]=10;[178]=9;[179]=8;[180]=15;[181]=14;[182]=13;[183]=12;[184]=3;[185]=2;[186]=1;[187]=0;[188]=7;[189]=6;[190]=5;[191]=4;[192]=12;[193]=13;[194]=14;[195]=15;[196]=8;[197]=9;[198]=10;[199]=11;[200]=4;[201]=5;[202]=6;[203]=7;[204]=0;[205]=1;[206]=2;[207]=3;[208]=13;[209]=12;[210]=15;[211]=14;[212]=9;[213]=8;[214]=11;[215]=10;[216]=5;[217]=4;[218]=7;[219]=6;[220]=1;[221]=0;[222]=3;[223]=2;[224]=14;[225]=15;[226]=12;[227]=13;[228]=10;[229]=11;[230]=8;[231]=9;[232]=6;[233]=7;[234]=4;[235]=5;[236]=2;[237]=3;[238]=0;[239]=1;[240]=15;[241]=14;[242]=13;[243]=12;[244]=11;[245]=10;[246]=9;[247]=8;[248]=7;[249]=6;[250]=5;[251]=4;[252]=3;[253]=2;[254]=1;[255]=0;};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4617 local or_map = {[0]=0;[1]=1;[2]=2;[3]=3;[4]=4;[5]=5;[6]=6;[7]=7;[8]=8;[9]=9;[10]=10;[11]=11;[12]=12;[13]=13;[14]=14;[15]=15;[16]=1;[17]=1;[18]=3;[19]=3;[20]=5;[21]=5;[22]=7;[23]=7;[24]=9;[25]=9;[26]=11;[27]=11;[28]=13;[29]=13;[30]=15;[31]=15;[32]=2;[33]=3;[34]=2;[35]=3;[36]=6;[37]=7;[38]=6;[39]=7;[40]=10;[41]=11;[42]=10;[43]=11;[44]=14;[45]=15;[46]=14;[47]=15;[48]=3;[49]=3;[50]=3;[51]=3;[52]=7;[53]=7;[54]=7;[55]=7;[56]=11;[57]=11;[58]=11;[59]=11;[60]=15;[61]=15;[62]=15;[63]=15;[64]=4;[65]=5;[66]=6;[67]=7;[68]=4;[69]=5;[70]=6;[71]=7;[72]=12;[73]=13;[74]=14;[75]=15;[76]=12;[77]=13;[78]=14;[79]=15;[80]=5;[81]=5;[82]=7;[83]=7;[84]=5;[85]=5;[86]=7;[87]=7;[88]=13;[89]=13;[90]=15;[91]=15;[92]=13;[93]=13;[94]=15;[95]=15;[96]=6;[97]=7;[98]=6;[99]=7;[100]=6;[101]=7;[102]=6;[103]=7;[104]=14;[105]=15;[106]=14;[107]=15;[108]=14;[109]=15;[110]=14;[111]=15;[112]=7;[113]=7;[114]=7;[115]=7;[116]=7;[117]=7;[118]=7;[119]=7;[120]=15;[121]=15;[122]=15;[123]=15;[124]=15;[125]=15;[126]=15;[127]=15;[128]=8;[129]=9;[130]=10;[131]=11;[132]=12;[133]=13;[134]=14;[135]=15;[136]=8;[137]=9;[138]=10;[139]=11;[140]=12;[141]=13;[142]=14;[143]=15;[144]=9;[145]=9;[146]=11;[147]=11;[148]=13;[149]=13;[150]=15;[151]=15;[152]=9;[153]=9;[154]=11;[155]=11;[156]=13;[157]=13;[158]=15;[159]=15;[160]=10;[161]=11;[162]=10;[163]=11;[164]=14;[165]=15;[166]=14;[167]=15;[168]=10;[169]=11;[170]=10;[171]=11;[172]=14;[173]=15;[174]=14;[175]=15;[176]=11;[177]=11;[178]=11;[179]=11;[180]=15;[181]=15;[182]=15;[183]=15;[184]=11;[185]=11;[186]=11;[187]=11;[188]=15;[189]=15;[190]=15;[191]=15;[192]=12;[193]=13;[194]=14;[195]=15;[196]=12;[197]=13;[198]=14;[199]=15;[200]=12;[201]=13;[202]=14;[203]=15;[204]=12;[205]=13;[206]=14;[207]=15;[208]=13;[209]=13;[210]=15;[211]=15;[212]=13;[213]=13;[214]=15;[215]=15;[216]=13;[217]=13;[218]=15;[219]=15;[220]=13;[221]=13;[222]=15;[223]=15;[224]=14;[225]=15;[226]=14;[227]=15;[228]=14;[229]=15;[230]=14;[231]=15;[232]=14;[233]=15;[234]=14;[235]=15;[236]=14;[237]=15;[238]=14;[239]=15;[240]=15;[241]=15;[242]=15;[243]=15;[244]=15;[245]=15;[246]=15;[247]=15;[248]=15;[249]=15;[250]=15;[251]=15;[252]=15;[253]=15;[254]=15;[255]=15;};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4618 local and_map = {[0]=0;[1]=0;[2]=0;[3]=0;[4]=0;[5]=0;[6]=0;[7]=0;[8]=0;[9]=0;[10]=0;[11]=0;[12]=0;[13]=0;[14]=0;[15]=0;[16]=0;[17]=1;[18]=0;[19]=1;[20]=0;[21]=1;[22]=0;[23]=1;[24]=0;[25]=1;[26]=0;[27]=1;[28]=0;[29]=1;[30]=0;[31]=1;[32]=0;[33]=0;[34]=2;[35]=2;[36]=0;[37]=0;[38]=2;[39]=2;[40]=0;[41]=0;[42]=2;[43]=2;[44]=0;[45]=0;[46]=2;[47]=2;[48]=0;[49]=1;[50]=2;[51]=3;[52]=0;[53]=1;[54]=2;[55]=3;[56]=0;[57]=1;[58]=2;[59]=3;[60]=0;[61]=1;[62]=2;[63]=3;[64]=0;[65]=0;[66]=0;[67]=0;[68]=4;[69]=4;[70]=4;[71]=4;[72]=0;[73]=0;[74]=0;[75]=0;[76]=4;[77]=4;[78]=4;[79]=4;[80]=0;[81]=1;[82]=0;[83]=1;[84]=4;[85]=5;[86]=4;[87]=5;[88]=0;[89]=1;[90]=0;[91]=1;[92]=4;[93]=5;[94]=4;[95]=5;[96]=0;[97]=0;[98]=2;[99]=2;[100]=4;[101]=4;[102]=6;[103]=6;[104]=0;[105]=0;[106]=2;[107]=2;[108]=4;[109]=4;[110]=6;[111]=6;[112]=0;[113]=1;[114]=2;[115]=3;[116]=4;[117]=5;[118]=6;[119]=7;[120]=0;[121]=1;[122]=2;[123]=3;[124]=4;[125]=5;[126]=6;[127]=7;[128]=0;[129]=0;[130]=0;[131]=0;[132]=0;[133]=0;[134]=0;[135]=0;[136]=8;[137]=8;[138]=8;[139]=8;[140]=8;[141]=8;[142]=8;[143]=8;[144]=0;[145]=1;[146]=0;[147]=1;[148]=0;[149]=1;[150]=0;[151]=1;[152]=8;[153]=9;[154]=8;[155]=9;[156]=8;[157]=9;[158]=8;[159]=9;[160]=0;[161]=0;[162]=2;[163]=2;[164]=0;[165]=0;[166]=2;[167]=2;[168]=8;[169]=8;[170]=10;[171]=10;[172]=8;[173]=8;[174]=10;[175]=10;[176]=0;[177]=1;[178]=2;[179]=3;[180]=0;[181]=1;[182]=2;[183]=3;[184]=8;[185]=9;[186]=10;[187]=11;[188]=8;[189]=9;[190]=10;[191]=11;[192]=0;[193]=0;[194]=0;[195]=0;[196]=4;[197]=4;[198]=4;[199]=4;[200]=8;[201]=8;[202]=8;[203]=8;[204]=12;[205]=12;[206]=12;[207]=12;[208]=0;[209]=1;[210]=0;[211]=1;[212]=4;[213]=5;[214]=4;[215]=5;[216]=8;[217]=9;[218]=8;[219]=9;[220]=12;[221]=13;[222]=12;[223]=13;[224]=0;[225]=0;[226]=2;[227]=2;[228]=4;[229]=4;[230]=6;[231]=6;[232]=8;[233]=8;[234]=10;[235]=10;[236]=12;[237]=12;[238]=14;[239]=14;[240]=0;[241]=1;[242]=2;[243]=3;[244]=4;[245]=5;[246]=6;[247]=7;[248]=8;[249]=9;[250]=10;[251]=11;[252]=12;[253]=13;[254]=14;[255]=15;}
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4619
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4620 local not_map = {[0]=15;[1]=14;[2]=13;[3]=12;[4]=11;[5]=10;[6]=9;[7]=8;[8]=7;[9]=6;[10]=5;[11]=4;[12]=3;[13]=2;[14]=1;[15]=0;};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4621 local rshift1_map = {[0]=0;[1]=0;[2]=1;[3]=1;[4]=2;[5]=2;[6]=3;[7]=3;[8]=4;[9]=4;[10]=5;[11]=5;[12]=6;[13]=6;[14]=7;[15]=7;};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4622 local rshift1carry_map = {[0]=0;[1]=8;[2]=0;[3]=8;[4]=0;[5]=8;[6]=0;[7]=8;[8]=0;[9]=8;[10]=0;[11]=8;[12]=0;[13]=8;[14]=0;[15]=8;};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4623 local lshift1_map = {[0]=0;[1]=2;[2]=4;[3]=6;[4]=8;[5]=10;[6]=12;[7]=14;[8]=0;[9]=2;[10]=4;[11]=6;[12]=8;[13]=10;[14]=12;[15]=14;};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4624 local lshift1carry_map = {[0]=0;[1]=0;[2]=0;[3]=0;[4]=0;[5]=0;[6]=0;[7]=0;[8]=1;[9]=1;[10]=1;[11]=1;[12]=1;[13]=1;[14]=1;[15]=1;};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4625 local arshift1carry_map = {[0]=0;[1]=0;[2]=0;[3]=0;[4]=0;[5]=0;[6]=0;[7]=0;[8]=8;[9]=8;[10]=8;[11]=8;[12]=8;[13]=8;[14]=8;[15]=8;};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4626
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4627 module "bit"
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4628
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4629 local bit_mt = {__tostring = function(t) return ("%x%x%x%x%x%x%x%x"):format(t[1],t[2],t[3],t[4],t[5],t[6],t[7],t[8]); end};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4630 local function do_bop(a, b, op)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4631 return setmetatable({
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4632 op[a[1]*16+b[1]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4633 op[a[2]*16+b[2]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4634 op[a[3]*16+b[3]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4635 op[a[4]*16+b[4]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4636 op[a[5]*16+b[5]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4637 op[a[6]*16+b[6]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4638 op[a[7]*16+b[7]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4639 op[a[8]*16+b[8]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4640 }, bit_mt);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4641 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4642 local function do_uop(a, op)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4643 return setmetatable({
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4644 op[a[1]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4645 op[a[2]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4646 op[a[3]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4647 op[a[4]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4648 op[a[5]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4649 op[a[6]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4650 op[a[7]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4651 op[a[8]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4652 }, bit_mt);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4653 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4654
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4655 function bxor(a, b) return do_bop(a, b, xor_map); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4656 function bor(a, b) return do_bop(a, b, or_map); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4657 function band(a, b) return do_bop(a, b, and_map); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4658
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4659 function bnot(a) return do_uop(a, not_map); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4660 local function _rshift1(t)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4661 local carry = 0;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4662 for i=1,8 do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4663 local t_i = rshift1_map[t[i]] + carry;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4664 carry = rshift1carry_map[t[i]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4665 t[i] = t_i;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4666 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4667 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4668 function rshift(a, i)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4669 local t = {a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4670 for n = 1,i do _rshift1(t); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4671 return setmetatable(t, bit_mt);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4672 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4673 local function _arshift1(t)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4674 local carry = arshift1carry_map[t[1]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4675 for i=1,8 do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4676 local t_i = rshift1_map[t[i]] + carry;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4677 carry = rshift1carry_map[t[i]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4678 t[i] = t_i;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4679 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4680 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4681 function arshift(a, i)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4682 local t = {a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4683 for n = 1,i do _arshift1(t); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4684 return setmetatable(t, bit_mt);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4685 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4686 local function _lshift1(t)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4687 local carry = 0;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4688 for i=8,1,-1 do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4689 local t_i = lshift1_map[t[i]] + carry;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4690 carry = lshift1carry_map[t[i]];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4691 t[i] = t_i;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4692 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4693 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4694 function lshift(a, i)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4695 local t = {a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4696 for n = 1,i do _lshift1(t); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4697 return setmetatable(t, bit_mt);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4698 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4699
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4700 local function _cast(a)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4701 if type(a) == "number" then a = ("%x"):format(a);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4702 elseif type(a) == "table" then return a;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4703 elseif type(a) ~= "string" then error("string expected, got "..type(a), 2); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4704 local t = {0,0,0,0,0,0,0,0};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4705 a = "00000000"..a;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4706 a = a:sub(-8);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4707 for i = 1,8 do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4708 t[i] = tonumber(a:sub(i,i), 16) or error("Number format error", 2);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4709 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4710 return setmetatable(t, bit_mt);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4711 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4712
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4713 local function wrap1(f)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4714 return function(a, ...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4715 if type(a) ~= "table" then a = _cast(a); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4716 a = f(a, ...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4717 a = tonumber(tostring(a), 16);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4718 if a > 0x7fffffff then a = a - 1 - 0xffffffff; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4719 return a;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4720 end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4721 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4722 local function wrap2(f)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4723 return function(a, b, ...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4724 if type(a) ~= "table" then a = _cast(a); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4725 if type(b) ~= "table" then b = _cast(b); end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4726 a = f(a, b, ...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4727 a = tonumber(tostring(a), 16);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4728 if a > 0x7fffffff then a = a - 1 - 0xffffffff; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4729 return a;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4730 end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4731 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4732
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4733 bxor = wrap2(bxor);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4734 bor = wrap2(bor);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4735 band = wrap2(band);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4736 bnot = wrap1(bnot);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4737 lshift = wrap1(lshift);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4738 rshift = wrap1(rshift);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4739 arshift = wrap1(arshift);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4740 cast = wrap1(_cast);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4741
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4742 bits = 32;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4743
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4744 return _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4745 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4746 package.preload['verse.client'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4747 local verse = require "verse";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4748 local stream = verse.stream_mt;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4749
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4750 local jid_split = require "util.jid".split;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4751 local adns = require "net.adns";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4752 local lxp = require "lxp";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4753 local st = require "util.stanza";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4754
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4755 -- Shortcuts to save having to load util.stanza
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4756 verse.message, verse.presence, verse.iq, verse.stanza, verse.reply, verse.error_reply =
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4757 st.message, st.presence, st.iq, st.stanza, st.reply, st.error_reply;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4758
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4759 local new_xmpp_stream = require "util.xmppstream".new;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4760
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4761 local xmlns_stream = "http://etherx.jabber.org/streams";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4762
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4763 local function compare_srv_priorities(a,b)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4764 return a.priority < b.priority or (a.priority == b.priority and a.weight > b.weight);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4765 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4766
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4767 local stream_callbacks = {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4768 stream_ns = xmlns_stream,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4769 stream_tag = "stream",
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4770 default_ns = "jabber:client" };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4771
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4772 function stream_callbacks.streamopened(stream, attr)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4773 stream.stream_id = attr.id;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4774 if not stream:event("opened", attr) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4775 stream.notopen = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4776 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4777 return true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4778 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4779
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4780 function stream_callbacks.streamclosed(stream)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4781 return stream:event("closed");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4782 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4783
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4784 function stream_callbacks.handlestanza(stream, stanza)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4785 if stanza.attr.xmlns == xmlns_stream then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4786 return stream:event("stream-"..stanza.name, stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4787 elseif stanza.attr.xmlns then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4788 return stream:event("stream/"..stanza.attr.xmlns, stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4789 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4790
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4791 return stream:event("stanza", stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4792 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4793
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4794 function stream:reset()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4795 if self.stream then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4796 self.stream:reset();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4797 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4798 self.stream = new_xmpp_stream(self, stream_callbacks);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4799 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4800 self.notopen = true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4801 return true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4802 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4803
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4804 function stream:connect_client(jid, pass)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4805 self.jid, self.password = jid, pass;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4806 self.username, self.host, self.resource = jid_split(jid);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4807
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4808 -- Required XMPP features
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4809 self:add_plugin("tls");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4810 self:add_plugin("sasl");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4811 self:add_plugin("bind");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4812 self:add_plugin("session");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4813
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4814 function self.data(conn, data)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4815 local ok, err = self.stream:feed(data);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4816 if ok then return; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4817 self:debug("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4818 self:close("xml-not-well-formed");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4819 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4820
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4821 self:hook("connected", function () self:reopen(); end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4822 self:hook("incoming-raw", function (data) return self.data(self.conn, data); end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4823
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4824 self.curr_id = 0;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4825
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4826 self.tracked_iqs = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4827 self:hook("stanza", function (stanza)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4828 local id, type = stanza.attr.id, stanza.attr.type;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4829 if id and stanza.name == "iq" and (type == "result" or type == "error") and self.tracked_iqs[id] then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4830 self.tracked_iqs[id](stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4831 self.tracked_iqs[id] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4832 return true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4833 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4834 end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4835
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4836 self:hook("stanza", function (stanza)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4837 if stanza.attr.xmlns == nil or stanza.attr.xmlns == "jabber:client" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4838 if stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4839 local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4840 if xmlns then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4841 ret = self:event("iq/"..xmlns, stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4842 if not ret then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4843 ret = self:event("iq", stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4844 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4845 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4846 if ret == nil then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4847 self:send(verse.error_reply(stanza, "cancel", "service-unavailable"));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4848 return true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4849 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4850 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4851 ret = self:event(stanza.name, stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4852 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4853 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4854 return ret;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4855 end, -1);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4856
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4857 self:hook("outgoing", function (data)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4858 if data.name then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4859 self:event("stanza-out", data);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4860 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4861 end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4862
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4863 self:hook("stanza-out", function (stanza)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4864 if not stanza.attr.xmlns then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4865 self:event(stanza.name.."-out", stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4866 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4867 end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4868
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4869 local function stream_ready()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4870 self:event("ready");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4871 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4872 self:hook("session-success", stream_ready, -1)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4873 self:hook("bind-success", stream_ready, -1);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4874
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4875 local _base_close = self.close;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4876 function self:close(reason)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4877 if not self.notopen then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4878 self:send("</stream:stream>");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4879 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4880 return _base_close(self);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4881 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4882
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4883 local function start_connect()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4884 -- Initialise connection
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4885 self:connect(self.connect_host or self.host, self.connect_port or 5222);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4886 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4887
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4888 if not (self.connect_host or self.connect_port) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4889 -- Look up SRV records
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4890 adns.lookup(function (answer)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4891 if answer then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4892 local srv_hosts = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4893 self.srv_hosts = srv_hosts;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4894 for _, record in ipairs(answer) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4895 table.insert(srv_hosts, record.srv);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4896 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4897 table.sort(srv_hosts, compare_srv_priorities);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4898
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4899 local srv_choice = srv_hosts[1];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4900 self.srv_choice = 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4901 if srv_choice then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4902 self.connect_host, self.connect_port = srv_choice.target, srv_choice.port;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4903 self:debug("Best record found, will connect to %s:%d", self.connect_host or self.host, self.connect_port or 5222);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4904 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4905
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4906 self:hook("disconnected", function ()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4907 if self.srv_hosts and self.srv_choice < #self.srv_hosts then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4908 self.srv_choice = self.srv_choice + 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4909 local srv_choice = srv_hosts[self.srv_choice];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4910 self.connect_host, self.connect_port = srv_choice.target, srv_choice.port;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4911 start_connect();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4912 return true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4913 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4914 end, 1000);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4915
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4916 self:hook("connected", function ()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4917 self.srv_hosts = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4918 end, 1000);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4919 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4920 start_connect();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4921 end, "_xmpp-client._tcp."..(self.host)..".", "SRV");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4922 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4923 start_connect();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4924 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4925 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4926
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4927 function stream:reopen()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4928 self:reset();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4929 self:send(st.stanza("stream:stream", { to = self.host, ["xmlns:stream"]='http://etherx.jabber.org/streams',
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4930 xmlns = "jabber:client", version = "1.0" }):top_tag());
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4931 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4932
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4933 function stream:send_iq(iq, callback)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4934 local id = self:new_id();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4935 self.tracked_iqs[id] = callback;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4936 iq.attr.id = id;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4937 self:send(iq);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4938 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4939
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4940 function stream:new_id()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4941 self.curr_id = self.curr_id + 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4942 return tostring(self.curr_id);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4943 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4944 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4945 package.preload['verse.component'] = (function (...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4946 local verse = require "verse";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4947 local stream = verse.stream_mt;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4948
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4949 local jid_split = require "util.jid".split;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4950 local lxp = require "lxp";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4951 local st = require "util.stanza";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4952 local sha1 = require "util.sha1".sha1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4953
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4954 -- Shortcuts to save having to load util.stanza
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4955 verse.message, verse.presence, verse.iq, verse.stanza, verse.reply, verse.error_reply =
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4956 st.message, st.presence, st.iq, st.stanza, st.reply, st.error_reply;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4957
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4958 local new_xmpp_stream = require "util.xmppstream".new;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4959
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4960 local xmlns_stream = "http://etherx.jabber.org/streams";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4961 local xmlns_component = "jabber:component:accept";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4962
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4963 local stream_callbacks = {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4964 stream_ns = xmlns_stream,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4965 stream_tag = "stream",
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4966 default_ns = xmlns_component };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4967
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4968 function stream_callbacks.streamopened(stream, attr)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4969 stream.stream_id = attr.id;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4970 if not stream:event("opened", attr) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4971 stream.notopen = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4972 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4973 return true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4974 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4975
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4976 function stream_callbacks.streamclosed(stream)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4977 return stream:event("closed");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4978 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4979
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4980 function stream_callbacks.handlestanza(stream, stanza)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4981 if stanza.attr.xmlns == xmlns_stream then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4982 return stream:event("stream-"..stanza.name, stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4983 elseif stanza.attr.xmlns or stanza.name == "handshake" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4984 return stream:event("stream/"..(stanza.attr.xmlns or xmlns_component), stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4985 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4986
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4987 return stream:event("stanza", stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4988 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4989
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4990 function stream:reset()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4991 if self.stream then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4992 self.stream:reset();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4993 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4994 self.stream = new_xmpp_stream(self, stream_callbacks);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4995 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4996 self.notopen = true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4997 return true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4998 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
4999
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5000 function stream:connect_component(jid, pass)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5001 self.jid, self.password = jid, pass;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5002 self.username, self.host, self.resource = jid_split(jid);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5003
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5004 function self.data(conn, data)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5005 local ok, err = self.stream:feed(data);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5006 if ok then return; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5007 stream:debug("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5008 stream:close("xml-not-well-formed");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5009 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5010
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5011 self:hook("incoming-raw", function (data) return self.data(self.conn, data); end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5012
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5013 self.curr_id = 0;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5014
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5015 self.tracked_iqs = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5016 self:hook("stanza", function (stanza)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5017 local id, type = stanza.attr.id, stanza.attr.type;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5018 if id and stanza.name == "iq" and (type == "result" or type == "error") and self.tracked_iqs[id] then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5019 self.tracked_iqs[id](stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5020 self.tracked_iqs[id] = nil;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5021 return true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5022 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5023 end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5024
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5025 self:hook("stanza", function (stanza)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5026 if stanza.attr.xmlns == nil or stanza.attr.xmlns == "jabber:client" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5027 if stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5028 local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5029 if xmlns then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5030 ret = self:event("iq/"..xmlns, stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5031 if not ret then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5032 ret = self:event("iq", stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5033 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5034 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5035 if ret == nil then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5036 self:send(verse.error_reply(stanza, "cancel", "service-unavailable"));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5037 return true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5038 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5039 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5040 ret = self:event(stanza.name, stanza);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5041 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5042 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5043 return ret;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5044 end, -1);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5045
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5046 self:hook("opened", function (attr)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5047 print(self.jid, self.stream_id, attr.id);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5048 local token = sha1(self.stream_id..pass, true);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5049
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5050 self:send(st.stanza("handshake", { xmlns = xmlns_component }):text(token));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5051 self:hook("stream/"..xmlns_component, function (stanza)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5052 if stanza.name == "handshake" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5053 self:event("authentication-success");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5054 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5055 end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5056 end);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5057
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5058 local function stream_ready()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5059 self:event("ready");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5060 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5061 self:hook("authentication-success", stream_ready, -1);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5062
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5063 -- Initialise connection
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5064 self:connect(self.connect_host or self.host, self.connect_port or 5347);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5065 self:reopen();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5066 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5067
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5068 function stream:reopen()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5069 self:reset();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5070 self:send(st.stanza("stream:stream", { to = self.host, ["xmlns:stream"]='http://etherx.jabber.org/streams',
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5071 xmlns = xmlns_component, version = "1.0" }):top_tag());
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5072 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5073
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5074 function stream:close(reason)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5075 if not self.notopen then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5076 self:send("</stream:stream>");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5077 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5078 local on_disconnect = self.conn.disconnect();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5079 self.conn:close();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5080 on_disconnect(conn, reason);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5081 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5082
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5083 function stream:send_iq(iq, callback)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5084 local id = self:new_id();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5085 self.tracked_iqs[id] = callback;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5086 iq.attr.id = id;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5087 self:send(iq);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5088 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5089
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5090 function stream:new_id()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5091 self.curr_id = self.curr_id + 1;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5092 return tostring(self.curr_id);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5093 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5094 end)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5095
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5096 -- Use LuaRocks if available
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5097 pcall(require, "luarocks.require");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5098
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5099 -- Load LuaSec if available
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5100 pcall(require, "ssl");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5101
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5102 local server = require "net.server";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5103 local events = require "util.events";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5104
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5105 module("verse", package.seeall);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5106 local verse = _M;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5107 _M.server = server;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5108
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5109 local stream = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5110 stream.__index = stream;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5111 stream_mt = stream;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5112
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5113 verse.plugins = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5114
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5115 function verse.new(logger, base)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5116 local t = setmetatable(base or {}, stream);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5117 t.id = tostring(t):match("%x*$");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5118 t:set_logger(logger, true);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5119 t.events = events.new();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5120 t.plugins = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5121 return t;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5122 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5123
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5124 verse.add_task = require "util.timer".add_task;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5125
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5126 verse.logger = logger.init;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5127 verse.log = verse.logger("verse");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5128
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5129 function verse.set_logger(logger)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5130 verse.log = logger;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5131 server.setlogger(logger);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5132 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5133
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5134 function verse.filter_log(levels, logger)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5135 local level_set = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5136 for _, level in ipairs(levels) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5137 level_set[level] = true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5138 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5139 return function (level, name, ...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5140 if level_set[level] then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5141 return logger(level, name, ...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5142 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5143 end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5144 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5145
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5146 local function error_handler(err)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5147 verse.log("error", "Error: %s", err);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5148 verse.log("error", "Traceback: %s", debug.traceback());
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5149 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5150
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5151 function verse.set_error_handler(new_error_handler)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5152 error_handler = new_error_handler;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5153 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5154
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5155 function verse.loop()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5156 return xpcall(server.loop, error_handler);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5157 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5158
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5159 function verse.step()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5160 return xpcall(server.step, error_handler);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5161 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5162
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5163 function verse.quit()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5164 return server.setquitting(true);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5165 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5166
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5167 function stream:connect(connect_host, connect_port)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5168 connect_host = connect_host or "localhost";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5169 connect_port = tonumber(connect_port) or 5222;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5170
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5171 -- Create and initiate connection
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5172 local conn = socket.tcp()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5173 conn:settimeout(0);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5174 local success, err = conn:connect(connect_host, connect_port);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5175
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5176 if not success and err ~= "timeout" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5177 self:warn("connect() to %s:%d failed: %s", connect_host, connect_port, err);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5178 return self:event("disconnected", { reason = err }) or false, err;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5179 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5180
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5181 local conn = server.wrapclient(conn, connect_host, connect_port, new_listener(self), "*a");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5182 if not conn then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5183 self:warn("connection initialisation failed: %s", err);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5184 return self:event("disconnected", { reason = err }) or false, err;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5185 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5186
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5187 self.conn = conn;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5188 self.send = function (stream, data)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5189 self:event("outgoing", data);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5190 data = tostring(data);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5191 self:event("outgoing-raw", data);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5192 return conn:write(data);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5193 end;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5194 return true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5195 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5196
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5197 function stream:close()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5198 if not self.conn then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5199 verse.log("error", "Attempt to close disconnected connection - possibly a bug");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5200 return;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5201 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5202 local on_disconnect = self.conn.disconnect();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5203 self.conn:close();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5204 on_disconnect(conn, reason);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5205 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5206
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5207 -- Logging functions
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5208 function stream:debug(...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5209 if self.logger and self.log.debug then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5210 return self.logger("debug", ...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5211 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5212 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5213
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5214 function stream:warn(...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5215 if self.logger and self.log.warn then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5216 return self.logger("warn", ...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5217 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5218 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5219
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5220 function stream:error(...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5221 if self.logger and self.log.error then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5222 return self.logger("error", ...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5223 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5224 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5225
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5226 function stream:set_logger(logger, levels)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5227 local old_logger = self.logger;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5228 if logger then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5229 self.logger = logger;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5230 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5231 if levels then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5232 if levels == true then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5233 levels = { "debug", "info", "warn", "error" };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5234 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5235 self.log = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5236 for _, level in ipairs(levels) do
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5237 self.log[level] = true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5238 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5239 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5240 return old_logger;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5241 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5242
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5243 function stream_mt:set_log_levels(levels)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5244 self:set_logger(nil, levels);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5245 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5246
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5247 -- Event handling
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5248 function stream:event(name, ...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5249 self:debug("Firing event: "..tostring(name));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5250 return self.events.fire_event(name, ...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5251 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5252
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5253 function stream:hook(name, ...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5254 return self.events.add_handler(name, ...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5255 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5256
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5257 function stream:unhook(name, handler)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5258 return self.events.remove_handler(name, handler);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5259 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5260
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5261 function verse.eventable(object)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5262 object.events = events.new();
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5263 object.hook, object.unhook = stream.hook, stream.unhook;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5264 local fire_event = object.events.fire_event;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5265 function object:event(name, ...)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5266 return fire_event(name, ...);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5267 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5268 return object;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5269 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5270
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5271 function stream:add_plugin(name)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5272 if self.plugins[name] then return true; end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5273 if require("verse.plugins."..name) then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5274 local ok, err = verse.plugins[name](self);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5275 if ok ~= false then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5276 self:debug("Loaded %s plugin", name);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5277 self.plugins[name] = true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5278 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5279 self:warn("Failed to load %s plugin: %s", name, err);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5280 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5281 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5282 return self;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5283 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5284
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5285 -- Listener factory
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5286 function new_listener(stream)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5287 local conn_listener = {};
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5288
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5289 function conn_listener.onconnect(conn)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5290 stream.connected = true;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5291 stream:event("connected");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5292 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5293
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5294 function conn_listener.onincoming(conn, data)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5295 stream:event("incoming-raw", data);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5296 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5297
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5298 function conn_listener.ondisconnect(conn, err)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5299 stream.connected = false;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5300 stream:event("disconnected", { reason = err });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5301 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5302
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5303 function conn_listener.ondrain(conn)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5304 stream:event("drained");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5305 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5306
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5307 function conn_listener.onstatus(conn, new_status)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5308 stream:event("status", new_status);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5309 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5310
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5311 return conn_listener;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5312 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5313
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5314
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5315 local log = require "util.logger".init("verse");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5316
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5317 return verse;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5318 end)
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5319 -- README
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5320 -- Squish verse into this dir, then squish them into one, which you move
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5321 -- and rename to mod_ircd.lua in your prosody modules/plugins dir.
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5322 --
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5323 -- IRC spec:
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5324 -- http://tools.ietf.org/html/rfc2812
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5325 local _module = module
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5326 module = _G.module
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5327 local module = _module
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5328 --
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5329 local component_jid, component_secret, muc_server =
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5330 module.host, nil, module:get_option("conference_server");
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5331
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5332 package.loaded["util.sha1"] = require "util.encodings";
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5333 local verse = require "verse"
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5334 require "verse.component"
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5335 require "socket"
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5336 c = verse.new();--verse.logger())
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5337 c:add_plugin("groupchat");
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5338
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5339 local function verse2prosody(e)
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5340 return c:event("stanza", e.stanza) or true;
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5341 end
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5342 module:hook("message/bare", verse2prosody);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5343 module:hook("message/full", verse2prosody);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5344 module:hook("presence/bare", verse2prosody);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5345 module:hook("presence/full", verse2prosody);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5346 c.type = "component";
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5347 c.send = core_post_stanza;
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5348
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5349 -- This plugin is actually a verse based component, but that mode is currently commented out
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5350
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5351 -- Add some hooks for debugging
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5352 --c:hook("opened", function () print("Stream opened!") end);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5353 --c:hook("closed", function () print("Stream closed!") end);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5354 --c:hook("stanza", function (stanza) print("Stanza:", stanza) end);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5355
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5356 -- This one prints all received data
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5357 --c:hook("incoming-raw", print, 1000);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5358 --c:hook("stanza", print, 1000);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5359 --c:hook("outgoing-raw", print, 1000);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5360
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5361 -- Print a message after authentication
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5362 --c:hook("authentication-success", function () print("Logged in!"); end);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5363 --c:hook("authentication-failure", function (err) print("Failed to log in! Error: "..tostring(err.condition)); end);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5364
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5365 -- Print a message and exit when disconnected
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5366 --c:hook("disconnected", function () print("Disconnected!"); os.exit(); end);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5367
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5368 -- Now, actually start the connection:
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5369 --c.connect_host = "127.0.0.1"
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5370 --c:connect_component(component_jid, component_secret);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5371
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5372 local jid = require "util.jid";
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5373
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5374 local function irc2muc(channel, nick)
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5375 return jid.join(channel:gsub("^#", ""), muc_server, nick)
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5376 end
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5377 local function muc2irc(room)
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5378 local channel, _, nick = jid.split(room);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5379 return "#"..channel, nick;
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5380 end
335
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5381 local rolemap = {
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5382 moderator = "@",
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5383 participant = "+",
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5384 }
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5385 local modemap = {
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5386 moderator = "o",
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5387 participant = "v",
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5388 }
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5389
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5390 local irc_listener = { default_port = 6667, default_mode = "*l" };
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5391
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5392 local sessions = {};
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5393 local jids = {};
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5394 local commands = {};
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5395
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5396 local nicks = {};
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5397
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5398 local st = require "util.stanza";
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5399
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5400 local conference_server = muc_server;
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5401
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5402 local function irc_close_session(session)
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5403 session.conn:close();
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5404 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5405
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5406 function irc_listener.onincoming(conn, data)
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5407 local session = sessions[conn];
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5408 if not session then
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5409 session = { conn = conn, host = component_jid, reset_stream = function () end,
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5410 close = irc_close_session, log = logger.init("irc"..(conn.id or "1")),
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5411 rooms = {},
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5412 roster = {} };
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5413 sessions[conn] = session;
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5414 function session.data(data)
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5415 local command, args = data:match("^%s*([^ ]+) *(.*)%s*$");
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5416 if not command then
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5417 return;
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5418 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5419 command = command:upper();
335
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5420 if not session.nick then
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5421 if not (command == "USER" or command == "NICK") then
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5422 session.send(":" .. session.host .. " 451 " .. command .. " :You have not registered")
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5423 end
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5424 end
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5425 if commands[command] then
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5426 local ret = commands[command](session, args);
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5427 if ret then
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5428 session.send(ret.."\r\n");
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5429 end
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5430 else
335
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5431 session.send(":" .. session.host .. " 421 " .. session.nick .. " " .. command .. " :Unknown command")
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5432 module:log("debug", "Unknown command: %s", command);
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5433 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5434 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5435 function session.send(data)
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5436 return conn:write(data.."\r\n");
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5437 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5438 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5439 if data then
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5440 session.data(data);
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5441 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5442 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5443
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5444 function irc_listener.ondisconnect(conn, error)
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5445 local session = sessions[conn];
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5446 for _, room in pairs(session.rooms) do
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5447 room:leave("Disconnected");
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5448 end
335
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5449 jids[session.full_jid] = nil;
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5450 nicks[session.nick] = nil;
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5451 sessions[conn] = nil;
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5452 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5453
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5454 function commands.NICK(session, nick)
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5455 if session.nick then
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5456 session.send(":"..session.host.." 484 * "..nick.." :I'm afraid I can't let you do that, "..nick);
335
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5457 --TODO Loop throug all rooms and change nick, with help from Verse.
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5458 return;
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5459 end
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5460 nick = nick:match("^[%w_]+");
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5461 if nicks[nick] then
236
24582ea48471 mod_ircd: Fix "nickname in use" reply
Matthew Wild <mwild1@gmail.com>
parents: 212
diff changeset
5462 session.send(":"..session.host.." 433 * "..nick.." :The nickname "..nick.." is already in use");
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5463 return;
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5464 end
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5465 local full_jid = jid.join(nick, component_jid, "ircd");
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5466 jids[full_jid] = session;
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5467 nicks[nick] = session;
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5468 session.nick = nick;
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5469 session.full_jid = full_jid;
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5470 session.type = "c2s";
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5471 session.send(":"..session.host.." 001 "..session.nick.." :Welcome to XMPP via the "..session.host.." gateway "..session.nick);
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5472 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5473
329
febfb59502fc mod_ircd: Add QUIT command.
Kim Alvefur <zash@zash.se>
parents: 326
diff changeset
5474 function commands.USER(session, params)
febfb59502fc mod_ircd: Add QUIT command.
Kim Alvefur <zash@zash.se>
parents: 326
diff changeset
5475 -- FIXME
febfb59502fc mod_ircd: Add QUIT command.
Kim Alvefur <zash@zash.se>
parents: 326
diff changeset
5476 -- Empty command for now
febfb59502fc mod_ircd: Add QUIT command.
Kim Alvefur <zash@zash.se>
parents: 326
diff changeset
5477 end
febfb59502fc mod_ircd: Add QUIT command.
Kim Alvefur <zash@zash.se>
parents: 326
diff changeset
5478
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5479 function commands.JOIN(session, channel)
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5480 local room_jid = irc2muc(channel);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5481 print(session.full_jid);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5482 local room, err = c:join_room(room_jid, session.nick, { source = session.full_jid } );
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5483 if not room then
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5484 return ":"..session.host.." ERR :Could not join room: "..err
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5485 end
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5486 session.rooms[channel] = room;
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5487 room.channel = channel;
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5488 room.session = session;
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5489 session.send(":"..session.nick.." JOIN :"..channel);
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5490 session.send(":"..session.host.." 332 "..session.nick.." "..channel.." :Connection in progress...");
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5491 room:hook("message", function(event)
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5492 if not event.body then return end
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5493 local nick, body = event.nick, event.body;
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5494 if nick ~= session.nick then
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5495 if body:sub(1,4) == "/me " then
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5496 body = "\1ACTION ".. body:sub(5) .. "\1"
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5497 end
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5498 session.send(":"..nick.." PRIVMSG "..channel.." :"..body);
329
febfb59502fc mod_ircd: Add QUIT command.
Kim Alvefur <zash@zash.se>
parents: 326
diff changeset
5499 --FIXME PM's probably won't work
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5500 end
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5501 end);
455
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5502 room:hook("subject-changed", function(changed)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5503 session.send((":%s TOPIC %s :%s"):format(changed.by, channel, changed.to or ""));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5504 end);
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5505 end
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5506
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5507 c:hook("groupchat/joined", function(room)
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5508 local session = room.session or jids[room.opts.source];
335
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5509 local channel = room.channel;
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5510 session.send((":%s!%s JOIN %s :"):format(session.nick, session.nick, channel));
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5511 if room.topic then
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5512 session.send((":%s 332 %s :%s"):format(session.host, channel, room.topic));
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5513 end
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5514 commands.NAMES(session, channel)
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5515 --FIXME Ones own mode get's lost
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5516 --session.send((":%s MODE %s +%s %s"):format(session.host, room.channel, modemap[nick.role], nick.nick));
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5517 room:hook("occupant-joined", function(nick)
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5518 session.send((":%s!%s JOIN :%s"):format(nick.nick, nick.nick, channel));
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5519 if nick.role and modemap[nick.role] then
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5520 session.send((":%s MODE %s +%s %s"):format(session.host, room.channel, modemap[nick.role], nick.nick));
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5521 end
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5522 end);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5523 room:hook("occupant-left", function(nick)
335
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5524 session.send((":%s!%s PART %s :"):format(nick.nick, nick.nick, channel));
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5525 end);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5526 end);
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5527
335
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5528 function commands.NAMES(session, channel)
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5529 local nicks = { };
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5530 local room = session.rooms[channel];
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5531 if not room then return end
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5532 -- TODO Break this out into commands.NAMES
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5533 for nick, n in pairs(room.occupants) do
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5534 if n.role and rolemap[n.role] then
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5535 nick = rolemap[n.role] .. nick;
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5536 end
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5537 table.insert(nicks, nick);
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5538 end
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5539 nicks = table.concat(nicks, " ");
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5540 --:molyb.irc.bnfh.org 353 derp = #grill-bit :derp hyamobi walt snuggles_ E-Rock kng grillbit gunnarbot Frink shedma zagabar zash Mrw00t Appiah J10 lectus peck EricJ soso mackt offer hyarion @pettter MMN-o
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5541 session.send((":%s 353 %s = %s :%s"):format(session.host, session.nick, channel, nicks));
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5542 session.send((":%s 366 %s %s :End of /NAMES list."):format(session.host, session.nick, channel));
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5543 session.send(":"..session.host.." 353 "..session.nick.." = "..channel.." :"..nicks);
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5544 end
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5545
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5546 function commands.PART(session, channel)
132
d4ff1cd414e5 mod_ircd: Add PING command / Echo PART back
Florian Zeitz <florob@babelmonkeys.de>
parents: 111
diff changeset
5547 local channel, part_message = channel:match("^([^:]+):?(.*)$");
d4ff1cd414e5 mod_ircd: Add PING command / Echo PART back
Florian Zeitz <florob@babelmonkeys.de>
parents: 111
diff changeset
5548 channel = channel:match("^([%S]*)");
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5549 session.rooms[channel]:leave(part_message);
132
d4ff1cd414e5 mod_ircd: Add PING command / Echo PART back
Florian Zeitz <florob@babelmonkeys.de>
parents: 111
diff changeset
5550 session.send(":"..session.nick.." PART :"..channel);
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5551 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5552
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5553 function commands.PRIVMSG(session, message)
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5554 local channel, message = message:match("^(%S+) :(.+)$");
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5555 if message and #message > 0 and session.rooms[channel] then
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5556 if message:sub(1,8) == "\1ACTION " then
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5557 message = "/me ".. message:sub(9,-2)
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5558 end
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5559 module:log("debug", "%s sending PRIVMSG \"%s\" to %s", session.nick, message, channel);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5560 session.rooms[channel]:send_message(message);
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5561 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5562 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5563
455
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5564 function commands.TOPIC(session, message)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5565 if not message then return end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5566 local channel, topic = message:match("^(%S+) :(.*)$");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5567 if not channel then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5568 channel = message:match("^(%S+)");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5569 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5570 if not channel then return end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5571 local room = session.rooms[channel];
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5572 if topic then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5573 room:set_subject(topic)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5574 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5575 session.send((":%s TOPIC %s :%s"):format(session.host, channel, room.subject or ""));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5576 -- first should be who set it, but verse doesn't provide that yet, so we'll
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5577 -- just say it was the server
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5578 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5579 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5580
132
d4ff1cd414e5 mod_ircd: Add PING command / Echo PART back
Florian Zeitz <florob@babelmonkeys.de>
parents: 111
diff changeset
5581 function commands.PING(session, server)
d4ff1cd414e5 mod_ircd: Add PING command / Echo PART back
Florian Zeitz <florob@babelmonkeys.de>
parents: 111
diff changeset
5582 session.send(":"..session.host..": PONG "..server);
d4ff1cd414e5 mod_ircd: Add PING command / Echo PART back
Florian Zeitz <florob@babelmonkeys.de>
parents: 111
diff changeset
5583 end
d4ff1cd414e5 mod_ircd: Add PING command / Echo PART back
Florian Zeitz <florob@babelmonkeys.de>
parents: 111
diff changeset
5584
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5585 function commands.WHO(session, channel)
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5586 if session.rooms[channel] then
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5587 local room = session.rooms[channel]
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5588 for nick in pairs(room.occupants) do
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5589 --n=MattJ 91.85.191.50 irc.freenode.net MattJ H :0 Matthew Wild
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5590 session.send(":"..session.host.." 352 "..session.nick.." "..channel.." "..nick.." "..nick.." "..session.host.." "..nick.." H :0 "..nick);
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5591 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5592 session.send(":"..session.host.." 315 "..session.nick.." "..channel.. " :End of /WHO list");
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5593 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5594 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5595
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5596 function commands.MODE(session, channel)
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5597 session.send(":"..session.host.." 324 "..session.nick.." "..channel.." +J");
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5598 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5599
329
febfb59502fc mod_ircd: Add QUIT command.
Kim Alvefur <zash@zash.se>
parents: 326
diff changeset
5600 function commands.QUIT(session, message)
febfb59502fc mod_ircd: Add QUIT command.
Kim Alvefur <zash@zash.se>
parents: 326
diff changeset
5601 session.send("ERROR :Closing Link: "..session.nick);
febfb59502fc mod_ircd: Add QUIT command.
Kim Alvefur <zash@zash.se>
parents: 326
diff changeset
5602 for _, room in pairs(session.rooms) do
febfb59502fc mod_ircd: Add QUIT command.
Kim Alvefur <zash@zash.se>
parents: 326
diff changeset
5603 room:leave(message);
febfb59502fc mod_ircd: Add QUIT command.
Kim Alvefur <zash@zash.se>
parents: 326
diff changeset
5604 end
335
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5605 jids[session.full_jid] = nil;
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5606 nicks[session.nick] = nil;
8b81257c9dc5 mod_ircd: Don't allow any command until nick has been set. Split out NAMES into a command.
Kim Alvefur <zash@zash.se>
parents: 329
diff changeset
5607 sessions[session.conn] = nil;
329
febfb59502fc mod_ircd: Add QUIT command.
Kim Alvefur <zash@zash.se>
parents: 326
diff changeset
5608 session:close();
febfb59502fc mod_ircd: Add QUIT command.
Kim Alvefur <zash@zash.se>
parents: 326
diff changeset
5609 end
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5610
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5611 function commands.RAW(session, data)
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5612 --c:send(data)
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5613 end
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5614
455
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5615 local function desetup()
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5616 require "net.connlisteners".deregister("irc");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5617 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5618
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5619 --c:hook("ready", function ()
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5620 require "net.connlisteners".register("irc", irc_listener);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5621 require "net.connlisteners".start("irc");
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5622 --end);
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5623
455
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 447
diff changeset
5624 module:hook("module-unloaded", desetup)
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5625 --print("Starting loop...")
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5626 --verse.loop()
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5627
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5628 --[[ TODO
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5629
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5630 This is so close to working as a Prosody plugin you know ^^
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5631 Zash: :D
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5632 MattJ: That component function can go
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5633 Prosody fires events now
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5634 but verse fires "message" where Prosody fires "message/bare"
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5635 [20:59:50]
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5636 Easy... don't connect_component
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5637 hook "message/*" and presence, and whatever
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5638 and call c:event("message", ...)
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5639 module:hook("message/bare", function (e) c:event("message", e.stanza) end)
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5640 as an example
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5641 That's so bad ^^
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5642 and override c:send() to core_post_stanza...
111
3de60860adca mod_ircd: Initial commit of a wonderfully hacky but working IRC->XMPP interface for Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5643
326
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5644 --]]
282abba844e8 mod_ircd: Partial rewrite, use verse for MUC
Kim Alvefur <zash@zash.se>
parents: 282
diff changeset
5645