source: trunk/third/firefox/config/mac-set-timebomb.pl @ 21695

Revision 21695, 4.7 KB checked in by rbasch, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r21694, which included commits to RCS files with non-trunk default branches.
Line 
1#!/usr/local/bin/perl5 -w
2#
3# The contents of this file are subject to the Netscape Public
4# License Version 1.1 (the "License"); you may not use this file
5# except in compliance with the License. You may obtain a copy of
6# the License at http://www.mozilla.org/NPL/
7#
8# Software distributed under the License is distributed on an "AS
9# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10# implied. See the License for the specific language governing
11# rights and limitations under the License.
12#
13# The Original Code is Mozilla Communicator client code.
14#
15# The Initial Developer of the Original Code is Netscape Communications
16# Corporation.  Portions created by Netscape are
17# Copyright (C) 1998 Netscape Communications Corporation. All
18# Rights Reserved.
19#
20# Contributor(s):
21#
22#  set-timebomb.pl --- set the timebomb to N days from today's date.
23#
24#  Modified: Chris Yeh <cyeh@netscape.com>, 31-Aug-98
25#  Created: Jamie Zawinski <jwz@mozilla.org>, 24-Aug-98.
26
27my $progname = $0;
28my $contents;
29
30# This is the preferences file that gets read and written.
31# So run this script with src/mozilla/build/ as the current directory.
32#
33my $prefs = ":mozilla:modules:libpref:src:init:all.js";
34
35
36# from noah, who should be shot
37sub ctime {
38  local (@weekday, @month, $time, $TZ);
39  local ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
40
41  @weekday = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
42  @month = ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
43            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
44
45  $time = (length (@_) > 0)? $_[0] : time() ;
46  $TZ = defined($ENV{'TZ'}) ? ( $ENV{'TZ'} ? $ENV{'TZ'} : 'UTC' ) : '';
47  ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
48      ($TZ eq 'UTC') ? gmtime($time) : localtime($time);
49  if ($TZ =~ /^([^:\d+\-,]{3,})([+-]?\d{1,2}(:\d{1,2}){0,2})([^\d+\-,]{3,})?/o)
50    {
51      $TZ = $isdst ? $4 : $1;
52    }
53
54  if ($TZ ne "") { $TZ .= " "; }
55  $year += ($year < 70) ? 2000 : 1900;
56  return sprintf ("%s %s %02d %02d:%02d:%02d %s%4d",
57                  $weekday[$wday], $month[$mon], $mday, $hour, $min, $sec,
58                  $TZ, $year);
59}
60
61sub read_file {
62    $contents = "";
63    open(IN, "<$prefs");
64    while (<IN>) {
65        $contents .= $_;
66    }
67    close(IN);
68}
69
70sub write_file {
71    open(OUT, ">$prefs");
72    print OUT $contents;
73    close(OUT);
74#     print STDERR "$progname: wrote $prefs\n";
75}
76
77sub get_pref {
78    my ($lvalue) = @_;
79    $_ = $contents;
80    die ("$lvalue unset?\n") unless m@^config\("$lvalue","(.*)"\);@m;
81    return $1;
82}
83
84sub set_pref {
85    my ($lvalue,$rvalue) = @_;
86    $_ = $contents;
87#    die("$lvalue unset?\n") unless (m@^(pref|config)\("$lvalue",@m);
88    die("$lvalue unset?\n")
89        unless s@^(pref|config)(\("$lvalue",)(.*)(\).*)$@$1$2$rvalue$4@m;
90    $contents = $_;
91}
92
93# no longer used?
94sub set_indirected_bomb {
95    my ($bomb) = @_;
96    my ($secret) = get_pref("timebomb.relative_timebomb_secret_name");
97    set_pref($secret, $bomb);
98}
99
100sub time_diff {
101    use Time::Local;
102    my($time, $result) = @_;
103    my $diff = 2082830400 - (timegm(localtime) - time);
104    return $result =~ /mac/ ?
105        $time + $diff : $time - $diff;
106}
107
108sub set_bomb {
109    my ($warning_days, $bomb_days) = @_;
110
111#    die("warning_days ($warning_days) must be greater than 0.")
112#       unless ($warning_days > 0);
113
114    die("bomb_days ($bomb_days) must be greater than 0.")
115        unless ($bomb_days > 0);
116    die("warning_days ($warning_days) must be less than " .
117        "bomb_days ($bomb_days)\n")
118        unless ($warning_days < $bomb_days);
119
120    my $mactime = time;
121
122#   MacPerl stores date and times from 1904 instead of 1970
123#   Conversion routine thanks to Chris Nandor (pudge@pobox.com)
124
125    $now = time_diff($mactime, 'unix');
126
127    my $bomb = $now + ($bomb_days * 24 * 60 * 60);
128    my $warn = $now + ($warning_days * 24 * 60 * 60);
129
130    set_pref("timebomb.expiration_time", $bomb);
131    set_pref("timebomb.warning_time", $warn);
132    set_pref("timebomb.relative_timebomb_days", -1);
133    set_pref("timebomb.relative_timebomb_warning_days", -1);
134
135    set_indirected_bomb(0);
136
137#     print STDERR sprintf("%s: timebomb goes off in %2d days (%s)\n",
138#                        $progname, $bomb_days, ctime($bomb));
139#     print STDERR sprintf("%s:  warning goes off in %2d days (%s)\n",
140#                        $progname, $warning_days, ctime($warn));
141}
142
143sub main {
144    my ($warning_days, $bomb_days) = @_;
145    die ("usage: $progname [ days-until-warning [ days-until-timebomb ]]\n")
146        if ($#_ >= 2);
147
148    if ($#_ == 0) {
149        $bomb_days = $warning_days;
150        $warning_days = -1;
151    }
152    if (!$bomb_days || $bomb_days <= 0) {
153        $bomb_days = 30;
154    }
155    if ($warning_days < 0) {
156        $warning_days = $bomb_days - int($bomb_days / 3);
157        if ($warning_days < $bomb_days - 10) {
158            $warning_days = $bomb_days - 10;
159        }
160    }
161
162    read_file();
163    set_bomb($warning_days, $bomb_days);
164    write_file();
165    return 0;
166}
167
168exit(&main(@ARGV));
169
170
Note: See TracBrowser for help on using the repository browser.