commit 27667e2dcb93be886bb4bb5edfa3b1e326c9ae17
parent f7bd580d71c990bb9bee732b90579562617a5079
Author: Dominik Schmidt <das1993@hotmail.com>
Date: Thu, 5 Oct 2017 12:55:14 +0200
Open a control socket instead of listening to stdin.
Diffstat:
src/bot.d | | | 80 | +++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------- |
1 file changed, 54 insertions(+), 26 deletions(-)
diff --git a/src/bot.d b/src/bot.d
@@ -69,10 +69,13 @@ struct Watcher{
struct Bot{
Address ircAddress;
Socket ircSocket;
+ Socket controlSocket;
+ string controlPath;
+ Address controlAddress;
IrcClient ircClient;
Telegram telegram, telegram_listener;
LookupTable lut;
- Watcher w_irc,w_tele,w_stdin;
+ Watcher w_irc,w_tele,w_ctrl;
ev_loop_t *eventloop;
string proxy_url;
@@ -212,6 +215,9 @@ struct Bot{
ircSocket=new SslSocket(af);
ircClient=new IrcClient(ircSocket);
+ controlAddress = new UnixAddress(controlPath);
+ controlSocket = new Socket(AddressFamily.UNIX, SocketType.STREAM);
+
//lut.connect("#bastli_wasserstoffreakteur", -8554836);
@@ -356,6 +362,9 @@ struct Bot{
//ircClient.connect(new InternetAddress("127.0.0.1",6667));
info("Connecting to IRC");
ircClient.connect(ircAddress);
+
+ controlSocket.bind(controlAddress);
+ controlSocket.listen(10);
}
void setupEventloop(){
@@ -388,30 +397,46 @@ struct Bot{
}
});
- w_stdin=Watcher(stdin.fileno, (revents){
- auto l=stdin.readln.chomp;
- auto split=l.findSplit(" ");
- switch(split[0]){
- case "quit":
- quit();
- break;
- case "link":
- auto split2=split[2].findSplit(" ");
- link(split2[0], split2[2].to!long);
- break;
- case "links":
- foreach(i,tt; lut._irc.byPair){
- foreach(t; tt){
- writeln(i, " <-> ", t);
- }
+ w_ctrl=Watcher(controlSocket.handle, (revents){
+ auto ns=controlSocket.accept();
+ trace("New control connection ");
+ Watcher *nw;
+ nw=new Watcher(ns.handle, (rrevents){
+ char[512] buf;
+ auto ret=ns.receive(buf);
+ if(ret==0){
+ ev_io_stop(eventloop, &nw.io);
+ delete nw;
+ }
+ auto line=buf[0..ret];
+ trace("New control command ",buf);
+ foreach(l; line.splitter("\n")){
+ auto split=l.findSplit(" ");
+ switch(split[0]){
+ case "quit":
+ quit();
+ break;
+ case "link":
+ auto split2=split[2].findSplit(" ");
+ link(split2[0].idup, split2[2].to!long);
+ break;
+ case "links":
+ foreach(i,tt; lut._irc.byPair){
+ foreach(t; tt){
+ ns.send("%s <-> %s\n".format(i,t));
+ }
+ }
+ break;
+ case "unlink":
+ break;
+ case "":
+ break;
+ default:
+ ns.send(l~": unknown command\n");
}
- break;
- case "unlink":
- break;
- default:
- writeln(l, ": unknown command");
- }
-
+ }
+ });
+ ev_io_start(eventloop, &nw.io);
});
}
@@ -425,14 +450,14 @@ struct Bot{
void start(){
info("Starting the event-listeners");
ev_io_start(eventloop, &w_irc.io);
- ev_io_start(eventloop, &w_stdin.io);
+ ev_io_start(eventloop, &w_ctrl.io);
}
void stop(){
trace("Stopping the Eventloop");
ev_io_stop(eventloop, &w_tele.io);
ev_io_stop(eventloop, &w_irc.io);
- ev_io_stop(eventloop, &w_stdin.io);
+ ev_io_stop(eventloop, &w_ctrl.io);
ev_break(eventloop, EVBREAK_ALL);
}
@@ -444,6 +469,8 @@ struct Bot{
void disconnect(){
telegram.disconnect();
telegram_listener.disconnect();
+ controlSocket.close();
+ std.file.remove(controlPath);
}
void save(File f){
@@ -465,6 +492,7 @@ struct Bot{
int main(string[] args){
Bot b=Bot(args[1]);
b.proxy_url=args[2];
+ b.controlPath=args[3];
b.initialize();
if(exists("savefile")){
auto f=File("savefile", "r");