-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
142 lines (111 loc) · 3.53 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
/*jslint indent: 2, node: true, nomen: true, plusplus: true, todo: true */
"use strict";
var util = require("util");
var _ = require("lodash");
var express = require("express");
var fleece = require("fleece");
var irc = require("irc");
var log = require("floorine");
var settings = require("./settings");
var Server = function () {
var self = this;
self.app = express(log);
self.app.use(express.bodyParser());
self.app.use(express.basicAuth(self.auth.bind(self)));
self.app.post("/deploy/:project/:env", self.handle_deploy.bind(self));
settings.irc.options.channels = _.keys(settings.channels);
self.irc_client = new irc.Client(settings.irc.server, settings.irc.nick, settings.irc.options);
self.irc_client.on("message", self.on_msg.bind(self));
self.irc_client.on("error", function (msg) {
log.error("IRC error:", msg);
});
};
Server.prototype.auth = function (user, pass) {
return settings.users[user] && settings.users[user] === pass;
};
Server.prototype.handle_deploy = function (req, res) {
var self = this,
msg;
if (!req.body) {
log.error("No request body");
return;
}
msg = util.format("%s deployed %s to %s", req.user, req.params.project, req.params.env);
_.each(self.irc_client.chans, function (channel) {
var chan = settings.channels[channel.key];
if (chan && _.contains(chan.announcements, "deploys")) {
log.debug("Saying %s: %s", channel.key, msg);
self.irc_client.say(channel.key, msg);
}
});
res.send(204);
};
Server.prototype.listen = function (cb) {
var self = this;
log.log("Listening on port %s", settings.http_port);
// TODO: https (need to async.parallel the app.listen()s)
self.app.listen(settings.http_port, function (err, result) {
if (err) {
return cb(err, result);
}
log.log("Connecting to %s as %s", settings.irc.server, settings.irc.nick);
self.irc_client.connect(3, function (result) {
log.log("IRC connect:", result);
// connect doesn't seem to send errors
cb(null, result);
});
});
};
Server.prototype.on_msg = function (from, to, msg) {
var self = this,
chan = settings.channels[to],
urls,
url_regex = new RegExp("(?:https?:\\/\\/)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,4}(?::[0-9]{1-5})?\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)", "gi");
log.log("%s -> %s: %s", from, to, msg);
if (from === self.nick || _.contains(settings.ignored_users, from)) {
return;
}
if (to.slice(0, 1) !== "#") {
// to = from;
return;
}
if (!chan || !_.contains(chan.announcements, "urls")) {
return;
}
urls = msg.match(url_regex);
if (!urls) {
return;
}
_.each(urls, function (msg_url) {
fleece.describe_url(msg_url, function (err, result) {
if (err || !result) {
return;
}
let processed_title = ' \u001F' + result;
self.irc_client.say(to, processed_title);
});
});
};
Server.prototype.stop = function (reason, cb) {
var self = this;
self.irc_client.disconnect(util.format("Shutting down: %s", reason), cb);
};
exports.run = function () {
var server = new Server();
log.set_log_level(settings.log_level);
function shut_down(signal) {
log.warn("Got %s. Shutting down...", signal);
server.stop(util.format("Got %s.", signal), function () {
log.log("All done.");
process.exit(0);
});
}
process.on("SIGTERM", function () {shut_down("SIGTERM"); });
process.on("SIGINT", function () {shut_down("SIGINT"); });
server.listen(function (err) {
if (err) {
log.error(err);
process.exit(1);
}
});
};