by 高須 98.4.22 → ホームページ
Perlページ コンピュータのページ
同様の内容をシェルで書いたものよりも、 だいぶ速くなりました。また、ML数が多い時に増やすのも簡単です。
slocalを使った自動分類方法(メールが着い たら即座に分類)という方法もありますが、危ない時もあります。
$max_mail メール数 適当に大きい値にしておきます。
$mail_directory inc でメールが入る場所。普通は~/Mail/inboxです。
フルパスで書いておきます。
@input_subject Subject行のキーワード、フォルダー名 の組を
1行に1組ずつ書いて行きます。
この例の2行目をコピーして使います。
@input_to 同様にTo行のキーワード、フォルダー名の組。
注意: ・input_subjectとinput_toは、上から順に処理します。
メール数の多いMLは、上の方に書いた方がよいです。
・subjectとtoの両方で検索すると時間がかかります。
なるべくsubjectで検索するようにします。
名前が短いMLはsubject行だけでは特定できないので、
to行で検索します。
(2)~/Mail/の下に、inboxディレクトリの他、
(1)で指定したフォルダー名のディレクトリを作っておきます。
以下のソースを例えば、mail.pl というファイルにセーブし、
chmod 755 mail.pl
によって、実行可能にする。
mail.pl
により、実行できます。
メールの少ない日曜などにテストされることをお勧めします。
#!/usr/bin/perl
# mail.pl by M. Takasu, April 22, 1998
# put mail into folders
#
$max_mail = 300; # max number of mails in Mail/inbox to be processed
$mail_directory = "/home/usr1/your_id/Mail/inbox"; #full path
@input_subject = ( # classification by Subject
"ABC-ML: ", "ABC"
, "XY" , "xy"
);
@input_to = ( # classification by To field
"MLsonoichi@address.jp", "ABC"
, "MLsononi@address2.jp", "pq"
);
#------------don't change below unless necessary
system("inc");
chdir($mail_directory) || " cannot change directory";
system("pwd");
&data_subject;
&data_to;
for ($i_mail=1; $i_mail <= $max_mail ;$i_mail ++){ # start new mail
if(-e $i_mail) { # check if the mail exists
print "processing mail number: ", $i_mail, "\n";
system ("rm -f temp1");
system ("cp $i_mail temp1");
open(inputfile, temp1)|| die "can not open file \n";
&get_lines;
close(inputfile);
$find_classify = "no";
if($find_subject eq "yes"){
&classify_subject;
}
if($find_classify eq "no"){
&classify_to;
}
} # end of file
}
#---------------------------------------------------------
# start of subroutines
#--------------------------------------------------------
# classify according to "To" field
#------------------------------------------------------
sub classify_to{
local($i);
local($suteru);
($suteru, $line_to) = split("To:", $line_to);
for ($i=0; $find_classify eq "no" && $i< $max_to ; $i ++){
if($line_to =~ /$w_to[$i]/) {
$find_classify = "yes";
system("refile +$folder_to[$i] $i_mail");
}
}
}
#---------------------------------------
# classify according to "Subject:" field
#---------------------------------------------------------
sub classify_subject{
local($suteru);
local($i);
local($suteru);
($suteru, $line_subject) = split("Subject:", $line_subject);
for ($i=0; $find_classify eq "no" && $i< $max_subject ; $i ++){
if($line_subject =~ /$w_subject[$i]/) {
$find_classify = "yes";
system("refile +$folder_subject[$i] $i_mail");
}
}
}
#--------------------------------------------------------
# reorganize input data for "To" field
#--------------------------------------------------------
sub data_to{
local ($i) = 0 ;
local ($j);
local ($max_input) ;
$max_input= @input_to;
for ($j=0; $i < $max_input ; $j++ ){
$w_to[$j] = $input_to[$i];
$i ++ ;
$folder_to[$j] = $input_to[$i];
$i ++ ;
}
$max_to=@w_to;
}
#--------------------------------------------------------
# reorganize input data for "Subject:" field
#--------------------------------------------------------
sub data_subject{
local ($i) = 0 ;
local ($j);
local ($max_input) ;
$max_input= @input_subject;
for ($j=0; $i < $max_input ; $j++ ){
$w_subject[$j] = $input_subject[$i];
$i ++ ;
$folder_subject[$j] = $input_subject[$i];
$i ++ ;
}
$max_subject=@w_subject;
}
#---------------------------------------------------------
# get "Subject"and "To" fields from the header
#---------------------------------------------------------
sub get_lines{
$find_subject = "no"; # the mail may not have subject line
# so $find_subject is not local
local($find_to) = "no";
local($find_from) = "no";
local($line);
while(<inputfile>){
$line=$_;
if($find_subject eq "no" && $line =~ /^Subject/){
$line_subject = $line;
$find_subject ="yes";
}
if($find_to eq "no" && $line =~ /^To/){
$line_to = $line;
$find_to ="yes";
}
}
}
Perlページ コンピュータのページ