forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmac-set-timebomb.pl
170 lines (141 loc) · 4.74 KB
/
mac-set-timebomb.pl
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
#!/usr/local/bin/perl5 -w
#
# The contents of this file are subject to the Netscape Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/NPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is Mozilla Communicator client code.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s):
#
# set-timebomb.pl --- set the timebomb to N days from today's date.
#
# Modified: Chris Yeh <[email protected]>, 31-Aug-98
# Created: Jamie Zawinski <[email protected]>, 24-Aug-98.
my $progname = $0;
my $contents;
# This is the preferences file that gets read and written.
# So run this script with src/mozilla/build/ as the current directory.
#
my $prefs = ":mozilla:modules:libpref:src:init:all.js";
# from noah, who should be shot
sub ctime {
local (@weekday, @month, $time, $TZ);
local ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
@weekday = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
@month = ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
$time = (length (@_) > 0)? $_[0] : time() ;
$TZ = defined($ENV{'TZ'}) ? ( $ENV{'TZ'} ? $ENV{'TZ'} : 'UTC' ) : '';
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
($TZ eq 'UTC') ? gmtime($time) : localtime($time);
if ($TZ =~ /^([^:\d+\-,]{3,})([+-]?\d{1,2}(:\d{1,2}){0,2})([^\d+\-,]{3,})?/o)
{
$TZ = $isdst ? $4 : $1;
}
if ($TZ ne "") { $TZ .= " "; }
$year += ($year < 70) ? 2000 : 1900;
return sprintf ("%s %s %02d %02d:%02d:%02d %s%4d",
$weekday[$wday], $month[$mon], $mday, $hour, $min, $sec,
$TZ, $year);
}
sub read_file {
$contents = "";
open(IN, "<$prefs");
while (<IN>) {
$contents .= $_;
}
close(IN);
}
sub write_file {
open(OUT, ">$prefs");
print OUT $contents;
close(OUT);
# print STDERR "$progname: wrote $prefs\n";
}
sub get_pref {
my ($lvalue) = @_;
$_ = $contents;
die ("$lvalue unset?\n") unless m@^config\("$lvalue","(.*)"\);@m;
return $1;
}
sub set_pref {
my ($lvalue,$rvalue) = @_;
$_ = $contents;
# die("$lvalue unset?\n") unless (m@^(pref|config)\("$lvalue",@m);
die("$lvalue unset?\n")
unless s@^(pref|config)(\("$lvalue",)(.*)(\).*)$@$1$2$rvalue$4@m;
$contents = $_;
}
# no longer used?
sub set_indirected_bomb {
my ($bomb) = @_;
my ($secret) = get_pref("timebomb.relative_timebomb_secret_name");
set_pref($secret, $bomb);
}
sub time_diff {
use Time::Local;
my($time, $result) = @_;
my $diff = 2082830400 - (timegm(localtime) - time);
return $result =~ /mac/ ?
$time + $diff : $time - $diff;
}
sub set_bomb {
my ($warning_days, $bomb_days) = @_;
# die("warning_days ($warning_days) must be greater than 0.")
# unless ($warning_days > 0);
die("bomb_days ($bomb_days) must be greater than 0.")
unless ($bomb_days > 0);
die("warning_days ($warning_days) must be less than " .
"bomb_days ($bomb_days)\n")
unless ($warning_days < $bomb_days);
my $mactime = time;
# MacPerl stores date and times from 1904 instead of 1970
# Conversion routine thanks to Chris Nandor ([email protected])
$now = time_diff($mactime, 'unix');
my $bomb = $now + ($bomb_days * 24 * 60 * 60);
my $warn = $now + ($warning_days * 24 * 60 * 60);
set_pref("timebomb.expiration_time", $bomb);
set_pref("timebomb.warning_time", $warn);
set_pref("timebomb.relative_timebomb_days", -1);
set_pref("timebomb.relative_timebomb_warning_days", -1);
set_indirected_bomb(0);
# print STDERR sprintf("%s: timebomb goes off in %2d days (%s)\n",
# $progname, $bomb_days, ctime($bomb));
# print STDERR sprintf("%s: warning goes off in %2d days (%s)\n",
# $progname, $warning_days, ctime($warn));
}
sub main {
my ($warning_days, $bomb_days) = @_;
die ("usage: $progname [ days-until-warning [ days-until-timebomb ]]\n")
if ($#_ >= 2);
if ($#_ == 0) {
$bomb_days = $warning_days;
$warning_days = -1;
}
if (!$bomb_days || $bomb_days <= 0) {
$bomb_days = 30;
}
if ($warning_days < 0) {
$warning_days = $bomb_days - int($bomb_days / 3);
if ($warning_days < $bomb_days - 10) {
$warning_days = $bomb_days - 10;
}
}
read_file();
set_bomb($warning_days, $bomb_days);
write_file();
return 0;
}
exit(&main(@ARGV));