使用SpamAssassin的java代码示例

此代码仅为示例,在代码调试过程中遇到问题,可以参考本博客的另外一篇SpamAssassin注意事项文章,也许您从中可以得到一些建议。

/**
 * Copyright cn.sh.zeli
 */
package cn.sh.zeli.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.util.StringTokenizer;

import javax.mail.internet.MimeMessage;

/**
 * 使用SpamAssassin进行垃圾评分,并取得相应评分值
 * <PRE>
 * 注意:
 * 启动 spamd 守护进程时一定要设置 <font color=red>allowed-ips</font>, 是可以连接到此 spamd 实例的授权主机或网络(使用 IP 地址)的列表。
 * 示例: /usr/bin/spamd --daemonize --listen-ip 0.0.0.0 --allowed-ips 192.168.1.155 --pidfile /var/run/spamd.pid
 * </PRE>
 * 
 * @author lawuu
 */
public class SpamAssassinInvoker {
	
	/**
	 * 默认端口
	 */
	public static final int _DEFAULT_PORT = 783;
	private int port = _DEFAULT_PORT;
	private String host;

	public SpamAssassinInvoker(String host) {
		this.host = host;
	}
	public SpamAssassinInvoker(String host, int port) {
		this.host = host;
		this.port = port;
	}
	

	/**
	 * 扫描邮件,得到分值
	 * @param obj: javax.mail.internet.MimeMessage | String | ..
	 * @return
	 */
	public SpamScore scanMail(Object obj) {
		
		Socket socket = null;
        OutputStream out = null;
        BufferedReader in = null;
        
        if (null == obj || null == host) {
        	throw new RuntimeException("方法输入参数和host信息不能为null");
        }

		SpamScore ss = new SpamScore();
		
        try {
            socket = new Socket(host, port);
            
            out = socket.getOutputStream();

            in = new BufferedReader(new InputStreamReader(socket
                    .getInputStream()));
            out.write("CHECK SPAMC/1.2\r\n\r\n".getBytes());

            // pass the message to spamd
            if (obj instanceof MimeMessage) {
            	((MimeMessage) obj).writeTo(out);
            } else {
                out.write(obj.toString().getBytes());
            }
            
            out.flush();
            socket.shutdownOutput();
            
            String s = null;
            while (null != (s = in.readLine())) {
//	            System.out.println(s);
                if (s.startsWith("Spam:")) {
                    StringTokenizer t = new StringTokenizer(s, " ");
                    boolean spam;
                    try {
                        t.nextToken();
                        spam = Boolean.valueOf(t.nextToken()).booleanValue();
                    } catch (Exception e) {
                    	e.printStackTrace();
                        return ss;
                    }
                    t.nextToken();
                    // 评分值
                	ss.setScore(getDouble(t.nextToken()));
                    t.nextToken();
                    // 评分阈值
                	ss.setRequireScore(getDouble(t.nextToken()));
                    
                	ss.setSpam(spam);
                	
                	return ss;
                }
            }
            return ss;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            try {
                in.close();
                out.close();
                socket.close();
            } catch (Exception e) {
            	e.printStackTrace();
            }
        }
	}

	double getDouble(String s) {
		try {
			return Double.parseDouble(s);
		} catch (NumberFormatException e) {
			return 0;
		}
	}
	
	/**
	 * testing....
	 * @param args
	 */
	public static void main(String[] args) {
		SpamAssassinInvoker sai = new SpamAssassinInvoker("192.168.1.11");
		SpamScore ss = sai.scanMail("我的测萨嘎是力进噶;是;我;;发送;对方撒娇拉斯大陆福建省电力发撒旦 ");
		
		System.out.println(ss);
	}
	
	/**
	 * Spam评分对象
	 *
	 * @author lawuu
	 */
	public class SpamScore implements Serializable {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 3111382201357553077L;

		/**
		 * 是否垃圾邮件
		 */
		private boolean spam;
		
		/**
		 * 计算得出的评分
		 */
		private double score;
		
		/**
		 * 评分阈值
		 */
		private double requireScore;

		public SpamScore() {
			
		}
		
		public SpamScore(boolean spam, double score, double requireScore) {
			this.spam = spam;
			this.score = score;
			this.requireScore = requireScore;
		}
		
		
		/* (non-Javadoc)
		 * @see java.lang.Object#toString()
		 */
		@Override
		public String toString() {
			return new StringBuffer("Spam:").append(spam)
				.append(" score:").append(score)
				.append(" requireScore:").append(requireScore).toString();
		}

		/**
		 * @return the spam
		 */
		public boolean isSpam() {
			return spam;
		}

		/**
		 * @param spam the spam to set
		 */
		public void setSpam(boolean spam) {
			this.spam = spam;
		}

		/**
		 * @return the score
		 */
		public double getScore() {
			return score;
		}

		/**
		 * @param score the score to set
		 */
		public void setScore(double score) {
			this.score = score;
		}

		/**
		 * @return the requireScore
		 */
		public double getRequireScore() {
			return requireScore;
		}

		/**
		 * @param requireScore the requireScore to set
		 */
		public void setRequireScore(double requireScore) {
			this.requireScore = requireScore;
		}
		
		
	}

	
}

你可能感兴趣的:(java,.net,socket)