Commit 1c384447 authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

Initial version.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@1594 b35dd754-fafc-0310-a699-88a17e54d16e
parent f29ffc9f
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Search Plugin Changelog</title>
<style type="text/css">
BODY {
font-size : 100%;
}
BODY, TD, TH {
font-family : tahoma, verdana, arial, helvetica, sans-serif;
font-size : 0.8em;
}
H2 {
font-size : 10pt;
font-weight : bold;
padding-left : 1em;
}
A:hover {
text-decoration : none;
}
H1 {
font-family : tahoma, arial, helvetica, sans-serif;
font-size : 1.4em;
font-weight: bold;
border-bottom : 1px #ccc solid;
padding-bottom : 2px;
}
TT {
font-family : courier new;
font-weight : bold;
color : #060;
}
PRE {
font-family : courier new;
font-size : 100%;
}
</style>
</head>
<body>
<h1>
ContentFilter Plugin Changelog
</h1>
<p><b>1.0</b> -- July, 2005</p>
<ul>
<li>Initial release.</li>
</ul>
</body>
</html>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<!-- Main plugin class -->
<class>org.jivesoftware.messenger.plugin.ContentFilterPlugin</class>
<!-- Plugin meta-data -->
<name>ContentFilter</name>
<description>Scans message packets for defined patterns</description>
<author>Conor Hayes</author>
<version>1.0</version>
<minServerVersion>2.2.0</minServerVersion>
<!-- UI extension -->
<adminconsole>
<tab id="tab-server">
<sidebar id="sidebar-server-settings">
<item id="contentfilter-props-edit-form" name="Content Filter"
url="contentfilter-props-edit-form.jsp"
description="Click to configure content filter" />
</sidebar>
</tab>
</adminconsole>
</plugin>
\ No newline at end of file
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>ContentFilter Plugin Readme</title>
<style type="text/css">
BODY {
font-size : 100%;
}
BODY, TD, TH {
font-family : tahoma, verdana, arial, helvetica, sans-serif;
font-size : 0.8em;
}
H2 {
font-size : 10pt;
font-weight : bold;
}
A:hover {
text-decoration : none;
}
H1 {
font-family : tahoma, arial, helvetica, sans-serif;
font-size : 1.4em;
font-weight: bold;
border-bottom : 1px #ccc solid;
padding-bottom : 2px;
}
TT {
font-family : courier new;
font-weight : bold;
color : #060;
}
PRE {
font-family : courier new;
font-size : 100%;
}
</style>
</head>
<body>
<h1>
ContentFilter Plugin Readme
</h1>
<h2>Overview</h2>
<p>
The content filter plugin allows admins to configure various actions based on
message content. These actions include notifying the admin of content matches,
notifying the sender that a message was rejected or masking the content with
alternative content.
</p>
<h2>Installation</h2>
<p>
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>
<h2>Configuration</h2>
<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 Jive Messenger Admin Console.
</p>
<h2>Using the Plugin</h2>
<p>
After the plugin has been configured, nothing else needs to be done to use it.
</p>
</body>
</html>
/**
* $RCSfile$
* $Revision$
* $Date$
*
* 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.messenger.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;
}
}
\ No newline at end of file
package org.jivesoftware.messenger.plugin;
import java.util.regex.PatternSyntaxException;
import junit.framework.TestCase;
import org.xmpp.packet.Message;
/**
* Basic unit tests for ContentFilter.
*
* @author chayes
*/
public class ContentFilterTest extends TestCase
{
private ContentFilter filter;
public static void main(String[] args)
{
junit.textui.TestRunner.run(ContentFilterTest.class);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception
{
filter = new ContentFilter();
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception
{
super.tearDown();
}
public void testSetInvalidPatterns()
{
try
{
filter.setPatterns("$*[");
}
catch (PatternSyntaxException e)
{
System.out.println(e.getMessage());
System.out.println(e.getPattern());
}
}
public void testSetContentMask()
{
assertFalse(filter.isMaskingContent());
filter.setMask("dummy");
assertTrue(filter.isMaskingContent());
filter.clearMask();
assertFalse(filter.isMaskingContent());
}
public void testFilterWithEmptyMessage()
{
Message message = new Message();
boolean matched = filter.filter(message);
// no matches should be found
assertFalse(matched);
// message should not have changed
assertEquals(new Message().toXML(), message.toXML());
}
public void testFilterMessageSubject()
{
// filter on the word fox
filter.setPatterns("fox");
// test message
Message message = new Message();
message.setSubject("the quick brown fox jumped over the lazy dog");
boolean matched = filter.filter(message);
// matches should be found
assertTrue(matched);
// content has not changed as there is no content mask
assertEquals("the quick brown fox jumped over the lazy dog", message
.getSubject());
assertNull(message.getBody());
}
public void testFilterMessageSubjectWithMask()
{
// filter on the word fox
filter.setPatterns("fox");
//set a content mask
filter.setMask("**");
// test message
Message message = new Message();
message.setSubject("the quick brown fox jumped over the lazy dog");
boolean matched = filter.filter(message);
// matches should be found
assertTrue(matched);
// content has changed
assertEquals("the quick brown ** jumped over the lazy dog", message
.getSubject());
assertNull(message.getBody());
}
public void testFilterMessageBody()
{
// filter on the word fox
filter.setPatterns("fox");
// test message
Message message = new Message();
message.setBody("the quick brown fox jumped over the lazy dog");
boolean matched = filter.filter(message);
// matches should be found
assertTrue(matched);
// content has not changed as there is no content mask
assertEquals("the quick brown fox jumped over the lazy dog", message
.getBody());
assertNull(message.getSubject());
}
public void testFilterMessageBodyWithMask()
{
// filter on the word "fox" and "dog"
filter.setPatterns("fox,dog");
filter.setMask("**");
// test message
Message message = new Message();
message.setBody("the quick brown fox jumped over the lazy dog");
boolean matched = filter.filter(message);
// matches should not be found
assertTrue(matched);
// content has changed
assertEquals("the quick brown ** jumped over the lazy **", message
.getBody());
assertNull(message.getSubject());
}
}
\ No newline at end of file
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