File Coverage

blib/lib/TRD/WebRelayMail.pm
Criterion Covered Total %
statement 18 61 29.5
branch 0 6 0.0
condition 0 3 0.0
subroutine 6 12 50.0
pod 6 6 100.0
total 30 88 34.0


line stmt bran cond sub pod time code
1             package TRD::WebRelayMail;
2              
3 1     1   40024 use warnings;
  1         6  
  1         76  
4 1     1   6 use strict;
  1         3  
  1         139  
5 1     1   6 use Carp;
  1         7  
  1         114  
6 1     1   4020 use CGI;
  1         26092  
  1         8  
7 1     1   1956 use LWP;
  1         97332  
  1         34  
8             #use TRD::DebugLog;
9              
10             #$TRD::DebugLog::enabled = 1;
11             #$TRD::DebugLog::file = '/tmp/ichi.log';
12              
13 1     1   850 use version;
  1         1976  
  1         5  
14             our $VERSION = qv('0.0.5');
15              
16             =head1 FUNCTIONS
17              
18             =head2 new
19              
20             TRD::WebRelayMailオブジェクトを作成するコンストラクタです。
21             my $relaymail = new TRD::WebRelayMail;
22              
23             =cut
24             #======================================================================
25             sub new {
26 0     0 1   my $pkg = shift;
27 0           bless {
28             params => {
29             proxy => undef,
30             },
31             sendUrl => undef,
32             }, $pkg;
33             };
34              
35             =head2 setSendUrl( $url )
36              
37             リレー先URLを設定します。
38             $relaymail->setSendUrl( 'http://foobar/relay/rmail.cgi' );
39             リレー先のcgiには、TRD::WebRelayMail->Recv()を使用します。
40              
41             =cut
42             #======================================================================
43             sub setSendUrl
44             {
45 0     0 1   my $self = shift;
46 0           my $sendurl = shift;
47              
48 0           $self->{params}->{sendUrl} = $sendurl;
49             }
50              
51             =head2 setProxy( $proxy )
52              
53             リレー先URLへ接続する際のProxyを設定します。
54             $relaymail->setProxy( 'http://proxy.foobar:8080/' );
55             TRD::WebRelayMain->Send()を使用する際にProxyを使用します。
56              
57             =cut
58             #======================================================================
59             sub setProxy
60             {
61 0     0 1   my $self = shift;
62 0           my $proxy = shift;
63              
64 0           $self->{params}->{proxy} = $proxy;
65             }
66              
67             =head2 Send( $message )
68              
69             リレーメールを送信します。
70             my $res = $relaymail->Send( $mime->stringify );
71              
72             $messageは、MIME::Entityで作成された物を使用します。
73             返り値は、=1 送信完了。!=1 エラー。
74              
75             =cut
76             #======================================================================
77             sub Send
78             {
79 0     0 1   my $self = shift;
80 0           my $mail_message = shift;
81              
82 0           my $res = 0;
83              
84 0           my $logname = $ENV{'LOGNAME'};
85 0           my $hostname = `hostname`;
86 0           chop( $hostname );
87 0           my $sender = $logname. '@'. $hostname;
88              
89 0           my $url = $self->{params}->{sendUrl};
90 0           my $query_string = 'sender='. &url_encode( $sender ). '&message='. &url_encode( $mail_message );
91              
92             #dlog( 'url='. $url );
93             #dlog( 'query_string='. $query_string );
94              
95 0           my $browser = LWP::UserAgent->new(
96             agent => 'TRD::WebRelayMail('. $VERSION. ')',
97             );
98              
99 0 0         if( exists( $self->{params}->{proxy} ) ){
100 0           $browser->proxy( 'http', $self->{params}->{proxy} );
101             }
102              
103 0           my $req = HTTP::Request->new( POST => $url );
104 0           $req->content( $query_string );
105              
106 0           my $response = $browser->request( $req );
107              
108             #dlog( 'response='. $response->content );
109 0 0         if( $response->content eq 'ok' ){
110 0           $res = 1;
111             }
112 0           return $res;
113             }
114              
115             =head2 Recv( $cgi )
116              
117             リレーメールを受信します。
118             $cgiはCGIを設定します。
119             TRD::WebRelayMail->setSendUrlで設定した受信cgiで使用します。
120              
121             =cut
122             #======================================================================
123             sub Recv
124             {
125 0     0 1   my $self = shift;
126 0           my $cgi = shift;
127 0           my $data;
128 0           my $res = 0;
129              
130 0           my $sender = $cgi->param( 'sender' );
131 0           my $message = $cgi->param( 'message' );
132              
133 0           $data->{'sender'} = $sender;
134 0           $data->{'message'} = $message;
135              
136 0 0 0       if(( $sender ne '' )&&( $message ne '' )){
137 0           $res = 1;
138             }
139 0           $data->{'res'} = $res;
140              
141 0           return $data;
142             }
143              
144             =head2 url_encode( $str )
145              
146             文字列をURLエンコードします。
147             $str = $relaymail->url_encode( $str );
148              
149             =cut
150             #======================================================================
151             sub url_encode
152             {
153 0     0 1   my $str = shift;
154              
155 0           $str =~s/([^\w ])/'%'.unpack('H2', $1)/eg;
  0            
156 0           $str =~tr/ /+/;
157              
158 0           return $str;
159             }
160              
161             1; # Magic true value required at end of module
162             __END__
163              
164             =head1 NAME
165              
166             TRD::WebRelayMail - メールを送れないホストからのWebメールリレーモジュール
167              
168             =head1 VERSION
169              
170             This document describes TRD::WebRelayMail version 0.0.4
171              
172              
173             =head1 SYNOPSIS
174              
175             # 送信用
176             use strict;
177             use Jcode;
178             use MIME::Entity;
179             use TRD::WebRelayMail;
180              
181             my $relaymail = new TRD::WebRelayMail;
182             $relaymail->setSendUrl( 'http://foobar/relay/rmail.cgi' );
183             my $mail_to = 'receiver@barbas.com';
184             my $mail_from = 'sender@barbas.com';
185             my $mail_subject = 'TEST MAIL';
186             my $mail_body = 'THIS IS TEST';
187              
188             $mail_subject = jcode( $mail_subject )->mime_encode;
189             Jcode::convert( \$mail_body, 'jis' );
190              
191             my $mime = MIME::Entity->build(
192             From => $mail_from,
193             To => $mail_to,
194             Subject => $mail_subject,
195             Type => 'text/plain; charset="iso-2022-jp"',
196             Data => $mail_body,
197             );
198              
199             my $res = $relaymail->Send( $mime->stringify );
200             if( !$res ){
201             print "NG\n";
202             } else {
203             print "OK\n";
204             }
205              
206             #[EOT]
207              
208             # 受信用
209             use strict;
210             use CGI;
211             use TRD::WebRelayMail;
212              
213             my $cgi = new CGI;
214             my $relaymail = new TRD::WebRelayMail();
215              
216             my $data = $relaymail->Recv( $cgi );
217              
218             if( !$data->{'res'} ){
219             print "Content-type: text/html\n\n";
220             print "ng";
221             } else {
222             print "Content-type: text/html\n\n";
223             print "ok";
224              
225             open( my $m, "| /usr/sbin/sendmail -t" );
226             print $m $data->{'message'};
227             close( $m );
228             }
229              
230             # [EOT]
231              
232             =head1 DESCRIPTION
233              
234             =for author to fill in:
235             Write a full description of the module and its features here.
236             Use subsections (=head2, =head3) as appropriate.
237              
238              
239             =head1 INTERFACE
240              
241             =for author to fill in:
242             Write a separate section listing the public components of the modules
243             interface. These normally consist of either subroutines that may be
244             exported, or methods that may be called on objects belonging to the
245             classes provided by the module.
246              
247              
248             =head1 DIAGNOSTICS
249              
250             =for author to fill in:
251             List every single error and warning message that the module can
252             generate (even the ones that will "never happen"), with a full
253             explanation of each problem, one or more likely causes, and any
254             suggested remedies.
255              
256             =over
257              
258             =item C<< Error message here, perhaps with %s placeholders >>
259              
260             [Description of error here]
261              
262             =item C<< Another error message here >>
263              
264             [Description of error here]
265              
266             [Et cetera, et cetera]
267              
268             =back
269              
270              
271             =head1 CONFIGURATION AND ENVIRONMENT
272              
273             =for author to fill in:
274             A full explanation of any configuration system(s) used by the
275             module, including the names and locations of any configuration
276             files, and the meaning of any environment variables or properties
277             that can be set. These descriptions must also include details of any
278             configuration language used.
279            
280             TRD::WebRelayMail requires no configuration files or environment variables.
281              
282              
283             =head1 DEPENDENCIES
284              
285             =for author to fill in:
286             A list of all the other modules that this module relies upon,
287             including any restrictions on versions, and an indication whether
288             the module is part of the standard Perl distribution, part of the
289             module's distribution, or must be installed separately. ]
290              
291             None.
292              
293              
294             =head1 INCOMPATIBILITIES
295              
296             =for author to fill in:
297             A list of any modules that this module cannot be used in conjunction
298             with. This may be due to name conflicts in the interface, or
299             competition for system or program resources, or due to internal
300             limitations of Perl (for example, many modules that use source code
301             filters are mutually incompatible).
302              
303             None reported.
304              
305              
306             =head1 BUGS AND LIMITATIONS
307              
308             =for author to fill in:
309             A list of known problems with the module, together with some
310             indication Whether they are likely to be fixed in an upcoming
311             release. Also a list of restrictions on the features the module
312             does provide: data types that cannot be handled, performance issues
313             and the circumstances in which they may arise, practical
314             limitations on the size of data sets, special cases that are not
315             (yet) handled, etc.
316              
317             No bugs have been reported.
318              
319             Please report any bugs or feature requests to
320             C<bug-trd-webrelaymail@rt.cpan.org>, or through the web interface at
321             L<http://rt.cpan.org>.
322              
323              
324             =head1 AUTHOR
325              
326             Takuya Ichikawa C<< <ichi@cpan.org> >>
327              
328              
329             =head1 LICENCE AND COPYRIGHT
330              
331             Copyright (c) 2009, Takuya Ichikawa C<< <ichi@cpan.org> >>. All rights reserved.
332              
333             This module is free software; you can redistribute it and/or
334             modify it under the same terms as Perl itself. See L<perlartistic>.
335              
336              
337             =head1 DISCLAIMER OF WARRANTY
338              
339             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
340             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
341             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
342             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
343             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
344             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
345             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
346             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
347             NECESSARY SERVICING, REPAIR, OR CORRECTION.
348              
349             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
350             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
351             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
352             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
353             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
354             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
355             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
356             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
357             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
358             SUCH DAMAGES.