mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-30 11:00:38 +00:00
Allow calling signal-cli without -u flag
For daemon command all local users will be exposed as dbus objects If only one local user exists, all other commands will use that user, otherwise a user has to be specified.
This commit is contained in:
parent
a97bbf8608
commit
ca86c421eb
19 changed files with 356 additions and 172 deletions
|
@ -4,6 +4,7 @@ import net.sourceforge.argparse4j.impl.Arguments;
|
|||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
||||
import org.asamk.signal.DbusConfig;
|
||||
import org.asamk.signal.DbusReceiveMessageHandler;
|
||||
import org.asamk.signal.JsonDbusReceiveMessageHandler;
|
||||
import org.asamk.signal.dbus.DbusSignalImpl;
|
||||
|
@ -14,13 +15,11 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.asamk.signal.DbusConfig.SIGNAL_BUSNAME;
|
||||
import static org.asamk.signal.DbusConfig.SIGNAL_OBJECTPATH;
|
||||
import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
|
||||
|
||||
public class DaemonCommand implements LocalCommand {
|
||||
public class DaemonCommand implements MultiLocalCommand {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(ReceiveCommand.class);
|
||||
|
||||
|
@ -46,46 +45,98 @@ public class DaemonCommand implements LocalCommand {
|
|||
logger.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
|
||||
}
|
||||
|
||||
DBusConnection conn = null;
|
||||
try {
|
||||
boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
|
||||
|
||||
DBusConnection.DBusBusType busType;
|
||||
if (ns.getBoolean("system")) {
|
||||
busType = DBusConnection.DBusBusType.SYSTEM;
|
||||
} else {
|
||||
busType = DBusConnection.DBusBusType.SESSION;
|
||||
}
|
||||
|
||||
try (DBusConnection conn = DBusConnection.getConnection(busType)) {
|
||||
String objectPath = DbusConfig.getObjectPath();
|
||||
Thread t = run(conn, objectPath, m, ignoreAttachments, inJson);
|
||||
|
||||
conn.requestBusName(DbusConfig.getBusname());
|
||||
|
||||
try {
|
||||
DBusConnection.DBusBusType busType;
|
||||
if (ns.getBoolean("system")) {
|
||||
busType = DBusConnection.DBusBusType.SYSTEM;
|
||||
} else {
|
||||
busType = DBusConnection.DBusBusType.SESSION;
|
||||
}
|
||||
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;
|
||||
} catch (DBusException e) {
|
||||
e.printStackTrace();
|
||||
return 2;
|
||||
}
|
||||
boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
|
||||
try {
|
||||
m.receiveMessages(1,
|
||||
TimeUnit.HOURS,
|
||||
false,
|
||||
ignoreAttachments,
|
||||
inJson
|
||||
? 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());
|
||||
return 3;
|
||||
} catch (AssertionError e) {
|
||||
handleAssertionError(e);
|
||||
return 1;
|
||||
}
|
||||
} finally {
|
||||
if (conn != null) {
|
||||
conn.disconnect();
|
||||
t.join();
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
return 0;
|
||||
} catch (DBusException | IOException e) {
|
||||
logger.error("Dbus command failed", e);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int handleCommand(final Namespace ns, final List<Manager> managers) {
|
||||
boolean inJson = ns.getString("output").equals("json") || ns.getBoolean("json");
|
||||
|
||||
// TODO delete later when "json" variable is removed
|
||||
if (ns.getBoolean("json")) {
|
||||
logger.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
|
||||
}
|
||||
|
||||
boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
|
||||
|
||||
DBusConnection.DBusBusType busType;
|
||||
if (ns.getBoolean("system")) {
|
||||
busType = DBusConnection.DBusBusType.SYSTEM;
|
||||
} else {
|
||||
busType = DBusConnection.DBusBusType.SESSION;
|
||||
}
|
||||
|
||||
try (DBusConnection conn = DBusConnection.getConnection(busType)) {
|
||||
List<Thread> receiveThreads = new ArrayList<>();
|
||||
for (Manager m : managers) {
|
||||
String objectPath = DbusConfig.getObjectPath(m.getUsername());
|
||||
Thread thread = run(conn, objectPath, m, ignoreAttachments, inJson);
|
||||
receiveThreads.add(thread);
|
||||
}
|
||||
|
||||
conn.requestBusName(DbusConfig.getBusname());
|
||||
|
||||
for (Thread t : receiveThreads) {
|
||||
try {
|
||||
t.join();
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
} catch (DBusException | IOException e) {
|
||||
logger.error("Dbus command failed", e);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
private Thread run(
|
||||
DBusConnection conn, String objectPath, Manager m, boolean ignoreAttachments, boolean inJson
|
||||
) throws DBusException {
|
||||
conn.exportObject(objectPath, new DbusSignalImpl(m));
|
||||
|
||||
final Thread thread = new Thread(() -> {
|
||||
while (true) {
|
||||
try {
|
||||
m.receiveMessages(1,
|
||||
TimeUnit.HOURS,
|
||||
false,
|
||||
ignoreAttachments,
|
||||
inJson
|
||||
? new JsonDbusReceiveMessageHandler(m, conn, objectPath)
|
||||
: new DbusReceiveMessageHandler(m, conn, objectPath));
|
||||
} catch (IOException e) {
|
||||
logger.warn("Receiving messages failed, retrying", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
logger.info("Exported dbus object: " + objectPath);
|
||||
|
||||
thread.start();
|
||||
|
||||
return thread;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,8 +3,14 @@ package org.asamk.signal.commands;
|
|||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
|
||||
import org.asamk.Signal;
|
||||
import org.asamk.signal.dbus.DbusSignalImpl;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
|
||||
public interface DbusCommand extends Command {
|
||||
public interface DbusCommand extends LocalCommand {
|
||||
|
||||
int handleCommand(Namespace ns, Signal signal);
|
||||
|
||||
default int handleCommand(final Namespace ns, final Manager m) {
|
||||
return handleCommand(ns, new DbusSignalImpl(m));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ public class GetUserStatusCommand implements LocalCommand {
|
|||
registered = m.areUsersRegistered(new HashSet<>(ns.getList("number")));
|
||||
} catch (IOException e) {
|
||||
System.err.println("Unable to check if users are registered");
|
||||
return 1;
|
||||
return 3;
|
||||
}
|
||||
|
||||
// Output
|
||||
|
|
|
@ -3,7 +3,6 @@ package org.asamk.signal.commands;
|
|||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
||||
import org.asamk.Signal;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.groups.GroupId;
|
||||
import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
|
||||
|
@ -35,15 +34,15 @@ public class JoinGroupCommand implements LocalCommand {
|
|||
linkUrl = GroupInviteLinkUrl.fromUri(uri);
|
||||
} catch (GroupInviteLinkUrl.InvalidGroupLinkException e) {
|
||||
System.err.println("Group link is invalid: " + e.getMessage());
|
||||
return 2;
|
||||
return 1;
|
||||
} catch (GroupInviteLinkUrl.UnknownGroupLinkVersionException e) {
|
||||
System.err.println("Group link was created with an incompatible version: " + e.getMessage());
|
||||
return 2;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (linkUrl == null) {
|
||||
System.err.println("Link is not a signal group invitation link");
|
||||
return 2;
|
||||
return 1;
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -64,16 +63,13 @@ public class JoinGroupCommand implements LocalCommand {
|
|||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
handleIOException(e);
|
||||
return 1;
|
||||
} catch (Signal.Error.AttachmentInvalid e) {
|
||||
System.err.println("Failed to add avatar attachment for group\": " + e.getMessage());
|
||||
return 1;
|
||||
return 3;
|
||||
} catch (DBusExecutionException e) {
|
||||
System.err.println("Failed to send message: " + e.getMessage());
|
||||
return 1;
|
||||
return 2;
|
||||
} catch (GroupLinkNotActiveException e) {
|
||||
System.err.println("Group link is not valid: " + e.getMessage());
|
||||
return 2;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
package org.asamk.signal.commands;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import net.sourceforge.argparse4j.impl.Arguments;
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
@ -9,11 +14,6 @@ import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
|
|||
import org.asamk.signal.manager.storage.groups.GroupInfo;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -23,7 +23,8 @@ import java.util.stream.Collectors;
|
|||
public class ListGroupsCommand implements LocalCommand {
|
||||
|
||||
private static Set<String> resolveMembers(Manager m, Set<SignalServiceAddress> addresses) {
|
||||
return addresses.stream().map(m::resolveSignalServiceAddress)
|
||||
return addresses.stream()
|
||||
.map(m::resolveSignalServiceAddress)
|
||||
.map(SignalServiceAddress::getLegacyIdentifier)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
@ -34,7 +35,7 @@ public class ListGroupsCommand implements LocalCommand {
|
|||
System.out.println();
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getMessage());
|
||||
return 1;
|
||||
return 3;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -65,7 +66,8 @@ public class ListGroupsCommand implements LocalCommand {
|
|||
|
||||
@Override
|
||||
public void attachToSubparser(final Subparser subparser) {
|
||||
subparser.addArgument("-d", "--detailed").action(Arguments.storeTrue())
|
||||
subparser.addArgument("-d", "--detailed")
|
||||
.action(Arguments.storeTrue())
|
||||
.help("List the members and group invite links of each group. If output=json, then this is always set");
|
||||
|
||||
subparser.help("List group information including names, ids, active status, blocked status and members");
|
||||
|
@ -114,10 +116,16 @@ public class ListGroupsCommand implements LocalCommand {
|
|||
public Set<String> requestingMembers;
|
||||
public String groupInviteLink;
|
||||
|
||||
public JsonGroup(String id, String name, boolean isMember, boolean isBlocked,
|
||||
Set<String> members, Set<String> pendingMembers,
|
||||
Set<String> requestingMembers, String groupInviteLink)
|
||||
{
|
||||
public JsonGroup(
|
||||
String id,
|
||||
String name,
|
||||
boolean isMember,
|
||||
boolean isBlocked,
|
||||
Set<String> members,
|
||||
Set<String> pendingMembers,
|
||||
Set<String> requestingMembers,
|
||||
String groupInviteLink
|
||||
) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.isMember = isMember;
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package org.asamk.signal.commands;
|
||||
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
|
||||
import org.asamk.signal.manager.Manager;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface MultiLocalCommand extends LocalCommand {
|
||||
|
||||
int handleCommand(Namespace ns, List<Manager> m);
|
||||
|
||||
@Override
|
||||
default int handleCommand(final Namespace ns, final Manager m) {
|
||||
return handleCommand(ns, List.of(m));
|
||||
}
|
||||
}
|
|
@ -141,12 +141,9 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
|
|||
System.out.println();
|
||||
}
|
||||
});
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
|
||||
return 1;
|
||||
} catch (DBusException e) {
|
||||
e.printStackTrace();
|
||||
return 1;
|
||||
return 2;
|
||||
}
|
||||
while (true) {
|
||||
try {
|
||||
|
|
|
@ -20,7 +20,10 @@ public class RemovePinCommand implements LocalCommand {
|
|||
try {
|
||||
m.setRegistrationLockPin(Optional.absent());
|
||||
return 0;
|
||||
} catch (IOException | UnauthenticatedResponseException e) {
|
||||
} catch (UnauthenticatedResponseException e) {
|
||||
System.err.println("Remove pin error: " + e.getMessage());
|
||||
return 2;
|
||||
} catch (IOException e) {
|
||||
System.err.println("Remove pin error: " + e.getMessage());
|
||||
return 3;
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import org.asamk.Signal;
|
|||
import org.asamk.signal.manager.groups.GroupIdFormatException;
|
||||
import org.asamk.signal.util.IOUtils;
|
||||
import org.asamk.signal.util.Util;
|
||||
import org.freedesktop.dbus.errors.UnknownObject;
|
||||
import org.freedesktop.dbus.exceptions.DBusExecutionException;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -50,7 +51,7 @@ public class SendCommand implements DbusCommand {
|
|||
return 1;
|
||||
} catch (DBusExecutionException e) {
|
||||
System.err.println("Failed to send message: " + e.getMessage());
|
||||
return 1;
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -89,7 +90,7 @@ public class SendCommand implements DbusCommand {
|
|||
return 1;
|
||||
} catch (DBusExecutionException e) {
|
||||
System.err.println("Failed to send message: " + e.getMessage());
|
||||
return 1;
|
||||
return 2;
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -99,9 +100,12 @@ public class SendCommand implements DbusCommand {
|
|||
} catch (AssertionError e) {
|
||||
handleAssertionError(e);
|
||||
return 1;
|
||||
} catch (UnknownObject e) {
|
||||
System.err.println("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage());
|
||||
return 1;
|
||||
} catch (DBusExecutionException e) {
|
||||
System.err.println("Failed to send message: " + e.getMessage());
|
||||
return 1;
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,10 @@ public class SendContactsCommand implements LocalCommand {
|
|||
try {
|
||||
m.sendContacts();
|
||||
return 0;
|
||||
} catch (IOException | UntrustedIdentityException e) {
|
||||
} catch (UntrustedIdentityException e) {
|
||||
System.err.println("SendContacts error: " + e.getMessage());
|
||||
return 2;
|
||||
} catch (IOException e) {
|
||||
System.err.println("SendContacts error: " + e.getMessage());
|
||||
return 3;
|
||||
}
|
||||
|
|
|
@ -23,7 +23,10 @@ public class SetPinCommand implements LocalCommand {
|
|||
String registrationLockPin = ns.getString("registrationLockPin");
|
||||
m.setRegistrationLockPin(Optional.of(registrationLockPin));
|
||||
return 0;
|
||||
} catch (IOException | UnauthenticatedResponseException e) {
|
||||
} catch (UnauthenticatedResponseException e) {
|
||||
System.err.println("Set pin error: " + e.getMessage());
|
||||
return 2;
|
||||
} catch (IOException e) {
|
||||
System.err.println("Set pin error: " + e.getMessage());
|
||||
return 3;
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ public class UpdateGroupCommand implements DbusCommand {
|
|||
return 1;
|
||||
} catch (DBusExecutionException e) {
|
||||
System.err.println("Failed to send message: " + e.getMessage());
|
||||
return 1;
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ public class UploadStickerPackCommand implements LocalCommand {
|
|||
return 3;
|
||||
} catch (StickerPackInvalidException e) {
|
||||
System.err.println("Invalid sticker pack: " + e.getMessage());
|
||||
return 3;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue