-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
297 lines (206 loc) · 6.91 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
var irc = require('irc');
var env = require('./config.js');
var fs = require('fs');
var Bot = require('node-telegram-bot');
var moment = require('moment');
var filename = '';
//meeting configs
var meeting_time = moment().format('hh:mm A');
var meeting_date = moment().format('DD MMM YY');
var meeting_link = 'https://public.etherpad-mozilla.org/p/kerala-' + moment().format('DDMMMYY');
var meeting_logs = 'bots.mozillakerala.org/botzilla/logs/' + moment().format('DDMMMYY') + '.txt';
var wstream;
filename = './logs/' + moment().format('DDMMMYY') + '.txt';
console.log(filename);
//options for logging
var options = {
encoding: 'utf8',
flags: 'a'
};
wstream = fs.createWriteStream(filename, options);
//reading meeting status in sync
var meeting_status = fs.readFileSync('./mod', 'ascii');
//setting up irc configs
var client = new irc.Client(env.server, env.nick, {
channels: [
env.channel
]
});
//listening for messages from IRC
client.addListener('message', function (from, to, message) {
console.log(from + ': ' + message);
if (meeting_status == 'off')
return;
bot.sendMessage({
chat_id: env.tg_chatId,
text: from + ': ' + message
})
add_log(from + ': ' + message + '\n')
});
//log errors
client.addListener('error', function (message) {
console.log('error: ', message);
});
var bot = new Bot({
token: env.tgbot_token
}).on('message', function (message) {
console.log(message);
//if message is not text (stickers , image.....) then do nothing
if (!message.text) {
return;
}
var command = (message.text.split('/')[1]);
if (command) {
//remove bot name from the command
command = (command.replace(env.tgbot_name, '')).toLowerCase();
}
switch (command) {
case 'startmeeting':
startmeeting(message.from.id);
//exit from the function so that the /startmeeting is not send to the IRC
return;
case 'stopmeeting':
stopmeeting(message.from.id);
return;
case 'status':
send_status();
return;
case 'restart':
restart();
return;
default:
send(message);
}
}).start();
//send Message to IRC
function send(message) {
var msg;
if (meeting_status == 'off')
return;
//if the message is a reply , show (first_name in response to username)
if (message.reply_to_message) {
//if reply is to a botzilla message then parse name from the message
if (message.reply_to_message.from.first_name == env.tgbot_name.replace('@', '')) {
msg = message.from.first_name + '(in reply to ' + message.reply_to_message.from.first_name + ') : ' + message.text;
} else {
//Regular Expression to parse names from botzilla message
var reply_to = message.reply_to_message.text.match(/((?:[a-z0-9_@]+):+)/i);
if (reply_to != undefined) {
console.log('here');
msg = message.from.first_name + '(in reply to ' + reply_to[0] + ') : ' + message.text;
} else
msg = message.from.first_name + '(in reply to ' + message.reply_to_message.from.first_name + ') : ' + message.text;
}
} else {
msg = message.from.first_name + ': ' + message.text;
}
client.say(env.channel, msg);
msg += '\n';
add_log(msg);
console.log(message);
}
function startmeeting(from) {
if (from !== env.admin_id) {
return;
}
meeting_status = 'on';
//start logging to file
filename = './logs/' + moment().format('DDMMMYY') + '.txt';
wstream = fs.createWriteStream(filename, options);
fs.writeFile('./mod', 'on', 'ascii', function (err) {
//if error retry startmeeting
if (err) {
startmeeting(from);
} else {
meeting_time = moment().format('hh:mm A');
meeting_date = moment().format('DD MMM YY');
meeting_link = 'https://public.etherpad-mozilla.org/p/kerala-' + moment().format('DDMMMYY');
meeting_logs = 'bots.mozillakerala.org/botzilla/logs/' + moment().format('DDMMMYY') + '.txt';
var meeting_msg = strformat("A Meeting was just started by %admin_name% at %time% on %date%\n\nThe agenda of the meeting is at %datelink%\n\nYou better be civil from now onwards because I'm logging everything you say.\n\nI'll tell you where to find the log after the meeting ends.", {
time: meeting_time,
date: meeting_date,
datelink: meeting_link,
admin_name: env.admin_name
});
bot.sendMessage({
chat_id: env.tg_chatId,
text: meeting_msg
})
send({
from: {
first_name: env.admin_name
},
text: meeting_msg
});
}
});
}
function stopmeeting(from) {
if (from !== env.admin_id) {
return;
}
meeting_status = 'off';
//write status to mod file
fs.writeFile('./mod', 'off', 'ascii', function (err) {
//if error retry stopmeeting
if (err) {
startmeeting(from);
} else {
var meeting_msg = strformat("The meeting that %admin_name% started and chaired from %time% %date% has ended.\nYou can find the public logs of the meeting in the file", {
time: meeting_time,
date: meeting_date,
loglink: meeting_logs,
admin_name: env.admin_name
});
send({
from: {
first_name: env.admin_name
},
text: meeting_msg
});
bot.sendMessage({
chat_id: env.tg_chatId,
text: meeting_msg
})
client.say(env.channel, meeting_msg);
//stop logging to file
wstream.end();
//Send the log to Telegram
bot.sendDocument({
chat_id: env.tg_chatId,
caption: 'Chat log',
files: {
document: filename
}
}, function (err, msg) {
console.log(err);
console.log(msg);
});
}
});
}
function send_status() {
bot.sendMessage({
chat_id: env.tg_chatId,
parse_mode: 'Markdown',
text: 'Meeting mode *' + meeting_status.toUpperCase() + '*'
})
}
function restart() {
//Exit the program and Assume PM2 or Forever will restart the process
bot.sendMessage({
chat_id: env.tg_chatId,
parse_mode: 'Markdown',
text: 'Bot will restart * now *'
});
process.exit(1);
}
function add_log(msg) {
wstream.write(msg);
}
function strformat(str, obj) {
for (key in obj) {
str = str.replace(new RegExp('%' + key + '%', 'g'), obj[key]);
}
return str;
}