1 11 12 package org.jivesoftware.messenger.plugin; 13 14 import org.jivesoftware.messenger.MessageRouter; 15 import org.jivesoftware.messenger.Session; 16 import org.jivesoftware.messenger.XMPPServer; 17 import org.jivesoftware.messenger.container.Plugin; 18 import org.jivesoftware.messenger.container.PluginManager; 19 import org.jivesoftware.messenger.interceptor.InterceptorManager; 20 import org.jivesoftware.messenger.interceptor.PacketInterceptor; 21 import org.jivesoftware.messenger.interceptor.PacketRejectedException; 22 import org.jivesoftware.util.JiveGlobals; 23 import org.xmpp.packet.JID; 24 import org.xmpp.packet.Message; 25 import org.xmpp.packet.Packet; 26 27 import java.io.File ; 28 29 34 public class ContentFilterPlugin implements Plugin, PacketInterceptor { 35 36 42 public static final String VIOLATION_NOTIFICATION_ENABLED_PROPERTY = 43 "plugin.contentFilter.violation.notification.enabled"; 44 45 48 public static final String VIOLATION_NOTIFICATION_CONTACT_PROPERTY = 49 "plugin.contentFilter.violation.notification.contact"; 50 51 57 public static final String REJECTION_NOTIFICATION_ENABLED_PROPERTY = 58 "plugin.contentFilter.rejection.notification.enabled"; 59 60 64 public static final String REJECTION_MSG_PROPERTY = "plugin.contentFilter.rejection.msg"; 65 66 70 public static final String PATTERNS_ENABLED_PROPERTY = "plugin.contentFilter.patterns.enabled"; 71 72 75 public static final String PATTERNS_PROPERTY = "plugin.contentFilter.patterns"; 76 77 81 public static final String MASK_ENABLED_PROPERTY = "plugin.contentFilter.mask.enabled"; 82 83 89 public static final String MASK_PROPERTY = "plugin.contentFilter.mask"; 90 91 94 private InterceptorManager interceptorManager; 95 96 99 private MessageRouter messageRouter; 100 101 104 private ContentFilter contentFilter; 105 106 109 private boolean rejectionNotificationEnabled; 110 111 114 private String rejectionMessage; 115 116 119 private boolean violationNotificationEnabled; 120 121 124 private String violationContact; 125 126 129 private boolean patternsEnabled; 130 131 134 private String patterns; 135 136 139 private boolean maskEnabled; 140 141 144 private String mask; 145 146 149 private JID violationNotificationFrom; 150 151 public ContentFilterPlugin() { 152 contentFilter = new ContentFilter(); 153 interceptorManager = InterceptorManager.getInstance(); 154 violationNotificationFrom = new JID(XMPPServer.getInstance() 155 .getServerInfo().getName()); 156 messageRouter = XMPPServer.getInstance().getMessageRouter(); 157 } 158 159 public boolean isMaskEnabled() { 160 return maskEnabled; 161 } 162 163 public void setMaskEnabled(boolean enabled) { 164 maskEnabled = enabled; 165 JiveGlobals.setProperty(MASK_ENABLED_PROPERTY, enabled ? "true" : "false"); 166 167 changeContentFilterMask(); 168 } 169 170 public void setMask(String mas) { 171 mask = mas; 172 JiveGlobals.setProperty(MASK_PROPERTY, mas); 173 174 changeContentFilterMask(); 175 } 176 177 private void changeContentFilterMask() { 178 if (maskEnabled) { 179 contentFilter.setMask(mask); 180 } 181 else { 182 contentFilter.clearMask(); 183 } 184 } 185 186 public String getMask() { 187 return mask; 188 } 189 190 public boolean isPatternsEnabled() { 191 return patternsEnabled; 192 } 193 194 public void setPatternsEnabled(boolean enabled) { 195 patternsEnabled = enabled; 196 JiveGlobals.setProperty(PATTERNS_ENABLED_PROPERTY, enabled ? "true" 197 : "false"); 198 199 changeContentFilterPatterns(); 200 } 201 202 public void setPatterns(String patt) { 203 patterns = patt; 204 JiveGlobals.setProperty(PATTERNS_PROPERTY, patt); 205 206 changeContentFilterPatterns(); 207 } 208 209 private void changeContentFilterPatterns() { 210 if (patternsEnabled) { 211 contentFilter.setPatterns(patterns); 212 } 213 else { 214 contentFilter.clearPatterns(); 215 } 216 } 217 218 public String getPatterns() { 219 return patterns; 220 } 221 222 public boolean isRejectionNotificationEnabled() { 223 return rejectionNotificationEnabled; 224 } 225 226 public void setRejectionNotificationEnabled(boolean enabled) { 227 rejectionNotificationEnabled = enabled; 228 JiveGlobals.setProperty(REJECTION_NOTIFICATION_ENABLED_PROPERTY, 229 enabled ? "true" : "false"); 230 } 231 232 public String getRejectionMessage() { 233 return rejectionMessage; 234 } 235 236 public void setRejectionMessage(String message) { 237 this.rejectionMessage = message; 238 JiveGlobals.setProperty(REJECTION_MSG_PROPERTY, message); 239 } 240 241 public boolean isViolationNotificationEnabled() { 242 return violationNotificationEnabled; 243 } 244 245 public void setViolationNotificationEnabled(boolean enabled) { 246 violationNotificationEnabled = enabled; 247 JiveGlobals.setProperty(VIOLATION_NOTIFICATION_ENABLED_PROPERTY, 248 enabled ? "true" : "false"); 249 } 250 251 public void setViolationContact(String contact) { 252 violationContact = contact; 253 JiveGlobals.setProperty(VIOLATION_NOTIFICATION_CONTACT_PROPERTY, contact); 254 } 255 256 public String getViolationContact() { 257 return violationContact; 258 } 259 260 public void initializePlugin(PluginManager pManager, File pluginDirectory) { 261 initFilter(); 263 264 interceptorManager.addInterceptor(this); 266 } 267 268 private void initFilter() { 269 violationNotificationEnabled = JiveGlobals.getBooleanProperty( 271 VIOLATION_NOTIFICATION_ENABLED_PROPERTY, false); 272 273 violationContact = JiveGlobals.getProperty(VIOLATION_NOTIFICATION_CONTACT_PROPERTY, 275 "admin"); 276 277 rejectionNotificationEnabled = JiveGlobals.getBooleanProperty( 279 REJECTION_NOTIFICATION_ENABLED_PROPERTY, false); 280 281 rejectionMessage = JiveGlobals.getProperty(REJECTION_MSG_PROPERTY, 283 "Message rejected. This is an automated server response"); 284 285 patternsEnabled = JiveGlobals.getBooleanProperty(PATTERNS_ENABLED_PROPERTY, 287 false); 288 289 patterns = JiveGlobals.getProperty(PATTERNS_PROPERTY, "fox,dog"); 291 292 changeContentFilterPatterns(); 293 294 maskEnabled = JiveGlobals.getBooleanProperty(MASK_ENABLED_PROPERTY, false); 296 297 mask = JiveGlobals.getProperty(MASK_PROPERTY, "***"); 299 300 changeContentFilterMask(); 301 } 302 303 306 public void destroyPlugin() { 307 interceptorManager.removeInterceptor(this); 309 } 310 311 312 public void interceptPacket(Packet packet, Session session, boolean read, 313 boolean processed) throws PacketRejectedException { 314 if (patternsEnabled && !processed && (packet instanceof Message)) { 315 Message msg = (Message) packet; 316 317 boolean contentMatched = contentFilter.filter(msg); 319 320 if (contentMatched && violationNotificationEnabled) { 322 sendViolationNotification(msg); 323 } 324 325 if (contentMatched && !maskEnabled) { 327 PacketRejectedException rejected = new PacketRejectedException( 328 "Message rejected with disallowed content!"); 329 330 if (rejectionNotificationEnabled) { 331 rejected.setRejectionMessage(rejectionMessage); 334 } 335 336 throw rejected; 337 } 338 } 339 } 340 341 private void sendViolationNotification(Message offendingMsg) { 342 String subject = "Content filter notification!"; 343 344 String msg = "Disallowed content detected in message from:" 345 + offendingMsg.getFrom() + " to:" + offendingMsg.getTo() 346 + ", message was " 347 + (contentFilter.isMaskingContent() ? "altered" : "rejected"); 348 349 messageRouter.route(createServerMessage(subject, msg)); 350 } 351 352 private Message createServerMessage(String subject, String body) { 353 Message message = new Message(); 354 message.setTo(violationContact + "@" 355 + violationNotificationFrom.getDomain()); 356 message.setFrom(violationNotificationFrom); 357 message.setSubject(subject); 358 message.setBody(body); 359 return message; 360 } 361 } | Popular Tags |