Software /
code /
prosody
Annotate
plugins/storage/mod_ejabberd.lua @ 3415:5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sat, 31 Jul 2010 13:32:57 +0500 |
child | 3429:8cdb0179371a |
rev | line source |
---|---|
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; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 -- connect to db |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 local DBI = require "DBI"; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 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
|
15 local database; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 local driver, db = unpack(option_datastore_params); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 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
|
19 prosody.unlock_globals(); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 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
|
21 prosody.lock_globals(); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 assert(dbh, err); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 dbh:autocommit(true) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 database = dbh; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 -- initialize db |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 local ejabberd_init = module:require("ejabberd_init"); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 ejabberd_init.init(database); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 local st = require "util.stanza"; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 local _parse_xml = module:require("xmlparse"); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 local parse_xml_real = _parse_xml; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 local function parse_xml(str) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 local s = _parse_xml(str); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 if s and not s.gsub then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 return st.preserialize(s); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 local function unparse_xml(s) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 return tostring(st.deserialize(s)); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 local handlers = {}; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 handlers.accounts = { |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 get = function(self, user) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 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
|
50 local row = select and select:fetch(); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 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
|
52 end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 set = function(self, user, data) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 if data and data.password then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 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
|
56 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
|
57 else |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 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
|
59 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 handlers.vcard = { |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 get = function(self, user) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 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
|
65 local row = select and select:fetch(); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 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
|
67 end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 set = function(self, user, data) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 if data then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 data = unparse_xml(data); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 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
|
72 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
|
73 else |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 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
|
75 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 handlers.private = { |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 get = function(self, user) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 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
|
81 if select then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 local data = {}; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 for row in select:rows() do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 data[row[1]] = parse_xml(row[2]); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 return data; |
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 set = function(self, user, data) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 if data then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 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
|
92 for namespace,text in pairs(data) do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 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
|
94 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 return true; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 else |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 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
|
98 end |
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 -- TODO map_set, map_get |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
101 }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 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
|
103 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
|
104 handlers.roster = { |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 get = function(self, user) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 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
|
107 if select then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 local roster = { pending = {} }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 for row in select:rows() do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 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
|
111 local item = { groups = {} }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 if nick == "" then nick = nil; end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
113 item.nick = nick; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
114 item.subscription = subscription_map[subscription]; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 if ask == "N" then ask = nil; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 elseif ask == "O" then ask = "subscribe" |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
117 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
|
118 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
|
119 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
|
120 item.ask = ask; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 roster[jid] = item; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 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
|
125 if select then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 for row in select:rows() do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 local jid,grp = unpack(row); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 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
|
129 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
130 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 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
|
132 local row = select and select:fetch(); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 if row then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 roster[false] = { version = row[1]; }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 return roster; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 set = function(self, user, data) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
140 if data and next(data) ~= nil then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
141 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
|
142 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
|
143 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
|
144 local done = {}; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 local pending = data.pending or {}; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 for jid,item in pairs(data) do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
147 if jid and jid ~= "pending" then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
148 local subscription = subscription_map_reverse[item.subscription]; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 local ask; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
150 if pending[jid] then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 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
|
152 else |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 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
|
154 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 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
|
156 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
|
157 done[jid] = true; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
158 for group in pairs(item.groups) do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
159 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
|
160 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
162 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
163 for jid in pairs(pending) do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
164 if not done[jid] then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
165 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
|
166 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
167 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 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
|
169 if version then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
170 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
|
171 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
172 return true; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
173 else |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
174 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
|
175 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
|
176 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
|
177 end |
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 }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
180 |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
181 ----------------------------- |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
182 local driver = {}; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
183 driver.__index = driver; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
184 |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
185 function driver:prepare(sql) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
186 module:log("debug", "query: %s", sql); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
187 local err; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
188 if not self.sqlcache then self.sqlcache = {}; end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
189 local r = self.sqlcache[sql]; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
190 if r then return r; end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
191 r, err = database:prepare(sql); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
192 if not r then error("Unable to prepare SQL statement: "..err); end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
193 self.sqlcache[sql] = r; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
194 return r; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
195 end |
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, ...) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
198 local stmt,err = self:prepare(sql); |
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) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
217 local instance = setmetatable({}, self); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
218 instance.host = module.host; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
219 instance.datastore = datastore; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
220 local handler = handlers[datastore]; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
221 if not handler then return nil; end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
222 for key,val in pairs(handler) do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
223 instance[key] = val; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
224 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
225 if instance.init then instance:init(); end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
226 return instance; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
227 end |
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 ----------------------------- |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
230 |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
231 module:add_item("data-driver", driver); |