-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
197 lines (164 loc) · 4.14 KB
/
index.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
186
187
188
189
190
191
192
193
194
195
196
197
var superagent = require("superagent");
var Promise = require("bluebird");
function TempMailbox() {
var init = Promise.defer();
var first_fetch = Promise.defer();
var me = this;
this.agent = superagent.agent();
this.sequence = 0;
this.init_promise = init.promise;
this.messages = [];
this.fetch_outstanding = true;
this.current_fetch_promise = first_fetch.promise;
this.filter_welcome_email = true;
this.agent
.get("http://api.guerrillamail.com/ajax.php")
.query({
f: "get_email_address",
ip: "127.0.0.1",
agent: "rr-guerrillamail"
})
.end(function(err, res) {
if (err) {
init.reject("init failed");
return;
}
me.email_addr = res.body.email_addr;
me.sid_token = res.body.sid_token;
me.email_timestamp = res.body.email_timestamp;
me.alias = res.body.alias;
init.resolve(true);
me.fetch_outstanding = false;
me.checkMail().then(function() {
first_fetch.resolve();
});
});
}
TempMailbox.prototype.getEmailAddress = function() {
var me = this;
return this.init_promise.then(function() {
return me.email_addr;
});
};
TempMailbox.prototype.checkMail = function() {
var me = this;
if (this.fetch_outstanding) {
return this.current_fetch_promise;
}
this.fetch_outstanding = true;
var defer = Promise.defer();
this.agent
.get("http://api.guerrillamail.com/ajax.php")
.query({
f: "check_email",
seq: this.sequence
})
.end(function(err, res) {
if (err) {
defer.reject("request failed");
return;
}
res.body.list.forEach(function(m) {
if (me.filter_welcome_email) {
if (m.mail_id == 1) return;
}
me.messages.push(m);
me.sequence = m.mail_id;
});
me.fetch_outstanding = false;
defer.resolve(true);
});
this.current_fetch_promise = this.init_promise.then(function() {
return defer.promise;
});
return this.current_fetch_promise;
};
TempMailbox.prototype.getNextMailSummary = function() {
var me = this;
if (this.messages.length > 0) {
return new Promise(function(res, rej) {
res(me.messages.shift());
});
}
return this.checkMail().then(function() {
if (me.messages.length != 0) {
return me.getNextMailSummary();
}
return null;
});
};
TempMailbox.prototype.getMailDetail = function(id) {
var defer = Promise.defer();
this.agent
.get("http://api.guerrillamail.com/ajax.php")
.query({
f: "fetch_email",
email_id: id
})
.end(function(err, res) {
if (err) {
defer.reject("request failed");
return;
}
defer.resolve(res.body);
});
return this.init_promise.then(function() {
return defer.promise;
});
};
TempMailbox.prototype.waitForEmail = function(summaryFilter, iterationDelay) {
var me = this;
iterationDelay = iterationDelay || 2500;
function checkLoop(mail) {
if (mail) {
var match = summaryFilter(mail);
if (match) {
return me.getMailDetail(mail.mail_id);
}
return me.getNextMailSummary().then(checkLoop);
}
return Promise.delay(iterationDelay)
.then(function() {
return me.getNextMailSummary();
})
.then(checkLoop);
}
return this.init_promise
.then(function() {
return me.getNextMailSummary();
})
.then(checkLoop);
};
TempMailbox.prototype.deleteMail = function(id) {
var defer = Promise.defer();
this.agent
.get("http://api.guerrillamail.com/ajax.php")
.query({
f: "del_email",
email_ids: id
})
.end(function(err, res) {
if (err) {
defer.reject("request failed");
return;
}
defer.resolve(res.body);
});
return this.init_promise.then(function() {
return defer.promise;
});
};
module.exports = TempMailbox;
/*
var mailbox = new TempMailbox();
mailbox.getEmailAddress()
.then(function(addr) { console.log('email addr: ' + addr); })
.then(function() {
return mailbox.waitForEmail(function(m) {
return (m.mail_subject.indexOf('xyzzy') != -1);
});
})
.then(function(mail) {
console.log(mail.mail_body);
});
*/