Skip to content

Instantly share code, notes, and snippets.

@bkram
Created February 1, 2018 12:55
Show Gist options
  • Save bkram/41b58b66484dacd7e7cbe50ba395d319 to your computer and use it in GitHub Desktop.
Save bkram/41b58b66484dacd7e7cbe50ba395d319 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
#
# Troels Hansen @ Casalogic A/S
# Доработка скрипта - Шакиров Ленар, Шакиров Айнур (Группа компаний "Центр", г. Казань, Россия).
# Скрипт предоставлен форумом Zarafa на правах Public Domain. Скрипт предоставлен "как есть", его работа не гарантируется.
use Unicode::IMAPUtf7;
use Getopt::Std;
use Text::Iconv;
use strict;
my %opts;
my $folder;
my $path;
my @folder;
my $t = Unicode::IMAPUtf7->new();
my $converter = Text::Iconv->new("UTF-8","ISO-8859-10");
getopts("p:u:h",\%opts);
my $path = $opts{p};
my $user = $opts{u};
sub usage { return <<EOF;
Usage: -p maildir path
-u username
-h option displays this text
EOF
;}
die usage unless (defined $opts{p} and defined $opts{u});
die usage if $opts{h};
process_dir($opts{p});
sub process_dir {
my $path = shift;
# Open the directory.
opendir (DIR, $path) or die "Unable to open $path: $!";
# Read in the files.
# You will not generally want to process the '.' and '..' files,
my @files = grep { !/^\.{1,2}$/ } readdir (DIR);
# Close the directory.
closedir (DIR);
@files = map { $path . '/' . $_ } @files;
for (@files) {
if (-d $_) { # If the file is a directory
process_dir ($_);
} else {
# process mailfiles
read_files($_, $path);
}
}
}
sub read_files {
my $file = shift;
my $path = shift;
my $decoded_path;
#print "Debug path: $path\n";
if ( $path =~ m/.\../ ) { #message reside in a sub folder
if ( $file =~ m/\/cur\// ){ #message is read
$path = imap_decode($path);
print "read message = cur, sub: $file\n";
system("zarafa-dagent -s --read -C -F \"$path\" -p \".\" -f \"$file\" $user");
}elsif ( $file =~ m/\/new\// ){ # else must be an unread message
$path = imap_decode($path);
print "unread message = cur, sub: $file\n";
system("zarafa-dagent -s -C -F \"$path\" -p \".\" -f \"$file\" $user");
}
} else { #else the message must reside in the root
if ( $file =~ m/\/cur\// ){ #message is read
print "read message = cur, root inbox: $file\n";
system("zarafa-dagent -s --read -f \"$file\" $user");
}elsif ( $file =~ m/\/new\// ){ # else must be an new, unread message
print "unread message = cur, root inbox: $file\n";
system("zarafa-dagent -s -f \"$file\" $user");
}
}
}
sub imap_decode {
$folder = shift;
@folder = split(/\//,$folder);
pop(@folder);
$folder = pop(@folder);
$folder = $t->decode("$folder");
$folder = substr $folder, 1;
print "\n\nFrom folder: $folder\n";
return "$folder";
# return $converter->convert($folder);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment