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
|
#-*- perl -*-
#
# Copyright (C) 2001 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.
#
# $Id$
# $FML$
#
package MailingList::Messages;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK);
use Carp;
require Exporter;
@ISA = qw(Exporter);
sub new
{
my ($self, $args) = @_;
my ($type) = ref($self) || $self;
my $me = {};
if ($args) { create($me, $args);}
return bless $me, $type;
}
######################################################################
=head1 NAME
MailingList::Messages -- message manipulators
=head1 SYNOPSIS
my $m1 = new MailingList::Messages { content => \$body1 };
my $m2 = new MailingList::Messages;
$m2->create( { content => \$body2 });
# make a chain of $m1, $m2, ...
$m1->chain( $m2 );
# print the contents in the order: $m1, $m2, ...
$m1->print;
=head1 DESCRIPTION
A message has the content and a header including the next message
pointer, et. al. Out idea is similar to IPv6.
Message Format
%message = {
version => 1.0
content_type => message/rfc822
next => \%next_message
prev => \%prev_message
header => {
field_name => field_value
}
content => \$message_body
}
=item Function()
=cut
sub create
{
my ($self, $args) = @_;
$self->{ version } = $args->{ version } || 1.0;
$self->{ type } = $args->{ type } || 'message/rfc822';
$self->{ next } = $args->{ next } || undef;
$self->{ prev } = $args->{ prev } || undef;
$self->{ header } = $args->{ header } || undef;
$self->{ content } = $args->{ content } || '';
}
sub next_chain
{
my ($self, $ref_next_message) = @_;
$self->{ next } = $ref_next_message;
}
sub prev_chain
{
my ($self, $ref_prev_message) = @_;
$self->{ prev } = $ref_prev_message;
}
sub print
{
my ($self, $fd) = @_;
my $msg = $self;
# if $fd is not given, we use STDOUT.
unless (defined $fd) { $fd = \*STDOUT;}
MSG:
while (1) {
$self->_print($fd, $msg->{ content });
last MSG unless $msg->{ next };
$msg = $self->{ next };
}
}
# Descriptions: send the body part of the message to socket
# replace "\n" in the end of line with "\r\n" on memory.
# We should do it to use as less memory as possible.
# So we use substr() to process each line.
# Arguments: $self $socket $ref_to_body
# Side Effects: none
# Return Value: none
sub _print
{
my ($self, $fd, $r_body) = @_;
my $pp = 0;
my $maxlen = length($$r_body);
my $logfp = $self->{ _log_function };
$logfp = ref($logfp) eq 'CODE' ? $logfp : undef;
# write each line in buffer
my ($p, $len, $buf, $pbuf);
SMTP_IO:
while (1) {
$p = index($$r_body, "\n", $pp);
$len = $p - $pp + 1;
$buf = substr($$r_body, $pp, ($p < 0 ? $maxlen-$pp : $len));
if ($buf !~ /\r\n$/) { $buf =~ s/\n$/\r\n/;}
# ^. -> ..
$buf =~ s/^\./../;
print $fd $buf;
&$logfp($buf) if $logfp;
last SMTP_IO if $p < 0;
$pp = $p + 1;
}
}
sub size
{
my ($self) = @_;
my $c = $self->{ content };
length($$c);
}
sub is_empty
{
my ($self) = @_;
my $size = $self->size;
my $c = $self->{ content };
if ($size == 0) { return 1;}
if ($size <= 8) {
if ($$c =~ /^\s*$/) { return 1;}
}
# false
return 0;
}
sub set_log_function
{
my ($self, $fp) = @_;
$self->{ _log_function } = $fp; # log function pointer
}
######################################################################
=head1 AUTHOR
Ken'ichi Fukamachi
=head1 COPYRIGHT
Copyright (C) 2001 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.
=head1 HISTORY
MailingList::Messages.pm appeared in fml5.
=cut
1;
|