-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.js
124 lines (111 loc) · 3.49 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
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
"use strict";
require('dotenv').config()
const os = require('os');
const axios = require("axios");
const execSync = require("child_process").execSync;
const querystring = require("querystring");
const config = require("./config");
if (!config.slackToken) {
console.error("Missing Slack token. Set it in config.js");
process.exit(1);
}
function getLinuxWiFiName() {
return execSync("iwgetid -r") // Linux only
.toString()
.split("\n")
.filter(line => line.match(/.+/))
.find(ssid => true); // find first
}
function getLinuxIpAddress() {
return '';
// TODO: linux ip address support
}
function getMacWiFiName() {
return execSync("/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I") // macos only
.toString()
.split("\n")
.filter(line => line.includes(" SSID: "))
.map(line => line.match(/: (.*)/)[1])
.find(ssid => true); // find first
}
function getMacIpAddress() {
return '';
// TODO: Mac ip address support
}
function getWinWiFiName() {
return execSync("netsh wlan show interfaces") // Windows only
.toString()
.split("\n")
.filter(line => line.includes(" SSID "))
.map(line => line.match(/: (.*)/)[1])
.find(ssid => true); // find first
}
function getWinIpAddress() {
return execSync("ipconfig") // Windows only
.toString()
.split("\n")
.filter(line => line.includes("IPv4 Address"))
.map(line => line.match(/: (.*)/)[1])
.find(ssid => true); // find first
}
function setSlackStatus(token, status) {
return axios.post("https://slack.com/api/users.profile.set",
querystring.stringify({
token: token,
profile: JSON.stringify(status)
}), {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
}).then(function(response) {
console.log("Set Slack status API response: %j", response.data);
})
.catch(function(error) {
console.error("Set Slack status error: %s", error);
});
}
const platform = os.platform();
let ipAddress;
let getIpAddress;
let wiFiName;
let getWiFiName;
// Get appropriate function for platform
switch (platform) {
case 'darwin':
getWiFiName = getMacWiFiName;
getIpAddress = getMacIpAddress;
break;
case 'win32':
getWiFiName = getWinWiFiName;
getIpAddress = getWinIpAddress;
break;
case 'linux':
getWiFiName = getLinuxWiFiName;
getIpAddress = getLinuxIpAddress;
break;
default:
console.error('Unknown platform %s', platform);
process.exit(2);
}
setInterval(function() {
const newIpAddress = getIpAddress();
const newWiFiName = getWiFiName();
if (newWiFiName === wiFiName && newIpAddress === ipAddress) {
return;
}
ipAddress = newIpAddress;
wiFiName = newWiFiName;
console.log("Connected WiFi SSID: %s", wiFiName);
console.log("Connected IP: %s", ipAddress);
var status = config.statusByWiFiName[wiFiName];
if (!status) {
console.log("Status not specified for WiFi: %s", wiFiName);
status = config.statusByIpAddress[ipAddress];
if (!status) {
console.log("Status not specified for IP: %s", ipAddress);
return;
}
}
console.log("Setting Slack status to: %j", status);
setSlackStatus(config.slackToken, status);
}, config.updateInterval);