moved code to a new stdio command file

This commit is contained in:
technillogue 2020-12-29 19:12:58 -05:00
parent 218da34968
commit fae778cd48

View file

@ -6,90 +6,17 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.DbusReceiveMessageHandler;
import org.asamk.signal.JsonDbusReceiveMessageHandler;
import org.asamk.signal.ReceiveMessageHandler;
import org.asamk.signal.JsonReceiveMessageHandler;
import org.asamk.signal.dbus.DbusSignalImpl;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import static org.asamk.signal.DbusConfig.SIGNAL_BUSNAME;
import static org.asamk.signal.DbusConfig.SIGNAL_OBJECTPATH;
import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptions;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
class JsonInterface {
public String commandName;
public String recipient;
public String content;
public JsonNode details;
}
class InputReader implements Runnable {
private volatile boolean alive = true;
private Manager m;
public void terminate() {
this.alive = false;
}
InputReader (final Manager m) {
this.m = m;
}
@Override
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ObjectMapper jsonProcessor = new ObjectMapper();
while (alive) {
try {
String in = br.readLine();
if (in != null) {
JsonInterface command = jsonProcessor.readValue(in, JsonInterface.class);
if (command.commandName.equals("sendMessage")){
List<String> recipients = new ArrayList<String>();
recipients.add(command.recipient);
List<String> attachments = new ArrayList<>();
if (command.details != null && command.details.has("attachments") ) {
command.details.get("attachments").forEach(attachment -> {
if (attachment.isTextual()){
attachments.add(attachment.asText());
}
});
}
try {
// verbosity flag? better yet, json acknowledgement with timestamp or message id?
System.out.println("sentMessage '" + command.content + "' to " + command.recipient);
this.m.sendMessage(command.content, attachments, recipients);
} catch (AssertionError | EncapsulatedExceptions| AttachmentInvalidException | InvalidNumberException e) {
System.err.println("error in sending message");
e.printStackTrace(System.out);
}
} /* elif (command.commandName == "sendTyping") {
getMessageSender().sendTyping(signalServiceAddress?, ....)
}*/
}
} catch (IOException e) {
System.err.println(e);
alive = false;
}
}
}
}
public class DaemonCommand implements LocalCommand {
@ -112,7 +39,7 @@ public class DaemonCommand implements LocalCommand {
System.err.println("User is not registered.");
return 1;
}
/* DBusConnection conn = null;
DBusConnection conn = null;
try {
try {
DBusConnection.DBusBusType busType;
@ -121,9 +48,9 @@ public class DaemonCommand implements LocalCommand {
} else {
busType = DBusConnection.DBusBusType.SESSION;
}
// conn = DBusConnection.getConnection(busType);
// conn.exportObject(SIGNAL_OBJECTPATH, new DbusSignalImpl(m));
// conn.requestBusName(SIGNAL_BUSNAME);
conn = DBusConnection.getConnection(busType);
conn.exportObject(SIGNAL_OBJECTPATH, new DbusSignalImpl(m));
conn.requestBusName(SIGNAL_BUSNAME);
} catch (UnsatisfiedLinkError e) {
System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
return 1;
@ -131,17 +58,9 @@ public class DaemonCommand implements LocalCommand {
e.printStackTrace();
return 2;
}
*/
boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
InputReader reader = new InputReader(m);
Thread readerThread = new Thread(reader);
readerThread.start();
try {
m.receiveMessagesAndReadStdin(1, TimeUnit.HOURS, false, ignoreAttachments,
ns.getBoolean("json")
? new JsonReceiveMessageHandler(m)
: new ReceiveMessageHandler(m)
/*true*/);
m.receiveMessages(1, TimeUnit.HOURS, false, ignoreAttachments, ns.getBoolean("json") ? new JsonDbusReceiveMessageHandler(m, conn, SIGNAL_OBJECTPATH) : new DbusReceiveMessageHandler(m, conn, SIGNAL_OBJECTPATH));
return 0;
} catch (IOException e) {
System.err.println("Error while receiving messages: " + e.getMessage());
@ -149,9 +68,11 @@ public class DaemonCommand implements LocalCommand {
} catch (AssertionError e) {
handleAssertionError(e);
return 1;
} finally {
reader.terminate();
}
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
}