This repository has been archived by the owner on Oct 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
168 lines (157 loc) · 5.12 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
157
158
159
160
161
162
163
164
165
166
167
168
var _ = require('./lib/toolbelt');
var bodyParser = require('body-parser');
var config = require('config');
var db = require('./lib/db');
var express = require('express');
var ezirc = require('./lib/ezirc');
var fs = require('fs');
var http = require('http');
var https = require('https');
var ircdispatcher = require('./lib/ircdispatcher');
var l = require('./lib/log')('Server');
var messageparser = require('./lib/messageparser');
var q = require('q');
var slack = require('./lib/slack');
var slackdispatcher = require('./lib/slackdispatcher');
var httpsOptions = {
key: fs.readFileSync(config.server.sslPrivateKeyPath),
cert: fs.readFileSync(config.server.sslCertificatePath),
};
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/toirc', function(req, res){
l.debug('Got incoming msg to IRC: ', req.body);
messageparser.parseIrcMessage(req.body)
.then(function(message) {
return ircdispatcher.postMessage(message);
})
.then(function(result) {
if (result && result.sent)
l.verbose('IRC message sent to IRC server');
res.status(200).end();
}, function error (e) {
if (e.name === 'HttpError') {
// Send 200 so that Slack will display the message
res.status(200).json({text: 'Error: ' + e.message});
} else {
l.error('%s\nStack: "%s"', e.toString(), e.stack);
res.status(500);
}
})
.done();
});
app.post('/ircsetup', function(req, res) {
l.debug('Got IRC setup message from Slack');
messageparser.parseIrcSetupMessage(req.body)
.then(function(message) {
if (message.text.length === 0 || message.text.indexOf('help') >= 0) {
res.status(200).send('Try for instance "/ircsetup ssl=true server=irc.mozilla.org:6697 '
+ 'password=monkeybusiness channel=#development"');
return;
}
var existing = db.getServerConfig(message.channel_id);
if (existing) {
res.status(200).send('Channel is already connected to IRC');
return;
}
var parts = message.text.split(' ');
options = {channelMap: {}};
parts.forEach(function(part) {
var splitPos = part.indexOf('=');
if (splitPos < 1)
return;
var key = part.substring(0, splitPos);
var value = part.substring(splitPos + 1);
if (key === 'ssl') {
options.useTLS = (value === 'true');
} else if (key === 'server') {
var hostSpec = value.split(':');
options.host = hostSpec[0];
options.port = hostSpec.length > 1 ? hostSpec[1] : "6667";
} else if (key === 'password') {
options.serverPassword = value;
} else if (key === 'channel') {
options.channelMap[message.channel_id] = value;
}
});
if (!_.hasAll(options, 'host', 'port') || options.channelMap.length < 1) {
res.status(200).send('Must specify at least host:port and an irc channel name to map to');
return;
}
var serverConfig = db.getOrAddServerConfig(options.host, options.port);
_.defaults(serverConfig, options);
_.forOwn(options.channelMap, function(ircChannel, slackChannel) {
db.mapChannels(serverConfig, slackChannel, ircChannel);
});
createClientConnection(serverConfig)
.then(function() {
res.status(200).send('Setup and connection was successful');
}, function(err) {
res.status(200).send('ERROR! Message was: ', err.toString());
})
.done();
})
.done();
});
var createClientConnections = function() {
_.forOwn(db.allServerConfigs(), createClientConnection);
};
var createClientConnection = function(config) {
var all = [];
_.forOwn(config.channelMap, function(ircChannel, slackChannel) {
var promise = slack.getChannelInfo({channel: slackChannel})
.then(function(info) {
if (info && info.channel) {
connectSlackChannelToIrc(config, info.channel, ircChannel);
}
})
.done();
all.push(promise);
});
return q.all(all);
};
var connectSlackChannelToIrc = function(config, slackChannel, ircChannel) {
var all = [];
slackChannel.members.forEach(function(member) {
var promise = db.getOrAddClient(slackChannel.id, member, clientCreator(config, member))
.then(function(client) {
return ezirc.join(client, ircChannel);
})
.done();
all.push(promise);
});
return q.all(all);
};
var clientCreator = function(config, member) {
return function() {
var user = db.getUser(member);
if (!user)
l.warn('User %s not found', member);
var options = _.clone(config);
options = _.extend(options, {
nick: user.name,
userid: user.name,
username: user.real_name
});
return ezirc.connect(options)
.then(function(client) {
ezirc.onMessage(client, slackdispatcher.postMessage);
return client;
});
};
};
var startServer = function() {
var useHttps = config.server.useHttps;
var server = useHttps ? https.createServer(httpsOptions, app) : http.createServer(app);
server.listen(config.server.port, function() {
l.info('Listening for HTTP%s traffic on port %s', useHttps ? 'S' : '', config.server.port);
});
};
db.load();
slack.getUserList()
.then(db.cacheUsers)
.then(function() {
createClientConnections();
startServer();
})
.done();