-
Notifications
You must be signed in to change notification settings - Fork 17
/
messagestore.js
185 lines (157 loc) · 5.89 KB
/
messagestore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var mime = require("./mime");
// Message handling per session
this.MessageStore = MessageStore;
function MessageStore(user){
console.log("MessageStore created");
this.user = user;
var curtime = new Date().toLocaleString();
this.messages = [];
if(typeof this.registerHook == "function")
this.registerHook();
}
MessageStore.prototype.registerHook = null;
MessageStore.prototype.length = 0;
MessageStore.prototype.size = 0;
MessageStore.prototype.messages = [];
MessageStore.prototype.counter = 0;
MessageStore.prototype.addMessage = function(message){
message = message || {};
if(!message.date)
message.date = +new Date();
message.uid = "uid"+(++this.counter)+(+new Date());
message.size = this.buildMimeMail(message).length;
this.messages.push(message);
this.length++;
this.size += message.size;
};
MessageStore.prototype.stat = function(callback){
callback(null, this.length, this.size);
}
MessageStore.prototype.list = function(msg, callback){
var result = [];
if(msg){
if(isNaN(msg) || msg<1 || msg>this.messages.length ||
this.messages[msg-1].deleteFlag)
callback(null, false);
return msg+" "+this.messages[msg-1].size;
}
for(var i=0, len = this.messages.length;i<len;i++){
if(!this.messages[i].deleteFlag)
result.push((i+1)+" "+this.messages[i].size)
}
callback(null, result);
}
MessageStore.prototype.uidl = function(msg, callback){
var result = [];
if(msg){
if(isNaN(msg) || msg<1 || msg>this.messages.length ||
this.messages[msg-1].deleteFlag)
callback(null, false);
callback(null, msg+" "+this.messages[msg-1].uid);
}
for(var i=0, len = this.messages.length;i<len;i++){
if(!this.messages[i].deleteFlag)
result.push((i+1)+" "+this.messages[i].uid)
}
callback(null, result);
}
MessageStore.prototype.retr = function(msg, callback){
if(!msg || isNaN(msg) || msg<1 || msg>this.messages.length ||
this.messages[msg-1].deleteFlag)
return callback(null, false);
return callback(null, this.buildMimeMail(this.messages[msg-1]));
}
MessageStore.prototype.dele = function(msg, callback){
if(!msg || isNaN(msg) || msg<1 || msg>this.messages.length ||
this.messages[msg-1].deleteFlag)
return callback(null, false);
this.messages[msg-1].deleteFlag = true;
this.length--;
this.size -= this.messages[msg-1].size;
return callback(null, true);
}
MessageStore.prototype.rset = function(){
for(var i=0, len = this.messages.length; i<len;i++){
if(this.messages[i].deleteFlag){
this.messages[i].deleteFlag = false;
this.length++;
this.size += this.messages[i].size;
}
}
}
MessageStore.prototype.removeDeleted = function(){
for(var i=this.messages.length-1; i>=0;i--){
if(this.messages[i].deleteFlag){
this.messages.splice(i,1);
console.log("Deleted MSG #"+(i+1));
}
}
}
/**
* MessageStore#buildMimeMail(options) -> String
* - options (Object): e-mail options
* - fromName (String): the name of the sender
* - fromAddress (String): the e-mail address of the sender
* - toName (String): the name of the recepient
* - toAddress (String): the e-mail address of the recepient
* - date (Number): JS timestamp
* - subject (String): title of the message
* - text (String): plain text version of the message
* - html (String): html version of the message
*
* Generates a MIME formatted e-mail message to be sent to the client
**/
MessageStore.prototype.buildMimeMail = function(options){
options = options || {};
var from, to, subject, date, mime_boundary, attachments, header, body;
from = [];
if(options.fromName)
from.push(mime.encodeMimeWord(options.fromName, "Q"));
if(options.fromAddress)
from.push('<'+options.fromAddress+'>');
from = from.length?from.join(" "):"unknown@localhost";
to = [];
if(options.toName)
to.push(mime.encodeMimeWord(options.toName, "Q"));
if(options.toAddress)
to.push('<'+options.toAddress+'>');
to = to.length?to.join(" "):"unknown@localhost";
subject = mime.encodeMimeWord(options.subject || 'untitled message', "Q");
date = (options.date?new Date(options.date):new Date()).toGMTString();
mime_boundary = '----bd_n3-lunchhour'+(+new Date())+'----';
// header
header = mime.foldLine('From: '+from)+"\r\n"+
mime.foldLine('To: '+to)+"\r\n"+
mime.foldLine('Date: '+date)+"\r\n"+
mime.foldLine('Subject: '+subject)+"\r\n"+
mime.foldLine('MIME-Version: 1.0')+"\r\n"+
mime.foldLine('Content-Type: multipart/alternative; boundary="'+mime_boundary+'"')+"\r\n"+
"\r\n";
attachments = [];
if(options.text){
attachments.push(
'Content-Type: text/plain; charset="utf-8"'+"\r\n"+
'Content-Transfer-Encoding: quoted-printable'+"\r\n"+
"\r\n"+
mime.encodeQuotedPrintable(options.text)
);
}
if(options.html){
attachments.push(
'Content-Type: text/html; charset="utf-8"'+"\r\n"+
'Content-Transfer-Encoding: quoted-printable'+"\r\n"+
"\r\n"+
mime.encodeQuotedPrintable(options.html)
);
}
if(!attachments.length){
attachments.push(
'Content-Type: text/plain; charset="utf-8"'+"\r\n"+
'Content-Transfer-Encoding: base64'+"\r\n"+
"\r\n"+
mime.encodeBase64("(empty message)")
);
}
body = '--'+mime_boundary+"\r\n"+ attachments.join("\r\n"+'--'+mime_boundary+"\r\n")+"\r\n"+'--'+mime_boundary+"--\r\n\r\n";
return header + body;
}