[Bps-public-commit] rt-extension-mailgate branch, master, updated. e3de82f09eb304123266f16942515e3f76570a82

Chia-liang Kao clkao at bestpractical.com
Thu Nov 18 09:40:01 EST 2010


The branch, master has been updated
       via  e3de82f09eb304123266f16942515e3f76570a82 (commit)
       via  5db7e015e1ca4bdf0675d36043bcc53eed0cb125 (commit)
       via  4decfe745a933c0c6be09bbe428bd7c5116d4a66 (commit)
       via  20be9946efe633d280c646b5ca3de2a446efb8d3 (commit)
       via  ca335368ac1858355c56d9976e092ac172d8b309 (commit)
      from  7d83c44ce22c25290dea2dd8ea5fb8911cfe66e8 (commit)

Summary of changes:
 html/Admin/Queues/MailGate.html |   75 ++++++++++++++++++++++++++++++++------
 lib/RT/Extension/MailGate.pm    |   13 +++++--
 t/basic.t                       |   38 +------------------
 t/lib/MailGate.pm               |   46 ++++++++++++++++++++++++
 4 files changed, 121 insertions(+), 51 deletions(-)
 create mode 100644 t/lib/MailGate.pm

- Log -----------------------------------------------------------------
commit ca335368ac1858355c56d9976e092ac172d8b309
Author: Chia-liang Kao <clkao at clkao.org>
Date:   Thu Nov 18 19:38:54 2010 +0800

    refactor test

diff --git a/t/basic.t b/t/basic.t
index b8473d8..9949a5d 100644
--- a/t/basic.t
+++ b/t/basic.t
@@ -4,35 +4,8 @@ use warnings;
 use Test::More;
 use Test::TCP;
 use Net::IMAP::Server;
-
-package Demo::IMAP::Auth;
-$INC{'Demo/IMAP/Auth.pm'} = 1;
-use base 'Net::IMAP::Server::DefaultAuth';
-
-sub auth_plain {
-    my ( $self, $user, $pass ) = @_;
-
-    # XXX DO AUTH CHECK
-    $self->user($user);
-    return 1;
-}
-
-package Demo::IMAP::Model;
-$INC{'Demo/IMAP/Model.pm'} = 1;
-use base 'Net::IMAP::Server::DefaultModel';
-
-our $mailbox;
-
-sub init {
-    my $self = shift;
-    unless ($mailbox) {
-        $mailbox = Net::IMAP::Server::Mailbox->new();
-        $mailbox->add_child( name => "INBOX" );
-    }
-    $self->root( $mailbox );
-}
-
 package main;
+require 't/lib/MailGate.pm';
 
 BEGIN {
     $ENV{LC_ALL} = $ENV{LANG} = 'en_US.UTF-8';
@@ -131,13 +104,6 @@ test_tcp(
     },
     server => sub {
         my $port = shift;
-        Net::IMAP::Server->new(
-            auth_class  => "Demo::IMAP::Auth",
-            model_class => "Demo::IMAP::Model",
-            server_key => 'certs/server-key.pem',
-            server_cert => 'certs/server-cert.pem',
-            port        => empty_port(20000),
-            ssl_port    => $port
-        )->run();
+        t::MailGate->run_server($port);
     }
 );
diff --git a/t/lib/MailGate.pm b/t/lib/MailGate.pm
new file mode 100644
index 0000000..e425757
--- /dev/null
+++ b/t/lib/MailGate.pm
@@ -0,0 +1,46 @@
+package t::MailGate::Auth;
+$INC{'t/MailGate/Auth.pm'} = 1;
+use base 'Net::IMAP::Server::DefaultAuth';
+
+sub auth_plain {
+    my ( $self, $user, $pass ) = @_;
+
+    # XXX DO AUTH CHECK
+    $self->user($user);
+    return 1;
+}
+
+package t::MailGate::Model;
+$INC{'t/MailGate/Model.pm'} = 1;
+use base 'Net::IMAP::Server::DefaultModel';
+
+our $mailbox;
+
+sub init {
+    my $self = shift;
+    unless ($mailbox) {
+        $mailbox = Net::IMAP::Server::Mailbox->new();
+        $mailbox->add_child( name => "INBOX" );
+    }
+    $self->root( $mailbox );
+}
+
+package t::MailGate;
+use Test::TCP qw(empty_port);
+use Net::IMAP::Server;
+
+sub run_server {
+    my $class = shift;
+    my $port = shift;
+
+    Net::IMAP::Server->new(
+        auth_class  => "t::MailGate::Auth",
+        model_class => "t::MailGate::Model",
+        server_key => 'certs/server-key.pem',
+        server_cert => 'certs/server-cert.pem',
+        port        => empty_port(20000),
+        ssl_port    => $port
+    )->run();
+}
+
+1;

commit 20be9946efe633d280c646b5ca3de2a446efb8d3
Author: Chia-liang Kao <clkao at clkao.org>
Date:   Thu Nov 18 19:39:21 2010 +0800

    be graceful when failing imap connection

diff --git a/html/Admin/Queues/MailGate.html b/html/Admin/Queues/MailGate.html
index 003fba7..499cce3 100644
--- a/html/Admin/Queues/MailGate.html
+++ b/html/Admin/Queues/MailGate.html
@@ -80,7 +80,10 @@ Subject: <input type="text" name="TestMailSubject" value="<% $ARGS{TestMailSubje
 <& /Elements/Submit, Name => "CreateAndTestMailConfig", Label => loc('Create a Test Mail and Test Config') &>
 
 % if ($TestConfigResults) {
-<div class="error"><% $TestConfigResults->{Error} || '' %></div>
+% if ($TestConfigResults->{Error}) {
+<div class="error">Error: <% $TestConfigResults->{Error} || '' %></div>
+% }
+% else {
 <div>
 % unless (@{$TestConfigResults->{Content}}) {
 <div>No message for testing</div>
@@ -98,6 +101,7 @@ Subject: <input type="text" name="TestMailSubject" value="<% $ARGS{TestMailSubje
 % }
 </div>
 % }
+% }
 
 <& /Elements/Submit, Name => "SaveMailConfig", Label => loc('Save Changes') &>
 
@@ -215,6 +219,8 @@ EOF
              $TestConfigResults->{Error} = $@;
          }
     }
+
+    unless ($TestConfigResults->{Error}) {
     RT::Extension::MailGate->fetch_imap(
         sub {
             my $es = shift;
@@ -224,6 +230,7 @@ EOF
                                                             $action ? ( delete => $opt->{delete} ) : () };
             return 0;
         }, $opt);
+    }
 }
 
 

commit 4decfe745a933c0c6be09bbe428bd7c5116d4a66
Author: Chia-liang Kao <clkao at clkao.org>
Date:   Thu Nov 18 21:40:59 2010 +0800

    show errors

diff --git a/html/Admin/Queues/MailGate.html b/html/Admin/Queues/MailGate.html
index 499cce3..621de73 100644
--- a/html/Admin/Queues/MailGate.html
+++ b/html/Admin/Queues/MailGate.html
@@ -1,4 +1,8 @@
 <style type="text/css" media="screen">
+div.error {
+ color: red;
+}
+
 span.mail-action {
  float: left;
  width: 200px;
@@ -221,15 +225,19 @@ EOF
     }
 
     unless ($TestConfigResults->{Error}) {
-    RT::Extension::MailGate->fetch_imap(
-        sub {
-            my $es = shift;
-            my $action = RT::Extension::MailGate->mail_action(\@mailcfg, $opt, $es);
-            push @{$TestConfigResults->{Content} ||= []}, { message => $es,
-                                                            action => $action,
-                                                            $action ? ( delete => $opt->{delete} ) : () };
-            return 0;
-        }, $opt);
+
+        eval {
+            RT::Extension::MailGate->fetch_imap(
+                sub {
+                    my $es = shift;
+                    my $action = RT::Extension::MailGate->mail_action(\@mailcfg, $opt, $es);
+                    push @{$TestConfigResults->{Content} ||= []}, { message => $es,
+                                                                    action => $action,
+                                                                    $action ? ( delete => $opt->{delete} ) : () };
+                    return 0;
+                }, $opt);
+        };
+        $TestConfigResults->{Error} = $@ if $@;
     }
 }
 

commit 5db7e015e1ca4bdf0675d36043bcc53eed0cb125
Author: Chia-liang Kao <clkao at clkao.org>
Date:   Thu Nov 18 22:13:49 2010 +0800

    hint about Everyone's createticket grouprights

diff --git a/html/Admin/Queues/MailGate.html b/html/Admin/Queues/MailGate.html
index 621de73..708a997 100644
--- a/html/Admin/Queues/MailGate.html
+++ b/html/Admin/Queues/MailGate.html
@@ -89,7 +89,7 @@ Subject: <input type="text" name="TestMailSubject" value="<% $ARGS{TestMailSubje
 % }
 % else {
 <div>
-% unless (@{$TestConfigResults->{Content}}) {
+% unless (@{$TestConfigResults->{Content} ||= []}) {
 <div>No message for testing</div>
 % }
 % for my $res (@{$TestConfigResults->{Content}}) {
@@ -144,6 +144,14 @@ my @results;
 my $QueueObj = RT::Queue->new($session{'CurrentUser'});
 $QueueObj->Load($id);
 
+my $everyone_group;
+$everyone_group = RT::Group->new( RT->SystemUser );
+$everyone_group->LoadSystemInternalGroup( 'Everyone' );
+my $everyone_rights_hint = 0;
+unless ($everyone_group->Id && $everyone_group->PrincipalObj->HasRight( Right => 'CreateTicket', Object => $QueueObj ) ) {
+    push @results, "The system group 'Everyone' does not have the 'CreateTicket' right for this queue.  Click the 'Group Rights' tab to change the configuration";
+}
+
 my $title;
 
 if ($QueueObj->id) {

commit e3de82f09eb304123266f16942515e3f76570a82
Author: Chia-liang Kao <clkao at clkao.org>
Date:   Thu Nov 18 22:31:43 2010 +0800

    a fetch mail button.

diff --git a/html/Admin/Queues/MailGate.html b/html/Admin/Queues/MailGate.html
index 708a997..48188ce 100644
--- a/html/Admin/Queues/MailGate.html
+++ b/html/Admin/Queues/MailGate.html
@@ -77,6 +77,7 @@ Matched address:
 <input type="text" name="matched_address" value="<% $ARGS{matched_address} || '.*' %>">
 
 <& /Elements/Submit, Name => "TestMailConfig", Label => loc('Test Config') &>
+<& /Elements/Submit, Name => "FetchMail", Label => loc('Fetch Mail Now') &>
 To: <input type="text" name="TestMailTo" value="<% $ARGS{TestMailTo} || $QueueObj->CorrespondAddress %>">
 Cc: <input type="text" name="TestMailCc" value="<% $ARGS{TestMailCc} || '' %>">
 Bcc: <input type="text" name="TestMailBcc" value="<% $ARGS{TestMailBcc} || '' %>">
@@ -189,7 +190,7 @@ my $MailAccounts = [ $RT::System->Attributes->Named( 'ExternalMailAccounts' ) ];
 my $new_mail_config = { map { $_ => $ARGS{$_} } qw(action account mailbox matched_address) };
 
 my @mailcfg;
-if ($ARGS{TestMailConfig} || $ARGS{CreateAndTestMailConfig} || $ARGS{SaveMailConfig}) {
+if ($ARGS{TestMailConfig} || $ARGS{CreateAndTestMailConfig} || $ARGS{SaveMailConfig} || $ARGS{FetchMail}) {
     @mailcfg = RT::Extension::MailGate->load_mailconfig( $new_mail_config->{account}, $new_mail_config->{mailbox} );
     push @mailcfg, { %$new_mail_config, queue => $id } if $new_mail_config->{action};
 
@@ -209,6 +210,31 @@ if ($ARGS{SaveMailConfig} && $new_mail_config->{action}) {
 my $QueueMailConfig = [ $QueueObj->Attributes->Named('MailGateConfig') ];
 
 
+my $message_processed = 0;
+if ($ARGS{FetchMail}) {
+    my ($opt) = grep { $_->{name} eq $new_mail_config->{account} } map { $_->Content } @$MailAccounts;
+
+    eval {
+        RT::Extension::MailGate->fetch_imap(
+            sub {
+                my $es = shift;
+                my ( $status, $error_message, $ticket ) =
+                  RT::Extension::MailGate->process_message(\@mailcfg, $opt, $es);
+
+                if ($status > 0) {
+                    ++$message_processed;
+                    return 1;
+                }
+                else {
+                    push @results, "fail: $error_message";
+                    return 0;
+                }
+            }, $opt);
+    };
+    $TestConfigResults->{Error} = $@ if $@;
+    push @results, loc("[_1] messages processed", $message_processed);
+
+}
 
 if ($ARGS{TestMailConfig} || $ARGS{CreateAndTestMailConfig}) {
     my ($opt) = grep { $_->{name} eq $new_mail_config->{account} } map { $_->Content } @$MailAccounts;
@@ -238,9 +264,11 @@ EOF
             RT::Extension::MailGate->fetch_imap(
                 sub {
                     my $es = shift;
-                    my $action = RT::Extension::MailGate->mail_action(\@mailcfg, $opt, $es);
+                    my ($action, $queue) = RT::Extension::MailGate->mail_action(\@mailcfg, $opt, $es);
+
                     push @{$TestConfigResults->{Content} ||= []}, { message => $es,
-                                                                    action => $action,
+                                                                    (action => $action ? loc("[_1] in Queue [_2]", loc($action), $queue)
+                                                                                      : 'Skip'),
                                                                     $action ? ( delete => $opt->{delete} ) : () };
                     return 0;
                 }, $opt);
diff --git a/lib/RT/Extension/MailGate.pm b/lib/RT/Extension/MailGate.pm
index 956bdc6..1fd9676 100644
--- a/lib/RT/Extension/MailGate.pm
+++ b/lib/RT/Extension/MailGate.pm
@@ -40,10 +40,17 @@ sub mail_action {
         next unless $mail->header('To') =~ $_->{matched_address};
         my $q = RT::Queue->new(RT->SystemUser);
         $q->Load($_->{queue});
-        return "$_->{action} in Queue '@{[ $q->Name ]}'";
-#$self->loc("[_1] in Queue [_2]", $self->loc($_->{action}), $q->Name);
+        return ($_->{action}, $q->Name);
     }
-    return 'Skip';
+    return;
+}
+
+
+sub process_message {
+    my ($self, $mailcfg, $opt, $es) = @_;
+    my ($action, $queue) = RT::Extension::MailGate->mail_action($mailcfg, $opt, $es);
+
+    return RT::Interface::Email::Gateway( { queue => $queue, action => lc($action), message => $es->as_string } );
 }
 
 sub fetch_imap {

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list