-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
206 lines (175 loc) · 5.16 KB
/
app.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
198
199
200
201
202
203
204
205
206
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
//parsecookie
var cookie = require('cookie');
var session = require('express-session')
,RedisStore = require('connect-redis')(session);
var Imap = require('imap'),
inspect = require('util').inspect;
var MailListener = require("mail-listener2");
var routes = require('./routes/index');
//var users = require('./routes/users');
var app = express();
//socket
if(process.env.REDISTOGO_URL) {
var redisUrl = require("url").parse(process.env.REDISTOGO_URL);
var redisAuth = redisUrl.auth.split(':');
var store = new RedisStore({host: redisUrl.hostname, port: redisUrl.port, pass: redisAuth[1] });
}
else
var store = new RedisStore({host: '127.0.0.1', port: 6379 });
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
store : store,
secret : 'hjvkjnrwkjnvrwnui42oiu4io2ujofijienceklacnlieo12931',
resave : 'false',
saveUninitialized: 'true'
}));
var debug = require('debug')('generated-express-app');
//var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
var io = require('socket.io').listen(server);
var socketUsers = {};
var imap;
io.set('authorization', function (handshakeData, accept) {
if(handshakeData.headers.cookie) {
handshakeData.cookie = cookie.parse(handshakeData.headers.cookie);
handshakeData.sessionID = handshakeData.cookie['connect.sid'].split('.')[0].substring(2);
// console.log(handshakeData.cookie['connect.sid']);
store.get(handshakeData.sessionID, function (err, session) {
console.log(session);
if(typeof session.email === 'undefined' && typeof session.password === 'undefined') {
accept('Error', false);
} else {
imap = new Imap({
user: session.email,
password: session.password,
host: 'imap.gmail.com',
port: 993,
tls: true
});
accept(null, true);
}
});
}
});
//var io = req.io;
io.on('connection', function (client) {
console.log('connected socket');
// console.log(client.request);
var sessionID = client.request.headers.sessionID;
// console.log(handshakeData.cookie['connect.sid']);
socketUsers[sessionID] = client;
imap.on('error', function(err) {
var error = err.toString();
console.log(err);
switch(err.source) {
case 'timeout-auth':
case 'timeout':
case 'socket':
imap.connect();
break;
default:
break;
}
});
var mails = [];
// console.log(mailbox);
imap.connect();
imap.once('ready', function() {
imap.openBox('INBOX', false, function(err, box) {});
imap.on('mail', function(arriveMail) {
console.log('Mail arrived', arriveMail);
imap.openBox('INBOX', false, function(err, box) {
if (err) {
// var err = {'err' : 'not-logged-in'};
res.json(err);
console.log(err);
return;
}
var remainMsg;
var f = imap.seq.fetch(box.messages.total + ':*', {
bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
markSeen : true,
struct: true
});
f.on('message', function(msg, seqno) {
// console.log('Message #%d', seqno);
// var prefix = '(#' + seqno + ') ';
msg.on('body', function(stream, info) {
var buffer = '';
stream.on('data', function(chunk) {
buffer += chunk.toString('utf8');
});
stream.once('end', function() {
mails = Imap.parseHeader(buffer);
socketUsers[sessionID].emit('mail', { 'mail' : mails, 'seqno': seqno });
//console.log(mails);
});
});
/* msg.once('attributes', function(attrs) {
console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
});
msg.once('end', function() {
console.log(prefix + 'Finished');
});*/
});
f.once('error', function(err) {
console.log('Fetch error: ' + err);
});
f.once('end', function() {
console.log('Disconnecting');
});
});
});
});
});
io.on('disconnect', function() {
console.log('socket disconnected');
imap.end();
});
app.use('/', routes);
//app.use('/users', users);
/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;