aboutsummaryrefslogtreecommitdiffstats
path: root/interimap
diff options
context:
space:
mode:
Diffstat (limited to 'interimap')
-rwxr-xr-xinterimap68
1 files changed, 46 insertions, 22 deletions
diff --git a/interimap b/interimap
index 6442054..af8b7fd 100755
--- a/interimap
+++ b/interimap
@@ -51,14 +51,19 @@ sub usage(;$) {
}
exit $rv;
}
-usage(1) unless GetOptions(\%CONFIG, qw/config=s quiet|q target=s@ debug help|h repair delete rename/);
+
+my @COMMANDS = qw/repair delete rename/;
+usage(1) unless GetOptions(\%CONFIG, qw/config=s quiet|q target=s@ debug help|h watch:i/, @COMMANDS);
usage(0) if $CONFIG{help};
my $COMMAND = do {
- my @command = grep {exists $CONFIG{$_}} qw/repair delete rename/;
+ my @command = grep {exists $CONFIG{$_}} @COMMANDS;
usage(1) if $#command>0;
$command[0]
};
-usage(1) if defined $COMMAND and (($COMMAND eq 'delete' and !@ARGV) or $COMMAND eq 'rename' and $#ARGV != 1);
+usage(1) if defined $COMMAND and (($COMMAND eq 'delete' and !@ARGV) or ($COMMAND eq 'rename' and $#ARGV != 1));
+usage(1) if defined $COMMAND and defined $CONFIG{watch};
+usage(1) if $CONFIG{target} and !(defined $COMMAND and ($COMMAND eq 'delete'or $COMMAND eq 'rename'));
+$CONFIG{watch} = 60 if defined $CONFIG{watch} and $CONFIG{watch} == 0;
@ARGV = map {uc $_ eq 'INBOX' ? 'INBOX' : $_ } @ARGV; # INBOX is case-insensitive
@@ -200,7 +205,8 @@ $DBH->do('PRAGMA foreign_keys = ON');
sub msg($@) {
my $name = shift;
return unless @_;
- logger($name, @_) if defined $LOGGER_FD and $LOGGER_FD->fileno != fileno STDERR;
+ logger($name, @_) if defined $LOGGER_FD and defined $LOGGER_FD->fileno
+ and $LOGGER_FD->fileno != fileno STDERR;
my $prefix = defined $name ? "$name: " : '';
print STDERR $prefix, @_, "\n";
}
@@ -208,7 +214,8 @@ sub logger($@) {
my $name = shift;
return unless @_ and defined $LOGGER_FD;
my $prefix = '';
- if ($LOGGER_FD->fileno != fileno STDERR) {
+ if (defined $LOGGER_FD and defined $LOGGER_FD->fileno
+ and $LOGGER_FD->fileno != fileno STDERR) {
my ($s, $us) = Time::HiRes::gettimeofday();
$prefix = POSIX::strftime("%b %e %H:%M:%S", localtime($s)).".$us ";
}
@@ -221,6 +228,17 @@ logger(undef, ">>> $NAME $VERSION");
#############################################################################
# Connect to the local and remote IMAP servers
+my $LIST = '"" ';
+my @LIST_PARAMS;
+if (!defined $COMMAND or $COMMAND eq 'repair') {
+ $LIST = '('.uc($CONF->{_}->{'list-select-opts'}).') '.$LIST if defined $CONF->{_}->{'list-select-opts'};
+ $LIST .= (defined $CONF->{_}->{'list-mailbox'} ? '('.$CONF->{_}->{'list-mailbox'}.')' : '*') unless @ARGV;
+ @LIST_PARAMS = ('SUBSCRIBED', 'STATUS (UIDVALIDITY UIDNEXT HIGHESTMODSEQ)');
+}
+$LIST .= $#ARGV == 0 ? Net::IMAP::InterIMAP::quote($ARGV[0])
+ : ('('.join(' ',map {Net::IMAP::InterIMAP::quote($_)} @ARGV).')') if @ARGV;
+
+
my $IMAP;
foreach my $name (qw/local remote/) {
my %config = %{$CONF->{$name}};
@@ -246,19 +264,9 @@ foreach my $name (qw/local remote/) {
# XXX We shouldn't need to ask for STATUS responses here, and use
# NOTIFY's STATUS indicator instead. However Dovecot violates RFC
# 5464: http://dovecot.org/pipermail/dovecot/2015-July/101474.html
-
- my $list = '"" ';
- my @params;
- if (!defined $COMMAND or $COMMAND eq 'repair') {
- $list = '('.uc($CONF->{_}->{'list-select-opts'}).') '.$list if defined $CONF->{_}->{'list-select-opts'};
- $list .= (defined $CONF->{_}->{'list-mailbox'} ? '('.$CONF->{_}->{'list-mailbox'}.')' : '*') unless @ARGV;
- @params = ('SUBSCRIBED', 'STATUS (UIDVALIDITY UIDNEXT HIGHESTMODSEQ)');
- }
- $list .= $#ARGV == 0 ? Net::IMAP::InterIMAP::quote($ARGV[0])
- : ('('.join(' ',map {Net::IMAP::InterIMAP::quote($_)} @ARGV).')') if @ARGV;
- @{$IMAP->{$name}}{qw/mailboxes delims/} = $client->list($list, @params);
}
+@{$IMAP->{$_}}{qw/mailboxes delims/} = $IMAP->{$_}->{client}->list($LIST, @LIST_PARAMS) for qw/local remote/;
##############################################################################
#
@@ -410,7 +418,7 @@ elsif (defined $COMMAND and $COMMAND eq 'rename') {
# Synchronize mailbox and subscription lists
my @MAILBOXES;
-{
+sub sync_mailbox_list() {
my %mailboxes;
$mailboxes{$_} = 1 foreach keys %{$IMAP->{local}->{mailboxes}};
$mailboxes{$_} = 1 foreach keys %{$IMAP->{remote}->{mailboxes}};
@@ -497,8 +505,9 @@ my @MAILBOXES;
}
}
}
+
+sync_mailbox_list();
my ($lIMAP, $rIMAP) = map {$IMAP->{$_}->{client}} qw/local remote/;
-undef $IMAP;
#############################################################################
@@ -1150,8 +1159,6 @@ if (defined $COMMAND and $COMMAND eq 'repair') {
while(1) {
while(@MAILBOXES) {
- my $cache;
- my $update = 0;
if (defined $MAILBOX and ($lIMAP->is_dirty($MAILBOX) or $rIMAP->is_dirty($MAILBOX))) {
# $MAILBOX is dirty on either the local or remote mailbox
sync_messages($IDX, $MAILBOX);
@@ -1187,8 +1194,25 @@ while(1) {
}
}
# clean state!
- exit 0 unless defined $COMMAND and $COMMAND eq 'watch';
- wait_notifications(900);
+ exit 0 unless $CONFIG{watch};
+
+ # we need to issue a NOOP command or go back to AUTH state since the
+ # LIST command may not report the correct HIGHESTMODSEQ value for
+ # the mailbox currently selected.
+ if (defined $MAILBOX) {
+ # Prefer UNSELECT over NOOP commands as it requires a single command per cycle
+ if ($lIMAP->incapable('UNSELECT') or $rIMAP->incapable('UNSELECT')) {
+ $_->noop() foreach ($lIMAP, $rIMAP);
+ } else {
+ $_->unselect() foreach ($lIMAP, $rIMAP);
+ undef $MAILBOX;
+ }
+ }
+
+ sleep $CONFIG{watch};
+ # Refresh the mailbox list and status
+ @{$IMAP->{$_}}{qw/mailboxes delims/} = $IMAP->{$_}->{client}->list($LIST, @LIST_PARAMS) for qw/local remote/;
+ sync_mailbox_list();
}
END {