-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
156 lines (136 loc) · 4.42 KB
/
server.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
/*
Oswego CSA IRC bot
*/
var fs = require("fs");
try {
var config = JSON.parse(fs.readFileSync("config.json").toString());
} catch (ex) {
console.log('got an error: %s', ex);
process.exit(1);
}
// Mailer ================================================================
var nodemailer = require("nodemailer");
// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport("SMTP", {
service: config.mailer.service,
auth: {
//name: "client server name",
user: config.mailer.user,
pass: config.mailer.pass
},
maxConnections: 1
});
// END Mailer ============================================================
// setup e-mail data with unicode symbols
function mailOptions(message) {
return {
from: config.notify.from,
// sender address
to: config.notify.to,
// list of receivers
//replyTo: "" // replyTo address - don't really need it
subject: config.notify.subject,
// Subject line
text: message || "You got an irc message",
// plaintext body
html: message || "You got an irc message" // html body
};
}
// This is just a mail queue that will check every 5 seconds
// and send the message if there is any
var mailQueue = [];
function sendMail() {
if (mailQueue.length !== 0) {
var message = mailQueue.shift();
smtpTransport.sendMail(message, function (error, response) {
if (error) {
console.log(error);
// add the message back to the queue cause to try again later
mailQueue.push(message);
} else {
console.log("Message sent: " + response.message);
}
// I have to close it every time because the messages arrive out of order.
// If anyone has a better solution send a pull request.
// if you don't want to use this transport object anymore, uncomment following line
smtpTransport.close(); // shut down the connection pool, no more messages
});
}
}
setInterval(function() {
sendMail();
}, 10000);
/*
* To set the key/cert explicitly, you could do the following
var fs = require('fs');
var options = {
key: fs.readFileSync('privkey.pem'),
cert: fs.readFileSync('certificate.crt')
};
*/
var irc = require('irc');
var bot = new irc.Client(config.options.server, config.options.nick, config.options);
bot.on('message#', function(from, to, message, raw) {
//console.log('<%s> %s', from, message);
if (message.match(/poorman/)) {
// send me an email or something
console.log("got a message for me");
bot.emit('notify', message, from);
}
});
bot.on('message', function(from, to, message) {
console.log('%s => %s: %s', from, to, message);
if (to.match(/^[#&]/)) {
// channel message
if (message.match(/hello/i)) {
bot.say(to, 'Hello there ' + from);
}
}
//else {
// private message
//}
});
bot.on('notice', function(nick, to, text, message) {
if (nick === 'NickServ') {
// msg from nickserv
if (text.match(/This nickname is registered\. Please choose a different nickname/)) {
// asking for ident
bot.emit('identify');
}
}
});
bot.on('identify', function() {
bot.say('NickServ', 'identify ' + config.ident.password);
});
bot.on('error', function(message) {
console.error('ERROR: %s: %s', message.command, message.args.join(' '));
});
bot.on('notify', function(message, from) {
//var chunkedMailOpts = [];
var userTag = '[' + from + ']> ';
if (config.notify.textMessage) {
var subjectSize = config.notify.subject.length + 3; // 3 for 2 paren and a space for the subject
var charLimit = 140;
var chunkSize = charLimit - subjectSize - userTag.length;
var re = new RegExp('.{1,' + chunkSize + '}\\W', "g");
var chunks = message.match(re);
for (var i = 0; i < chunks.length; i++) {
mailQueue.push(mailOptions(userTag + chunks[i]));
}
} else {
mailQueue.push(mailOptions(userTag + message));
}
});
// See http://node-irc.readthedocs.org/en/latest/API.html#events
// bot.on('pm', function (nick, message) {
// console.log('Got private message from %s: %s', nick, message);
// });
// bot.on('join', function (channel, who) {
// console.log('%s has joined %s', who, channel);
// });
// bot.on('part', function (channel, who, reason) {
// console.log('%s has left %s: %s', who, channel, reason);
// });
// bot.on('kick', function (channel, who, by, reason) {
// console.log('%s was kicked from %s by %s: %s', who, channel, by, reason);
// });