| |
|
BCS
| /* * Hacked this together, though y'all might like it. <g>* */
module email;
private import std.date;
private import std.string;
private import std.socket;
private import std.socketstream;
//private import std.stdio;
/**
encapsulate an e-mail logging system.
usage:
declare at global scope:
EmailMessage email;
as first lines in main:
email = new EmailMessage(SMTPSrv, to, from, Sub);
scope(failure) email.send();
as first line in every other function put:
scope(failure)email.log(__FILE__,__LINE__,<some message>);
at other important points put in:
email.log(__FILE__,__LINE__,<some message>);
*/
class EmailMessage
{
char[] server, to, from, subject;
char[][] message;
int at;
/**
arg srv: the outbound SMTP server
arg t: the to address
arg f: the from address
arg sub: the subject
*/
this(char[] srv, char[] t, char[] f, char[] sub)
{
server = srv.dup;
to = t.dup;
from = f.dup;
subject = sub.dup;
at=0;
}
/**
arg file: __FILE__
arg line: __LINE__
agr msg: the message to print
*/
void log(char[] file, int line, char[] msg)
{
if(message.length <= at)
message.length = at+10;
auto time = getUTCtime();
message[at] = (
file ~ ":" ~
std.string.toString(line)~"@"~
toTimeString(time)~"("~
std.string.toString(time%TicksPerSecond)~
") >"~msg).dup;
at++;
}
/** sne off the message */
void send()
{
Stream outs;
auto sock = new Socket(AddressFamily.INET, SocketType.STREAM);
sock.connect(new InternetAddress(server, 25));
outs = new SocketStream(sock);
outs.writeString("HELO none@anon.org\r\n"
"MAIL FROM:<"~from~">\r\n"
"RCPT TO:<"~to~">\r\n"
"DATA\r\n"
"Subject: "~subject~\r\n
"Content-Type: text/plain; charset=us-ascii\r\n\r\n");
foreach(line; message[0..at])
{
if("." == line)
{
outs.writeString("..\r\n");
}
else
{
outs.writeString(line~\r\n);
}
}
outs.writeString(".\r\nQUIT\r\n");
outs.close;
at = 0;
}
}
/***EXAMPLE****/
private import std.compiler;
EmailMessage email;
void main()
{
email = new EmailMessage("10.0.0.1", "sendto@10.0.0.1", "sendto@10.0.0.1", "a test of somthing");
email.log(__FILE__,__LINE__,"Built@"__DATE__" "__TIME__);
email.log(__FILE__,__LINE__,std.compiler.name);
scope(failure)
{
writef("sending error report\n");
email.send();
}
email.log(__FILE__,__LINE__,"Global catch");
writef("going\n");
foo(30);
writef("done\n");
}
void foo(int i)
{
scope(failure)email.log(__FILE__,__LINE__,"foo catch");
if(i) bar(i-1);
throw new Error("foo");
}
void bar(int i)
{
scope(failure)email.log(__FILE__,__LINE__,"foo catch");
if(i) baz(i-1);
throw new Error("foo");
}
void baz(int i)
{
scope(failure)email.log(__FILE__,__LINE__,"foo catch");
if(i) foo(i-1);
throw new Error("foo");
}
|