-
Notifications
You must be signed in to change notification settings - Fork 0
/
ics-spanner.pl
executable file
·165 lines (147 loc) · 4 KB
/
ics-spanner.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
#!/usr/bin/env perl
use strict;
use warnings;
use Encode;
$| = 1;
if (@ARGV < 2 || $ARGV[0] !~ /^[0-9]+$/) {
usage();
}
my $max_filesize_kb = shift @ARGV;
my $return = 0;
my $success = 0;
foreach (@ARGV) {
if (-f $_) {
my $result = span_ics($_);
if ($result == 0) {
$success++;
} else {
$return++;
}
} else {
print "$_ is not a file\n";
$return++;
}
}
print { $return ? *STDERR : *STDOUT } $success + $return . " files processed, $success successful, $return error(s).\n";
exit $return;
############################################
sub write_files {
my $filename = $_[0];
my $cal_ref = $_[1];
(my $outputdirname = $filename) =~ s/\.ics$//;
mkdir $outputdirname;
my $filecount = 0;
my $fh;
local $\ = "\x0d\x0a"; # User CRLFs to join text and write after prints.
my $filehead = encode('UTF-8', join($\, @{ $cal_ref->{"head"} }));
my $filefoot = encode('UTF-8', join($\, @{ $cal_ref->{"foot"} }));
my $file_base_bytes = length($filehead) + length($filefoot);
my $max_file_bytes = $max_filesize_kb * 1024;
#if ($max_filesize_bytes < $filehead_bytes) {
#print "$filename header is " . sprintf("%.2f", $filehead_bytes / 1024) . "KB, too big for output size of $max_filesize_kb" . "KB";
#return 1;
#}
local $, = $\;
my $file_bytes = $file_base_bytes;
my $new_file = 1;
my $newfile = sub {
$filecount++;
open($fh, ">>", $outputdirname . "/" . $filecount . ".ics");
$file_bytes = $file_base_bytes;
};
my @cal_events = @{ $cal_ref->{"events"} };
foreach my $event (@cal_events) {
my $event_octets = encode('UTF-8', join($\, @{ $event }));
$file_bytes += length($event_octets);
if ($new_file == 1) {
if ($file_bytes > $max_file_bytes) {
print "$filename contains events too large for max filesize. Try " . sprintf("%.2f", $file_bytes / 1024) . "KB or larger.";
return 1;
}
&$newfile();
print $fh $filehead, $event_octets;
$new_file = 0;
} elsif ($file_bytes > $max_file_bytes) {
print $fh $filefoot;
close $fh;
&$newfile();
$file_bytes += length($event_octets);
if ($file_bytes > $max_file_bytes) {
print "$filename contains events too large for max filesize. Try " . sprintf("%.2f", $file_bytes / 1024) . "KB or larger.";
return 1;
};
print $fh $filehead, $event_octets;
} else {
print $fh $event_octets;
}
}
print $fh $filefoot;
close $fh;
return 0;
};
sub span_ics {
my $error = 0;
# Hash of Arrays. events is an array of arrays.
my %calendar = (
head => [],
events => [],
foot => [],
);
my $inputfile = $_[0];
my $fh;
local $/ = "\x0d\x0a";
open($fh, "<:encoding(UTF-8)", $inputfile) or die ("File $inputfile not found.\n");
local $\ = "\x0a"; #Set newlines after print
print "Beginning parsing of $inputfile";
my $is = 0; # InputStatus: 0 begin, 1 head, 2 in event, 3 end event, 4 end calendar
my $en = 0; # Event number
while (<$fh>) {
chomp;
if ($is == 0 && $_ =~ /^BEGIN:VCALENDAR$/) {
$is++;
push @{ $calendar{"head"} }, $_;
} elsif ($is == 1) {
if ($_ =~ /^BEGIN:VEVENT$|^BEGIN:VTODO$/) {
$is++;
push @{ $calendar{"events"} }, [ $_ ];
} else {
push @{ $calendar{"head"} }, $_;
}
} elsif ($is == 2) {
if ($_ =~ /^END:VEVENT$|^END:VTODO$/) {
$is++;
}
# pushes onto the array that is at the largest index of events array
push @{ $calendar{"events"}[-1] }, $_;
} elsif ($is == 3) {
if ($_ =~ /^BEGIN:VEVENT$|^BEGIN:VTODO$/) {
$is--;
push @{ $calendar{"events"} }, [ $_ ];
} elsif ($_ =~ /^END:VCALENDAR$/) {
$is++;
push @{ $calendar{"foot"} }, $_;
} else {
print "$.: Something doesn't look right. Exiting.";
$error++;
last;
}
} elsif ($is == 4) {
if ($_ =~ /.+/) {
print "$.: Trailing data for some reason. Exiting.";
$error++;
last;
}
} else {
print "$.: Something went wrong. Unknown input status or invalid file content.";
$error++;
last;
}
}
close $fh;
$error += write_files($inputfile, \%calendar);
return $error;
};
sub usage {
print "Usage: ics-spanner.pl <filesize_in_Kb> <ICS_file(s)>\n";
exit 0;
};