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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
|
#-*- perl -*-
#
# Copyright (C) 2004,2005,2006 Ken'ichi Fukamachi
# All rights reserved. This program is free software; you can
# redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: Calendar.pm,v 1.4 2006/07/09 12:11:12 fukachan Exp $
#
package FML::Demo::Calendar;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;
=head1 NAME
FML::Demo::Calendar - show a calendar (demonstration module).
=head1 SYNOPSIS
use FML::Demo::Calendar;
my $schedule = new FML::Demo::Calendar;
$schedule->parse;
# show table by w3m :-)
my $tmp = $schedule->tmpfilepath;
my $fh = new FileHandle $tmp, "w";
if (defined $wh) {
$schedule->print($fh);
$fh->close;
}
system "w3m -dump $tmp";
unlink $tmp if -f $tmp;
=head1 DESCRIPTION
C<CAUTION:> This module is created just for a demonstration to show
how to write a module intended for your personal use. This module is
not enough mature nor secure.
This is also a demonstration module to show how to use and build up
modules to couple with CPAN and FML modules. For exaple, this routine
uses C<HTML::CalendarMonthSimple> under cpan/lib.
It parses files in ~/.schedule/ and output the schedule of this month
as HTML TABLE by default. To see it, you need a WWW browser
e.g. "w3m".
=head1 FILES in ~/.schedule/
Theare are arbitrary number of files. This module treis to parse all
files here and use only valid entries found in them.
The file format follows:
# comment: the format is /^(\d+\/\d+)\s+(.*)/ or /^(*\/\d+)\s+(.*)/
DATE CONTENT
DATE CONTENT FORMAT IS ARBITORARY
where null lines or space lines are ignored.
# the first day!
01/01 shougatu yasumi
# 20 of each month
*/20 doctor.
=head1 METHODS
=head2 new($args)
Constructor.
It speculates C<user> by $args->{ user } or $ENV{'USER'} or UID
and determines the path for ~user/.schedule/.
C<$args> can take the following variables:
$args = {
schedule_dir => DIR,
schedule_file => FILE,
mode => MODE,
};
C<CAUTION:>
The string for ~user is restricted to ^[-\w\d\.\/_]+$.
PATH is reset at the last of new() method.
=cut
# Descriptions: constructor. $args is optional, passed via CGI.pm
# if fmlsci.cgi uses.
# OR
# libexec/loaders's $args if fmlsch uses.
# Arguments: OBJ($self) HASH_REF($args)
# Side Effects: none
# Return Value: OBJ
sub new
{
my ($self, $args) = @_;
my ($type) = ref($self) || $self;
my $me = {};
my $user = $args->{ user } || $ENV{'USER'};
# default directory holding schdule file(s): ~/.schedule/ by default.
use User::pwent;
unless (defined $user) {
my $p = getpwuid($<);
$user = $p->name;
}
my $pw = getpwnam($user);
my $home_dir = $pw->dir;
# simple check (not enough mature).
# This code is not for security but to avoid -T (taint mode) error ;)
use FML::Restriction::Base;
my $safe = new FML::Restriction::Base;
unless ($safe->regexp_match('fullpath', $home_dir)) {
croak("invalid home directory string");
}
# search files under ~/.schedule/ by default.
use File::Spec;
$me->{ _user } = $user;
$me->{ _schedule_dir } = File::Spec->catfile($home_dir, ".schedule");
$me->{ _schedule_file } = undef;
# import value from $args if specified.
for my $key ('schedule_dir', 'schedule_file', 'mode') {
if (defined $args->{ $key }) {
$me->{ "_$key" } = $args->{ $key };
}
}
# reset PATH to execute w3m.
$ENV{'PATH'} = '/bin/:/usr/bin:/usr/pkg/bin:/usr/local/bin';
return bless $me, $type;
}
=head2 tmpfilepath($args)
return a tmpfile path name, which is under ~/.schedule directory.
It creates just a file path not file itself.
=cut
# Descriptions: determine a template file location.
# Arguments: OBJ($self) HASH_REF($args)
# Side Effects: none
# Return Value: STR(filename)
sub tmpfilepath
{
my ($self, $args) = @_;
my $user = $self->{ _user };
my $dir = $self->{ _schedule_dir };
my $tmpdir = undef;
if (-w $dir) {
$tmpdir = $dir;
}
else {
croak("$dir not exists\n") unless -d $dir;
croak("$dir not writable\n") unless -w $dir;
}
# XXX we should not create a temporary file in the public area
# XXX such as /tmp/, so create it in ~/.schedule/.
if (defined $tmpdir) {
use File::Spec;
$self->{ _tmpfile } = File::Spec->catfile($tmpdir, ".tmp.$$.html");
return $self->{ _tmpfile };
}
else {
croak("cannot determine \$tmpdir");
}
}
=head2 parse($args)
Parse files in ~/.schedule/ or the specified schedule file.
=cut
# Descriptions: parse file(s).
# Arguments: OBJ($self) HASH_REF($args)
# Side Effects: update calendar entries in $self object
# (actually by _add_entry() calleded here)
# Return Value: none
sub parse
{
my ($self, $args) = @_;
my ($sec,$min,$hour,$mday,$month,$year,$wday) = localtime(time);
# get the date to show
$year = $args->{ year } || (1900 + $year);
$month = $args->{ month } || ($month + 1);
# schedule file
my $data_dir = $self->{ _schedule_dir };
my $data_file = $self->{ _schedule_file };
# pick up line matching this pattern
my @pat = (
sprintf("^%04d%02d(\\d{1,2})\\s+(.*)", $year, $month),
sprintf("^%04d/%02d/(\\d{1,2})\\s+(.*)", $year, $month),
sprintf("^%04d/%d/(\\d{1,2})\\s+(.*)", $year, $month),
sprintf("^%02d(\\d{1,2})\\s+(.*)", $month),
sprintf("^%02d/(\\d{1,2})\\s+(.*)", $month),
sprintf("^\\\*/(\\d{1,2})\\s+(.*)"),
);
if ($data_file && -f $data_file) {
$self->_analyze($year, $month, $data_file, \@pat);
}
elsif (-d $data_dir) {
$self->_analyze_dir($year, $month, $data_dir, \@pat);
}
else {
croak("invalid data");
}
}
# Descriptions: initialize Calendar object.
# Arguments: OBJ($self) STR($year) STR($month)
# Side Effects: none
# Return Value: none
sub _init_calendar
{
my ($self, $year, $month) = @_;
use HTML::CalendarMonthSimple;
my $cal = new HTML::CalendarMonthSimple('year'=> $year, 'month'=> $month);
if (defined $cal) {
$self->{ _calendar } = $cal;
}
else {
croak("cannot create Calendar object");
}
$cal->width('70%');
$cal->border(10);
$cal->header(sprintf("%04d/%02d %s", $year, $month, "schedule"));
$cal->bgcolor('pink');
}
# Descriptions: parse the specified file.
# Arguments: OBJ($self)
# STR($year) STR($month) STR($file) ARRAY_REF($pattern)
# Side Effects: none
# Return Value: none
sub _analyze_file
{
my ($self, $year, $month, $file, $pattern) = @_;
# initialize year+month dependent Calendar object
# since _analyze() adds matched data into this Calendar object.
$self->_init_calendar($year, $month);
$self->_analyze($file, $pattern);
}
# Descriptions: parse the specified files in the directory.
# Arguments: OBJ($self)
# STR($year) STR($month) STR($data_dir) ARRAY_REF($pattern)
# Side Effects: none
# Return Value: none
sub _analyze_dir
{
my ($self, $year, $month, $data_dir, $pattern) = @_;
# initialize year+month dependent Calendar object
# since _analyze() adds matched data into this Calendar object.
$self->_init_calendar($year, $month);
use DirHandle;
my $dh = new DirHandle $data_dir;
if (defined $dh) {
my $xdir;
DIR:
while (defined($xdir = $dh->read)) {
next DIR if $xdir =~ /~$/o;
next DIR if $xdir =~ /^\./o;
use File::Spec;
my $schedule_file = File::Spec->catfile($data_dir, $xdir);
if (-f $schedule_file) {
$self->_analyze($schedule_file, $pattern);
}
}
}
}
# Descriptions: open, read specified $file and
# analyze the line which matches $pattern.
# Arguments: OBJ($self) STR($file) STR($pattern)
# Side Effects: update $self object by _add_entry()
# Return Value: none
sub _analyze
{
my ($self, $file, $pattern) = @_;
# ignore if the file not exists.
return unless -f $file;
use FileHandle;
my $fh = new FileHandle $file;
if (defined $fh) {
my $buf;
LINE:
while ($buf = <$fh>) {
for my $pat (@$pattern) {
# XXX $pat is /($re_date)(.*)/.
if ($buf =~ /$pat/) {
$self->_add_entry($1, $2);
next LINE;
}
}
# for example, "*/24 something"
if (0 && $buf =~ /^\*\/(\d+)\s+(.*)/) {
$self->_add_entry($1, $2);
}
}
$fh->close();
}
}
# Descriptions: add calendar entry to $self object.
# Arguments: OBJ($self) STR($day) STR($buf)
# Side Effects: update $self object
# Return Value: none
sub _add_entry
{
my ($self, $day, $buf) = @_;
my $cal = $self->{ _calendar };
$day =~ s/^0//;
if (defined $day && defined $buf) {
print STDERR "day=$day buf=$buf\n" if 0;
$cal->addcontent($day, "<p>$buf");
}
}
=head2 print_as_html($fd)
print out the result as HTML.
You can specify the output channel by file descriptor C<$fd>.
=cut
# Descriptions: print Calendar by HTML::CalendarMonthSimple::as_HTML()
# method.
# Arguments: OBJ($self) HANDLE($fd)
# Side Effects: none
# Return Value: none
sub print_as_html
{
my ($self, $fd) = @_;
if (defined $self->{ _calendar }) {
$fd = defined $fd ? $fd : \*STDOUT;
print $fd $self->{ _calendar }->as_HTML;
}
else {
croak("undefined schedule object");
}
}
=head2 print_specific_month($fh, $month, $year)
print range specified by C<$month> and C<$year>.
C<$month> is a number or string among C<this>, C<next> and C<last>.
=cut
# Descriptions: print Calendar for specific month as HTML.
# Arguments: OBJ($self) HANDLE($fh) STR($month) [STR($year)]
# Side Effects: none
# Return Value: none
sub print_specific_month
{
my ($self, $fh, $month, $year) = @_;
my ($month_now, $year_now) = (localtime(time))[4,5];
my $default_year = 1900 + $year_now;
my $default_month = $month_now + 1;
my ($thismonth, $thisyear) = ($default_month, $default_year);
if ($month =~ /^\d+$/) {
$thismonth = $month;
$thisyear = $year if defined $year;
}
else {
# XXX-TODO: fix $thisyear ?
if ($default_month == 1) {
$thismonth = 2 if $month eq 'next';
$thismonth = 12 if $month eq 'last';
}
elsif ($default_month == 12) {
$thismonth = 1 if $month eq 'next';
$thismonth = 11 if $month eq 'last';
}
else {
$thismonth++ if $month eq 'next';
$thismonth-- if $month eq 'last';
}
}
print $fh "<A NAME=\"$month\">\n";
$self->parse( { month => $thismonth, year => $thisyear } );
# show overview if this month.
if ($thismonth == $default_month) {
print $fh "<pre>\n";
$self->_print_specific_day($fh, time);
$self->_print_specific_day($fh, time + 24*3600);
$self->_print_specific_day($fh, time + 48*3600);
print $fh "</pre>\n";
}
# calendar style
$self->print_as_html($fh);
}
# Descriptions: print schedule at the day specified by unix time $time.
# Arguments: OBJ($self) HANDLE($fh) NUM($time)
# Side Effects: none
# Return Value: none
sub _print_specific_day
{
my ($self, $fh, $time) = @_;
my $cal = $self->{ _calendar };
my ($sec,$min,$hour,$mday,$month,$year,$wday) = localtime($time);
my $buf = $cal->getcontent($mday) || '';
$buf =~ s/^\s*//;
$buf =~ s/<p>//;
$buf =~ s/<p>/,/g;
printf $fh "%02d: %s\n", $mday, $buf;
}
=head1 MODE
=head2 get_mode( )
show mode (string).
=head2 set_mode( $mode )
override mode.
The mode is either of 'text' or 'html'.
XXX: The mode is not used in this module itsef.
This is a pragma for other module use.
=cut
# Descriptions: show the current $mode.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: STR or undef
sub get_mode
{
my ($self) = @_;
return (defined $self->{ _mode } ? $self->{ _mode } : undef);
}
# Descriptions: override $mode.
# Arguments: OBJ($self) STR($mode)
# Side Effects: update $self object
# Return Value: STR
sub set_mode
{
my ($self, $mode) = @_;
if (defined $mode) {
$self->{ _mode } = $mode;
}
else {
$self->{ _mode } = undef;
}
}
=head1 CODING STYLE
See C<http://www.fml.org/software/FNF/> on fml coding style guide.
=head1 AUTHOR
Ken'chi Fukamachi
=head1 COPYRIGHT
Copyright (C) 2004,2005,2006 Ken'chi Fukamachi
All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.
=head1 HISTORY
FML::Demo::Calendar first appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.
Firstly this module name is C<TinyScheduler.pm> and renamed to
Calendar::Lite later. In 2004, it is renamed to FML::Demo::Calendar
again since this module must depend FML::* classes.
=cut
1;
|