3 $rcsid = q$Id: w3mmail.cgi.in,v 1.14 2004/08/30 16:32:24 ukai Exp $;
4 ($id = $rcsid) =~ s/^.*,v ([\d\.]*).*/$1/;
5 ($prog=$0) =~ s/.*\///;
7 $query = $ENV{'QUERY_STRING'};
8 $cookie_file = $ENV{'LOCAL_COOKIE_FILE'};
10 $SENDMAIL = '/usr/lib/sendmail';
11 $SENDMAIL = '/usr/sbin/sendmail' if -x '/usr/sbin/sendmail';
12 $SENDMAIL_OPT = '-oi -t';
14 if (-f $cookie_file) {
15 open(F, "< $cookie_file");
19 if ($query =~ s/^\w+://) {
21 $qurl = &html_quote($url);
24 if ($to =~ /^([^?]*)\?(.*)$/) {
28 $to = &url_unquote($to);
29 %opt = &parse_opt($opt);
32 push(@to, $opt{'to'}) if ($opt{'to'});
33 $opt{'to'} = join(',', @to);
34 if ($ENV{'REQUEST_METHOD'} eq 'POST') {
35 sysread(STDIN, $body, $ENV{'CONTENT_LENGTH'});
36 $content_type = $ENV{'CONTENT_TYPE'};
37 if ($content_type =~ /^multipart\/form-data;\s+boundary=(.*)$/) {
46 print "Content-Type: text/html; charset=$charset\r\n";
47 print "w3m-control: END\r\n";
48 print "w3m-control: PREV_LINK\r\n";
50 print "<html><head><title>W3M Mailer: $qurl</title></head>\n";
51 print "<body><h1>W3M Mailer: $qurl</h1>\n";
52 print "<form action=\"file://$0\" method='POST'>\n";
53 $local_cookie = &html_quote($local_cookie);
54 print "<input type='hidden' name='cookie' value=\"$local_cookie\">\n";
56 foreach $h ('from', 'to', 'cc', 'bcc', 'subject') {
57 $v = &lang_html_quote($opt{$h});
58 print "<tr><td>\u$h:<td><input type='text' name=\"$h\" value=\"$v\">\n";
62 $boundary = &html_quote($boundary);
63 print "<tr><td>Content-Type:<td>multipart/form-data; boundary=\"$boundary\"\n";
64 print "<input type='hidden' name='boundary' value=\"$boundary\">\n";
66 foreach $h (keys %opt) {
67 $qh = &html_quote($h);
68 $v = &lang_html_quote($opt{$h});
69 print "<tr><td>\u$h:<td>$v\n";
70 print "<input type='hidden' name=\"$qh\" value=\"$v\">\n";
72 print "<tr><td colspan=2>\n";
73 print "<textarea cols=40 rows=10 name='body'>\n";
75 print &lang_html_quote($body);
77 print "</textarea>\n";
79 print "<input type='submit' name='action' value='Preview'>\n";
81 print "</body></html>\n";
84 sysread(STDIN, $req, $ENV{'CONTENT_LENGTH'});
85 %opt = &parse_opt($req);
86 if ($local_cookie ne $opt{'cookie'}) {
87 print "Content-Type: text/plain\r\n";
89 print "Local cookie doesn't match: It may be an illegal execution\n";
92 delete $opt{'cookie'};
95 $act = $opt{'action'};
96 delete $opt{'action'};
97 $boundary = $opt{'boundary'};
98 delete $opt{'boundary'};
101 if ($act eq "Preview") {
102 print "Content-Type: text/html; charset=$charset\r\n";
103 print "w3m-control: DELETE_PREVBUF\r\n";
104 print "w3m-control: NEXT_LINK\r\n";
106 print "<html><head><title>W3M Mailer</title></head>\n";
108 print "<h1>W3M Mailer: preview</h1>\n";
109 print "<form action=\"file://$0\" method='POST'>\n";
110 $local_cookie = &html_quote($local_cookie);
111 print "<input type='hidden' name='cookie' value=\"$local_cookie\">\n";
114 foreach $h (keys %opt) {
115 $qh = &html_quote($h);
116 $v{$h} = &lang_html_quote($opt{$h});
118 print "\u$qh: $v{$h}\n";
121 ($cs,$cte,$body) = &lang_body(&lang_html_quote($body), 0);
122 print "Mime-Version: 1.0\n";
124 $boundary = &html_quote($boundary);
125 print "Content-Type: multipart/form-data;\n";
126 print " boundary=\"$boundary\"\n";
128 print "Content-Type: text/plain; charset=$cs\n";
130 # print "Content-Transfer-Encoding: $cte\n";
131 print "User-Agent: ", &html_quote("$ENV{'SERVER_SOFTWARE'} $prog/$id"),
135 print "\n" if ($body !~ /\n$/);
137 print "<input type='submit' name='action' value='Send'>\n";
140 foreach $h ('from', 'to', 'cc', 'bcc', 'subject') {
141 print "<tr><td>\u$h:<td><input type='text' name=\"$h\" value=\"$v{$h}\">\n";
145 print "<tr><td>Content-Type:<td>Content-Type: multipart/form-data; boundary=\"$boundary\"\n";
146 print "<input type='hidden' name=\"boundary\" value=\"$boundary\">\n";
148 foreach $h (keys %opt) {
149 $qh = &html_quote($h);
150 print "<tr><td>\u$qh:<td>$v{$h}\n";
151 print "<input type='hidden' name=\"$qh\" value=\"$v{$h}\">\n";
153 print "<tr><td colspan=2>\n";
154 print "<textarea cols=40 rows=10 name=body>\n";
158 print "</textarea>\n";
160 print "<input type='submit' name='action' value='Preview'><br>\n";
161 print "</body></html>\n";
164 # if ($opt{'from'}) {
165 # $sendmail_fromopt = '-f' . $opt{'from'};
167 unless (open(MAIL, "|$SENDMAIL $SENDMAIL_OPT")) {
168 print "Content-Type: text/html\r\n";
170 print "<html><head><title>W3M Mailer</title></head>\n";
171 print "<body><h1>W3M Mailer: open sendmail failed</h1>\n";
172 print "<p>", &html_quote($@), "</p>\n";
173 print "</body></html>\n";
176 foreach $h (keys %opt) {
177 $v = &lang_header($opt{$h});
179 print MAIL "\u$h: $v\n";
182 ($cs,$cte,$body) = &lang_body($body, 1);
184 print MAIL "Mime-Version: 1.0\n";
186 print MAIL "Content-Type: multipart/form-data;\n";
187 print MAIL " boundary=\"$boundary\"\n";
189 print MAIL "Content-Type: text/plain; charset=$cs\n";
191 print MAIL "Content-Transfer-Encoding: $cte\n";
192 print MAIL "User-Agent: $ENV{'SERVER_SOFTWARE'} $prog/$id\n";
196 print "w3m-control: DELETE_PREVBUF\r\n";
197 print "w3m-control: BACK\r\n";
200 print "Content-Type: text/html\r\n";
202 print "<html><head><title>W3M Mailer</title></head>\n";
203 print "<body><h1>W3M Mailer: close sendmail failed</h1>\n";
204 print "<p>", &html_quote($@), "</p>\n";
205 print "</body></html>\n";
211 $lang = $ENV{'LC_ALL'} || $ENV{'LC_CTYPE'} || $ENV{'LANG'};
212 if ($lang =~ /^ja/i) {
221 $charset = &guess_charset($lang);
226 if ($lang =~ /^ja/i) {
227 return &lang_header_ja(@_);
229 return &lang_header_default(@_);
234 if ($lang =~ /^ja/i) {
235 return &lang_body_ja(@_);
237 return &lang_body_default(@_);
241 sub lang_html_quote {
243 if ($lang =~ /^ja/i) {
244 if (/[\x80-\xFF]/ || /\033[\$\(][BJ@]/) {
245 $_ = &conv_nkf("-e", $_);
248 return &html_quote($_);
251 sub lang_header_default {
253 if ($h =~ s/([=_?\x80-\xFF])/sprintf("=%02x", ord($1))/ge) {
254 return "=?$charset?Q?$h?=";
260 sub lang_body_default {
261 local($body, $_7bit) = @_;
262 if ($body =~ /[\x80-\xFF]/) {
264 $body =~ s/([=\x80-\xFF])/sprintf("=%02x", ord($1))/ge;
265 return ($charset, "quoted-printable", $body);
267 return ($charset, "8bit", $body);
270 return ("US-ASCII", "7bit", $body);
276 if ($h =~ /[\x80-\xFF]/ || $h =~ /\033[\$\(][BJ@]/) {
277 $h = &conv_nkf("-j", $h);
285 local($body, $_7bit) = @_;
286 if ($body =~ /[\x80-\xFF]/ || $body =~ /\033[\$\(][BJ@]/) {
288 $body = &conv_nkf("-j", $body);
290 return ("ISO-2022-JP", "7bit", $body);
292 return ("US-ASCII", "7bit", $body);
301 local($body) = pop(@opt);
302 $body =~ s/\r+\n/\n/g;
311 open(STDOUT, ">&W2");
333 foreach $o (split('&', $opt)) {
334 if ($o =~ /(\w+)=(.*)/) {
335 $opt{"\L$1"} = &url_unquote($2);
350 s/[<>&"]/$QUOTE{$&}/g;
356 s/\+|%([0-9A-Fa-f][0-9A-Fa-f])/$& eq '+' ? ' ' : pack('c', hex($1))/ge;
361 local(%lang_charset) = (
384 return $lang_charset{$1} || 'iso-8859-1';
390 $lang =~ /^zh_tw/ && return 'EUC-TW';
391 $lang =~ /^zh/ && return 'GB2312';
392 $lang =~ /^ko/ && return 'EUC-KR';
395 /^euccn/i && return 'GB2312';
399 } elsif (/^iso8859/i) {
401 s/^iso8859/iso-8859-/i;