This repository was archived by the owner on Feb 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeepsleep.js
More file actions
executable file
·265 lines (231 loc) · 6.48 KB
/
deepsleep.js
File metadata and controls
executable file
·265 lines (231 loc) · 6.48 KB
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
/* Deep Sleep widget (1.3) javascript logic */
/* M. Beaumel (cochonou@fastmail.fm), 09/08/10 */
/*
* Copyright (C) 2010 M. Beaumel
*
* This file is part of Deep Sleep.
*
* Deep Sleep is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Deep Sleep is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Deep Sleep. If not, see <http://www.gnu.org/licenses/>.
*/
function setup() {
thePassButton = new AppleGlassButton(document.getElementById("passButton"), "OK", enterPass);
theConfigButton = new AppleGlassButton(document.getElementById("configButton"), "OK", enterConfig);
theInfoButton = new AppleInfoButton(document.getElementById("infoButton"), document.getElementById("front"), "white", "white", info);
debugMsg = false;
sleepMsg = false;
busy = false;
fader[0] = new fadeObj(0, 'sleepText', '666666', 'ffffff', 15, 15, false);
fader[0].message[1] = "Deep Sleep";
fader[1] = new fadeObj(1, 'clickText', '666666', 'ffffff', 15, 15, false);
fader[1].message[1] = "click to enter";
fader[2] = new fadeObj(2, 'msgText', '666666', 'ffffff', 15, 15, false);
fader[2].message[1] = "System is going down to sleep...";
front = document.getElementById("front");
back = document.getElementById("backPass");
config = document.getElementById("backConfig")
nameField = document.getElementById("nameField");
passField = document.getElementById("passField");
regularDropDown = document.getElementById("regularDropDown");
deepDropDown = document.getElementById("deepDropDown");
}
function showText() {
fade(0, 1, true);
fade(1, 1, true);
}
function hideText() {
fade(0, 1, false);
fade(1, 1, false);
if (debugMsg) {
fade(2, 2, false);
debugMsg = false;
}
if (sleepMsg) {
fade(2, 1, false);
sleepMsg = false;
}
}
function showBackPass() {
var name = widget.system("/usr/bin/whoami", null).outputString;
name = name.substring(0, name.length-1);
nameField.value = name;
if (window.widget)
widget.prepareForTransition("ToBack");
front.style.display="none";
back.style.display="block";
if (window.widget)
setTimeout ('widget.performTransition();', 0);
passField.focus();
busy = false;
}
function showBackConfig() {
if (busy) return;
var original = modeToIndex(parseInt(widget.system("pmset -g | grep hibernatemode | tail -c 2", null).outputString));
regularDropDown.selectedIndex = original;
var targetMode;
if (!widget.preferenceForKey("DeepSleepMode")) {
targetMode = "soft";
} else {
targetMode = widget.preferenceForKey("DeepSleepMode");
}
var target = stringToIndex(targetMode);
deepDropDown.selectedIndex = target;
if (window.widget)
widget.prepareForTransition("ToBack");
front.style.display="none";
config.style.display="block";
if (window.widget)
setTimeout ('widget.performTransition();', 0);
busy = false;
}
function hideBack() {
if (window.widget)
widget.prepareForTransition("ToFront");
back.style.display="none";
config.style.display="none";
front.style.display="block";
if (window.widget)
setTimeout ('widget.performTransition();', 0);
}
function passKeyPressed(event) {
if (event.keyCode == 13)
enterPass();
}
function enterPass() {
var name = nameField.value;
var password = passField.value;
passField.value = "";
widget.system("/bin/chmod o+x deepsleep", null);
var command = "/usr/bin/osascript -e 'do shell script \"/usr/sbin/chown root:wheel deepsleep && /bin/chmod u+s deepsleep\" user name \"" + name + "\" password \"" + password + "\" with administrator privileges'";
widget.system(command, null);
hideBack();
}
function enterConfig() {
widget.setPreferenceForKey(deepDropDown.options[deepDropDown.selectedIndex].value, "DeepSleepMode");
var regularMode = regularDropDown.options[regularDropDown.selectedIndex].value;
var command = "./deepsleep -bos ";
command += regularMode;
widget.system(command, null);
hideBack();
}
function goSleep() {
var targetMode;
if (!widget.preferenceForKey("DeepSleepMode")) {
targetMode = "soft";
} else {
targetMode = widget.preferenceForKey("DeepSleepMode");
}
var command = "./deepsleep -bm ";
command += targetMode;
var syscmd = widget.system(command, function() {;});
syscmd.onreadoutput = function (output) {fade(2,1,false); sleepMsg=false; fader[2].message[2]=output; fade(2,2,true); debugMsg=true;};
setTimeout(function () {busy = false}, 10000);
}
function activation() {
if (busy) return;
busy = true;
if ( ArePermsOK() ) {
fade(2, 1, true);
sleepMsg = true;
setTimeout(goSleep, 350);
} else {
if (IsFileVaultEnabled()) {
fader[2].message[2] = "Install the widget in root Library folder";
fade(2, 2, true);
debugMsg = true;
} else {
showBackPass();
}
}
}
function info() {
if ( ArePermsOK() ) {
showBackConfig();
} else {
if (IsFileVaultEnabled()) {
fader[2].message[2] = "Install the widget in root Library folder";
fade(2, 2, true);
debugMsg = true;
} else {
showBackPass();
}
}
}
function ArePermsOK() {
var ls = widget.system("/bin/ls -l deepsleep | awk '{print $1 $3}'", null).outputString;
var ownerIsRoot = ls.indexOf("root", 0);
var setuid = ls.charAt(3);
if ( (ownerIsRoot != -1) && (setuid.toLowerCase() == "s") ) return true;
else return false;
}
function IsFileVaultEnabled() {
var fileVault = widget.system("/bin/bash fvault.sh", null).outputString;
if (fileVault.indexOf("protected") != -1) return true;
else return false;
}
function modeToString(mode) {
var str;
switch(mode) {
case 0:
str = "hard";
break
case 1:
str = "soft";
break
case 3:
str = "dump";
break
case 5:
str = "soft";
break
case 7:
str = "dump";
break
default:
str = "hard";
}
return str;
}
function stringToIndex(str) {
var index;
if (str.indexOf("hard") != -1)
index = 0;
if (str.indexOf("dump") != -1)
index = 1;
if (str.indexOf("soft") != -1)
index = 2;
return index;
}
function modeToIndex(mode) {
var index;
switch(mode) {
case 0:
index = 0;
break
case 1:
index = 2;
break
case 3:
index = 1;
break
case 5:
index = 2;
break
case 7:
index = 1;
break
default:
index = 0;
}
return index;
}