-
Notifications
You must be signed in to change notification settings - Fork 0
/
ooo-disabler.js
executable file
·167 lines (129 loc) · 4.45 KB
/
ooo-disabler.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
const fs = require('fs');
const homedir = require('os').homedir();
const execSync = require('child_process').execSync;
const readline = require('readline');
const launchdPlistPath = homedir + '/Library/LaunchAgents/ooo-disabler.plist';
const configDirectory = homedir + '/.ooo-disabler';
const binaryPath = '/usr/local/bin/ooo-disabler';
if (process.argv.includes('--uninstall')) {
consolePrompt('\nAre you sure you want to uninstall ooo-disabler? (y/N)', (answer) => {
if (/^y|yes$/i.test(answer)) {
consoleRed('\nUninstalling ooo-disabler...\nDone\n');
uninstall();
}
process.exit(0);
});
}
else if (shouldTurnOffLaptop( getConfig() )) {
showNotification('WORKING OUT OF OFFICE DETECTED', 'Putting laptop to sleep in 30 seconds...');
setTimeout(() => {
showNotification('WORKING OUT OF OFFICE DETECTED', 'Putting laptop to sleep now');
setTimeout(() => {
disconnectWifi();
putToSleep();
}, 3 * 1000)
}, 30 * 1000);
}
function getConfig(){
const requredFields = ['HOME_WIFI_SSID', 'SELECTED_DAYS', 'START_AT', 'END_AT'];
let rawConfig;
try {
rawConfig = fs.readFileSync(configDirectory + '/.config', { encoding: 'utf8' });
}
catch(e) {
consoleRed('\nooo-disabler config not found or malformed. Try installing ooo-disabler again.\n');
process.exit(1);
}
const config = {};
rawConfig.trim().split(/\n/).forEach(row => {
const [ key, value ] = row.trim().split('=');
config[key] = value;
});
requredFields.forEach(field => {
if (!config[field]) {
consoleRed('\nIntallation incomplete.\nTry installing ooo-disabler again and make sure to fill out data on all steps.\n');
process.exit(1);
}
});
return config;
}
function isLidOpen(){
const cmd = `ioreg -r -k AppleClamshellState -d 4 | grep AppleClamshellState | head -1`;
return execCmd(cmd).indexOf('= No') > -1;
}
function isHomeWifiOrOff(homeWifiName){
const wifiNameCmd = `/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk -F: '/ SSID/{print $2}'`;
const wifiOffCmd = `/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I`;
const isConnectedToHomeWifi = execCmd(wifiNameCmd).trim() === homeWifiName;
const isWifiOff = execCmd(wifiOffCmd).trim() === 'AirPort: Off';
return isConnectedToHomeWifi || isWifiOff;
}
function parseConfigTime(timeString){
const values = timeString.split(':');
return {
hours: +values[0],
minutes: +values[1],
};
}
function isWorkHours(selectedDays, startAt, endAt){
const daysDict = {
'1': 'Monday',
'2': 'Tuesday',
'3': 'Wednesday',
'4': 'Thursday',
'5': 'Friday',
'6': 'Saturday',
'0': 'Sunday',
};
const now = new Date();
const currentDayName = daysDict[ now.getDay() ];
const currentHours = now.getHours();
const currentMinutes = now.getMinutes();
const start = parseConfigTime(startAt);
const end = parseConfigTime(endAt);
const isWorkDay = selectedDays.split(', ').includes(currentDayName);
const isAfterDayStarted = currentHours > start.hours || ((currentHours === start.hours) && (currentMinutes >= start.minutes));
const isBeforeDayEnded = currentHours < end.hours || ((currentHours === end.hours) && (currentMinutes < end.minutes));
return isWorkDay && isAfterDayStarted && isBeforeDayEnded;
}
function showNotification(title, text){
const cmd = `osascript -e 'display notification "${text}" with title "${title}" sound name "Glass"'`;
execCmd(cmd);
}
function consolePrompt(question, callback){
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(question, (answer) => {
callback(answer);
rl.close();
});
}
function consoleRed(text){
console.log('\x1b[31m', text);
}
function execCmd(cmd, isOutputToConsole){
const options = { encoding: 'utf8' };
if (isOutputToConsole) {
options.stdio = 'inherit';
}
return execSync(cmd, options);
}
function uninstall(){
execCmd(`launchctl unload ${launchdPlistPath}`, true);
execCmd(`rm -rf ${launchdPlistPath}`, true);
execCmd(`rm -rf ${binaryPath}`, true);
execCmd(`rm -rf ${configDirectory}`, true);
}
function disconnectWifi(){
execCmd('networksetup -setnetworkserviceenabled Wi-Fi off', true);
}
function putToSleep(){
execCmd('pmset sleepnow', true);
}
function shouldTurnOffLaptop(config){
return isLidOpen()
&& isHomeWifiOrOff(config.HOME_WIFI_SSID)
&& isWorkHours(config.SELECTED_DAYS, config.START_AT, config.END_AT);
}