Commit 461670da authored by conor's avatar conor

added new features to content filter, see changelog.html for details

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@3262 b35dd754-fafc-0310-a699-88a17e54d16e
parent 8e88fb87
......@@ -44,6 +44,19 @@
ContentFilter Plugin Changelog
</h1>
<p><b>1.2</b> -- Jan, 2006</p>
<ul>
<li>Added support to notify administrator by email on content match notification.</li>
<li>Added support to include original packet when sending violation notifications.</li>
<li>Added support to filter users presence status.</li>
<li>Increased size of patterns input area to allow more patterns.</li>
<li>Fixed masking function.</li>
<li>Added debug logging to help users identify problems.</li>
<li>Updated readme.html</li>
</ul>
<p><b>1.0.1</b> -- December 15, 2005</p>
<ul>
<li>Now requires Wildfire 2.4.0</li>
......
......@@ -53,7 +53,7 @@ alternative content.
<h2>Installation</h2>
<p>
Copy the contentfilter.jar into the plugins directory of your Wildfire
Copy the contentfilter.jar into the plugins directory of your Jive Messenger
installation. The plugin will then be automatically deployed. To upgrade to a
new version, copy the new contentfilter.jar file over the existing file.
</p>
......@@ -62,7 +62,7 @@ new version, copy the new contentfilter.jar file over the existing file.
<p>
By default, after the plugin has been deployed all of its features are disabled.
This plugin is configured via the "Content Filter" sidebar item located under the
"System" tab in the Wildfire Admin Console.
"System" tab in the Jive Messenger Admin Console.
</p>
<p>
......@@ -73,16 +73,28 @@ can be enhanced with more complex regular expressions as required e.g.:
<ul>
<li>for a complete word match, add boundary checks with \b e.g. \bfox\b will match against the word "fox" and nothing else.
<li>for case insensitive matchs add (?i) e.g. (?i)\bfox\b will match against "fox", "Fox", "foX" etc.
<li>it is also possible to group related patterns patterns e.g. fox|dog, this can be used to reduce the number of individual patterns to test for.
</ul>
</p>
<p>
If you choose to filter your users presence status and there is a content match then:
<ul>
<li>if you are masking content, other users will see a masked status.
<li>if you are rejecting content, other users not see the status change, how it affects the user with the invalid status is client dependant.
</ul>
</p>
<p>
Want to know more about regular expressions in Java? This official <a href="http://java.sun.com/docs/books/tutorial/extra/regex/">tutorial</a>
is useful.
</p>
<p>
The default mask is "***", you can change it to anything you like including smilies!
</p>
<h2>Using the Plugin</h2>
<p>
After the plugin has been configured, nothing else needs to be done to use it.
TODO - update
</p>
</body>
......
/**
* $RCSfile$
* $Revision: 1594 $
* $Date: 2005-07-04 14:08:42 -0300 (Mon, 04 Jul 2005) $
*
* Copyright (C) 2004 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.wildfire.plugin;
import org.xmpp.packet.Message;
import java.util.ArrayList;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Filters message content using regular expressions. If a content mask is
* provided message content will be altered.
*
* @author Conor Hayes
*/
public class ContentFilter {
private String patterns;
private Collection<Pattern> compiledPatterns = new ArrayList<Pattern>();
private String mask;
/**
* A default instance will allow all message content.
*
* @see #setPatterns(String)
* @see #setMask(String)
*/
public ContentFilter() {
}
/**
* Set the patterns to use for searching content.
*
* @param regExps a comma separated String of regular expressions
*/
public void setPatterns(String patterns) {
if (patterns != null) {
this.patterns = patterns;
String[] data = patterns.split(",");
compiledPatterns.clear();
for (int i = 0; i < data.length; i++) {
compiledPatterns.add(Pattern.compile(data[i]));
}
}
else {
clearPatterns();
}
}
public String getPatterns() {
return this.patterns;
}
/**
* Clears all patterns. Calling this method means that all message content
* will be allowed.
*/
public void clearPatterns() {
patterns = null;
compiledPatterns.clear();
}
/**
* Set the content replacement mask.
*
* @param mask the mask to use when replacing content
*/
public void setMask(String mask) {
this.mask = mask;
}
/**
* @return the current mask or null if none has been set
*/
public String getMask() {
return mask;
}
/**
* Clears the content mask.
*
* @see #filter(Message)
*/
public void clearMask() {
mask = null;
}
/**
* @return true if the filter is currently masking content, false otherwise
*/
public boolean isMaskingContent() {
return mask != null;
}
/**
* Filters message content.
*
* @param msg the message to filter, its subject/body may be altered if there
* are content matches and a content mask is set
* @return true if the msg content matched up, false otherwise
*/
public boolean filter(Message msg) {
boolean hasMatch = false;
if (msg.getSubject() != null) {
if (hasMatch(msg.getSubject())) {
hasMatch = true;
if (isMaskingContent()) {
String newSubject = replaceContent(msg.getSubject());
msg.setSubject(newSubject);
}
}
}
if (msg.getBody() != null) {
if (hasMatch(msg.getBody())) {
hasMatch = true;
if (isMaskingContent()) {
String newBody = replaceContent(msg.getBody());
msg.setBody(newBody);
}
}
}
return hasMatch;
}
private String replaceContent(String content) {
for (Pattern pattern : compiledPatterns) {
Matcher m = pattern.matcher(content);
content = m.replaceAll(mask);
}
return content;
}
/**
* Performs sequential search for any pattern match.
*
* @param content the content to search against
* @return true if a match is found, false otherwise
*/
private boolean hasMatch(String content) {
boolean hasMatch = false;
for (Pattern pattern : compiledPatterns) {
Matcher m = pattern.matcher(content);
if (m.find()) {
hasMatch = true;
break;
}
}
return hasMatch;
}
/**
* $RCSfile$
* $Revision: 1594 $
* $Date: 2005-07-04 18:08:42 +0100 (Mon, 04 Jul 2005) $
*
* Copyright (C) 2004 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.wildfire.plugin;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.dom4j.Element;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
/**
* Filters message content using regular expressions. If a content mask is
* provided message content will be altered.
*
* @author Conor Hayes
*/
public class ContentFilter {
private String patterns;
private Collection<Pattern> compiledPatterns = new ArrayList<Pattern>();
private String mask;
/**
* A default instance will allow all message content.
*
* @see #setPatterns(String)
* @see #setMask(String)
*/
public ContentFilter() {
}
/**
* Set the patterns to use for searching content.
*
* @param regExps a comma separated String of regular expressions
*/
public void setPatterns(String patterns) {
if (patterns != null) {
this.patterns = patterns;
String[] data = patterns.split(",");
compiledPatterns.clear();
for (int i = 0; i < data.length; i++) {
compiledPatterns.add(Pattern.compile(data[i]));
}
}
else {
clearPatterns();
}
}
public String getPatterns() {
return this.patterns;
}
/**
* Clears all patterns. Calling this method means that all message content
* will be allowed.
*/
public void clearPatterns() {
patterns = null;
compiledPatterns.clear();
}
/**
* Set the content replacement mask.
*
* @param mask the mask to use when replacing content
*/
public void setMask(String mask) {
this.mask = mask;
}
/**
* @return the current mask or null if none has been set
*/
public String getMask() {
return mask;
}
/**
* Clears the content mask.
*
* @see #filter(Message)
*/
public void clearMask() {
mask = null;
}
/**
* @return true if the filter is currently masking content, false otherwise
*/
public boolean isMaskingContent() {
return mask != null;
}
/**
* Filters packet content.
*
* @param packet the packet to filter, its content may be altered if there
* are content matches and a content mask is set
* @return true if the msg content matched up, false otherwise
*/
public boolean filter(Packet p) {
return process(p.getElement());
}
private boolean process(Element element) {
boolean matched = mask(element);
if (!matched || isMaskingContent())
{
//only check children if no match has yet been found
//or all content must be masked
Iterator iter = element.elementIterator();
while (iter.hasNext()) {
matched |= process((Element)iter.next());
}
}
return matched;
}
private boolean mask(Element element) {
boolean match = false;
String content = element.getText();
if ((content != null) && (content.length() > 0)) {
for (Pattern pattern : compiledPatterns) {
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
match = true;
if (isMaskingContent()) {
content = matcher.replaceAll(mask);
element.setText(content);
}
}
}
}
return match;
}
/**
* Applies mask to the given <code>content</code>
*
* @param content
* @return masked content
*/
private String mask(String content) {
for (Pattern pattern : compiledPatterns) {
Matcher m = pattern.matcher(content);
content = m.replaceAll(mask);
}
return content;
}
/**
* Applies patterns against the given <code>content</code>. Terminates on
* first match.
*
* @param content the content to search against
* @return true if a match is found, false otherwise
*/
private boolean hasMatch(String content) {
boolean hasMatch = false;
for (Pattern pattern : compiledPatterns) {
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
hasMatch = true;
break;
}
}
return hasMatch;
}
}
\ No newline at end of file
/**
* $RCSfile$
* $Revision: 1594 $
* $Date: 2005-07-04 14:08:42 -0300 (Mon, 04 Jul 2005) $
*
* Copyright (C) 2004 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.wildfire.plugin;
import org.jivesoftware.wildfire.MessageRouter;
import org.jivesoftware.wildfire.Session;
import org.jivesoftware.wildfire.XMPPServer;
import org.jivesoftware.wildfire.container.Plugin;
import org.jivesoftware.wildfire.container.PluginManager;
import org.jivesoftware.wildfire.interceptor.InterceptorManager;
import org.jivesoftware.wildfire.interceptor.PacketInterceptor;
import org.jivesoftware.wildfire.interceptor.PacketRejectedException;
import org.jivesoftware.util.JiveGlobals;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import java.io.File;
/**
* Content filter plugin.
*
* @author Conor Hayes
*/
public class ContentFilterPlugin implements Plugin, PacketInterceptor {
/**
* The expected value is a boolean, if true the user identified by the value
* of the property #VIOLATION_NOTIFICATION_CONTACT_PROPERTY will be notified
* every time there is a content match, otherwise no notification will be
* sent. Then default value is false.
*/
public static final String VIOLATION_NOTIFICATION_ENABLED_PROPERTY =
"plugin.contentFilter.violation.notification.enabled";
/**
* The expected value is a user name. The default value is "admin".
*/
public static final String VIOLATION_NOTIFICATION_CONTACT_PROPERTY =
"plugin.contentFilter.violation.notification.contact";
/**
* The expected value is a boolean, if true the sender will be notified when a
* message is rejected, otherwise the message will be silently rejected,i.e. the
* sender will not know that the message was rejected and the receiver will
* not get the message. The default value is false.
*/
public static final String REJECTION_NOTIFICATION_ENABLED_PROPERTY =
"plugin.contentFilter.rejection.notification.enabled";
/**
* The expected value is a string, containing the desired message for the
* sender notification.
*/
public static final String REJECTION_MSG_PROPERTY = "plugin.contentFilter.rejection.msg";
/**
* The expected value is a boolean, if true the value of #PATTERNS_PROPERTY
* will be used for pattern matching.
*/
public static final String PATTERNS_ENABLED_PROPERTY = "plugin.contentFilter.patterns.enabled";
/**
* The expected value is a comma separated string of regular expressions.
*/
public static final String PATTERNS_PROPERTY = "plugin.contentFilter.patterns";
/**
* The expected value is a boolean, if true the value of #MASK_PROPERTY will
* be used to mask matching content.
*/
public static final String MASK_ENABLED_PROPERTY = "plugin.contentFilter.mask.enabled";
/**
* The expected value is a string. If this property is set any
* matching content will not be rejected but masked with the given value.
* Setting a content mask means that property #SENDER_NOTIFICATION_ENABLED_PROPERTY
* is ignored. The default value is "**".
*/
public static final String MASK_PROPERTY = "plugin.contentFilter.mask";
/**
* the hook into the inteceptor chain
*/
private InterceptorManager interceptorManager;
/**
* used to send violation notifications
*/
private MessageRouter messageRouter;
/**
* delegate that does the real work of this plugin
*/
private ContentFilter contentFilter;
/**
* flags if sender should be notified of rejections
*/
private boolean rejectionNotificationEnabled;
/**
* the rejection msg to send
*/
private String rejectionMessage;
/**
* flags if content matches should result in admin notification
*/
private boolean violationNotificationEnabled;
/**
* the admin user to send violation notifications to
*/
private String violationContact;
/**
* flag if patterns should be used
*/
private boolean patternsEnabled;
/**
* the patterns to use
*/
private String patterns;
/**
* flag if mask should be used
*/
private boolean maskEnabled;
/**
* the mask to use
*/
private String mask;
/**
* violation notification messages will be from this JID
*/
private JID violationNotificationFrom;
public ContentFilterPlugin() {
contentFilter = new ContentFilter();
interceptorManager = InterceptorManager.getInstance();
violationNotificationFrom = new JID(XMPPServer.getInstance()
.getServerInfo().getName());
messageRouter = XMPPServer.getInstance().getMessageRouter();
}
public boolean isMaskEnabled() {
return maskEnabled;
}
public void setMaskEnabled(boolean enabled) {
maskEnabled = enabled;
JiveGlobals.setProperty(MASK_ENABLED_PROPERTY, enabled ? "true" : "false");
changeContentFilterMask();
}
public void setMask(String mas) {
mask = mas;
JiveGlobals.setProperty(MASK_PROPERTY, mas);
changeContentFilterMask();
}
private void changeContentFilterMask() {
if (maskEnabled) {
contentFilter.setMask(mask);
}
else {
contentFilter.clearMask();
}
}
public String getMask() {
return mask;
}
public boolean isPatternsEnabled() {
return patternsEnabled;
}
public void setPatternsEnabled(boolean enabled) {
patternsEnabled = enabled;
JiveGlobals.setProperty(PATTERNS_ENABLED_PROPERTY, enabled ? "true"
: "false");
changeContentFilterPatterns();
}
public void setPatterns(String patt) {
patterns = patt;
JiveGlobals.setProperty(PATTERNS_PROPERTY, patt);
changeContentFilterPatterns();
}
private void changeContentFilterPatterns() {
if (patternsEnabled) {
contentFilter.setPatterns(patterns);
}
else {
contentFilter.clearPatterns();
}
}
public String getPatterns() {
return patterns;
}
public boolean isRejectionNotificationEnabled() {
return rejectionNotificationEnabled;
}
public void setRejectionNotificationEnabled(boolean enabled) {
rejectionNotificationEnabled = enabled;
JiveGlobals.setProperty(REJECTION_NOTIFICATION_ENABLED_PROPERTY,
enabled ? "true" : "false");
}
public String getRejectionMessage() {
return rejectionMessage;
}
public void setRejectionMessage(String message) {
this.rejectionMessage = message;
JiveGlobals.setProperty(REJECTION_MSG_PROPERTY, message);
}
public boolean isViolationNotificationEnabled() {
return violationNotificationEnabled;
}
public void setViolationNotificationEnabled(boolean enabled) {
violationNotificationEnabled = enabled;
JiveGlobals.setProperty(VIOLATION_NOTIFICATION_ENABLED_PROPERTY,
enabled ? "true" : "false");
}
public void setViolationContact(String contact) {
violationContact = contact;
JiveGlobals.setProperty(VIOLATION_NOTIFICATION_CONTACT_PROPERTY, contact);
}
public String getViolationContact() {
return violationContact;
}
public void initializePlugin(PluginManager pManager, File pluginDirectory) {
// configure this plugin
initFilter();
// register with interceptor manager
interceptorManager.addInterceptor(this);
}
private void initFilter() {
// default to false
violationNotificationEnabled = JiveGlobals.getBooleanProperty(
VIOLATION_NOTIFICATION_ENABLED_PROPERTY, false);
// default to "admin"
violationContact = JiveGlobals.getProperty(VIOLATION_NOTIFICATION_CONTACT_PROPERTY,
"admin");
// default to false
rejectionNotificationEnabled = JiveGlobals.getBooleanProperty(
REJECTION_NOTIFICATION_ENABLED_PROPERTY, false);
// default to english
rejectionMessage = JiveGlobals.getProperty(REJECTION_MSG_PROPERTY,
"Message rejected. This is an automated server response");
// default to false
patternsEnabled = JiveGlobals.getBooleanProperty(PATTERNS_ENABLED_PROPERTY,
false);
//default to "fox,dog"
patterns = JiveGlobals.getProperty(PATTERNS_PROPERTY, "fox,dog");
changeContentFilterPatterns();
// default to false
maskEnabled = JiveGlobals.getBooleanProperty(MASK_ENABLED_PROPERTY, false);
//default to "***"
mask = JiveGlobals.getProperty(MASK_PROPERTY, "***");
changeContentFilterMask();
}
/**
* @see org.jivesoftware.wildfire.container.Plugin#destroyPlugin()
*/
public void destroyPlugin() {
// unregister with interceptor manager
interceptorManager.removeInterceptor(this);
}
public void interceptPacket(Packet packet, Session session, boolean read,
boolean processed) throws PacketRejectedException {
if (patternsEnabled && !processed && (packet instanceof Message)) {
Message msg = (Message) packet;
// filter the message
boolean contentMatched = contentFilter.filter(msg);
// notify contact of violations
if (contentMatched && violationNotificationEnabled) {
sendViolationNotification(msg);
}
// reject the message if not masking content
if (contentMatched && !maskEnabled) {
PacketRejectedException rejected = new PacketRejectedException(
"Message rejected with disallowed content!");
if (rejectionNotificationEnabled) {
// let the sender know about the rejection, this is
// only possible/useful if the content is not masked
rejected.setRejectionMessage(rejectionMessage);
}
throw rejected;
}
}
}
private void sendViolationNotification(Message offendingMsg) {
String subject = "Content filter notification!";
String msg = "Disallowed content detected in message from:"
+ offendingMsg.getFrom() + " to:" + offendingMsg.getTo()
+ ", message was "
+ (contentFilter.isMaskingContent() ? "altered" : "rejected");
messageRouter.route(createServerMessage(subject, msg));
}
private Message createServerMessage(String subject, String body) {
Message message = new Message();
message.setTo(violationContact + "@"
+ violationNotificationFrom.getDomain());
message.setFrom(violationNotificationFrom);
message.setSubject(subject);
message.setBody(body);
return message;
}
/**
* $RCSfile$
* $Revision: 1594 $
* $Date: 2005-07-04 18:08:42 +0100 (Mon, 04 Jul 2005) $
*
* Copyright (C) 2004 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.wildfire.plugin;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import org.jivesoftware.util.EmailService;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.Log;
import org.jivesoftware.wildfire.MessageRouter;
import org.jivesoftware.wildfire.Session;
import org.jivesoftware.wildfire.XMPPServer;
import org.jivesoftware.wildfire.container.Plugin;
import org.jivesoftware.wildfire.container.PluginManager;
import org.jivesoftware.wildfire.interceptor.InterceptorManager;
import org.jivesoftware.wildfire.interceptor.PacketInterceptor;
import org.jivesoftware.wildfire.interceptor.PacketRejectedException;
import org.jivesoftware.wildfire.user.User;
import org.jivesoftware.wildfire.user.UserManager;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence;
/**
* Content filter plugin.
*
* @author Conor Hayes
*/
public class ContentFilterPlugin implements Plugin, PacketInterceptor {
/**
* The expected value is a boolean, if true the user identified by the value
* of the property #VIOLATION_NOTIFICATION_CONTACT_PROPERTY will be notified
* every time there is a content match, otherwise no notification will be
* sent. Then default value is false.
*/
public static final String VIOLATION_NOTIFICATION_ENABLED_PROPERTY =
"plugin.contentFilter.violation.notification.enabled";
/**
* The expected value is a user name. The default value is "admin".
*/
public static final String VIOLATION_NOTIFICATION_CONTACT_PROPERTY =
"plugin.contentFilter.violation.notification.contact";
/**
* The expected value is a boolean, if true the user identified by the value
* of the property #VIOLATION_NOTIFICATION_CONTACT_PROPERTY, will also receive
* a copy of the offending packet. The default value is false.
*/
public static final String VIOLATION_INCLUDE_ORIGNAL_PACKET_ENABLED_PROPERTY =
"plugin.contentFilter.violation.notification.include.original.enabled";
/**
* The expected value is a boolean, if true the user identified by the value
* of the property #VIOLATION_NOTIFICATION_CONTACT_PROPERTY, will receive
* notification by IM. The default value is true.
*/
public static final String VIOLATION_NOTIFICATION_BY_IM_ENABLED_PROPERTY =
"plugin.contentFilter.violation.notification.by.im.enabled";
/**
* The expected value is a boolean, if true the user identified by the value
* of the property #VIOLATION_NOTIFICATION_CONTACT_PROPERTY, will receive
* notification by email. The default value is false.
*/
public static final String VIOLATION_NOTIFICATION_BY_EMAIL_ENABLED_PROPERTY =
"plugin.contentFilter.violation.notification.by.email.enabled";
/**
* The expected value is a boolean, if true the sender will be notified when a
* message is rejected, otherwise the message will be silently rejected,i.e. the
* sender will not know that the message was rejected and the receiver will
* not get the message. The default value is false.
*/
public static final String REJECTION_NOTIFICATION_ENABLED_PROPERTY =
"plugin.contentFilter.rejection.notification.enabled";
/**
* The expected value is a string, containing the desired message for the
* sender notification.
*/
public static final String REJECTION_MSG_PROPERTY = "plugin.contentFilter.rejection.msg";
/**
* The expected value is a boolean, if true the value of #PATTERNS_PROPERTY
* will be used for pattern matching.
*/
public static final String PATTERNS_ENABLED_PROPERTY = "plugin.contentFilter.patterns.enabled";
/**
* The expected value is a comma separated string of regular expressions.
*/
public static final String PATTERNS_PROPERTY = "plugin.contentFilter.patterns";
/**
* The expected value is a boolean, if true Presence packets will
* be filtered
*/
public static final String FILTER_STATUS_ENABLED_PROPERTY = "plugin.contentFilter.filter.status.enabled";
/**
* The expected value is a boolean, if true the value of #MASK_PROPERTY will
* be used to mask matching content.
*/
public static final String MASK_ENABLED_PROPERTY = "plugin.contentFilter.mask.enabled";
/**
* The expected value is a string. If this property is set any
* matching content will not be rejected but masked with the given value.
* Setting a content mask means that property #SENDER_NOTIFICATION_ENABLED_PROPERTY
* is ignored. The default value is "**".
*/
public static final String MASK_PROPERTY = "plugin.contentFilter.mask";
/**
* the hook into the inteceptor chain
*/
private InterceptorManager interceptorManager;
/**
* used to send violation notifications
*/
private MessageRouter messageRouter;
/**
* delegate that does the real work of this plugin
*/
private ContentFilter contentFilter;
/**
* flags if sender should be notified of rejections
*/
private boolean rejectionNotificationEnabled;
/**
* the rejection msg to send
*/
private String rejectionMessage;
/**
* flags if content matches should result in admin notification
*/
private boolean violationNotificationEnabled;
/**
* the admin user to send violation notifications to
*/
private String violationContact;
/**
* flags if original packet should be included in the message
* to the violation contact.
*/
private boolean violationIncludeOriginalPacketEnabled;
/**
* flags if violation contact should be notified by IM.
*/
private boolean violationNotificationByIMEnabled;
/**
* flags if violation contact should be notified by email.
*/
private boolean violationNotificationByEmailEnabled;
/**
* flag if patterns should be used
*/
private boolean patternsEnabled;
/**
* the patterns to use
*/
private String patterns;
/**
* flag if Presence packets should be filtered.
*/
private boolean filterStatusEnabled;
/**
* flag if mask should be used
*/
private boolean maskEnabled;
/**
* the mask to use
*/
private String mask;
/**
* violation notification messages will be from this JID
*/
private JID violationNotificationFrom;
public ContentFilterPlugin() {
contentFilter = new ContentFilter();
interceptorManager = InterceptorManager.getInstance();
violationNotificationFrom = new JID(XMPPServer.getInstance()
.getServerInfo().getName());
messageRouter = XMPPServer.getInstance().getMessageRouter();
}
public boolean isMaskEnabled() {
return maskEnabled;
}
public void setMaskEnabled(boolean enabled) {
maskEnabled = enabled;
JiveGlobals.setProperty(MASK_ENABLED_PROPERTY, enabled ? "true" : "false");
changeContentFilterMask();
}
public void setMask(String mas) {
mask = mas;
JiveGlobals.setProperty(MASK_PROPERTY, mas);
changeContentFilterMask();
}
private void changeContentFilterMask() {
if (maskEnabled) {
contentFilter.setMask(mask);
}
else {
contentFilter.clearMask();
}
}
public String getMask() {
return mask;
}
public boolean isPatternsEnabled() {
return patternsEnabled;
}
public void setPatternsEnabled(boolean enabled) {
patternsEnabled = enabled;
JiveGlobals.setProperty(PATTERNS_ENABLED_PROPERTY, enabled ? "true"
: "false");
changeContentFilterPatterns();
}
public void setPatterns(String patt) {
patterns = patt;
JiveGlobals.setProperty(PATTERNS_PROPERTY, patt);
changeContentFilterPatterns();
}
public boolean isFilterStatusEnabled() {
return filterStatusEnabled;
}
public void setFilterStatusEnabled(boolean enabled) {
filterStatusEnabled = enabled;
JiveGlobals.setProperty(FILTER_STATUS_ENABLED_PROPERTY, enabled ? "true"
: "false");
}
private void changeContentFilterPatterns() {
if (patternsEnabled) {
contentFilter.setPatterns(patterns);
}
else {
contentFilter.clearPatterns();
}
}
public String getPatterns() {
return patterns;
}
public boolean isRejectionNotificationEnabled() {
return rejectionNotificationEnabled;
}
public void setRejectionNotificationEnabled(boolean enabled) {
rejectionNotificationEnabled = enabled;
JiveGlobals.setProperty(REJECTION_NOTIFICATION_ENABLED_PROPERTY,
enabled ? "true" : "false");
}
public String getRejectionMessage() {
return rejectionMessage;
}
public void setRejectionMessage(String message) {
this.rejectionMessage = message;
JiveGlobals.setProperty(REJECTION_MSG_PROPERTY, message);
}
public boolean isViolationNotificationEnabled() {
return violationNotificationEnabled;
}
public void setViolationNotificationEnabled(boolean enabled) {
violationNotificationEnabled = enabled;
JiveGlobals.setProperty(VIOLATION_NOTIFICATION_ENABLED_PROPERTY,
enabled ? "true" : "false");
}
public void setViolationContact(String contact) {
violationContact = contact;
JiveGlobals.setProperty(VIOLATION_NOTIFICATION_CONTACT_PROPERTY, contact);
}
public String getViolationContact() {
return violationContact;
}
public boolean isViolationIncludeOriginalPacketEnabled() {
return violationIncludeOriginalPacketEnabled;
}
public void setViolationIncludeOriginalPacketEnabled(boolean enabled) {
violationIncludeOriginalPacketEnabled = enabled;
JiveGlobals.setProperty(VIOLATION_INCLUDE_ORIGNAL_PACKET_ENABLED_PROPERTY,
enabled ? "true" : "false");
}
public boolean isViolationNotificationByIMEnabled() {
return violationNotificationByIMEnabled;
}
public void setViolationNotificationByIMEnabled(boolean enabled) {
violationNotificationByIMEnabled = enabled;
JiveGlobals.setProperty(VIOLATION_NOTIFICATION_BY_IM_ENABLED_PROPERTY,
enabled ? "true" : "false");
}
public boolean isViolationNotificationByEmailEnabled() {
return violationNotificationByEmailEnabled;
}
public void setViolationNotificationByEmailEnabled(boolean enabled) {
violationNotificationByEmailEnabled = enabled;
JiveGlobals.setProperty(VIOLATION_NOTIFICATION_BY_EMAIL_ENABLED_PROPERTY,
enabled ? "true" : "false");
}
public void initializePlugin(PluginManager pManager, File pluginDirectory) {
// configure this plugin
initFilter();
// register with interceptor manager
interceptorManager.addInterceptor(this);
}
private void initFilter() {
// default to false
violationNotificationEnabled = JiveGlobals.getBooleanProperty(
VIOLATION_NOTIFICATION_ENABLED_PROPERTY, false);
// default to "admin"
violationContact = JiveGlobals.getProperty(VIOLATION_NOTIFICATION_CONTACT_PROPERTY,
"admin");
// default to true
violationNotificationByIMEnabled = JiveGlobals.getBooleanProperty(
VIOLATION_NOTIFICATION_BY_IM_ENABLED_PROPERTY, true);
// default to false
violationNotificationByEmailEnabled = JiveGlobals.getBooleanProperty(
VIOLATION_NOTIFICATION_BY_EMAIL_ENABLED_PROPERTY, false);
// default to false
violationIncludeOriginalPacketEnabled = JiveGlobals.getBooleanProperty(
VIOLATION_INCLUDE_ORIGNAL_PACKET_ENABLED_PROPERTY, false);
// default to false
rejectionNotificationEnabled = JiveGlobals.getBooleanProperty(
REJECTION_NOTIFICATION_ENABLED_PROPERTY, false);
// default to english
rejectionMessage = JiveGlobals.getProperty(REJECTION_MSG_PROPERTY,
"Message rejected. This is an automated server response");
// default to false
patternsEnabled = JiveGlobals.getBooleanProperty(PATTERNS_ENABLED_PROPERTY,
false);
//default to "fox,dog"
patterns = JiveGlobals.getProperty(PATTERNS_PROPERTY, "fox,dog");
changeContentFilterPatterns();
// default to false
filterStatusEnabled = JiveGlobals.getBooleanProperty(FILTER_STATUS_ENABLED_PROPERTY,
false);
// default to false
maskEnabled = JiveGlobals.getBooleanProperty(MASK_ENABLED_PROPERTY, false);
//default to "***"
mask = JiveGlobals.getProperty(MASK_PROPERTY, "***");
changeContentFilterMask();
}
/**
* @see org.jivesoftware.wildfire.container.Plugin#destroyPlugin()
*/
public void destroyPlugin() {
// unregister with interceptor manager
interceptorManager.removeInterceptor(this);
}
public void interceptPacket(Packet packet, Session session, boolean read,
boolean processed) throws PacketRejectedException {
if (isValidTargetPacket(packet, read, processed)) {
Packet original = packet;
if (Log.isDebugEnabled()) {
Log.debug("Content filter: intercepted packet:" + original.toString());
}
// make a copy of the original packet only if required,
// as it's an expensive operation
if (violationNotificationEnabled && violationIncludeOriginalPacketEnabled && maskEnabled) {
original = packet.createCopy();
}
// filter the packet
boolean contentMatched = contentFilter.filter(packet);
if (Log.isDebugEnabled()) {
Log.debug("Content filter: content matched? " + contentMatched);
}
// notify admin of violations
if (contentMatched && violationNotificationEnabled) {
if (Log.isDebugEnabled()) {
Log.debug("Content filter: sending violation notification.");
Log.debug("Content filter: include original msg?" +
this.violationIncludeOriginalPacketEnabled);
}
sendViolationNotification(original);
}
// msg will either be rejected silently, rejected with
// some notification to sender, or masked.
if (contentMatched) {
if (maskEnabled) {
//masking enabled, no further action required
if (Log.isDebugEnabled()) {
Log.debug("Content filter: masked content:" + packet.toString());
}
} else {
//no masking, msg must be rejected
if (Log.isDebugEnabled()) {
Log.debug("Content filter: rejecting packet.");
}
PacketRejectedException rejected = new PacketRejectedException(
"Packet rejected with disallowed content!");
if (rejectionNotificationEnabled) {
// let the sender know about the rejection, this is
// only possible/useful if the content is not masked
rejected.setRejectionMessage(rejectionMessage);
}
throw rejected;
}
}
}
}
private boolean isValidTargetPacket(Packet packet, boolean read, boolean processed) {
return patternsEnabled && !processed && read &&
(packet instanceof Message ||
(filterStatusEnabled && packet instanceof Presence));
}
private void sendViolationNotification(Packet originalPacket) {
String subject = "Content filter notification!";
String body = null;
if (originalPacket instanceof Message) {
Message originalMsg = (Message) originalPacket;
body = "Disallowed content detected in message from:"
+ originalMsg.getFrom() + " to:" + originalMsg.getTo()
+ ", message was "
+ (contentFilter.isMaskingContent() ? "altered." : "rejected.")
+ (violationIncludeOriginalPacketEnabled ?
"\nOriginal subject:" + (originalMsg.getSubject() != null ? originalMsg.getSubject() : "")
+ "\nOriginal content:" + (originalMsg.getBody() != null ? originalMsg.getBody() : "") : "");
} else {
//presence
Presence originalPresence = (Presence) originalPacket;
body = "Disallowed status detected in presence from:"
+ originalPresence.getFrom()
+ ", status was "
+ (contentFilter.isMaskingContent() ? "altered." : "rejected.")
+ (violationIncludeOriginalPacketEnabled ?
"\nOriginal status:" + originalPresence.getStatus() : "");
}
if (violationNotificationByIMEnabled) {
if (Log.isDebugEnabled()) {
Log.debug("Sending IM notification");
}
messageRouter.route(createServerMessage(subject, body));
}
if (violationNotificationByEmailEnabled) {
if (Log.isDebugEnabled()) {
Log.debug("Sending email notification");
}
sendViolationNotificationEmail(subject, body);
}
}
private Message createServerMessage(String subject, String body) {
Message message = new Message();
message.setTo(violationContact + "@"
+ violationNotificationFrom.getDomain());
message.setFrom(violationNotificationFrom);
message.setSubject(subject);
message.setBody(body);
return message;
}
private void sendViolationNotificationEmail(String subject, String body) {
List<MimeMessage> messages = new ArrayList<MimeMessage>();
EmailService emailService = EmailService.getInstance();
MimeMessage message = emailService.createMimeMessage();
String encoding = MimeUtility.mimeCharset("iso-8859-1");
try {
User user = UserManager.getInstance().getUser(violationContact);
message.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(user.getEmail()));
message.setFrom(new InternetAddress("no_reply@" + violationNotificationFrom, "Wildfire", encoding));
message.setText(body);
message.setSubject(subject);
messages.add(message);
} catch (Exception e) {
Log.error(e);
}
emailService.sendMessages(messages);
}
}
\ No newline at end of file
<%@ page import="java.util.*,
org.jivesoftware.wildfire.XMPPServer,
org.jivesoftware.wildfire.user.*,
org.jivesoftware.wildfire.plugin.ContentFilterPlugin,
org.jivesoftware.wildfire.plugin.ContentFilterPlugin,
org.jivesoftware.util.*"
%>
<%@ page import="java.util.regex.Pattern"%>
......@@ -13,10 +13,12 @@
boolean save = request.getParameter("save") != null;
boolean success = request.getParameter("success") != null;
//pattern options
//filter options
boolean patternsEnabled = ParamUtils.getBooleanParameter(request, "patternsenabled");
String patterns = ParamUtils.getParameter(request, "patterns");
String [] filterStatusChecked = ParamUtils.getParameters(request, "filterstatus");
boolean filterStatusEnabled = filterStatusChecked.length > 0;
//mask options
boolean maskEnabled = ParamUtils.getBooleanParameter(request, "maskenabled");
String mask = ParamUtils.getParameter(request, "mask");
......@@ -28,6 +30,10 @@
//notification options
boolean notificationEnabled = ParamUtils.getBooleanParameter(request, "notificationenabled");
String contactName = ParamUtils.getParameter(request, "contactname");
List<String> notificationOptions = Arrays.asList(ParamUtils.getParameters(request, "notificationcb"));
boolean notificationByIMEnabled = notificationOptions.contains("notificationbyim");
boolean notificationByEmailEnabled = notificationOptions.contains("notificationbyemail");
boolean includeOriginalEnabled = notificationOptions.contains("notificationincludeoriginal");
//get handle to plugin
ContentFilterPlugin plugin = (ContentFilterPlugin) XMPPServer.getInstance().getPluginManager().getPlugin("contentfilter");
......@@ -35,57 +41,70 @@
//input validation
Map<String, String> errors = new HashMap<String, String>();
if (save) {
if (patterns == null) {
errors.put("missingPatterns", "missingPatterns");
}
else {
String[] data = patterns.split(",");
try {
errors.put("missingPatterns", "missingPatterns");
} else {
String[] data = patterns.split(",");
try {
for (String aData : data) {
Pattern.compile(aData);
}
} catch (java.util.regex.PatternSyntaxException e) {
errors.put("patternSyntaxException", e.getMessage());
} catch (java.util.regex.PatternSyntaxException e) {
errors.put("patternSyntaxException", e.getMessage());
}
}
}
if (mask == null) {
errors.put("missingMask", "missingMask");
}
if (mask == null) {
errors.put("missingMask", "missingMask");
}
if (rejectionMsg == null) {
errors.put("missingRejectionMsg", "missingRejectionMsg");
}
if (rejectionMsg == null) {
errors.put("missingRejectionMsg", "missingRejectionMsg");
}
if (contactName == null) {
errors.put("missingContactName", "missingContactName");
} else {
contactName = contactName.trim().toLowerCase();
try {
UserManager.getInstance().getUser(contactName);
} catch (UserNotFoundException unfe) {
errors.put("userNotFound", "userNotFound");
}
}
if (contactName == null) {
errors.put("missingContactName", "missingContactName");
} else {
contactName = contactName.trim().toLowerCase();
try {
User user = UserManager.getInstance().getUser(contactName);
if (notificationByEmailEnabled) {
//verify that the user has an email address
if (user.getEmail() == null) {
errors.put("userEmailNotConfigured", "userEmailNotConfigured");
}
//verify that the email server is configured
if (!JiveGlobals.getBooleanProperty("mail.configured", false)) {
errors.put("mailServerNotConfigured", "mailServerNotConfigured");
}
}
} catch (UserNotFoundException unfe) {
errors.put("userNotFound", "userNotFound");
}
}
if (!notificationByIMEnabled && !notificationByEmailEnabled) {
errors.put("notificationFormatNotConfigured", "notificationFormatNotConfigured");
}
if (errors.size() == 0) {
plugin.setPatternsEnabled(patternsEnabled);
plugin.setPatterns(patterns);
plugin.setMaskEnabled(maskEnabled);
plugin.setMask(mask);
plugin.setViolationNotificationEnabled(notificationEnabled);
plugin.setViolationContact(contactName);
plugin.setRejectionNotificationEnabled(rejectionNotificationEnabled);
plugin.setRejectionMessage(rejectionMsg);
response.sendRedirect("contentfilter-props-edit-form.jsp?success=true");
return;
}
}
else {
if (errors.size() == 0) {
plugin.setPatternsEnabled(patternsEnabled);
plugin.setPatterns(patterns);
plugin.setFilterStatusEnabled(filterStatusEnabled);
plugin.setMaskEnabled(maskEnabled);
plugin.setMask(mask);
plugin.setViolationNotificationEnabled(notificationEnabled);
plugin.setViolationContact(contactName);
plugin.setViolationNotificationByIMEnabled(notificationByIMEnabled);
plugin.setViolationNotificationByEmailEnabled(notificationByEmailEnabled);
plugin.setViolationIncludeOriginalPacketEnabled(includeOriginalEnabled);
plugin.setRejectionNotificationEnabled(rejectionNotificationEnabled);
plugin.setRejectionMessage(rejectionMsg);
response.sendRedirect("contentfilter-props-edit-form.jsp?success=true");
return;
}
} else {
patterns = plugin.getPatterns();
mask = plugin.getMask();
contactName = plugin.getViolationContact();
......@@ -97,9 +116,13 @@
mask = plugin.getMask();
contactName = plugin.getViolationContact();
rejectionMsg = plugin.getRejectionMessage();
notificationByIMEnabled = plugin.isViolationNotificationByIMEnabled();
notificationByEmailEnabled = plugin.isViolationNotificationByEmailEnabled();
includeOriginalEnabled = plugin.isViolationIncludeOriginalPacketEnabled();
}
patternsEnabled = plugin.isPatternsEnabled();
filterStatusEnabled = plugin.isFilterStatusEnabled();
maskEnabled = plugin.isMaskEnabled();
notificationEnabled = plugin.isViolationNotificationEnabled();
rejectionNotificationEnabled = plugin.isRejectionNotificationEnabled();
......@@ -158,28 +181,31 @@ Use the form below to edit content filter settings.<br>
<table cellpadding="3" cellspacing="0" border="0" width="100%">
<tbody>
<tr>
<td width="1%">
<td width="1%">
<input type="radio" name="patternsenabled" value="false" id="not01"
<%= ((patternsEnabled) ? "" : "checked") %>>
</td>
<td width="99%">
<label for="not01"><b>Disabled</b></label> - Messages will not be filtered.
</td>
</tr>
<tr>
<td width="1%">
</td>
<td width="99%">
<label for="not01"><b>Disabled</b></label> - Packets will not be filtered.
</td>
</tr>
<tr>
<td width="1%">
<input type="radio" name="patternsenabled" value="true" id="not02"
<%= ((patternsEnabled) ? "checked" : "") %>>
</td>
<td width="99%">
<label for="not02"><b>Enabled</b></label> - Messages will be filtered.
</td>
</tr>
</td>
<td width="99%">
<label for="not02"><b>Enabled</b></label> - Packets will be filtered.
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="left">Patterns:&nbsp;
<input type="text" size="100" maxlength="100" name="patterns"
value="<%= (patterns != null ? patterns : "") %>">
<td align="left">Patterns:&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<textarea rows="10" cols="100" name="patterns"><%= (patterns != null ? patterns : "") %></textarea>
<% if (errors.containsKey("missingPatterns")) { %>
<span class="jive-error-text">
<br>Please enter comma separated, regular expressions.
......@@ -189,8 +215,12 @@ Use the form below to edit content filter settings.<br>
<br>Invalid regular expression: <%= errors.get("patternSyntaxException") %>. Please try again.
</span>
<% } %>
</td>
</tr>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="checkbox" name="filterstatus" value="filterstatus" <%= filterStatusEnabled ? "checked" : "" %>>Filter users presence status.</input></td>
</tr>
</tbody>
</table>
</div>
......@@ -203,7 +233,7 @@ Use the form below to edit content filter settings.<br>
<div>
<p>
Enable this feature to alter message content when there is a pattern match.
Enable this feature to alter packet content when there is a pattern match.
</p>
<table cellpadding="3" cellspacing="0" border="0" width="100%">
......@@ -214,7 +244,7 @@ Use the form below to edit content filter settings.<br>
<%= ((maskEnabled) ? "" : "checked") %>>
</td>
<td width="99%">
<label for="not01"><b>Disabled</b></label> - Messages will be rejected.
<label for="not01"><b>Disabled</b></label> - Packets will be rejected.
</td>
</tr>
<tr>
......@@ -223,7 +253,7 @@ Use the form below to edit content filter settings.<br>
<%= ((maskEnabled) ? "checked" : "") %>>
</td>
<td width="99%">
<label for="not02"><b>Enabled</b></label> - Messages will be masked.
<label for="not02"><b>Enabled</b></label> - Packets will be masked.
</td>
</tr>
<tr>
......@@ -250,7 +280,7 @@ Use the form below to edit content filter settings.<br>
<div>
<p>
Enable this feature to have the message sender notified whenever a message is rejected.
Enable this feature to have the sender notified whenever a packet is rejected.
NB: This feature is only operational if "Content Mask" feature is disabled.
</p>
......@@ -322,19 +352,39 @@ Use the form below to edit content filter settings.<br>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="left">Username:&nbsp;
<input type="text" size="20" maxlength="100" name="contactname"
value="<%= (contactName != null ? contactName : "") %>">@<%= XMPPServer.getInstance().getServerInfo().getName() %>
<% if (errors.containsKey("missingContactName")) { %>
<span class="jive-error-text">
<br>Please enter a username.
</span>
<% } else if (errors.containsKey("userNotFound")) { %>
<span class="jive-error-text">
<br>Could not find user. Please try again.
</span>
<% } %>
<td>&nbsp;</td>
<td align="left">Username:&nbsp
<input type="text" size="20" maxlength="100" name="contactname" value="<%= (contactName != null ? contactName : "") %>">@<%= XMPPServer.getInstance().getServerInfo().getName() %>
<% if (errors.containsKey("missingContactName")) { %>
<span class="jive-error-text">
<br>Please enter a username.
</span>
<% } else if (errors.containsKey("userNotFound")) { %>
<span class="jive-error-text">
<br>Could not find user. Please try again.
</span>
<% } %>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input type="checkbox" name="notificationcb" value="notificationbyim" <%= notificationByIMEnabled ? "checked" : "" %>>Notify by IM.</input>
<input type="checkbox" name="notificationcb" value="notificationbyemail" <%= notificationByEmailEnabled ? "checked" : "" %>>Notify by Email.</input>
<input type="checkbox" name="notificationcb" value="notificationincludeoriginal" <%= includeOriginalEnabled ? "checked" : "" %>>Include original packet.</input>
<% if (errors.containsKey("mailServerNotConfigured")) { %>
<span class="jive-error-text">
<br>Error, sending an email will fail because the mail server is not setup. Please go to the <a href="/system-email.jsp">mail settings page</a> and set the mail host.
</span>
<% } else if (errors.containsKey("userEmailNotConfigured")) { %>
<span class="jive-error-text">
<br>Please configure <a href="/user-properties.jsp?username=<%= contactName %>"><%= contactName %>'s</a> email address.
</span>
<% } else if (errors.containsKey("notificationFormatNotConfigured")) { %>
<span class="jive-error-text">
<br>Users must be notified by IM and/or Email.
</span>
<% } %>
</td>
</tr>
</tbody>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment