Skip to content
This repository has been archived by the owner on Oct 6, 2019. It is now read-only.

Commit

Permalink
Cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Hansen committed Jul 1, 2018
1 parent 078ba30 commit 976ba06
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 52 deletions.
13 changes: 6 additions & 7 deletions plugins/extras/p5.mail/Pop3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ public static void p5_pop3_get (ApplicationContext context, ActiveEventArgs e)
// Making sure we're able to post QUIT signal when done, regardless of what happens inside of this code.
try {

// Figuring out how many messages to retrieve, defaulting to "5" if not explicitly told something else by caller,
// Figuring out how many messages to retrieve, defaulting to "5", if not explicitly told something else by caller,
// making sure we never try to retrieve more messages than server actually has.
int noMessages = Math.Min (client.Count, e.Args.GetExChildValue ("count", context, 5));
var noMessages = Math.Min (client.Count, e.Args.GetExChildValue ("count", context, 5));

// Fetching messages from server, but not any more messages than caller requested, or number of available messages.
for (int idxMsg = 0; idxMsg < noMessages; idxMsg++) {
for (var idxMsg = 0; idxMsg < noMessages; idxMsg++) {

// Process message returned from POP3 server by building Node structure wrapping message.
var msgNode = ProcessMessage (
Expand All @@ -103,6 +103,9 @@ public static void p5_pop3_get (ApplicationContext context, ActiveEventArgs e)
// Invoking lambda callback, letting caller do his thing.
context.RaiseEvent ("eval-mutable", lambda);
}

// Disconnecting from server, making sure we send the QUIT signal.
client.Disconnect (true);

} catch {

Expand All @@ -111,10 +114,6 @@ public static void p5_pop3_get (ApplicationContext context, ActiveEventArgs e)
client.Disconnect (false);
throw;

} finally {

// Disconnecting from server, making sure we send the QUIT signal.
client.Disconnect (true);
}
}
}
Expand Down
22 changes: 5 additions & 17 deletions plugins/extras/p5.mail/Smtp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ public static void p5_smtp_send (ApplicationContext context, ActiveEventArgs e)
/*
* Sends all [envelopes] found.
*/
static void SendMessages (
ApplicationContext context,
Node args,
SmtpClient client)
static void SendMessages (ApplicationContext context, Node args, SmtpClient client)
{
// Looping through each message caller wants to send.
foreach (var idxEnvelopeNode in args.Children.Where (ix => ix.Name == "envelope")) {
Expand Down Expand Up @@ -113,10 +110,7 @@ static void SendMessages (
/*
* Creates and decorates MimeMessage according to given args.
*/
static MimeMessage CreateMessage (
ApplicationContext context,
Node envelopeNode,
List<Stream> streams)
static MimeMessage CreateMessage (ApplicationContext context, Node envelopeNode, List<Stream> streams)
{
// Creating message to return.
var message = new MimeMessage ();
Expand All @@ -128,7 +122,7 @@ static MimeMessage CreateMessage (
DecorateMessageEnvelope (context, envelopeNode, message);

// Retrieving [body] node of envelope, and doing basic syntax checking.
Node body = envelopeNode ["body"];
var body = envelopeNode ["body"];
if (body == null)
throw new LambdaException (
"No [body] found inside of [envelope]",
Expand All @@ -148,10 +142,7 @@ static MimeMessage CreateMessage (
/*
* Decorates headers of MimeMessage.
*/
static void DecorateMessageEnvelope (
ApplicationContext context,
Node args,
MimeMessage message)
static void DecorateMessageEnvelope (ApplicationContext context, Node args, MimeMessage message)
{
message.Subject = args.GetExChildValue ("Subject", context, "");

Expand Down Expand Up @@ -196,10 +187,7 @@ static void DecorateMessageEnvelope (
/*
* Retrieves all emails beneath the args node's child with the given name.
*/
static IEnumerable<MailboxAddress> GetAddresses (
ApplicationContext context,
Node args,
string name)
static IEnumerable<MailboxAddress> GetAddresses (ApplicationContext context, Node args, string name)
{
// Checking there exist a node with supplied name on args.
if (args [name] != null) {
Expand Down
36 changes: 8 additions & 28 deletions plugins/extras/p5.mail/helpers/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,12 @@ public static string GetBaseFolder (ApplicationContext context)
/// <param name="client">MailService type</param>
/// <param name="args">Active Event Arguments</param>
/// <param name="serverType">pop3 or smtp</param>
public static void ConnectServer (
ApplicationContext context,
MailService client,
Node args,
string serverType)
public static void ConnectServer (ApplicationContext context, MailService client, Node args, string serverType)
{
// Retrieving server settings, defaulting to those found in web.config, if not explicitly overridden.
string server =
args.GetExChildValue<string> ("server", context) ??
"localhost";
int port = args ["port"] != null ?
args.GetExChildValue<int> ("port", context) :
25;
bool useSsl = args ["ssl"] != null ?
args.GetExChildValue<bool> ("ssl", context) :
false;
var server = args.GetExChildValue ("server", context, "localhost");
var port = args.GetExChildValue ("port", context, 25);
bool useSsl = args.GetExChildValue ("ssl", context, false);

// Connecting client to server
client.Connect (
Expand All @@ -75,20 +65,10 @@ public static void ConnectServer (
// Fuck OATH2!! [quote; its creator!]
client.AuthenticationMechanisms.Remove ("XOAUTH2");

// Finding username and password to use to authenticate
string username = null, password = null;

// Checking if caller supplied username and password, and if so, using those credentials
if (args ["username"] != null) {

// Notice, this logic allows caller to supply null or empty password, in case specified server does
// not require authorisation to send emails, in which case, client.Authenticate below will never be invoked!
username = args.GetExChildValue ("username", context, "");
password = args.GetExChildValue ("password", context, "");

// Authenticating, unless username is empty or null
if (!string.IsNullOrEmpty (username))
client.Authenticate (username, password);
// Authenticating user, if credentials were supplied.
var username = args.GetExChildValue ("username", context, "");
if (!string.IsNullOrEmpty (username)) {
client.Authenticate (username, args.GetExChildValue ("password", context, ""));
}
}
}
Expand Down

0 comments on commit 976ba06

Please sign in to comment.