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

Chia-liang Kao clkao at bestpractical.com
Mon Oct 4 05:22:34 EDT 2010


The branch, master has been updated
       via  e2bb290ebd77688a002697150690995636ee0c7a (commit)
       via  2abdc9b4f6506824846f6f9fd25025e0a864f979 (commit)
      from  75ac7a9d505dccb82aa61ddc2af642aebb9ee18e (commit)

Summary of changes:
 .gitignore                                         |    5 +
 html/Admin/Queues/MailGate.html                    |  163 ++++++++++++++++++++
 .../MailGate/Admin/Elements/QueueTabs/Default      |   14 ++
 lib/RT/Extension/MailGate.pm                       |   28 +++-
 t/basic.t                                          |   60 ++++----
 5 files changed, 233 insertions(+), 37 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 html/Admin/Queues/MailGate.html
 create mode 100644 html/Callbacks/MailGate/Admin/Elements/QueueTabs/Default

- Log -----------------------------------------------------------------
commit 2abdc9b4f6506824846f6f9fd25025e0a864f979
Author: Chia-liang Kao <clkao at clkao.org>
Date:   Mon Oct 4 15:58:15 2010 +0800

    put message from imap client for testing.

diff --git a/lib/RT/Extension/MailGate.pm b/lib/RT/Extension/MailGate.pm
index 15b129f..f940555 100644
--- a/lib/RT/Extension/MailGate.pm
+++ b/lib/RT/Extension/MailGate.pm
@@ -6,19 +6,29 @@ our $VERSION = '0.1';
 use Net::IMAP::Simple;
 use Email::Simple;
 
+sub imap_from_opt {
+    my ($self, $opt) = @_;
+    my $imap = Net::IMAP::Simple->new($opt->{server}, use_ssl => $opt->{use_ssl}) or
+            die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";
+
+    # Log on
+    $imap->login($opt->{user},$opt->{pass}) or
+        die "Login failed: " . $imap->errstr . "\n";
+
+    return $imap;
+}
+
+sub append_message {
+    my ($self, $imap, $mailbox, $msg) = @_;
+    $imap->select($mailbox);
+    $imap->put($mailbox, $msg);
+}
 
 sub fetch_imap {
     my ($self, $cb, $opt) = @_;
 
     # Create the object
-    my $imap = Net::IMAP::Simple->new($opt->{server}, use_ssl => $opt->{use_ssl}) ||
-        die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";
-
-    # Log on
-    if (!$imap->login($opt->{user},$opt->{pass})) {
-        print STDERR "Login failed: " . $imap->errstr . "\n";
-        return 0;
-    }
+    my $imap = $self->imap_from_opt($opt);
 
     my $nm = $imap->select('INBOX');
 
@@ -26,7 +36,7 @@ sub fetch_imap {
         next if $imap->seen($i);
 
         my $processed = $cb->(
-            Email::Simple->new(join '', @{ $imap->top($i) } ) );
+            Email::Simple->new(join '', @{ $imap->get($i) } ) );
 
         if ($processed) {
             $imap->see($i);
diff --git a/t/basic.t b/t/basic.t
index bf8378b..b8473d8 100644
--- a/t/basic.t
+++ b/t/basic.t
@@ -21,33 +21,17 @@ package Demo::IMAP::Model;
 $INC{'Demo/IMAP/Model.pm'} = 1;
 use base 'Net::IMAP::Server::DefaultModel';
 
-sub init {
-    my $self = shift;
-    $self->root( Demo::IMAP::Mailbox->new() );
-    $self->root->add_child( name => "INBOX" );
-}
-
-package Demo::IMAP::Mailbox;
-use base qw/Net::IMAP::Server::Mailbox/;
-
-my $data = <<'EOF';
-From: jesse at example.com
-To: contact at example.com
-Subject: This is a test message!
+our $mailbox;
 
-Hello. I am executive assistant to the director of
-Bear Stearns, a failed investment Bank.  I have 
-access to USD6,000,000. ...
-EOF
-
-my $msg = Net::IMAP::Server::Message->new($data);
-
-sub load_data {
+sub init {
     my $self = shift;
-    $self->add_message($msg);
+    unless ($mailbox) {
+        $mailbox = Net::IMAP::Server::Mailbox->new();
+        $mailbox->add_child( name => "INBOX" );
+    }
+    $self->root( $mailbox );
 }
 
-
 package main;
 
 BEGIN {
@@ -57,13 +41,14 @@ BEGIN {
 }
 
 use RT;
-use RT::Test tests => 10;
+use RT::Test tests => 11;
 use RT::Test::Email;
 RT->Config->Set( LogToScreen => 'debug' );
 RT->Config->Set('Plugins',qw(RT::Extension::MailGate));
 
 my $imap_pid;
 
+
 use_ok('RT::Extension::MailGate');
 
 my $everyone_group = RT::Group->new( $RT::SystemUser );
@@ -75,14 +60,28 @@ ok( RT::Test->set_rights(
   },
 ), "Granted everybody the right to create tickets");
 
+sub append_test_message {
+    my ($imap, $msg) = @_;
+    RT::Extension::MailGate->append_message($imap, "INBOX", $msg || <<'EOF');
+From: jesse at example.com
+To: contact at example.com
+Subject: This is a test message!
+
+Hello. I am executive assistant to the director of
+Bear Stearns, a failed investment Bank.  I have 
+access to USD6,000,000. ...
+EOF
+}
 
 test_tcp(
     client => sub {
         my $port = shift;
         my $opt =
             { server => 'localhost:'.$port, use_ssl => 1,
-              user => 'test', pass => 'test', delete => 1, expluge => 1 };
+              user => 'test', pass => 'test', delete => 1, expunge => 1 };
 
+        my $imap = RT::Extension::MailGate->imap_from_opt($opt);
+        append_test_message($imap);
         my $queue = RT::Test->load_or_create_queue(
             Name              => 'Contact',
             CorrespondAddress => 'contact at example.com',
@@ -119,10 +118,16 @@ test_tcp(
         };
 
         my $cnt = 0;
-
         RT::Extension::MailGate->fetch_imap(
-            sub { ++$cnt }, $opt);
+            sub { ++$cnt; return 0 }, $opt);
         is($cnt, 0);
+
+        append_test_message($imap);
+
+        $cnt = 0;
+        RT::Extension::MailGate->fetch_imap(
+            sub { ++$cnt; return 0 }, $opt);
+        is($cnt, 1);
     },
     server => sub {
         my $port = shift;
@@ -136,4 +141,3 @@ test_tcp(
         )->run();
     }
 );
-

commit e2bb290ebd77688a002697150690995636ee0c7a
Author: Chia-liang Kao <clkao at clkao.org>
Date:   Mon Oct 4 16:16:50 2010 +0800

    checkpoint for basic ui.

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..aa7964e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+META.yml
+Makefile
+inc/
+pm_to_blib
+*~
diff --git a/html/Admin/Queues/MailGate.html b/html/Admin/Queues/MailGate.html
new file mode 100644
index 0000000..758a592
--- /dev/null
+++ b/html/Admin/Queues/MailGate.html
@@ -0,0 +1,163 @@
+<& /Admin/Elements/Header, Title => $title &>
+<& /Admin/Elements/QueueTabs, id => $QueueObj->id,
+    QueueObj => $QueueObj,
+    current_tab => 'Admin/Queues/MailGate.html?id='.$id,
+    Title => $title &>
+
+<& /Elements/ListActions, actions => \@results &>
+
+<h1>Mail Configuration</h1>
+
+% if ($QueueMailConfig) {
+    my ($comment) = $QueueObj->Attributes->Named( 'MailGateConfig-Comment' );
+    my ($correspond) = $QueueObj->Attributes->Named( 'MailGateConfig-Correspond' );
+#    { account => 'foo', mailbox => 'INBOX', matched_address => '*', }
+% }
+% else {
+No Configuration yet
+<h2>Add a new configuration:</h2>
+% if (!@$MailAccounts) {
+<p>
+Must configure mail accounts first.
+</p>
+% }
+% else {
+
+<form method="post">
+
+Action:
+<select name="action">
+<option value="Correspond" <% $ARGS{action} && $ARGS{action} eq 'Correspond' ? 'selected' : '' %>>Correspond</option>
+<option value="Comment" <% $ARGS{action} && $ARGS{action} eq 'Comment' ? 'selected' : '' %>>Comment</option>
+</select>
+Account:
+<select name="account">
+% for (@$MailAccounts) {
+% my $account_name = $_->Content->{name};
+<option value="<% $account_name %>" <% $ARGS{account} && $ARGS{account} eq $account_name ? 'selected' : '' %> ><% $account_name %></option>
+% }
+</select>
+
+Mailbox:
+<input type="text" name="mailbox" value="<% $ARGS{mailbox} || 'INBOX' %>">
+Matched address:
+<input type="text" name="matched_address" value="<% $ARGS{matched_address} || '*' %>">
+
+<& /Elements/Submit, Name => "TestMailConfig", Label => loc('Test Config') &>
+
+% if ($TestConfigResults) {
+<div class="error"><% $TestConfigResults->{Error} %></div>
+<div><%  Dumper($TestConfigResults->{Content}) %></div>
+% }
+
+<& /Elements/Submit, Name => "SaveMailConfig", Label => loc('Save Changes') &>
+
+
+</form>
+
+% }
+<h1>Available mail accounts</h1>
+
+% for (@$MailAccounts) {
+<form method="post">
+%   my $account = $_->Content;use Data::Dumper;
+<input type="submit" name="remove-<% $_->Id %>" value="Delete">
+<div><%  Dumper($account) %></div>
+</form>
+% }
+% }
+
+<h1>Add a new account</h1>
+<form method="post">
+<input type="hidden" name="AddMailAccount" value="1">
+<input type="hidden" name="type" value="imap">
+Name:<input type="text" name="name">
+Server and port:<input type="text" name="server">
+ssl?<input type="checkbox" name="use_ssl">
+username: <input type="text" name="user">
+password:<input type="text" name="pass">
+delete after fetch<input type="checkbox" name="delete">
+expunge after fetch<input type="checkbox" name="expunge">
+
+<& /Elements/Submit, Name => "Save", Label => loc('Save Changes') &>
+</form>
+<%init>
+
+my @results;
+my $QueueObj = RT::Queue->new($session{'CurrentUser'});
+$QueueObj->Load($id);
+
+my $title;
+
+if ($QueueObj->id) {
+    $title = loc("Configure Mail Gateway for queue [_1]", $QueueObj->Name);
+} else {
+    Abort(loc("Queue [_1] not found",$id));
+}
+
+my $QueueMailConfig;
+my $TestConfigResults;
+if (my @remove = map { m/remove-(.*)/ ? $1 : () } keys %ARGS) {
+    my $attr = RT::Attribute->new($session{'CurrentUser'});
+    for (@remove) {
+        $attr->Load($_);
+        my ($res, $msg) = $attr->Delete();
+        push @results, $msg;
+    }
+}
+if ($ARGS{AddMailAccount}) {
+    if ( $session{'CurrentUser'}->HasRight(Right => 'AdminQueue', Object => $RT::System) ) {
+        my @fields = qw(name type server use_ssl user pass delete expunge);
+        my $account = { map { $_ => $ARGS{$_} } @fields };
+        # XXX: setattribute always returns 1, there's no way to see if the AddAttribute actually worked
+        $RT::System->AddAttribute(Name => 'ExternalMailAccounts', Description => 'External Mail Account ', Content => $account);
+        push @results, loc("Account '[_1]' added", $account->{name});
+    }
+    else {
+        push @results, loc("No permission to create queues");
+    }
+}
+
+
+my $MailAccounts = [ $RT::System->Attributes->Named( 'ExternalMailAccounts' ) ];
+
+
+my $new_mail_config = { map { $_ => $ARGS{$_} } qw(action account mailbox matched_address) };
+if ($ARGS{SaveMailConfig}) {
+
+}
+
+if ($ARGS{TestMailConfig}) {
+    my ($opt) = grep { $_->{name} eq $new_mail_config->{account} } map { $_->Content } @$MailAccounts;
+
+delete $opt->{expunge};
+    my $imap = eval { RT::Extension::MailGate->imap_from_opt($opt) };
+    if ($imap) {
+
+my $data = <<'EOF';
+From: rt at example.com
+To: contact at example.com
+Subject: This is a test message!
+
+This is a automated generated test message for configuring your rt mail gateway.
+EOF
+
+    RT::Extension::MailGate->append_message($imap, "INBOX", $data);
+
+    RT::Extension::MailGate->fetch_imap(
+        sub {
+            my $es = shift;
+            push @{$TestConfigResults->{Content} ||= []}, { message => $es,
+                                                            action => '....' };
+            return 0;
+        }, $opt);
+    }
+    else {
+        $TestConfigResults->{Error} = $@;
+    }
+}
+
+</%init>
+<%args>
+$id => 1
+</%args>
diff --git a/html/Callbacks/MailGate/Admin/Elements/QueueTabs/Default b/html/Callbacks/MailGate/Admin/Elements/QueueTabs/Default
new file mode 100644
index 0000000..fc4c8a1
--- /dev/null
+++ b/html/Callbacks/MailGate/Admin/Elements/QueueTabs/Default
@@ -0,0 +1,14 @@
+<%init>
+if ($QueueObj) {
+
+    my $path = 'Admin/Queues/MailGate.html?id='. $QueueObj->Id;
+    $tabs->{'this'}{'subtabs'}{'z-mg'} = {
+		title => loc('Mail Gateway'),
+        path => $path
+	};
+}
+</%init>
+<%args>
+$QueueObj => undef
+$tabs => undef
+</%args>

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



More information about the Bps-public-commit mailing list