File: HeaderFile.pm

package info (click to toggle)
cyrus-imapd 3.6.1-4%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,688 kB
  • sloc: ansic: 255,928; perl: 97,730; javascript: 9,266; sh: 5,537; yacc: 2,651; cpp: 2,128; makefile: 2,099; lex: 660; xml: 621; python: 388; awk: 303; asm: 262
file content (232 lines) | stat: -rwxr-xr-x 4,635 bytes parent folder | download | duplicates (8)
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
#!/usr/bin/perl -c

# Package to handle Cyrus Header files

package Cyrus::HeaderFile;

use strict;
use warnings;

use IO::File;
use IO::File::fcntl;
use IO::Handle;
use File::Temp;
use Data::Dumper;

use Cyrus::DList;

=pod

=head1 NAME

Cyrus::HeaderFile - A pure perl interface to the "cyrus.header" file
format as generated by Cyrus IMAPd.

=head1 EXAMPLES

Like Cyrus::IndexFile, uses fcntl locking (default for Cyrus on systems
which support it)

my $header = Cyrus::HeaderFile->new_file("path/to/cyrus.header");

XXX: see index_uids.pl

=cut

our $HL1 = qq{\241\002\213\015Cyrus mailbox header};
our $HL2 = qq{"The best thing about this system was that it had lots of goals."};
our $HL3 = qq{\t--Jim Morris on Andrew};

=head1 PUBLIC API

=over

=item Cyrus::HeaderFile->new($fh)

Read the header file in $fh

=cut

sub new {
  my $class = shift;
  my $handle = shift;

  # read header
  local $/ = undef;
  my $body = <$handle>;

  my $Self = bless {}, ref($class) || $class;
  $Self->{handle} = $handle; # keep for locking
  $Self->{rawheader} = $body;
  $Self->{header} = $Self->parse_header($body);

  return $Self;
}

=item Cyrus::HeaderFile->new_file($fname, $lockopts)

Open the file to read, optionally locking it with IO::File::fcntl.  If you
pass a scalar for lockopts then it will be locked with ['lock_ex'], otherwise
you can pass a tuple, e.g. ['lock_ex', 5] for a 5 second timeout.

This function will die if it can't open or lock the file.  On success, it
calls $class->new() with the filehandle.

=cut

sub new_file {
  my $class = shift;
  my $file = shift;
  my $lockopts = shift;

  my $fh;
  if ($lockopts) {
    $lockopts = ['lock_ex'] unless ref($lockopts) eq 'ARRAY';
    $fh = IO::File::fcntl->new($file, '+<', @$lockopts)
          || die "Can't open $file for locked read: $!";
  } else {
    $fh = IO::File->new("< $file")
          || die "Can't open $file for read: $!";
  }

  return $class->new($fh);
}

=item $header->header([$Field])

Return the entire header as a hash, or individual named field.

=cut

sub header {
  my $Self = shift;
  my $Field = shift;

  if ($Field) {
    return $Self->{header}{$Field};
  }

  return $Self->{header};
}

=item $header->write_header($fh, $headerData)

Write a header file with the data (e.g. returned from ->header())
to the given filehandle.

=cut

sub write_header {
  my $Self = shift;
  my $fh = shift;
  my $header = shift || $Self->header();

  $fh->print($Self->make_header($header));
}

# XXX still writes old-style header!
sub make_header {
  my $Self = shift;
  my $ds = shift || $Self->header();

  # NOTE: no tab separator if no uniqueid!
  my $qr_uuid = $ds->{QuotaRoot};
  $qr_uuid .= "\t$ds->{UniqueId}" if $ds->{UniqueId};

  # NOTE: acl and flags should have '' as the last element!
  my $flags = join(" ", @{$ds->{Flags}}, '');
  my $acl = join("\t", @{$ds->{ACL}}, '');

  my $buf = <<EOF;
$HL1
$HL2
$HL3
$qr_uuid
$flags
$acl
EOF
  return $buf;
}

=item $header->write_newheader($fh, $headerData)

Write a new-style header file with the data (e.g. returned from ->{dlistheader})
to the given filehandle.

=cut

sub write_newheader {
  my $Self = shift;
  my $fh = shift;
  my $dl = shift || $Self->{dlistheader};

  my $buf = <<EOF;
$HL1
$HL2
$HL3
$dl
EOF

  $fh->print($buf);
}

sub parse_header {
  my $Self = shift;
  my $body = shift;

  my @lines = split /\n/, $body;

  die "Not a mailbox header file" unless $lines[0] eq $HL1;
  die "Not a mailbox header file" unless $lines[1] eq $HL2;
  die "Not a mailbox header file" unless $lines[2] eq $HL3;

  my ($quotaroot, $uniqueid, @flags, @acl);

  if (substr($lines[3], 0, 1) eq '%') {
    # new dlist-style header
    $Self->{dlistheader} = $lines[3];
    my $dlist = Cyrus::DList->parse_string($lines[3]);
    my $hash = $dlist->as_perl();

    $quotaroot = $hash->{Q} // '';
    $uniqueid = $hash->{I};
    @flags = @{$hash->{U}} if ref $hash->{U};
    if (ref $hash->{A}) {
        my $order = delete $hash->{A}->{__kvlist_order};

        if ($order && ref $order eq 'ARRAY') {
            foreach my $k (@{$order}) {
                my $v = delete $hash->{A}->{$k};
                push @acl, $k, $v;
            }
        }

        push @acl, %{$hash->{A}};
    }
  }
  else {
    ($quotaroot, $uniqueid) = split /\t/, $lines[3];
    @flags = split / /, $lines[4];
    @acl = split /\t/, $lines[5];
  }

  return {
    QuotaRoot => $quotaroot,
    UniqueId => $uniqueid,
    Flags => \@flags,
    ACL => \@acl,
  };
}

=back

=head1 AUTHOR AND COPYRIGHT

Bron Gondwana <brong@fastmail.fm> - Copyright 2008 FastMail

Licenced under the same terms as Cyrus IMAPd.

=cut


1;