2013-04-12 12:31:58 -07:00
|
|
|
/* Copyright 2011-2013 Yorba Foundation
|
2011-09-30 17:29:03 -07:00
|
|
|
*
|
|
|
|
|
* This software is licensed under the GNU Lesser General Public License
|
2013-04-12 12:31:58 -07:00
|
|
|
* (version 2.1 or later). See the COPYING file in this distribution.
|
2011-09-30 17:29:03 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
MainLoop? main_loop = null;
|
|
|
|
|
int ec = 0;
|
2012-05-16 17:13:44 -07:00
|
|
|
Geary.Endpoint? endpoint = null;
|
2011-09-30 17:29:03 -07:00
|
|
|
Geary.Credentials? credentials = null;
|
|
|
|
|
Geary.ComposedEmail? composed_email = null;
|
|
|
|
|
|
|
|
|
|
async void main_async() throws Error {
|
2012-05-16 17:13:44 -07:00
|
|
|
Geary.Smtp.ClientSession session = new Geary.Smtp.ClientSession(endpoint);
|
2011-09-30 17:29:03 -07:00
|
|
|
|
|
|
|
|
Geary.Smtp.Greeting? greeting = yield session.login_async(credentials);
|
|
|
|
|
stdout.printf("%s\n", greeting.to_string());
|
|
|
|
|
|
2012-05-16 17:13:44 -07:00
|
|
|
for (int ctr = 0; ctr < arg_count; ctr++) {
|
|
|
|
|
string subj_msg = "#%d".printf(ctr + 1);
|
2013-03-08 14:54:59 -08:00
|
|
|
composed_email.subject = subj_msg;
|
2013-04-24 18:26:56 -07:00
|
|
|
|
|
|
|
|
if (Geary.String.is_empty(arg_file)) {
|
|
|
|
|
composed_email.body_text = subj_msg;
|
|
|
|
|
} else {
|
|
|
|
|
string contents;
|
|
|
|
|
FileUtils.get_contents(arg_file, out contents);
|
|
|
|
|
|
|
|
|
|
composed_email.body_text = contents;
|
|
|
|
|
}
|
2012-05-16 17:13:44 -07:00
|
|
|
|
2014-01-29 18:18:31 -08:00
|
|
|
Geary.RFC822.Message msg = new Geary.RFC822.Message.from_composed_email(composed_email, null);
|
2012-05-16 17:13:44 -07:00
|
|
|
stdout.printf("\n\n%s\n\n", msg.to_string());
|
|
|
|
|
|
2013-03-15 17:23:12 -07:00
|
|
|
yield session.send_email_async(msg.sender, msg);
|
2012-05-16 17:13:44 -07:00
|
|
|
|
|
|
|
|
stdout.printf("Sent email #%d\n", ctr);
|
|
|
|
|
}
|
2011-09-30 17:29:03 -07:00
|
|
|
|
2011-10-04 18:44:18 -07:00
|
|
|
Geary.Smtp.Response? logout = yield session.logout_async();
|
|
|
|
|
stdout.printf("%s\n", logout.to_string());
|
2011-09-30 17:29:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void on_main_completed(Object? object, AsyncResult result) {
|
|
|
|
|
try {
|
|
|
|
|
main_async.end(result);
|
|
|
|
|
} catch (Error err) {
|
|
|
|
|
stderr.printf("%s\n", err.message);
|
|
|
|
|
ec = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (main_loop != null)
|
|
|
|
|
main_loop.quit();
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-16 17:13:44 -07:00
|
|
|
string arg_hostname;
|
|
|
|
|
int arg_port = 25;
|
2012-05-24 12:14:05 -07:00
|
|
|
bool arg_debug = false;
|
2012-05-16 17:13:44 -07:00
|
|
|
bool arg_gmail = false;
|
|
|
|
|
bool arg_no_tls = false;
|
|
|
|
|
string arg_user;
|
|
|
|
|
string arg_pass;
|
|
|
|
|
string arg_from;
|
|
|
|
|
string arg_to;
|
|
|
|
|
int arg_count = 1;
|
2013-04-24 18:26:56 -07:00
|
|
|
string? arg_file = null;
|
2012-05-16 17:13:44 -07:00
|
|
|
const OptionEntry[] options = {
|
2012-05-24 12:14:05 -07:00
|
|
|
{ "debug", 0, 0, OptionArg.NONE, ref arg_debug, "Output debugging information", null },
|
2012-05-16 17:13:44 -07:00
|
|
|
{ "host", 'h', 0, OptionArg.STRING, ref arg_hostname, "SMTP server host", "<hostname-or-dotted-address>" },
|
|
|
|
|
{ "port", 'P', 0, OptionArg.INT, ref arg_port, "SMTP server port", "<port-number>" },
|
|
|
|
|
{ "gmail", 'G', 0, OptionArg.NONE, ref arg_gmail, "Gmail SMTP (no-tls ignored)", null },
|
|
|
|
|
{ "no-tls", 'I', 0, OptionArg.NONE, ref arg_no_tls, "Do not use TLS (insecure)", null },
|
|
|
|
|
{ "user", 'u', 0, OptionArg.STRING, ref arg_user, "SMTP server username", "<username>" },
|
|
|
|
|
{ "pass", 'p', 0, OptionArg.STRING, ref arg_pass, "SMTP server password", "<password>" },
|
|
|
|
|
{ "from", 'f', 0, OptionArg.STRING, ref arg_from, "From (sender)", "<email>" },
|
|
|
|
|
{ "to", 't', 0, OptionArg.STRING, ref arg_to, "To (recipient)", "<email>" },
|
|
|
|
|
{ "count", 'c', 0, OptionArg.INT, ref arg_count, "Number of emails to send", null },
|
2013-04-24 18:26:56 -07:00
|
|
|
{ "file", 'i', 0, OptionArg.STRING, ref arg_file, "File to send (must be RFC 822 ready!)", "<filename>"},
|
2012-05-16 17:13:44 -07:00
|
|
|
{ null }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool verify_required(string? arg, string name) {
|
|
|
|
|
if (!Geary.String.is_empty(arg))
|
|
|
|
|
return true;
|
2011-09-30 17:29:03 -07:00
|
|
|
|
2012-05-16 17:13:44 -07:00
|
|
|
stdout.printf("%s required\n", name);
|
2011-09-30 17:29:03 -07:00
|
|
|
|
2012-05-16 17:13:44 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(string[] args) {
|
|
|
|
|
var context = new OptionContext("");
|
|
|
|
|
context.set_help_enabled(true);
|
|
|
|
|
context.add_main_entries(options, null);
|
|
|
|
|
try {
|
|
|
|
|
context.parse(ref args);
|
|
|
|
|
} catch (Error err) {
|
|
|
|
|
error ("Failed to parse command line: %s", err.message);
|
|
|
|
|
}
|
2011-09-30 17:29:03 -07:00
|
|
|
|
2012-05-16 17:13:44 -07:00
|
|
|
if (!arg_gmail && !verify_required(arg_hostname, "Hostname"))
|
|
|
|
|
return 1;
|
2011-10-07 17:33:34 -07:00
|
|
|
|
2012-05-16 17:13:44 -07:00
|
|
|
if (!verify_required(arg_from, "From:"))
|
2011-09-30 17:29:03 -07:00
|
|
|
return 1;
|
|
|
|
|
|
2012-05-16 17:13:44 -07:00
|
|
|
if (!verify_required(arg_to, "To:"))
|
|
|
|
|
return 1;
|
2011-10-07 17:33:34 -07:00
|
|
|
|
2012-05-16 17:13:44 -07:00
|
|
|
if (!verify_required(arg_user, "Username"))
|
|
|
|
|
return 1;
|
2011-09-30 17:29:03 -07:00
|
|
|
|
2012-05-16 17:13:44 -07:00
|
|
|
if (!verify_required(arg_pass, "Password"))
|
|
|
|
|
return 1;
|
2011-09-30 17:29:03 -07:00
|
|
|
|
2012-05-16 17:13:44 -07:00
|
|
|
if (arg_count < 1)
|
|
|
|
|
arg_count = 1;
|
|
|
|
|
|
|
|
|
|
if (arg_gmail) {
|
2012-05-24 12:14:05 -07:00
|
|
|
endpoint = new Geary.Endpoint("smtp.gmail.com", Geary.Smtp.ClientConnection.DEFAULT_PORT_STARTTLS,
|
|
|
|
|
Geary.Endpoint.Flags.STARTTLS | Geary.Endpoint.Flags.GRACEFUL_DISCONNECT,
|
2012-05-16 17:13:44 -07:00
|
|
|
Geary.Smtp.ClientConnection.DEFAULT_TIMEOUT_SEC);
|
|
|
|
|
} else {
|
|
|
|
|
Geary.Endpoint.Flags flags = Geary.Endpoint.Flags.GRACEFUL_DISCONNECT;
|
|
|
|
|
if (!arg_no_tls)
|
2012-05-24 12:14:05 -07:00
|
|
|
flags |= Geary.Endpoint.Flags.SSL;
|
2011-09-30 17:29:03 -07:00
|
|
|
|
2012-05-16 17:13:44 -07:00
|
|
|
endpoint = new Geary.Endpoint(arg_hostname, (uint16) arg_port, flags,
|
|
|
|
|
Geary.Smtp.ClientConnection.DEFAULT_TIMEOUT_SEC);
|
2011-09-30 17:29:03 -07:00
|
|
|
}
|
2012-05-24 12:14:05 -07:00
|
|
|
|
|
|
|
|
stdout.printf("Enabling debug: %s\n", arg_debug.to_string());
|
|
|
|
|
if (arg_debug)
|
|
|
|
|
Geary.Logging.log_to(stdout);
|
|
|
|
|
|
2012-05-16 17:13:44 -07:00
|
|
|
credentials = new Geary.Credentials(arg_user, arg_pass);
|
|
|
|
|
|
2011-10-07 17:33:34 -07:00
|
|
|
composed_email = new Geary.ComposedEmail(new DateTime.now_local(),
|
2012-05-16 17:13:44 -07:00
|
|
|
new Geary.RFC822.MailboxAddresses.single(new Geary.RFC822.MailboxAddress(null, arg_from)));
|
2011-10-07 17:33:34 -07:00
|
|
|
composed_email.to = new Geary.RFC822.MailboxAddresses.single(
|
2012-05-16 17:13:44 -07:00
|
|
|
new Geary.RFC822.MailboxAddress(null, arg_to));
|
2011-09-30 17:29:03 -07:00
|
|
|
|
|
|
|
|
main_loop = new MainLoop();
|
|
|
|
|
|
|
|
|
|
main_async.begin(on_main_completed);
|
|
|
|
|
|
|
|
|
|
main_loop.run();
|
|
|
|
|
|
|
|
|
|
return ec;
|
|
|
|
|
}
|
|
|
|
|
|