started adding email processing capabilities to cs
This commit is contained in:
parent
4c3d3c68e1
commit
504de54d61
10 changed files with 352 additions and 97 deletions
195
lib/WebGUI/Workflow/Activity/GetCsMail.pm
Normal file
195
lib/WebGUI/Workflow/Activity/GetCsMail.pm
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
package WebGUI::Workflow::Activity::GetCsMail;
|
||||
|
||||
|
||||
=head1 LEGAL
|
||||
|
||||
-------------------------------------------------------------------
|
||||
WebGUI is Copyright 2001-2006 Plain Black Corporation.
|
||||
-------------------------------------------------------------------
|
||||
Please read the legal notices (docs/legal.txt) and the license
|
||||
(docs/license.txt) that came with this distribution before using
|
||||
this software.
|
||||
-------------------------------------------------------------------
|
||||
http://www.plainblack.com info@plainblack.com
|
||||
-------------------------------------------------------------------
|
||||
|
||||
=cut
|
||||
|
||||
use strict;
|
||||
use base 'WebGUI::Workflow::Activity';
|
||||
use WebGUI::Mail::Get;
|
||||
use WebGUI::Asset;
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Package WebGUI::Workflow::Activity::GetCsMail
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
Retrieve the incoming mail messages for a Collaboration System.
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
See WebGUI::Workflow::Activity for details on how to use any activity.
|
||||
|
||||
=head1 METHODS
|
||||
|
||||
These methods are available from this class:
|
||||
|
||||
=cut
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 addPost ( parent, class, message, user )
|
||||
|
||||
Adds a post to this collaboration system.
|
||||
|
||||
=head3 parent
|
||||
|
||||
Either a collaboration system object reference, or a thread/post object reference.
|
||||
|
||||
=head3 message
|
||||
|
||||
The message retrieved from WebGUI::Mail::Get.
|
||||
|
||||
=head3 user
|
||||
|
||||
The user doing the posting.
|
||||
|
||||
=cut
|
||||
|
||||
sub addPost {
|
||||
my $self = shift;
|
||||
my $parent = shift;
|
||||
my $message = shift;
|
||||
my $user = shift;
|
||||
my @attachments = ();
|
||||
my $content = "";
|
||||
my $class = (ref $parent eq "WebGUI::Asset::Wobject::Collaboration") ? "WebGUI::Asset::Post::Thread" : "WebGUI::Asset::Post";
|
||||
foreach my $part (@{$message->{parts}}) {
|
||||
if (($part->{type} eq "text/plain" || $part->{type} eq "text/html") && $part->{filename} eq "") {
|
||||
my $text = $message->{content};
|
||||
if ($part->{type} eq "text/plain") {
|
||||
$text =~ s/\n/\<br \/\>/g;
|
||||
}
|
||||
$content .= $text;
|
||||
} else {
|
||||
push(@attachments, $part);
|
||||
}
|
||||
}
|
||||
my $post = $parent->addChild({
|
||||
className=>$class,
|
||||
title=>$message->{subject},
|
||||
menuTitle => $message->{subject},
|
||||
url=>$message->{subject},
|
||||
content=>$content,
|
||||
ownerUserId=>$user->getId,
|
||||
username=>$user->profileField("alias") || $user->username,
|
||||
});
|
||||
if (scalar(@attachments)) {
|
||||
my $storage = $post->getStorageLocation;
|
||||
foreach my $file (@attachments) {
|
||||
my $filename = $file->{filename};
|
||||
unless ($filename) {
|
||||
$file->{type} =~ m/\/(.*)/;
|
||||
my $type = $1;
|
||||
$filename = $self->session->id->generate.".".$type;
|
||||
}
|
||||
$storage->addFileFromScalar($file->{content}, $filename);
|
||||
}
|
||||
}
|
||||
$post->postProcess;
|
||||
$post->requestCommit;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 definition ( session, definition )
|
||||
|
||||
See WebGUI::Workflow::Activity::defintion() for details.
|
||||
|
||||
=cut
|
||||
|
||||
sub definition {
|
||||
my $class = shift;
|
||||
my $session = shift;
|
||||
my $definition = shift;
|
||||
my $i18n = WebGUI::International->new($session, "Asset_Collaboration");
|
||||
push(@{$definition}, {
|
||||
name=>$i18n->get("get cs mail"),
|
||||
properties=> { }
|
||||
});
|
||||
return $class->SUPER::definition($session,$definition);
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 execute ( )
|
||||
|
||||
See WebGUI::Workflow::Activity::execute() for details.
|
||||
|
||||
=cut
|
||||
|
||||
sub execute {
|
||||
my $self = shift;
|
||||
my $cs = shift;
|
||||
return $self->COMPLETE unless ($cs->get("getMail"));
|
||||
my $start = time();
|
||||
my $mail = WebGUI::Mail::Get->connect($self->session,{
|
||||
server=>$cs->get("mailHost"),
|
||||
account=>$cs->get("mailAccount"),
|
||||
password=>$cs->get("mailUser")
|
||||
});
|
||||
return $self->COMPLETE unless (defined $mail);
|
||||
my $i18n = WebGUI::International->new($self->session, "Asset_Collaboration");
|
||||
while (my $message = $mail->getNextMessage) {
|
||||
next unless (scalar(@{$message->{parts}})); # no content, skip it
|
||||
my $from = $message->{from};
|
||||
$from =~ /<(\S+\@\S+)>/;
|
||||
$from = $1 || $from;
|
||||
$from =~ /(\S+\@\S+)/;
|
||||
my $user = WebGUI::User->newByEmail($self->session, $from);
|
||||
unless (defined $user) {
|
||||
my $send = WebGUI::Mail::Send->create($self->session, {
|
||||
to=>$message->{from},
|
||||
inReplyTo=>$message->{messageId},
|
||||
subject=>$cs->get("mailPrefix").$i18n->get("rejected")." ".$self->{subject},
|
||||
from=>$cs->get("mailAddress")
|
||||
});
|
||||
$send->addText($i18n->get("rejected because no user account"));
|
||||
$send->send;
|
||||
next;
|
||||
}
|
||||
my $post = undef;
|
||||
if ($message->{inReplyTo}) {
|
||||
$message->{inReplyTo} =~ m/WebGUI\-([\w_-]{22})/;
|
||||
my $id = $1;
|
||||
$post = WebGUI::Asset->newByDynamicClass($self->session, $id);
|
||||
}
|
||||
if (defined $post && $cs->get("allowReplies") && $user->isInGroup($cs->get("postGroupId")) && ($user->isInGroup($cs->get("subscriptionGroupId")) || $user->isInGroup($post->get("subscriptionGroupId")))) {
|
||||
$self->addPost($post, $message, $user);
|
||||
} elsif ($user->isInGroup($cs->get("postGroupId")) && $user->isInGroup($cs->get("subscriptionGroupId"))) {
|
||||
$self->addPost($cs, $message, $user);
|
||||
} else {
|
||||
my $send = WebGUI::Mail::Send->create($self->session, {
|
||||
to=>$message->{from},
|
||||
inReplyTo=>$message->{messageId},
|
||||
subject=>$cs->get("mailPrefix").$i18n->get("rejected")." ".$self->{subject},
|
||||
from=>$cs->get("mailAddress")
|
||||
});
|
||||
$send->addText($i18n->get("rejected because not allowed"));
|
||||
$send->send;
|
||||
}
|
||||
# just in case there are a lot of messages, we should release after a minutes worth of retrieving
|
||||
last if (time() > $start + 60);
|
||||
}
|
||||
$mail->disconnect;
|
||||
return $self->COMPLETE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
1;
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue