/**
* Created by Administrator on 2014/6/25.
*/
var debug = console.log;
var net = require ("net");
var flag = false;//数据处理标志
var dataMessage = [];
var fromAddress = [];
var toAddress = [];
var dateStore=[];//存储发邮件的时间
var USERNAME = ["wsg@skh.whu.edu.cn", "lxn@skh.whu.edu.cn", "hj@skh.whu.edu.cn", "zcj@skh.whu.edu.cn", "zcg@skh.whu.edu.cn", "634360230@qq.com"];
var STATE = { CONNECTED: 1, HELOED: 2, MAILFROMED: 3, RCPTTOED: 4, DATAED: 5};
var server = net.createServer (function (socket) {
socket.write ("220 " + socket.remoteAddress + "\r\n");
var input = "";
var state = STATE.CONNECTED;
socket.setEncoding ("utf8");
socket.on ('error', function (e) {
debug (e);
});
socket.on ('data', function (data) {
input += data;
debug (input);
var index = input.indexOf ("\r\n");
if (index == -1)
return;
// input = input.slice (0, index);
if (state == STATE.RCPTTOED) {
if (flag) {
// 数据输入结束标志
var index = input.indexOf ("\r\n.\r\n");
if (index == -1)
return;
else {
state = STATE.DATAED;
//存储消息
dataMessage.push (input);
socket.write ("250 queued\r\n");
input = "";
flag = false;
return;
}
// flag = false;
} else {
// var input=input.slice(0,index);//test
var result = input.match (/^data/i);
if (result) {
socket.write ("354 ok, send\r\n");
input = " ";
}
else {
socket.write ("The DataFormat is not right!\r\n");
input = "";
}
flag = true;
return;
}
}
input = input.slice (0, index);//位置不能换,否则,影响data的处理
switch (state) {
case STATE.CONNECTED:
var result = input.match (/^helo\s\w+$/i);
if (result) {
socket.write ("250 Hello\r\n");
state = STATE.HELOED;
input = "";
}
else {
socket.write ("The HeloFormat is not right!\r\n");
input = "";
}
break;
case STATE.HELOED:
var result = input.match (/^mail from:((\w+\.)*\w+@(\w+\.)*\w+)$/i);
if (result) {
//验证邮箱的存在
if (searchMailFrom ()) {
socket.write ("250 OK\r\n");
state = STATE.MAILFROMED;
fromAddress.push (input);//存发件人邮箱
input = "";
}
else {
socket.write ("The MailFromAddress is not exist!\r\n");
input = "";
}
}
else {
socket.write ("The MailFromFormat is not right!\r\n");
input = "";
}
break;
case STATE.MAILFROMED:
var result = input.match (/^rcpt to:((\w+\.)*\w+@(\w+\.)*\w+)$/i);
if (result) {
//验证邮箱的存在
if (searchMailTo ()) {
socket.write ("250 OK\r\n");
toAddress.push (input);//存取收件人的邮箱
state = STATE.RCPTTOED;
input = "";
}
else {
socket.write ("The MailToAddress is not exist!\r\n");
input = "";
}
}
else {
socket.write ("The RcptFormat is not right!\r\n");
input = "";
}
break;
case STATE.DATAED:
if (index == -1)
break;
else {
var result = input.match (/^quit/i);
if (result) {
socket.write ("221 goodbye\r\n");
//存储时间
var myDate=new Date();
var year=myDate.getFullYear();
var month=Number(myDate.getMonth())+1;
var date=myDate.getDate();
var hour=myDate.getHours();
var min=myDate.getMinutes();
var sec=myDate.getSeconds();
var dateYMD=year+"-"+month+"-"+date+" "+hour+":"+min+":"+sec;
dateStore.push(dateYMD);
input = "";
socket.end ();
}
else
input = "";
break;
}
}
//验证rcpt to邮箱存在的函数
function searchMailTo () {
var i = 0;
input = input.slice (8, input.length);
while (i < USERNAME.length) {
var str1 = input.indexOf (USERNAME[i]);
var str2 = USERNAME[i].indexOf (input);
if (str1 == 0 && str2 == 0)
return true;
else
i++;
if (i >= USERNAME.length)
return false;
}
}
//验证mail from邮箱存在的函数
function searchMailFrom () {
var i = 0;
input = input.slice (10, input.length);
while (i < USERNAME.length) {
var str1 = input.indexOf (USERNAME[i]);
var str2 = USERNAME[i].indexOf (input);
if (str1 == 0 && str2 == 0)
return true;
else
i++;
if (i >= USERNAME.length)
return false;
}
}
});
});
server.listen (25, "localhost");
/**
* Created by Administrator on 2014/6/27.
* pop3server
*/
var net = require ("net");
var STATEP = { CONNECTED: 1, USER: 2, PSW: 3, LIST: 4};
var pop3ClientArray = [
{USER: "zcg@skh.whu.edu.cn", PSW: "zcg"},
{USER: "hj@skh.whu.edu.cn", PSW: "hj"},
{USER: "lxn@skh.whu.edu.cn", PSW: "lxn"}
];
var returnpath = [];
var popserver = net.createServer (function (socket) {
socket.write ("+OK POP3" + "\r\n");
var state = STATEP.CONNECTED;
var temp = "";//保存匹配的登陆用户,并用于匹配相应的PSW
var input = "";
socket.setEncoding ("utf8");
socket.on ('error', function (e) {
debug (e);
});
socket.on ("data", function (data) {
input += data;
var index = input.indexOf ("\r\n");
if (index == -1)
return;
input = input.slice (0, index);
debug (input);
//计算所发消息的字节数
var lenFor=function(str){
var str=new String(str);
return str.length;
}
// var lenFor = function (str) {
// var str = new String (str);
// var byteLen = 0, len = str.length;
// if (str) {
// for (var i = 0; i < len; i++) {
// if (str.charCodeAt (i) > 255) {
// byteLen += 2;
// }
// else {
// byteLen++;
//