Annotate

tools/xep227toprosody.lua @ 3709:db1c1ddc79e8

tools/xep227toprosody.lua: Convert to use util.xmppstream
author Matthew Wild <mwild1@gmail.com>
date Tue, 07 Dec 2010 19:20:33 +0000
parent 3540:bc139431830b
child 3710:59fbe4536c69
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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";
3709
db1c1ddc79e8 tools/xep227toprosody.lua: Convert to use util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents: 3540
diff changeset
39 local xmppstream = require "util.xmppstream";
db1c1ddc79e8 tools/xep227toprosody.lua: Convert to use util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents: 3540
diff changeset
40 local new_xmpp_handlers = xmppstream.new_sax_handlers;
2507
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 local dm = require "util.datamanager"
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 dm.set_data_path("data");
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 local ns_xep227 = "http://www.xmpp.org/extensions/xep-0227.html#ns";
3709
db1c1ddc79e8 tools/xep227toprosody.lua: Convert to use util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents: 3540
diff changeset
45 local ns_separator = xmppstream.ns_separator;
db1c1ddc79e8 tools/xep227toprosody.lua: Convert to use util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents: 3540
diff changeset
46 local ns_pattern = xmppstream.ns_pattern;
2507
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
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 function store_vcard(username, host, stanza)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 -- create or update vCard for username@host
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 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
53 print("["..(err or "success").."] stored vCard: "..username.."@"..host);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 function store_password(username, host, password)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 -- create or update account for username@host
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 local ret, err = dm.store(username, host, "accounts", {password = password});
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 print("["..(err or "success").."] stored account: "..username.."@"..host.." = "..password);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 function store_roster(username, host, roster_items)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 -- 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
64 local roster = dm.load(username, host, "roster") or {};
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 -- merge imported roster-items with loaded roster
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 for item_tag in roster_items:childtags() do
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 -- jid for this roster-item
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 local item_jid = item_tag.attr.jid
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 -- validate item stanzas
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 if (item_tag.name == "item") and (item_jid ~= "") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 -- prepare roster item
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 -- TODO: is the subscription attribute optional?
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 local item = {subscription = item_tag.attr.subscription, groups = {}};
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 -- optional: give roster item a real name
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 if item_tag.attr.name then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 item.name = item_tag.attr.name;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 -- optional: iterate over group stanzas inside item stanza
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 for group_tag in item_tag:childtags() do
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 local group_name = group_tag:get_text();
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 if (group_tag.name == "group") and (group_name ~= "") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 item.groups[group_name] = true;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 else
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 print("[error] invalid group stanza: "..group_tag:pretty_print());
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 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 -- store item in roster
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 roster[item_jid] = item;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 print("[success] roster entry: " ..username.."@"..host.." - "..item_jid);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 else
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 print("[error] invalid roster stanza: " ..item_tag:pretty_print());
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 -- store merged roster-table
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 local ret, err = dm.store(username, host, "roster", roster);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 print("["..(err or "success").."] stored roster: " ..username.."@"..host);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 function store_private(username, host, private_items)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 local private = dm.load(username, host, "private") or {};
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 for ch in private_items:childtags() do
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 --print("private :"..ch:pretty_print());
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 private[ch.name..":"..ch.attr.xmlns] = st.preserialize(ch);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 print("[success] private item: " ..username.."@"..host.." - "..ch.name);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 local ret, err = dm.store(username, host, "private", private);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 print("["..(err or "success").."] stored private: " ..username.."@"..host);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 function store_offline_messages(username, host, offline_messages)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 -- TODO: maybe use list_load(), append and list_store() instead
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 -- of constantly reopening the file with list_append()?
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 for ch in offline_messages:childtags() do
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 --print("message :"..ch:pretty_print());
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 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
117 print("["..(err or "success").."] stored offline message: " ..username.."@"..host.." - "..ch.attr.from);
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 2508
diff changeset
118 end
2507
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119 end
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
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 function store_subscription_request(username, host, presence_stanza)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 local from_bare = presence_stanza.attr.from;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 -- 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
126 local roster = dm.load(username, host, "roster") or {};
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 local item = roster[from_bare];
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 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
130 return; -- already subscribed, do nothing
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 -- add to table of pending subscriptions
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134 if not roster.pending then roster.pending = {}; end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 roster.pending[from_bare] = true;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 -- store updated roster-table
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138 local ret, err = dm.store(username, host, "roster", roster);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 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
140 end
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
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144 local curr_host = "";
2508
fed7f19db0da xep227toprosody: Fixed some global accesses.
Waqas Hussain <waqas20@gmail.com>
parents: 2507
diff changeset
145 local user_name = "";
2507
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
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148 local cb = {
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 stream_tag = "user",
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150 stream_ns = ns_xep227,
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151 };
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
152 function cb.streamopened(session, attr)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 session.notopen = false;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 user_name = attr.name;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 store_password(user_name, curr_host, attr.password);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157 function cb.streamclosed(session)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158 session.notopen = true;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159 user_name = "";
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161 function cb.handlestanza(session, stanza)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 --print("Parsed stanza "..stanza.name.." xmlns: "..(stanza.attr.xmlns or ""));
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163 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
164 store_vcard(user_name, curr_host, stanza);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 elseif (stanza.name == "query") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 if (stanza.attr.xmlns == "jabber:iq:roster") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 store_roster(user_name, curr_host, stanza);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 elseif (stanza.attr.xmlns == "jabber:iq:private") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 store_private(user_name, curr_host, stanza);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 elseif (stanza.name == "offline-messages") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 store_offline_messages(user_name, curr_host, stanza);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
173 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
174 store_subscription_request(user_name, curr_host, stanza);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
175 else
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 print("UNHANDLED stanza "..stanza.name.." xmlns: "..(stanza.attr.xmlns or ""));
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 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179
3709
db1c1ddc79e8 tools/xep227toprosody.lua: Convert to use util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents: 3540
diff changeset
180 local user_handlers = new_xmpp_handlers({ notopen = true }, cb);
2507
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
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
184 local lxp_handlers = {
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
185 --count = 0
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
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188 -- 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
189 function lxp_handlers.StartElement(parser, elementname, attributes)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
190 local curr_ns, name = elementname:match(ns_pattern);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
191 if name == "" then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
192 curr_ns, name = "", curr_ns;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
193 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
194 --io.write("+ ", string.rep(" ", count), name, " (", curr_ns, ")", "\n")
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
195 --count = count + 1;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
196 if curr_host ~= "" then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
197 -- forward to xmlhandlers
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
198 user_handlers:StartElement(elementname, attributes);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
199 elseif (curr_ns == ns_xep227) and (name == "host") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
200 curr_host = attributes["jid"]; -- start of host element
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
201 print("Begin parsing host "..curr_host);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
202 elseif (curr_ns ~= ns_xep227) or (name ~= "server-data") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
203 io.stderr:write("Unhandled XML element: ", name, "\n");
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
204 os.exit(1);
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 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
207
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
208 -- 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
209 function lxp_handlers.EndElement(parser, elementname)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
210 local curr_ns, name = elementname:match(ns_pattern);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
211 if name == "" then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
212 curr_ns, name = "", curr_ns;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
213 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
214 --count = count - 1;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
215 --io.write("- ", string.rep(" ", count), name, " (", curr_ns, ")", "\n")
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
216 if curr_host ~= "" then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
217 if (curr_ns == ns_xep227) and (name == "host") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
218 print("End parsing host "..curr_host);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
219 curr_host = "" -- end of host element
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
220 else
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
221 -- forward to xmlhandlers
2508
fed7f19db0da xep227toprosody: Fixed some global accesses.
Waqas Hussain <waqas20@gmail.com>
parents: 2507
diff changeset
222 user_handlers:EndElement(elementname);
2507
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
223 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
224 elseif (curr_ns ~= ns_xep227) or (name ~= "server-data") then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
225 io.stderr:write("Unhandled XML element: ", name, "\n");
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
226 os.exit(1);
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 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
229
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
230 function lxp_handlers.CharacterData(parser, string)
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
231 if curr_host ~= "" then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
232 -- forward to xmlhandlers
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
233 user_handlers:CharacterData(string);
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 end
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
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
239 local arg = ...;
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
240 local help = "/? -? ? /h -h /help -help --help";
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
241 if not arg or help:find(arg, 1, true) then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
242 print([[XEP-227 importer for Prosody
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
243
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
244 Usage: xep227toprosody.lua filename.xml
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 ]]);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
247 os.exit(1);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
248 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
249
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
250 local file = io.open(arg);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
251 if not file then
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
252 io.stderr:write("Could not open file: ", arg, "\n");
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
253 os.exit(0);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
254 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
255
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
256 local parser = lxp.new(lxp_handlers, ns_separator);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
257 for l in file:lines() do
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
258 parser:parse(l);
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
259 end
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
260 parser:parse();
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
261 parser:close();
a8ce11633597 xep227toprosody: Initial commit (thanks stefan).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
262 file:close();