Annotate

mod_roster_command/mod_roster_command.lua @ 453:1969310ea06a

mod_roster_command: Manage rosters through prosodyctl
author Matthew Wild <mwild1@gmail.com>
date Tue, 11 Oct 2011 01:41:22 +0100
child 492:f806c8a7f985
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
453
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -----------------------------------------------------------
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- mod_roster_command: Manage rosters through prosodyctl
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- version 0.02
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 -----------------------------------------------------------
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- Copyright (C) 2011 Matthew Wild
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- Copyright (C) 2011 Adam Nielsen
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 --
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 -- This project is MIT/X11 licensed. Please see the
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 -- COPYING file in the source package for more information.
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 -----------------------------------------------------------
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 -- Workaround for lack of util.startup...
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 _G.bare_sessions = _G.bare_sessions or {};
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local rostermanager = require "core.rostermanager";
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local storagemanager = require "core.storagemanager";
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local jid = require "util.jid";
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local warn = prosodyctl.show_warning;
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 -- Make a *one-way* subscription. User will see when contact is online,
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 -- contact will not see when user is online.
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 function subscribe(user_jid, contact_jid)
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local user_username, user_host = jid.split(user_jid);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local contact_username, contact_host = jid.split(contact_jid);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 if not hosts[user_host] then
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 warn("The host '%s' is not configured for this server.", user_host);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 return;
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 storagemanager.initialize_host(user_host);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 -- Update user's roster to say subscription request is pending...
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 rostermanager.set_contact_pending_out(user_username, user_host, contact_jid);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 if hosts[contact_host] then
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if contact_host ~= user_host then
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 storagemanager.initialize_host(contact_host);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 -- Update contact's roster to say subscription request is pending...
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 rostermanager.set_contact_pending_in(contact_username, contact_host, user_jid);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 -- Update contact's roster to say subscription request approved...
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 rostermanager.subscribed(contact_username, contact_host, user_jid);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 -- Update user's roster to say subscription request approved...
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 rostermanager.process_inbound_subscription_approval(user_username, user_host, contact_jid);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 -- Make a mutual subscription between jid1 and jid2. Each JID will see
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 -- when the other one is online.
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 function subscribe_both(jid1, jid2)
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 subscribe(jid1, jid2);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 subscribe(jid2, jid1);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 -- Unsubscribes user from contact (not contact from user, if subscribed).
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 function unsubscribe(user_jid, contact_jid)
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local user_username, user_host = jid.split(user_jid);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local contact_username, contact_host = jid.split(contact_jid);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 if not hosts[user_host] then
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 warn("The host '%s' is not configured for this server.", user_host);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 return;
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 storagemanager.initialize_host(user_host);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 -- Update user's roster to say subscription is cancelled...
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 rostermanager.unsubscribe(user_username, user_host, contact_jid);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 if hosts[contact_host] then
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 if contact_host ~= user_host then
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 storagemanager.initialize_host(contact_host);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 -- Update contact's roster to say subscription is cancelled...
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 rostermanager.unsubscribed(contact_username, contact_host, user_jid);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 -- Cancel any subscription in either direction.
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 function unsubscribe_both(jid1, jid2)
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 unsubscribe(jid1, jid2);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 unsubscribe(jid2, jid1);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 -- Set the name shown and group used in the contact list
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 function rename(user_jid, contact_jid, contact_nick, contact_group)
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 local user_username, user_host = jid.split(user_jid);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 if not hosts[user_host] then
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 warn("The host '%s' is not configured for this server.", user_host);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 return;
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 storagemanager.initialize_host(user_host);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 -- Load user's roster and find the contact
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 local roster = rostermanager.load_roster(user_username, user_host);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 local item = roster[contact_jid];
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 if item then
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 if contact_nick then
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 item.name = contact_nick;
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 if contact_group then
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 item.groups = {}; -- Remove from all current groups
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 item.groups[contact_group] = true;
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 rostermanager.save_roster(user_username, user_host, roster);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 function module.command(arg)
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 local command = arg[1];
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 if not command then
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 warn("Valid subcommands: (un)subscribe(_both) | rename");
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 return 0;
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 table.remove(arg, 1);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 if command == "subscribe" then
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 subscribe(arg[1], arg[2]);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 return 0;
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 elseif command == "subscribe_both" then
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 subscribe_both(arg[1], arg[2]);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 return 0;
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 elseif command == "unsubscribe" then
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 unsubscribe(arg[1], arg[2]);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 return 0;
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 elseif command == "unsubscribe_both" then
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 unsubscribe_both(arg[1], arg[2]);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 return 0;
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 elseif command == "rename" then
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 rename(arg[1], arg[2], arg[3], arg[4]);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 return 0;
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 else
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 warn("Unknown command: %s", command);
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 return 1;
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 end
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 return 0;
1969310ea06a mod_roster_command: Manage rosters through prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 end