Software /
code /
prosody
Annotate
plugins/mod_storage_sql_ejabberd.lua @ 3907:1e82c9b9db11
prosody.cfg.lua.dist: Add note about external gateways
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 21 Dec 2010 01:47:49 +0000 |
parent | 3646:66c5fb109e64 |
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; |
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 |
3646
66c5fb109e64
Rename storage/mod_ejabberd to mod_storage_sql_ejabberd. Also rename configuration option. Untested.
Matthew Wild <mwild1@gmail.com>
parents:
3429
diff
changeset
|
15 local params = module:get_option("sql_ejabberd") or error("No sql_ejabberd config option"); |
3415
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 |
3646
66c5fb109e64
Rename storage/mod_ejabberd to mod_storage_sql_ejabberd. Also rename configuration option. Untested.
Matthew Wild <mwild1@gmail.com>
parents:
3429
diff
changeset
|
18 module:log("debug", "Opening database: %s", "dbi:"..params.driver..":"..params.database); |
3415
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 prosody.unlock_globals(); |
3646
66c5fb109e64
Rename storage/mod_ejabberd to mod_storage_sql_ejabberd. Also rename configuration option. Untested.
Matthew Wild <mwild1@gmail.com>
parents:
3429
diff
changeset
|
20 local dbh, err = DBI.Connect( |
66c5fb109e64
Rename storage/mod_ejabberd to mod_storage_sql_ejabberd. Also rename configuration option. Untested.
Matthew Wild <mwild1@gmail.com>
parents:
3429
diff
changeset
|
21 params.driver, params.database, |
66c5fb109e64
Rename storage/mod_ejabberd to mod_storage_sql_ejabberd. Also rename configuration option. Untested.
Matthew Wild <mwild1@gmail.com>
parents:
3429
diff
changeset
|
22 params.username, params.password, |
66c5fb109e64
Rename storage/mod_ejabberd to mod_storage_sql_ejabberd. Also rename configuration option. Untested.
Matthew Wild <mwild1@gmail.com>
parents:
3429
diff
changeset
|
23 params.host, params.port |
66c5fb109e64
Rename storage/mod_ejabberd to mod_storage_sql_ejabberd. Also rename configuration option. Untested.
Matthew Wild <mwild1@gmail.com>
parents:
3429
diff
changeset
|
24 ); |
3415
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 prosody.lock_globals(); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 assert(dbh, err); |
3646
66c5fb109e64
Rename storage/mod_ejabberd to mod_storage_sql_ejabberd. Also rename configuration option. Untested.
Matthew Wild <mwild1@gmail.com>
parents:
3429
diff
changeset
|
27 dbh:autocommit(true); |
3415
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 database = dbh; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 end |
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 -- initialize db |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 local ejabberd_init = module:require("ejabberd_init"); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 ejabberd_init.init(database); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 |
3429
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
35 local sqlcache = {}; |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
36 local function prepare(sql) |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
37 module:log("debug", "query: %s", sql); |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
38 local err; |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
39 local r = sqlcache[sql]; |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
40 if not r then |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
41 r, err = database:prepare(sql); |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
42 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
|
43 sqlcache[sql] = r; |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
44 end |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
45 return r; |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
46 end |
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
47 |
3415
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 local _parse_xml = module:require("xmlparse"); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 local function parse_xml(str) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 local s = _parse_xml(str); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 if s and not s.gsub then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 return st.preserialize(s); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 end |
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 local function unparse_xml(s) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 return tostring(st.deserialize(s)); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 end |
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 |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 local handlers = {}; |
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.accounts = { |
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 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
|
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 { password = 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 and data.password then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 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
|
71 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
|
72 else |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 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
|
74 end |
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 }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 handlers.vcard = { |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 get = function(self, user) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 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
|
80 local row = select and select:fetch(); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 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
|
82 end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 set = function(self, user, data) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 if data then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 data = unparse_xml(data); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 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
|
87 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
|
88 else |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 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
|
90 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 handlers.private = { |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 get = function(self, user) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 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
|
96 if select then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 local data = {}; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 for row in select:rows() do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 data[row[1]] = parse_xml(row[2]); |
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 return data; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 set = function(self, user, data) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 if data then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 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
|
107 for namespace,text in pairs(data) do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 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
|
109 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 return true; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 else |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 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
|
113 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
114 end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 -- TODO map_set, map_get |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
117 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
|
118 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
|
119 handlers.roster = { |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
120 get = function(self, user) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 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
|
122 if select then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 local roster = { pending = {} }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 for row in select:rows() do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
125 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
|
126 local item = { groups = {} }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 if nick == "" then nick = nil; end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 item.nick = nick; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
129 item.subscription = subscription_map[subscription]; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
130 if ask == "N" then ask = nil; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 elseif ask == "O" then ask = "subscribe" |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 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
|
133 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
|
134 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
|
135 item.ask = ask; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 roster[jid] = item; |
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 |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 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
|
140 if select then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
141 for row in select:rows() do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 local jid,grp = unpack(row); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 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
|
144 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 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
|
147 local row = select and select:fetch(); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
148 if row then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 roster[false] = { version = row[1]; }; |
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 return roster; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
152 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
154 set = function(self, user, data) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 if data and next(data) ~= nil then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
156 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
|
157 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
|
158 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
|
159 local done = {}; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
160 local pending = data.pending or {}; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 for jid,item in pairs(data) do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
162 if jid and jid ~= "pending" then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
163 local subscription = subscription_map_reverse[item.subscription]; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
164 local ask; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
165 if pending[jid] then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
166 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
|
167 else |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 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
|
169 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
170 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
|
171 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
|
172 done[jid] = true; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
173 for group in pairs(item.groups) do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
174 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
|
175 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
176 end |
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 for jid in pairs(pending) do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
179 if not done[jid] then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
180 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
|
181 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
182 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
183 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
|
184 if version then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
185 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
|
186 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
187 return true; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
188 else |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
189 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
|
190 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
|
191 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
|
192 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
193 end; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
194 }; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
195 |
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 local driver = {}; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
198 driver.__index = driver; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
199 |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
200 function driver:query(sql, ...) |
3429
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
201 local stmt,err = prepare(sql); |
3415
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
202 if not stmt then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
203 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
|
204 return nil, err; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
205 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
206 local ok, err = stmt:execute(...); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
207 if not ok then |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
208 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
|
209 return nil, err; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
210 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
211 return stmt; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
212 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
213 function driver:modify(sql, ...) |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
214 local stmt, err = self:query(sql, ...); |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
215 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
|
216 return nil, err; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
217 end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
218 |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
219 function driver:open(datastore, typ) |
3429
8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
Waqas Hussain <waqas20@gmail.com>
parents:
3415
diff
changeset
|
220 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
|
221 local handler = handlers[datastore]; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
222 if not handler then return nil; end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
223 for key,val in pairs(handler) do |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
224 instance[key] = val; |
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 if instance.init then instance:init(); end |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
227 return instance; |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
228 end |
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 |
5ba0e094a5e2
storage/mod_ejabberd (and associated library): Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
232 module:add_item("data-driver", driver); |