Comparison

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