📄 whitelistmanager.java
字号:
} else if (removeFlag != null && removeFlag.equals(subject)) { manageRemoveRequest(mail); } else { StringWriter sout = new StringWriter(); PrintWriter out = new PrintWriter(sout, true); out.println("Answering on behalf of: " + whitelistManagerAddress); out.println("ERROR: Unknown command in the subject line: " + subject); sendReplyFromPostmaster(mail, sout.toString()); } return; } if (automaticInsert) { checkAndInsert(senderMailAddress, recipients); } } /** Returns a string describing this mailet. * * @return a string describing this mailet */ public String getMailetInfo() { return "White List Manager mailet"; } /** Loops through each address in the recipient list, checks if in the senders * list and inserts in it otherwise. */ private void checkAndInsert(MailAddress senderMailAddress, Collection recipients) throws MessagingException { String senderUser = senderMailAddress.getUser().toLowerCase(Locale.US); String senderHost = senderMailAddress.getHost().toLowerCase(Locale.US); senderUser = getPrimaryName(senderUser); Connection conn = null; PreparedStatement selectStmt = null; PreparedStatement insertStmt = null; boolean dbUpdated = false; try { for (Iterator i = recipients.iterator(); i.hasNext(); ) { ResultSet selectRS = null; try { MailAddress recipientMailAddress = (MailAddress)i.next(); String recipientUser = recipientMailAddress.getUser().toLowerCase(Locale.US); String recipientHost = recipientMailAddress.getHost().toLowerCase(Locale.US); if (getMailetContext().isLocalServer(recipientHost)) { // not a remote recipient, so skip continue; } if (conn == null) { conn = datasource.getConnection(); } if (selectStmt == null) { selectStmt = conn.prepareStatement(selectByPK); } selectStmt.setString(1, senderUser); selectStmt.setString(2, senderHost); selectStmt.setString(3, recipientUser); selectStmt.setString(4, recipientHost); selectRS = selectStmt.executeQuery(); if (selectRS.next()) { //This address was already in the list continue; } if (insertStmt == null) { insertStmt = conn.prepareStatement(insert); } insertStmt.setString(1, senderUser); insertStmt.setString(2, senderHost); insertStmt.setString(3, recipientUser); insertStmt.setString(4, recipientHost); insertStmt.executeUpdate(); dbUpdated = true; } finally { theJDBCUtil.closeJDBCResultSet(selectRS); } //Commit our changes if necessary. if (conn != null && dbUpdated && !conn.getAutoCommit()) { conn.commit(); dbUpdated = false; } } } catch (SQLException sqle) { log("Error accessing database", sqle); throw new MessagingException("Exception thrown", sqle); } finally { theJDBCUtil.closeJDBCStatement(selectStmt); theJDBCUtil.closeJDBCStatement(insertStmt); //Rollback our changes if necessary. try { if (conn != null && dbUpdated && !conn.getAutoCommit()) { conn.rollback(); dbUpdated = false; } } catch (Exception e) {} theJDBCUtil.closeJDBCConnection(conn); } } /** Manages a display request. */ private void manageDisplayRequest(Mail mail) throws MessagingException { MailAddress senderMailAddress = mail.getSender(); String senderUser = senderMailAddress.getUser().toLowerCase(Locale.US); String senderHost = senderMailAddress.getHost().toLowerCase(Locale.US); senderUser = getPrimaryName(senderUser); Connection conn = null; PreparedStatement selectStmt = null; ResultSet selectRS = null; StringWriter sout = new StringWriter(); PrintWriter out = new PrintWriter(sout, true); try { out.println("Answering on behalf of: " + whitelistManagerAddress); out.println("Displaying white list of " + (new MailAddress(senderUser, senderHost)) + ":"); out.println(); conn = datasource.getConnection(); selectStmt = conn.prepareStatement(selectBySender); selectStmt.setString(1, senderUser); selectStmt.setString(2, senderHost); selectRS = selectStmt.executeQuery(); while (selectRS.next()) { MailAddress mailAddress = new MailAddress(selectRS.getString(1), selectRS.getString(2)); out.println(mailAddress.toInternetAddress().toString()); } out.println(); out.println("Finished"); sendReplyFromPostmaster(mail, sout.toString()); } catch (SQLException sqle) { out.println("Error accessing the database"); sendReplyFromPostmaster(mail, sout.toString()); throw new MessagingException("Error accessing database", sqle); } finally { theJDBCUtil.closeJDBCResultSet(selectRS); theJDBCUtil.closeJDBCStatement(selectStmt); theJDBCUtil.closeJDBCConnection(conn); } } /** Manages an insert request. */ private void manageInsertRequest(Mail mail) throws MessagingException { MailAddress senderMailAddress = mail.getSender(); String senderUser = senderMailAddress.getUser().toLowerCase(Locale.US); String senderHost = senderMailAddress.getHost().toLowerCase(Locale.US); senderUser = getPrimaryName(senderUser); Connection conn = null; PreparedStatement selectStmt = null; PreparedStatement insertStmt = null; boolean dbUpdated = false; StringWriter sout = new StringWriter(); PrintWriter out = new PrintWriter(sout, true); try { out.println("Answering on behalf of: " + whitelistManagerAddress); out.println("Inserting in the white list of " + (new MailAddress(senderUser, senderHost)) + " ..."); out.println(); MimeMessage message = mail.getMessage() ; Object content= message.getContent(); if (message.getContentType().startsWith("text/plain") && content instanceof String) { StringTokenizer st = new StringTokenizer((String) content, " \t\n\r\f,;:<>"); while (st.hasMoreTokens()) { ResultSet selectRS = null; try { MailAddress recipientMailAddress; try { recipientMailAddress = new MailAddress(st.nextToken()); } catch (javax.mail.internet.ParseException pe) { continue; } String recipientUser = recipientMailAddress.getUser().toLowerCase(Locale.US); String recipientHost = recipientMailAddress.getHost().toLowerCase(Locale.US); if (getMailetContext().isLocalServer(recipientHost)) { // not a remote recipient, so skip continue; } if (conn == null) { conn = datasource.getConnection(); } if (selectStmt == null) { selectStmt = conn.prepareStatement(selectByPK); } selectStmt.setString(1, senderUser); selectStmt.setString(2, senderHost); selectStmt.setString(3, recipientUser); selectStmt.setString(4, recipientHost); selectRS = selectStmt.executeQuery(); if (selectRS.next()) { //This address was already in the list out.println("Skipped: " + recipientMailAddress); continue; } if (insertStmt == null) { insertStmt = conn.prepareStatement(insert); } insertStmt.setString(1, senderUser); insertStmt.setString(2, senderHost); insertStmt.setString(3, recipientUser); insertStmt.setString(4, recipientHost); insertStmt.executeUpdate(); dbUpdated = true; out.println("Inserted: " + recipientMailAddress); } finally { theJDBCUtil.closeJDBCResultSet(selectRS); } } if (dbUpdated) { log("Insertion request issued by " + senderMailAddress); } //Commit our changes if necessary. if (conn != null && dbUpdated && !conn.getAutoCommit()) { conn.commit() ; dbUpdated = false; } } else { out.println("The message must be plain - no action"); } out.println(); out.println("Finished"); sendReplyFromPostmaster(mail, sout.toString()); } catch (SQLException sqle) { out.println("Error accessing the database"); sendReplyFromPostmaster(mail, sout.toString()); throw new MessagingException("Error accessing the database", sqle); } catch (IOException ioe) { out.println("Error getting message content"); sendReplyFromPostmaster(mail, sout.toString()); throw new MessagingException("Error getting message content", ioe); } finally { theJDBCUtil.closeJDBCStatement(selectStmt); theJDBCUtil.closeJDBCStatement(insertStmt); //Rollback our changes if necessary. try { if (conn != null && dbUpdated && !conn.getAutoCommit()) { conn.rollback() ; dbUpdated = false; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -