Skip to content

Commit 341b530

Browse files
committed
Added auto-approve to auto-subscribe for private channels.
1 parent 9f06d46 commit 341b530

5 files changed

Lines changed: 258 additions & 112 deletions

File tree

configuration.properties.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ users.admin = user1@example.com;crawler@searchengine.org
2121
# A list of channels (local or remote) to which to subscribe new users
2222
# Note that the channels will not be created - they must already exist
2323
channels.autosubscribe=lounge@example.com;welcome@example.com
24+
25+
# If any of the 'channels.autosubscribe' channels are private local channels,
26+
# then whether to automatically approve the user.
27+
# NOTE: This will only work on local private channels
28+
channels.autosubscribe.autoapprove=false

src/main/java/org/buddycloud/channelserver/Configuration.java

Lines changed: 115 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -13,118 +13,134 @@
1313
import org.apache.log4j.Logger;
1414
import org.xmpp.packet.JID;
1515

16-
public class Configuration extends Properties
17-
{
16+
public class Configuration extends Properties {
1817
private static final Logger LOGGER = Logger.getLogger(Configuration.class);
19-
18+
2019
private static final long serialVersionUID = 1L;
21-
20+
2221
private static final String ARRAY_PROPERTY_SEPARATOR = ";";
2322

2423
public static final String CONFIGURATION_SERVER_DOMAIN = "server.domain";
2524
public static final String CONFIGURATION_SERVER_CHANNELS_DOMAIN = "server.domain.channels";
2625
public static final String CONFIGURATION_SERVER_TOPICS_DOMAIN = "server.domain.topics";
27-
26+
2827
public static final String CONFIGURATION_ADMIN_USERS = "users.admin";
2928

3029
public static final String CONFIGURATION_CHANNELS_AUTOSUBSCRIBE = "channels.autosubscribe";
31-
30+
public static final String CONFIGURATION_CHANNELS_AUTOSUBSCRIBE_AUTOAPPROVE = "channels.autosubscribe.autoapprove";
31+
3232
private static final String CONFIGURATION_FILE = "configuration.properties";
33-
private static Configuration instance = null;
34-
33+
private static Configuration instance = null;
34+
3535
private Collection<JID> adminUsers = new ArrayList<JID>();
3636
private Collection<JID> autosubscribeChannels = new ArrayList<JID>();
37-
37+
3838
private Properties conf;
39-
40-
private Configuration()
41-
{
42-
try {
43-
conf = new Properties();
44-
File f = new File(CONFIGURATION_FILE);
45-
46-
if(f.exists()) {
47-
LOGGER.info("Found " + CONFIGURATION_FILE + " in working directory.");
48-
load(new FileInputStream(f));
49-
} else {
50-
// Otherwise attempt to load it from the classpath
51-
LOGGER.info("No " + CONFIGURATION_FILE + " found in working directory. Attempting to load from classpath.");
52-
load(this.getClass().getClassLoader().getResourceAsStream(CONFIGURATION_FILE));
53-
}
54-
} catch (Exception e) {
55-
System.out.println(e.getMessage());
56-
System.exit(1);
57-
}
58-
}
59-
60-
private void setupCollections() {
61-
adminUsers = getJIDArrayProperty(CONFIGURATION_ADMIN_USERS);
62-
autosubscribeChannels = getJIDArrayProperty(CONFIGURATION_CHANNELS_AUTOSUBSCRIBE);
39+
40+
private Configuration() {
41+
try {
42+
conf = new Properties();
43+
File f = new File(CONFIGURATION_FILE);
44+
45+
if (f.exists()) {
46+
LOGGER.info("Found " + CONFIGURATION_FILE
47+
+ " in working directory.");
48+
load(new FileInputStream(f));
49+
} else {
50+
// Otherwise attempt to load it from the classpath
51+
LOGGER.info("No "
52+
+ CONFIGURATION_FILE
53+
+ " found in working directory. Attempting to load from classpath.");
54+
load(this.getClass().getClassLoader()
55+
.getResourceAsStream(CONFIGURATION_FILE));
56+
}
57+
} catch (Exception e) {
58+
System.out.println(e.getMessage());
59+
System.exit(1);
60+
}
61+
}
62+
63+
private void setupCollections() {
64+
adminUsers = getJIDArrayProperty(CONFIGURATION_ADMIN_USERS);
65+
autosubscribeChannels = getJIDArrayProperty(CONFIGURATION_CHANNELS_AUTOSUBSCRIBE);
66+
}
67+
68+
public Collection<JID> getAdminUsers() {
69+
return adminUsers;
70+
}
71+
72+
public Collection<JID> getAutosubscribeChannels() {
73+
return autosubscribeChannels;
74+
}
75+
76+
public static Configuration getInstance() {
77+
if (null == instance) {
78+
instance = new Configuration();
79+
}
80+
return instance;
81+
}
82+
83+
public String getProperty(String key) {
84+
return conf.getProperty(key);
85+
}
86+
87+
public String getProperty(String key, String defaultValue) {
88+
return conf.getProperty(key, defaultValue);
89+
}
90+
91+
public void load(InputStream inputStream) throws IOException {
92+
conf.load(inputStream);
93+
setupCollections();
94+
}
95+
96+
private Collection<String> getStringArrayProperty(String key) {
97+
String prop = getProperty(key);
98+
99+
if (null == prop) {
100+
return Collections.emptyList();
101+
}
102+
103+
return Arrays.asList(prop.split(ARRAY_PROPERTY_SEPARATOR));
104+
}
105+
106+
private Collection<JID> getJIDArrayProperty(String key) {
107+
Collection<String> props = getStringArrayProperty(key);
108+
109+
Collection<JID> jids = new ArrayList<JID>(props.size());
110+
111+
for (String prop : props) {
112+
jids.add(new JID(prop));
113+
}
114+
115+
return jids;
116+
}
117+
118+
public String getServerDomain() {
119+
return getProperty(CONFIGURATION_SERVER_DOMAIN);
120+
}
121+
122+
public String getServerChannelsDomain() {
123+
return getProperty(CONFIGURATION_SERVER_CHANNELS_DOMAIN);
124+
}
125+
126+
public String getServerTopicsDomain() {
127+
return getProperty(CONFIGURATION_SERVER_TOPICS_DOMAIN);
128+
}
129+
130+
public boolean getBooleanProperty(
131+
final String key, final boolean defaultValue) {
132+
String value = getProperty(key);
133+
134+
if(value != null) {
135+
if(value.equalsIgnoreCase("true")) {
136+
return true;
137+
}
138+
if(value.equalsIgnoreCase("false")) {
139+
return false;
140+
}
141+
LOGGER.warn("Invalid boolean property value for " + key + ": " + value);
142+
}
143+
144+
return defaultValue;
63145
}
64-
65-
public Collection<JID> getAdminUsers() {
66-
return adminUsers;
67-
}
68-
69-
public Collection<JID> getAutosubscribeChannels() {
70-
return autosubscribeChannels;
71-
}
72-
73-
public static Configuration getInstance()
74-
{
75-
if (null == instance) {
76-
instance = new Configuration();
77-
}
78-
return instance;
79-
}
80-
81-
public String getProperty(String key)
82-
{
83-
return conf.getProperty(key);
84-
}
85-
86-
public String getProperty(String key, String defaultValue)
87-
{
88-
return conf.getProperty(key, defaultValue);
89-
}
90-
91-
public void load(InputStream inputStream) throws IOException
92-
{
93-
conf.load(inputStream);
94-
setupCollections();
95-
}
96-
97-
private Collection<String> getStringArrayProperty(String key) {
98-
String prop = getProperty(key);
99-
100-
if(null == prop) {
101-
return Collections.emptyList();
102-
}
103-
104-
return Arrays.asList(prop.split(ARRAY_PROPERTY_SEPARATOR));
105-
}
106-
107-
private Collection<JID> getJIDArrayProperty(String key) {
108-
Collection<String> props = getStringArrayProperty(key);
109-
110-
Collection<JID> jids = new ArrayList<JID>(props.size());
111-
112-
for(String prop : props) {
113-
jids.add(new JID(prop));
114-
}
115-
116-
return jids;
117-
}
118-
119-
public String getServerDomain() {
120-
return getProperty(CONFIGURATION_SERVER_DOMAIN);
121-
}
122-
123-
public String getServerChannelsDomain() {
124-
return getProperty(CONFIGURATION_SERVER_CHANNELS_DOMAIN);
125-
}
126-
127-
public String getServerTopicsDomain() {
128-
return getProperty(CONFIGURATION_SERVER_TOPICS_DOMAIN);
129-
}
130146
}

src/main/java/org/buddycloud/channelserver/packetprocessor/iq/namespace/register/RegisterSet.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import org.buddycloud.channelserver.db.exception.NodeStoreException;
1111
import org.buddycloud.channelserver.packetprocessor.PacketProcessor;
1212
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.JabberPubsub;
13+
import org.buddycloud.channelserver.pubsub.accessmodel.AccessModels;
14+
import org.buddycloud.channelserver.pubsub.model.impl.NodeSubscriptionImpl;
15+
import org.buddycloud.channelserver.pubsub.subscription.Subscriptions;
1316
import org.dom4j.Element;
1417
import org.dom4j.QName;
1518
import org.xmpp.packet.IQ;
@@ -118,29 +121,47 @@ private void autosubscribeToChannels(final JID from) {
118121
IQ subscribe = new IQ();
119122

120123
subscribe.setType(Type.set);
121-
124+
122125
Element el = subscribe.getElement();
123126
Element pubsubEl = el.addElement("pubsub",
124127
JabberPubsub.NAMESPACE_URI);
125128
Element subscribeEl = pubsubEl.addElement("subscribe");
126-
subscribeEl.addAttribute("node",
127-
Conf.getPostChannelNodename(channel));
129+
130+
String channelNodeId = Conf.getPostChannelNodename(channel);
131+
132+
subscribeEl.addAttribute("node", channelNodeId);
128133
subscribeEl.addAttribute("jid", from.toBareJID().toString());
129134

130135
try {
131-
if(channelManager.isLocalJID(channel)) {
136+
if (channelManager.isLocalJID(channel)) {
132137
subscribe.setFrom(from);
133138
subscribe.setTo(conf.getServerChannelsDomain());
134139
} else {
135140
subscribe.setFrom(conf.getServerChannelsDomain());
136141
subscribe.setTo(channel.getDomain());
137-
138-
Element actorEl = pubsubEl.addElement(QName.get("actor", JabberPubsub.NS_BUDDYCLOUD));
139-
142+
143+
Element actorEl = pubsubEl.addElement(QName.get("actor",
144+
JabberPubsub.NS_BUDDYCLOUD));
145+
140146
actorEl.setText(from.toBareJID());
141147
}
142148

143149
outQueue.put(subscribe);
150+
151+
// If auto-approve is set, and this is a local private channel
152+
// then set the user to subscribed
153+
if (conf.getBooleanProperty(
154+
Configuration.CONFIGURATION_CHANNELS_AUTOSUBSCRIBE_AUTOAPPROVE,
155+
false)
156+
&& channelManager.isLocalJID(channel)
157+
&& AccessModels.authorize.toString().equals(
158+
channelManager.getNodeConfValue(channelNodeId,
159+
Conf.ACCESS_MODEL))) {
160+
channelManager
161+
.addUserSubscription(new NodeSubscriptionImpl(Conf
162+
.getPostChannelNodename(channel), from,
163+
Subscriptions.subscribed));
164+
}
144165
} catch (InterruptedException e) {
145166
LOGGER.error("Could not auto-subscribe " + from + " to "
146167
+ channel, e);

0 commit comments

Comments
 (0)