Tue, 03 Aug 2010 10:58:15 +0100
sessionmanager: Remove dependency on eventmanager
3415
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 | |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 | local setmetatable = setmetatable; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 | local error = error; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 | local unpack = unpack; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 | local module = module; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 | local tostring = tostring; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 | local pairs, next = pairs, next; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 | local prosody = prosody; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 | local assert = assert; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 | local require = require; |
3429
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
11 | local st = require "util.stanza"; |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
12 | local DBI = require "DBI"; |
3415
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 | |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 | -- connect to db |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 | local option_datastore_params = module:get_option("datastore_params") or error("Missing option: datastore_params"); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 | local database; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 | do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 | local driver, db = unpack(option_datastore_params); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 | module:log("debug", "Opening database: %s", "dbi:"..driver..":"..db); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 | prosody.unlock_globals(); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 | local dbh, err = DBI.Connect(unpack(option_datastore_params)); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 | prosody.lock_globals(); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 | assert(dbh, err); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 | dbh:autocommit(true) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 | database = dbh; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 | |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 | -- initialize db |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 | local ejabberd_init = module:require("ejabberd_init"); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 | ejabberd_init.init(database); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 | |
3429
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
32 | local sqlcache = {}; |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
33 | local function prepare(sql) |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
34 | module:log("debug", "query: %s", sql); |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
35 | local err; |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
36 | local r = sqlcache[sql]; |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
37 | if not r then |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
38 | r, err = database:prepare(sql); |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
39 | if not r then error("Unable to prepare SQL statement: "..err); end |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
40 | sqlcache[sql] = r; |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
41 | end |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
42 | return r; |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
43 | end |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
44 | |
3415
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 | local _parse_xml = module:require("xmlparse"); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 | local function parse_xml(str) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 | local s = _parse_xml(str); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 | if s and not s.gsub then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 | return st.preserialize(s); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 | local function unparse_xml(s) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 | return tostring(st.deserialize(s)); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 | |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 | |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 | local handlers = {}; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 | |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 | handlers.accounts = { |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 | get = function(self, user) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 | local select = self:query("select password from users where username=? and host=?", user, self.host); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 | local row = select and select:fetch(); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 | if row then return { password = row[1] }; end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 | end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 | set = function(self, user, data) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 | if data and data.password then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 | return self:modify("update users set password=? where username=? and host=?", data.password, user, self.host) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 | or self:modify("insert into users (username, host, password) values (?, ?, ?)", user, self.host, data.password); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 | else |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 | return self:modify("delete from users where username=? and host=?", user, self.host); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 | end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 | }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 | handlers.vcard = { |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 | get = function(self, user) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 | local select = self:query("select vcard from vcard where username=? and host=?", user, self.host); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 | local row = select and select:fetch(); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 | if row then return parse_xml(row[1]); end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 | end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 | set = function(self, user, data) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 | if data then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 | data = unparse_xml(data); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 | return self:modify("update vcard set vcard=? where username=? and host=?", data, user, self.host) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 | or self:modify("insert into vcard (username, host, vcard) values (?, ?, ?)", user, self.host, data); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 | else |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 | return self:modify("delete from vcard where username=? and host=?", user, self.host); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 | end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 | }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 | handlers.private = { |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 | get = function(self, user) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 | local select = self:query("select namespace,data from private_storage where username=? and host=?", user, self.host); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 | if select then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 | local data = {}; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 | for row in select:rows() do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 | data[row[1]] = parse_xml(row[2]); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 | return data; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 | end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
101 | set = function(self, user, data) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 | if data then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 | self:modify("delete from private_storage where username=? and host=?", user, self.host); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 | for namespace,text in pairs(data) do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 | self:modify("insert into private_storage (username, host, namespace, data) values (?, ?, ?, ?)", user, self.host, namespace, unparse_xml(text)); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 | return true; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 | else |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 | return self:modify("delete from private_storage where username=? and host=?", user, self.host); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 | end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 | -- TODO map_set, map_get |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
113 | }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
114 | local subscription_map = { N = "none", B = "both", F = "from", T = "to" }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 | local subscription_map_reverse = { none = "N", both = "B", from = "F", to = "T" }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 | handlers.roster = { |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
117 | get = function(self, user) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
118 | local select = self:query("select jid,nick,subscription,ask,server,subscribe,type from rosterusers where username=?", user); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
119 | if select then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
120 | local roster = { pending = {} }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 | for row in select:rows() do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 | local jid,nick,subscription,ask,server,subscribe,typ = unpack(row); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 | local item = { groups = {} }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 | if nick == "" then nick = nil; end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
125 | item.nick = nick; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 | item.subscription = subscription_map[subscription]; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 | if ask == "N" then ask = nil; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 | elseif ask == "O" then ask = "subscribe" |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
129 | elseif ask == "I" then roster.pending[jid] = true; ask = nil; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
130 | elseif ask == "B" then roster.pending[jid] = true; ask = "subscribe"; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 | else module:log("debug", "bad roster_item.ask: %s", ask); ask = nil; end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 | item.ask = ask; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 | roster[jid] = item; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 | |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 | select = self:query("select jid,grp from rostergroups where username=?", user); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 | if select then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 | for row in select:rows() do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 | local jid,grp = unpack(row); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
140 | if roster[jid] then roster[jid].groups[grp] = true; end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
141 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 | select = self:query("select version from roster_version where username=?", user); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
144 | local row = select and select:fetch(); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 | if row then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 | roster[false] = { version = row[1]; }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
147 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
148 | return roster; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
150 | end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 | set = function(self, user, data) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
152 | if data and next(data) ~= nil then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 | self:modify("delete from rosterusers where username=?", user); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
154 | self:modify("delete from rostergroups where username=?", user); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 | self:modify("delete from roster_version where username=?", user); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
156 | local done = {}; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
157 | local pending = data.pending or {}; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
158 | for jid,item in pairs(data) do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
159 | if jid and jid ~= "pending" then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
160 | local subscription = subscription_map_reverse[item.subscription]; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 | local ask; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
162 | if pending[jid] then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
163 | if item.ask then ask = "B"; else ask = "I"; end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
164 | else |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
165 | if item.ask then ask = "O"; else ask = "N"; end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
166 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
167 | local r = self:modify("insert into rosterusers (username,jid,nick,subscription,ask,askmessage,server,subscribe) values (?, ?, ?, ?, ?, '', '', '')", user, jid, item.nick or "", subscription, ask); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 | if not r then module:log("debug", "--- :( %s", tostring(r)); end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
169 | done[jid] = true; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
170 | for group in pairs(item.groups) do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
171 | self:modify("insert into rostergroups (username,jid,grp) values (?, ?, ?)", user, jid, group); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
172 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
173 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
174 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
175 | for jid in pairs(pending) do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
176 | if not done[jid] then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
177 | self:modify("insert into rosterusers (username,jid,nick,subscription,ask,askmessage,server,subscribe) values (?, ?, ?, ?, ?. ''. ''. '')", user, jid, "", "N", "I"); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
178 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
179 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
180 | local version = data[false] and data[false].version; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
181 | if version then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
182 | self:modify("insert into roster_version (username,version) values (?, ?)", user, version); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
183 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
184 | return true; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
185 | else |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
186 | self:modify("delete from rosterusers where username=?", user); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
187 | self:modify("delete from rostergroups where username=?", user); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
188 | self:modify("delete from roster_version where username=?", user); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
189 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
190 | end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
191 | }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
192 | |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
193 | ----------------------------- |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
194 | local driver = {}; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
195 | driver.__index = driver; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
196 | |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
197 | function driver:query(sql, ...) |
3429
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
198 | local stmt,err = prepare(sql); |
3415
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
199 | if not stmt then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
200 | module:log("error", "Failed to prepare SQL [[%s]], error: %s", sql, err); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
201 | return nil, err; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
202 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
203 | local ok, err = stmt:execute(...); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
204 | if not ok then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
205 | module:log("error", "Failed to execute SQL [[%s]], error: %s", sql, err); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
206 | return nil, err; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
207 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
208 | return stmt; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
209 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
210 | function driver:modify(sql, ...) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
211 | local stmt, err = self:query(sql, ...); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
212 | if stmt and stmt:affected() > 0 then return stmt; end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
213 | return nil, err; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
214 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
215 | |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
216 | function driver:open(datastore, typ) |
3429
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
217 | local instance = setmetatable({ host = module.host, datastore = datastore }, self); |
3415
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
218 | local handler = handlers[datastore]; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
219 | if not handler then return nil; end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
220 | for key,val in pairs(handler) do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
221 | instance[key] = val; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
222 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
223 | if instance.init then instance:init(); end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
224 | return instance; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
225 | end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
226 | |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
227 | ----------------------------- |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
228 | |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
229 | module:add_item("data-driver", driver); |