vacation.pl : adding unicode support - thanks to amsys for this

git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@58 a1433add-5e2c-0410-b055-b7f2511e0802
postfixadmin-2.3
David Goodwin 17 years ago
parent d4707a049f
commit 2ae28e1eb2

@ -56,7 +56,7 @@ CREATE TABLE vacation (
active tinyint(4) NOT NULL default '1', active tinyint(4) NOT NULL default '1',
PRIMARY KEY (email), PRIMARY KEY (email),
KEY email (email) KEY email (email)
) TYPE=InnoDB COMMENT='Postfix Admin - Virtual Vacation' CHARACTER SET latin1; ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci TYPE=InnoDB COMMENT='Postfix Admin - Virtual Vacation' ;
# vacation_notification table # vacation_notification table
@ -66,4 +66,4 @@ CREATE TABLE vacation_notification (
notified_at timestamp NOT NULL default now(), notified_at timestamp NOT NULL default now(),
CONSTRAINT vacation_notification_pkey PRIMARY KEY(on_vacation, notified), CONSTRAINT vacation_notification_pkey PRIMARY KEY(on_vacation, notified),
FOREIGN KEY (on_vacation) REFERENCES vacation(email) ON DELETE CASCADE FOREIGN KEY (on_vacation) REFERENCES vacation(email) ON DELETE CASCADE
) TYPE=InnoDB COMMENT='Postfix Admin - Virtual Vacation Notifications' CHARACTER SET latin1; ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci TYPE=InnoDB COMMENT='Postfix Admin - Virtual Vacation Notifications';

@ -33,6 +33,9 @@
# Use the Perl Mail::Sendmail module for sending mail # Use the Perl Mail::Sendmail module for sending mail
# Check for headers that start with blank lines (patch from forum) # Check for headers that start with blank lines (patch from forum)
# #
# 2007-08-20 Martin Ambroz <amsys@trustica.cz>
# Added initial Unicode support
#
# Requirements: # Requirements:
# You need to have the DBD::Pg or DBD::MySQL perl-module installed. # You need to have the DBD::Pg or DBD::MySQL perl-module installed.
# You need to have the Mail::Sendmail module installed. # You need to have the Mail::Sendmail module installed.
@ -54,11 +57,12 @@
# http://dag.wieers.com/home-made/apt/packages.php # http://dag.wieers.com/home-made/apt/packages.php
# #
use DBI; use DBI;
use MIME::Words qw(:all); use MIME::Base64;
use Email::Valid; use Email::Valid;
use strict; use strict;
use Mail::Sendmail; use Mail::Sendmail;
my $db_host; # leave alone use Encode;
#my $db_host; # leave alone
# ========== begin configuration ========== # ========== begin configuration ==========
@ -66,9 +70,13 @@ my $db_host; # leave alone
# to restrict access to the script, so that only the vacation user # to restrict access to the script, so that only the vacation user
# can read it. # can read it.
# db_type - either 'Pg' or 'mysql' # db_type - uncomment one of these
my $db_type = 'Pg'; #my $db_type = 'Pg';
# $db_host = 'localhost'; # Uncomment (and adjust, if needed) your DB #my $db_type = 'mysql';
my $db_host; # Comment this if you are not connecting via
# local socket
#my $db_host = 'localhost'; # Uncomment (and adjust, if needed) your DB
# host-name here, if you want to connect via # host-name here, if you want to connect via
# a TCP socket # a TCP socket
@ -76,18 +84,16 @@ my $db_user = 'vacation'; # What DB-user to connect as
my $db_pass = ''; # What password (if any) to connect with my $db_pass = ''; # What password (if any) to connect with
my $db_name = 'postfix'; # Name of database to use my $db_name = 'postfix'; # Name of database to use
my $charset = 'ISO-8859-1'; # Character set of vacation messages.
#my $logfile = "/tmp/vacation-log";
my $logfile=''; my $logfile='';
#my $logfile = "/var/log/vacation/vacation.log";
my $syslog = 1; # 1 if log entries should be sent to syslog my $syslog = 1; # 1 if log entries should be sent to syslog
#my $debugfile = "/tmp/vacation-debug"; # Specify a file name here for example: /tmp/vacation.debug
my $debugfile=''; my $debugfile='';
#my $debugfile = "/var/log/vacation/vacation.debug"; # Specify a file name here for example: /tmp/vacation.debug
# =========== end configuration =========== # =========== end configuration ===========
binmode (STDIN,':utf8');
my $dbh; my $dbh;
if (defined($db_host)) { if (defined($db_host)) {
@ -101,6 +107,11 @@ if (!$dbh) {
exit(0); exit(0);
} }
# mysql only, needs FIX for ALL databases
if ($db_type eq "mysql") {
$dbh->do("SET CHARACTER SET utf8;");
}
# used to detect infinite address lookup loops # used to detect infinite address lookup loops
my $loopcount=0; my $loopcount=0;
@ -109,6 +120,7 @@ sub do_debug {
if ( $debugfile ) { if ( $debugfile ) {
my $date; my $date;
open (DEBUG, ">> $debugfile") or die ("Unable to open debug file"); open (DEBUG, ">> $debugfile") or die ("Unable to open debug file");
binmode (DEBUG, ':utf8');
chop ($date = `date "+%Y/%m/%d %H:%M:%S"`); chop ($date = `date "+%Y/%m/%d %H:%M:%S"`);
print DEBUG "====== $date ======\n"; print DEBUG "====== $date ======\n";
printf DEBUG "%s | %s | %s | %s | %s | %s\n", $in1, $in2, $in3, $in4, $in5, $in6; printf DEBUG "%s | %s | %s | %s | %s | %s\n", $in1, $in2, $in3, $in4, $in5, $in6;
@ -144,6 +156,7 @@ sub do_log {
my $date; my $date;
if ( $syslog ) { if ( $syslog ) {
open (SYSLOG, "|/usr/bin/logger -p mail.info -t Vacation") or die ("Unable to open logger"); open (SYSLOG, "|/usr/bin/logger -p mail.info -t Vacation") or die ("Unable to open logger");
binmode(SYSLOG, ':utf8');
if ($logmessage) { if ($logmessage) {
printf SYSLOG "Orig-To: %s From: %s MessageID: %s Subject: %s. Log message: $%s", $to, $from, $messageid, $subject, $logmessage; printf SYSLOG "Orig-To: %s From: %s MessageID: %s Subject: %s. Log message: $%s", $to, $from, $messageid, $subject, $logmessage;
} else { } else {
@ -153,6 +166,7 @@ sub do_log {
} }
if ( $logfile ) { if ( $logfile ) {
open (LOG, ">> $logfile") or die ("Unable to open log file"); open (LOG, ">> $logfile") or die ("Unable to open log file");
binmode (LOG, ':utf8');
chop ($date = `date "+%Y/%m/%d %H:%M:%S"`); chop ($date = `date "+%Y/%m/%d %H:%M:%S"`);
if ($logmessage) { if ($logmessage) {
print LOG "$date: To: $to From: $from Subject: $subject MessageID: $messageid. Log message: $logmessage\n"; print LOG "$date: To: $to From: $from Subject: $subject MessageID: $messageid. Log message: $logmessage\n";
@ -165,22 +179,23 @@ sub do_log {
sub do_mail { sub do_mail {
# from, to, subject, body # from, to, subject, body
my ($from, $to, $plainsubject, $body) = @_; my ($from, $to, $subject, $orig_subject, $body) = @_;
my $subject = encode_mimewords($plainsubject); my $vacation_subject = encode('MIME-Q',$subject);
my $old_subject = encode('MIME-Q',$orig_subject);
my %mail; my %mail;
%mail = ( %mail = (
'To' => $to, 'To' => $to,
'From' => $from, 'From' => $from,
'Subject' => $subject, 'Subject' => "$vacation_subject (Re: $old_subject)",
'MIME-Version' => '1.0', 'MIME-Version' => '1.0',
'Content-Type' => "text/plain; charset=\"$charset\"", 'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => 'base64',
'Precedence' => 'junk', 'Precedence' => 'junk',
'X-Loop' => 'Postfix Admin Virtual Vacation', 'X-Loop' => 'Postfix Admin Virtual Vacation',
'Message' => $body 'Message' => encode_base64($body)
); );
sendmail(%mail) or do_log($Mail::Sendmail::error); sendmail(%mail) or do_log($Mail::Sendmail::error);
do_debug("Mail::Sendmail said : " . $Mail::Sendmail::log); do_debug('Mail::Sendmail said :' . $Mail::Sendmail::log,'','','','','');
} }
sub panic { sub panic {
@ -266,6 +281,7 @@ sub find_real_address {
sub send_vacation_email { sub send_vacation_email {
my ($email, $orig_subject, $orig_from, $orig_to, $orig_messageid) = @_; my ($email, $orig_subject, $orig_from, $orig_to, $orig_messageid) = @_;
my $query = qq{SELECT subject,body FROM vacation WHERE email=?}; my $query = qq{SELECT subject,body FROM vacation WHERE email=?};
my $old_subject = decode('MIME-Q',$orig_subject);
my $stm = $dbh->prepare($query) or panic_prepare($query); my $stm = $dbh->prepare($query) or panic_prepare($query);
$stm->execute($email) or panic_execute($query,"email='$email'"); $stm->execute($email) or panic_execute($query,"email='$email'");
my $rv = $stm->rows; my $rv = $stm->rows;
@ -275,7 +291,7 @@ sub send_vacation_email {
do_debug ("[SEND RESPONSE] for $orig_messageid:\n", "FROM: $email (orig_to: $orig_to)\n", "TO: $orig_from\n", "SUBJECT: $orig_subject\n", "VACATION SUBJECT: $row[0]\n", "VACATION BODY: $row[1]\n"); do_debug ("[SEND RESPONSE] for $orig_messageid:\n", "FROM: $email (orig_to: $orig_to)\n", "TO: $orig_from\n", "SUBJECT: $orig_subject\n", "VACATION SUBJECT: $row[0]\n", "VACATION BODY: $row[1]\n");
# do_mail(from, to, subject, body); # do_mail(from, to, subject, body);
do_mail ($email, $orig_from, $row[0] . " (Re: $orig_subject)", $row[1]); do_mail ($email, $orig_from, $row[0], $old_subject, $row[1]);
do_log ($orig_messageid, $orig_to, $orig_from, $orig_subject); do_log ($orig_messageid, $orig_to, $orig_from, $orig_subject);
} }

Loading…
Cancel
Save