BastliBridge

git clone git://xatko.vsos.ethz.ch/BastliBridge.git
Log | Files | Refs | Submodules | README

mail.d (1424B)


      1 module bastlibridge.interfaces.mail;
      2 import bastlibridge.base;
      3 import std.process;
      4 import std.format;
      5 import std.algorithm;
      6 import std.concurrency;
      7 
      8 
      9 final class Mail: Endpoint{
     10 	string shellcmd;
     11 	__gshared Tid tid;
     12 	
     13 	this(Manager m, string args){
     14 		super(m,args);
     15 		shellcmd=args.idup;
     16 	}
     17 	
     18 	override void open(){
     19 		
     20 	}
     21 	
     22 	override Channel join(string c){
     23 		return new MailAddress();
     24 	}
     25 	
     26 	override void part(Channel c){
     27 		
     28 	}
     29 	
     30 	override void sendMessage(Message m, Channel c){
     31 		auto msg=m.getMessage();
     32 		if(msg.length==0){
     33 			info("Dropping empty message from mail endpoint");
     34 			return;
     35 		}
     36 		trace("Sending mail to ", c._name, " with command \"", shellcmd, `"`);
     37 		auto pipe=pipeShell(shellcmd);
     38 		trace("Process ", pipe.pid, " opened");
     39 		formattedWrite(pipe.stdin.lockingTextWriter, "Content-Type: text/plain; charset=utf-8\nTo: %s\nSubject: New message on %s\n\n%s\nAuthor: %s", c._name, m.getChannelName, m.getMessage(), m.userName());
     40 		trace("Text written to stdin");
     41 		pipe.stdin.close();
     42 		pipe.stdout.close();
     43 		trace("Closed pipes, waiting for termination of ", pipe.pid);
     44 		if(pipe.pid.wait()!=0){
     45 			warning("Sending mail failed");
     46 		}
     47 		else{
     48 			trace("Mail successfully sent");
     49 		}
     50 	}
     51 	
     52 	override void run(){
     53 		synchronized(this){
     54 			tid=thisTid();
     55 		}
     56 		while(!receiveOnly!bool()){}
     57 	}
     58 
     59 	override void stop(){
     60 		synchronized(this){
     61 			send(tid, true);
     62 		}
     63 	}
     64 }
     65 
     66 final class MailAddress : Channel{
     67 	
     68 }