📄 contentfilterplugin.java
字号:
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"); try { changeContentFilterPatterns(); } catch (PatternSyntaxException e) { Log.warn("Resetting to default patterns of ContentFilterPlugin", e); // Existing patterns are invalid so reset to default ones setPatterns("fox,dog"); } // 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, "***"); // default to false allowOnMatch = JiveGlobals.getBooleanProperty( ALLOW_ON_MATCH_PROPERTY, false); //v1.2.2 backwards compatibility if (maskEnabled) { allowOnMatch = true; } 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 allowed and optionally masked. // allowing a message without masking can be useful if the admin // simply wants to get notified of matches without interrupting // the conversation in the (spy mode!) if (contentMatched) { if (allowOnMatch) { if (Log.isDebugEnabled()) { Log.debug("Content filter: allowed content:" + packet.toString()); } // no further action required } else { // 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! (" + originalPacket.getFrom().getNode() + ")"; 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 " + (allowOnMatch ? "allowed" + (contentFilter.isMaskingContent() ? " and masked." : " but not masked.") : "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 " + (allowOnMatch ? "allowed" + (contentFilter.isMaskingContent() ? " and masked." : " but not masked.") : "rejected.") + (violationIncludeOriginalPacketEnabled ? "\nOriginal status:" + originalPresence.getStatus() : ""); } if (violationNotificationByIMEnabled) { if (Log.isDebugEnabled()) { Log.debug("Content filter: sending IM notification"); } sendViolationNotificationIM(subject, body); } if (violationNotificationByEmailEnabled) { if (Log.isDebugEnabled()) { Log.debug("Content filter: sending email notification"); } sendViolationNotificationEmail(subject, body); } } private void sendViolationNotificationIM(String subject, String body) { Message message = createServerMessage(subject, body); // TODO consider spining off a separate thread here, // in high volume situations, it will result in // in faster response and notification is not required // to be real time. messageRouter.route(message); } 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) { try { User user = UserManager.getInstance().getUser(violationContact); //this is automatically put on a another thread for execution. EmailService.getInstance().sendMessage(user.getName(), user.getEmail(), "Wildfire", "no_reply@" + violationNotificationFrom.getDomain(), subject, body, null); } catch (Throwable e) { // catch throwable in case email setup is invalid Log.error("Content Filter: Failed to send email, please review Wildfire setup", e); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -