| 1 |
# Directives |
| 2 |
# ( none) |
| 3 |
# Start-up Actions |
| 4 |
|
| 5 |
{ |
| 6 |
my $mibs = "Mail::IMAPClient::BodyStructure"; |
| 7 |
my $subpartCount = 0; |
| 8 |
my $partCount = 0; |
| 9 |
|
| 10 |
sub take_optional_items($$@) |
| 11 |
{ my ($r, $items) = (shift, shift); |
| 12 |
foreach (@_) |
| 13 |
{ my $opt = $_ .'(?)'; |
| 14 |
exists $items->{$opt} or next; |
| 15 |
$r->{$_} = UNIVERSAL::isa($items->{$opt}, 'ARRAY') |
| 16 |
? $items->{$opt}[0] : $items->{$opt}; |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
sub merge_hash($$) |
| 21 |
{ my $to = shift; |
| 22 |
my $from = shift or return; |
| 23 |
while( my($k,$v) = each %$from) { $to->{$k} = $v } |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
# Atoms |
| 28 |
|
| 29 |
TEXT: /^"TEXT"|^TEXT/i { $return = "TEXT" } |
| 30 |
PLAIN: /^"PLAIN"|^PLAIN/i { $return = "PLAIN" } |
| 31 |
HTML: /"HTML"|HTML/i { $return = "HTML" } |
| 32 |
MESSAGE: /^"MESSAGE"|^MESSAGE/i { $return = "MESSAGE"} |
| 33 |
RFC822: /^"RFC822"|^RFC822/i { $return = "RFC822" } |
| 34 |
NIL: /^NIL/i { $return = "NIL" } |
| 35 |
RFCNONCOMPLY: /^\(\)/i { $return = "NIL" } |
| 36 |
NUMBER: /^(\d+)/ { $return = $item[1] } |
| 37 |
|
| 38 |
# Strings: |
| 39 |
|
| 40 |
SINGLE_QUOTED_STRING: "'" /(?:\\['\\]|[^'])*/ "'" { $return = $item{__PATTERN1__} } |
| 41 |
DOUBLE_QUOTED_STRING: '"' /(?:\\["\\]|[^"])*/ '"' { $return = $item{__PATTERN1__} } |
| 42 |
|
| 43 |
BARESTRING: ...!/^[)('"]/ /^(?!\(|\))(?:\\ |\S)+/ |
| 44 |
{ $return = $item{__PATTERN1__} } |
| 45 |
|
| 46 |
STRING: DOUBLE_QUOTED_STRING | SINGLE_QUOTED_STRING | BARESTRING |
| 47 |
|
| 48 |
STRINGS: "(" STRING(s) ")" { $return = $item{'STRING(s)'} } |
| 49 |
|
| 50 |
textlines: NIL | NUMBER |
| 51 |
|
| 52 |
rfc822message: MESSAGE RFC822 { $return = "MESSAGE RFC822" } |
| 53 |
|
| 54 |
bodysubtype: PLAIN | HTML | NIL | STRING |
| 55 |
|
| 56 |
key: STRING |
| 57 |
value: NIL | NUMBER | STRING | KVPAIRS |
| 58 |
|
| 59 |
kvpair: ...!")" key value |
| 60 |
{ $return = { $item{key} => $item{value} } } |
| 61 |
|
| 62 |
KVPAIRS: "(" kvpair(s) ")" |
| 63 |
{ $return = { map { (%$_) } @{$item{'kvpair(s)'}} } } |
| 64 |
|
| 65 |
bodytype: STRING |
| 66 |
bodyparms: NIL | KVPAIRS |
| 67 |
bodydisp: NIL | KVPAIRS |
| 68 |
bodyid: ...!/[()]/ NIL | STRING |
| 69 |
bodydesc: ...!/[()]/ NIL | STRING |
| 70 |
bodysize: ...!/[()]/ NIL | NUMBER |
| 71 |
bodyenc: NIL | STRING | KVPAIRS |
| 72 |
bodyMD5: NIL | STRING |
| 73 |
bodylang: NIL | STRING | STRINGS |
| 74 |
bodyextra: NIL | STRING | STRINGS |
| 75 |
bodyloc: NIL | STRING |
| 76 |
bodyextensions: NIL | STRING |
| 77 |
|
| 78 |
personalname: NIL | STRING |
| 79 |
sourceroute: NIL | STRING |
| 80 |
mailboxname: NIL | STRING |
| 81 |
hostname: NIL | STRING |
| 82 |
|
| 83 |
addressstruct: "(" personalname sourceroute mailboxname hostname ")" |
| 84 |
{ bless { personalname => $item{personalname} |
| 85 |
, sourceroute => $item{sourceroute} |
| 86 |
, mailboxname => $item{mailboxname} |
| 87 |
, hostname => $item{hostname} |
| 88 |
}, 'Mail::IMAPClient::BodyStructure::Address'; |
| 89 |
} |
| 90 |
|
| 91 |
subject: NIL | STRING |
| 92 |
inreplyto: NIL | STRING |
| 93 |
messageid: NIL | STRING |
| 94 |
date: NIL | STRING |
| 95 |
|
| 96 |
ADDRESSES: NIL | RFCNONCOMPLY |
| 97 |
| "(" addressstruct(s) ")" { $return = $item{'addressstruct(s)'} } |
| 98 |
|
| 99 |
cc: ADDRESSES |
| 100 |
bcc: ADDRESSES |
| 101 |
from: ADDRESSES |
| 102 |
replyto: ADDRESSES |
| 103 |
sender: ADDRESSES |
| 104 |
to: ADDRESSES |
| 105 |
|
| 106 |
envelopestruct: "(" date subject from sender replyto to cc |
| 107 |
bcc inreplyto messageid ")" |
| 108 |
{ $return = bless {}, "Mail::IMAPClient::BodyStructure::Envelope"; |
| 109 |
$return->{$_} = $item{$_} |
| 110 |
for qw/date subject from sender replyto to cc/ |
| 111 |
, qw/bcc inreplyto messageid/; |
| 112 |
1; |
| 113 |
} |
| 114 |
|
| 115 |
basicfields: bodysubtype bodyparms(?) bodyid(?) |
| 116 |
bodydesc(?) bodyenc(?) bodysize(?) |
| 117 |
{ $return = { bodysubtype => $item{bodysubtype} }; |
| 118 |
take_optional_items($return, \%item, |
| 119 |
qw/bodyparms bodyid bodydesc bodyenc bodysize/); |
| 120 |
1; |
| 121 |
} |
| 122 |
|
| 123 |
textmessage: TEXT <commit> basicfields textlines(?) bodyMD5(?) |
| 124 |
bodydisp(?) bodylang(?) bodyextra(?) bodyextensions(s?) |
| 125 |
{ |
| 126 |
$return = $item{basicfields} || {}; |
| 127 |
$return->{bodytype} = 'TEXT'; |
| 128 |
take_optional_items($return, \%item |
| 129 |
, qw/textlines bodyMD5 bodydisp bodylang bodyextra/); |
| 130 |
1; |
| 131 |
} |
| 132 |
|
| 133 |
othertypemessage: bodytype basicfields bodyMD5(?) bodydisp(?) |
| 134 |
bodylang(?) bodyextra(?) |
| 135 |
{ $return = { bodytype => $item{bodytype} }; |
| 136 |
take_optional_items($return, \%item |
| 137 |
, qw/bodyMD5 bodydisp bodylang bodyextra/ ); |
| 138 |
merge_hash($return, $item{basicfields}); |
| 139 |
1; |
| 140 |
} |
| 141 |
|
| 142 |
nestedmessage: rfc822message <commit> bodyparms bodyid bodydesc bodyenc |
| 143 |
# bodysize envelopestruct bodystructure textlines |
| 144 |
bodysize envelopestruct(?) bodystructure(?) textlines(?) |
| 145 |
bodyMD5(?) bodydisp(?) bodylang(?) bodyextra(?) |
| 146 |
{ |
| 147 |
$return = {}; |
| 148 |
$return->{$_} = $item{$_} |
| 149 |
for qw/bodyparms bodyid bodydesc bodyenc bodysize/; |
| 150 |
# envelopestruct bodystructure textlines/; |
| 151 |
|
| 152 |
take_optional_items($return, \%item |
| 153 |
, qw/envelopestruct bodystructure textlines/ |
| 154 |
, qw/bodyMD5 bodydisp bodylang bodyextra/); |
| 155 |
|
| 156 |
merge_hash($return, $item{bodystructure}[0]); |
| 157 |
merge_hash($return, $item{basicfields}); |
| 158 |
$return->{bodytype} = "MESSAGE" ; |
| 159 |
$return->{bodysubtype} = "RFC822" ; |
| 160 |
1; |
| 161 |
} |
| 162 |
|
| 163 |
multipart: subpart(s) <commit> bodysubtype |
| 164 |
bodyparms(?) bodydisp(?) bodylang(?) bodyloc(?) bodyextra(?) bodyextensions(s?) |
| 165 |
<defer: $subpartCount = 0> |
| 166 |
{ $return = |
| 167 |
{ bodysubtype => $item{bodysubtype} |
| 168 |
, bodytype => 'MULTIPART' |
| 169 |
, bodystructure => $item{'subpart(s)'} |
| 170 |
}; |
| 171 |
take_optional_items($return, \%item |
| 172 |
, qw/bodyparms bodydisp bodylang bodyloc bodyextra/); |
| 173 |
1; |
| 174 |
} |
| 175 |
|
| 176 |
subpart: "(" part ")" {$return = $item{part}} <defer: ++$subpartCount;> |
| 177 |
|
| 178 |
part: multipart { $return = bless $item{multipart}, $mibs } |
| 179 |
| textmessage { $return = bless $item{textmessage}, $mibs } |
| 180 |
| nestedmessage { $return = bless $item{nestedmessage}, $mibs } |
| 181 |
| othertypemessage { $return = bless $item{othertypemessage}, $mibs } |
| 182 |
|
| 183 |
bodystructure: "(" part(s) ")" |
| 184 |
{ $return = $item{'part(s)'} } |
| 185 |
|
| 186 |
start: /.*?\(.*?BODYSTRUCTURE \(/i part(1) /\).*\)\r?\n?/ |
| 187 |
{ $return = $item{'part(1)'}[0] } |
| 188 |
|
| 189 |
envelope: /.*?\(.*?ENVELOPE/ envelopestruct /.*\)/ |
| 190 |
{ $return = $item{envelopestruct} } |