tools/xep227toprosody.lua

Fri, 11 Jun 2010 21:30:24 +0500

author
Waqas Hussain <waqas20@gmail.com>
date
Fri, 11 Jun 2010 21:30:24 +0500
changeset 3239
5ea90ee96022
parent 2508
fed7f19db0da
child 3540
bc139431830b
permissions
-rwxr-xr-x

sessionmanager: Fixed a traceback on invalid usernames (typo in previous commit).

2507
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env lua
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 -- Prosody IM
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Matthew Wild
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 -- Copyright (C) 2008-2009 Waqas Hussain
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 -- Copyright (C) 2010 Stefan Gehn
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 --
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 -- This project is MIT/X11 licensed. Please see the
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 -- COPYING file in the source package for more information.
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 --
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 -- FIXME: XEP-0227 supports XInclude but luaexpat does not
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 --
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 -- XEP-227 elements and their current level of support:
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 -- Hosts : supported
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 -- Users : supported
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 -- Rosters : supported, needs testing
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 -- Offline Messages : supported, needs testing
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 -- Private XML Storage : supported, needs testing
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 -- vCards : supported, needs testing
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 -- Privacy Lists: UNSUPPORTED
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 -- http://xmpp.org/extensions/xep-0227.html#privacy-lists
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 -- mod_privacy uses dm.load(username, host, "privacy"); and stores stanzas 1:1
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 -- Incoming Subscription Requests : supported
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 package.path = package.path..";../?.lua";
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 package.cpath = package.cpath..";../?.so"; -- needed for util.pposix used in datamanager
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 -- ugly workaround for getting datamanager to work outside of prosody :(
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 prosody = { };
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 prosody.platform = "unknown";
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 if os.getenv("WINDIR") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 prosody.platform = "windows";
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 elseif package.config:sub(1,1) == "/" then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 prosody.platform = "posix";
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 local lxp = require "lxp";
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 local st = require "util.stanza";
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 local init_xmlhandlers = require "core.xmlhandlers";
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 local dm = require "util.datamanager"
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 dm.set_data_path("data");
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 local ns_separator = "\1";
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 local ns_xep227 = "http://www.xmpp.org/extensions/xep-0227.html#ns";
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 -----------------------------------------------------------------------
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 function store_vcard(username, host, stanza)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 -- create or update vCard for username@host
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 local ret, err = dm.store(username, host, "vcard", st.preserialize(stanza));
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 print("["..(err or "success").."] stored vCard: "..username.."@"..host);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 function store_password(username, host, password)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 -- create or update account for username@host
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 local ret, err = dm.store(username, host, "accounts", {password = password});
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 print("["..(err or "success").."] stored account: "..username.."@"..host.." = "..password);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 function store_roster(username, host, roster_items)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 -- fetch current roster-table for username@host if he already has one
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 local roster = dm.load(username, host, "roster") or {};
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 -- merge imported roster-items with loaded roster
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 for item_tag in roster_items:childtags() do
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 -- jid for this roster-item
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 local item_jid = item_tag.attr.jid
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 -- validate item stanzas
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 if (item_tag.name == "item") and (item_jid ~= "") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 -- prepare roster item
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 -- TODO: is the subscription attribute optional?
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 local item = {subscription = item_tag.attr.subscription, groups = {}};
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 -- optional: give roster item a real name
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 if item_tag.attr.name then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 item.name = item_tag.attr.name;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 -- optional: iterate over group stanzas inside item stanza
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 for group_tag in item_tag:childtags() do
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 local group_name = group_tag:get_text();
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 if (group_tag.name == "group") and (group_name ~= "") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 item.groups[group_name] = true;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 else
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 print("[error] invalid group stanza: "..group_tag:pretty_print());
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 -- store item in roster
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 roster[item_jid] = item;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 print("[success] roster entry: " ..username.."@"..host.." - "..item_jid);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 else
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 print("[error] invalid roster stanza: " ..item_tag:pretty_print());
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 -- store merged roster-table
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 local ret, err = dm.store(username, host, "roster", roster);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 print("["..(err or "success").."] stored roster: " ..username.."@"..host);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 function store_private(username, host, private_items)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 local private = dm.load(username, host, "private") or {};
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 for ch in private_items:childtags() do
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 --print("private :"..ch:pretty_print());
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 private[ch.name..":"..ch.attr.xmlns] = st.preserialize(ch);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 print("[success] private item: " ..username.."@"..host.." - "..ch.name);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 local ret, err = dm.store(username, host, "private", private);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 print("["..(err or "success").."] stored private: " ..username.."@"..host);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 function store_offline_messages(username, host, offline_messages)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 -- TODO: maybe use list_load(), append and list_store() instead
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 -- of constantly reopening the file with list_append()?
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 for ch in offline_messages:childtags() do
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 --print("message :"..ch:pretty_print());
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 local ret, err = dm.list_append(username, host, "offline", st.preserialize(ch));
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 print("["..(err or "success").."] stored offline message: " ..username.."@"..host.." - "..ch.attr.from);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 function store_subscription_request(username, host, presence_stanza)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 local from_bare = presence_stanza.attr.from;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 -- fetch current roster-table for username@host if he already has one
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 local roster = dm.load(username, host, "roster") or {};
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 local item = roster[from_bare];
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 if item and (item.subscription == "from" or item.subscription == "both") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 return; -- already subscribed, do nothing
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 -- add to table of pending subscriptions
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 if not roster.pending then roster.pending = {}; end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134 roster.pending[from_bare] = true;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 -- store updated roster-table
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 local ret, err = dm.store(username, host, "roster", roster);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138 print("["..(err or "success").."] stored subscription request: " ..username.."@"..host.." - "..from_bare);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 -----------------------------------------------------------------------
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 local curr_host = "";
2508
fed7f19db0da xep227toprosody: Fixed some global accesses.
Waqas Hussain <waqas20@gmail.com>
parents: 2507
diff changeset
144 local user_name = "";
2507
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147 local cb = {
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148 stream_tag = "user",
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 stream_ns = ns_xep227,
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150 };
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151 function cb.streamopened(session, attr)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
152 session.notopen = false;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 user_name = attr.name;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 store_password(user_name, curr_host, attr.password);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 function cb.streamclosed(session)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157 session.notopen = true;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158 user_name = "";
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160 function cb.handlestanza(session, stanza)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161 --print("Parsed stanza "..stanza.name.." xmlns: "..(stanza.attr.xmlns or ""));
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 if (stanza.name == "vCard") and (stanza.attr.xmlns == "vcard-temp") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163 store_vcard(user_name, curr_host, stanza);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 elseif (stanza.name == "query") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 if (stanza.attr.xmlns == "jabber:iq:roster") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 store_roster(user_name, curr_host, stanza);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 elseif (stanza.attr.xmlns == "jabber:iq:private") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 store_private(user_name, curr_host, stanza);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 elseif (stanza.name == "offline-messages") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 store_offline_messages(user_name, curr_host, stanza);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 elseif (stanza.name == "presence") and (stanza.attr.xmlns == "jabber:client") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
173 store_subscription_request(user_name, curr_host, stanza);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
174 else
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
175 print("UNHANDLED stanza "..stanza.name.." xmlns: "..(stanza.attr.xmlns or ""));
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179 local user_handlers = init_xmlhandlers({ notopen = true, }, cb);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
180
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181 -----------------------------------------------------------------------
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
182
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
183 local lxp_handlers = {
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
184 --count = 0
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
185 };
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
186
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
187 -- TODO: error handling for invalid opening elements if curr_host is empty
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188 function lxp_handlers.StartElement(parser, elementname, attributes)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
189 local curr_ns, name = elementname:match(ns_pattern);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
190 if name == "" then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
191 curr_ns, name = "", curr_ns;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
192 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
193 --io.write("+ ", string.rep(" ", count), name, " (", curr_ns, ")", "\n")
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
194 --count = count + 1;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
195 if curr_host ~= "" then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
196 -- forward to xmlhandlers
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
197 user_handlers:StartElement(elementname, attributes);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
198 elseif (curr_ns == ns_xep227) and (name == "host") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
199 curr_host = attributes["jid"]; -- start of host element
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
200 print("Begin parsing host "..curr_host);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
201 elseif (curr_ns ~= ns_xep227) or (name ~= "server-data") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
202 io.stderr:write("Unhandled XML element: ", name, "\n");
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
203 os.exit(1);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
204 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
205 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
206
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
207 -- TODO: error handling for invalid closing elements if host is empty
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
208 function lxp_handlers.EndElement(parser, elementname)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
209 local curr_ns, name = elementname:match(ns_pattern);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
210 if name == "" then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
211 curr_ns, name = "", curr_ns;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
212 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
213 --count = count - 1;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
214 --io.write("- ", string.rep(" ", count), name, " (", curr_ns, ")", "\n")
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
215 if curr_host ~= "" then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
216 if (curr_ns == ns_xep227) and (name == "host") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
217 print("End parsing host "..curr_host);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
218 curr_host = "" -- end of host element
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
219 else
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
220 -- forward to xmlhandlers
2508
fed7f19db0da xep227toprosody: Fixed some global accesses.
Waqas Hussain <waqas20@gmail.com>
parents: 2507
diff changeset
221 user_handlers:EndElement(elementname);
2507
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
222 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
223 elseif (curr_ns ~= ns_xep227) or (name ~= "server-data") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
224 io.stderr:write("Unhandled XML element: ", name, "\n");
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
225 os.exit(1);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
226 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
227 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
228
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
229 function lxp_handlers.CharacterData(parser, string)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
230 if curr_host ~= "" then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
231 -- forward to xmlhandlers
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
232 user_handlers:CharacterData(string);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
233 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
234 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
235
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
236 -----------------------------------------------------------------------
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
237
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
238 local arg = ...;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
239 local help = "/? -? ? /h -h /help -help --help";
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
240 if not arg or help:find(arg, 1, true) then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
241 print([[XEP-227 importer for Prosody
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
242
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
243 Usage: xep227toprosody.lua filename.xml
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
244
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
245 ]]);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
246 os.exit(1);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
247 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
248
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
249 local file = io.open(arg);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
250 if not file then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
251 io.stderr:write("Could not open file: ", arg, "\n");
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
252 os.exit(0);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
253 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
254
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
255 local parser = lxp.new(lxp_handlers, ns_separator);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
256 for l in file:lines() do
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
257 parser:parse(l);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
258 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
259 parser:parse();
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
260 parser:close();
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
261 file:close();

mercurial