forked from eskerda/slack-keep-presence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
97 lines (82 loc) · 2.42 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
#!/usr/bin/env node
"use strict"
var fs = require('fs');
var path = require('path');
var argparse = require('argparse');
var toml = require('toml');
var _ = require('lodash');
var SlackPresence = require('./');
var parser = new argparse.ArgumentParser({
description: 'Keeps your user active whenever auto-away kicks in',
});
parser.addArgument(['-t', '--token'], {
addHelp: true,
help: ['Your slack token. Can be set by env var SLACK_TOKEN.',
'Get one at https://api.slack.com/web'].join('\n'),
});
parser.addArgument(['-d', '--debug'], {
addHelp: true,
help: 'Log all slack RTM events',
action: 'storeConst',
constant: true,
});
parser.addArgument(['-n', '--notifications'], {
addHelp: true,
help: 'Try to use system notifications for mentions and DMs',
action: 'storeConst',
constant: true,
});
parser.addArgument(['-m', '--msg'], {
addHelp: true,
help: '(Auto Away) Respond to mentions and PM with this msg',
});
parser.addArgument(['-c', '--config'], {
addHelp: true,
help: 'Read config file',
});
var args = parser.parseArgs();
// remove unset things from args
_.each(args, function(value, key, list) {
if (value == null || value == undefined)
delete list[key];
});
var config_path;
if (args['config']) {
config_path = path.join(process.cwd(), args['config'])
} else {
config_path = path.join(process.env.HOME, '.config/slack-keep-presence')
}
fs.readFile(config_path, 'utf8', function(err, data) {
var config;
if (err) {
if (args['config']) {
console.error('No such file:', args['config']);
process.exit(1);
}
// Otherwise, failure to read $HOME/.config/slack-keep-presence is not
// a big deal
config = args;
} else {
// Read TOML file
try {
config = toml.parse(data);
} catch (e) {
console.error("Error parsing configuration file:",
"L" + e.line, "C" + e.column, e.message);
process.exit(1);
}
config = _.defaultsDeep(args, config);
}
var token;
if (process.env.SLACK_TOKEN) {
token = process.env.SLACK_TOKEN;
} else {
token = config['token'];
}
if (!token) {
console.error("Token not configured, see usage:");
return parser.printHelp();
}
var slack_presence = new SlackPresence(token, config);
slack_presence.init();
});