Search in sources :

Example 1 with io.kamax.matrix.hs._MatrixRoom

use of io.kamax.matrix.hs._MatrixRoom in project mxisd by kamax-io.

the class MessageEventProcessor method process.

@Override
public void process(JsonObject ev, _MatrixID sender, String roomId) {
    MatrixJsonRoomMessageEvent msgEv = new MatrixJsonRoomMessageEvent(ev);
    if (StringUtils.equals("m.notice", msgEv.getBodyType())) {
        log.info("Ignoring automated message");
        return;
    }
    _MatrixRoom room = client.getRoom(roomId);
    if (!m.getProfile().hasAnyRole(sender, m.getConfig().getAppsvc().getFeature().getAdmin().getAllowedRoles())) {
        room.sendNotice("You are not allowed to interact with me.");
        return;
    }
    List<_MatrixID> joinedUsers = room.getJoinedUsers().stream().map(_MatrixUserProfile::getId).collect(Collectors.toList());
    boolean joinedWithMainUser = joinedUsers.contains(client.getWhoAmI());
    boolean isAdminPrivate = joinedWithMainUser && joinedUsers.size() == 2;
    if (!StringUtils.equals("m.text", msgEv.getBodyType())) {
        log.info("Unsupported message event type: {}", msgEv.getBodyType());
        return;
    }
    String command = msgEv.getBody();
    if (!isAdminPrivate) {
        if (!StringUtils.startsWith(command, "!" + Mxisd.Name + " ")) {
            // Not for us
            return;
        }
        command = command.substring(("!" + Mxisd.Name + " ").length());
    }
    try {
        CommandLineParser p = new DefaultParser();
        CommandLine cmdLine = p.parse(new Options(), command.split(" ", 0));
        String cmd = cmdLine.getArgList().get(0);
        CommandProcessor cp = processors.get(cmd);
        if (Objects.isNull(cp)) {
            room.sendNotice("Unknown command: " + command + "\n\n" + getHelp());
        } else {
            cp.process(m, client, room, cmdLine);
        }
    } catch (ParseException e) {
        room.sendNotice("Invalid input" + "\n\n" + getHelp());
    } catch (RuntimeException e) {
        room.sendNotice("Error when running command: " + e.getMessage());
    }
}
Also used : MatrixJsonRoomMessageEvent(io.kamax.matrix.json.event.MatrixJsonRoomMessageEvent) InviteCommandProcessor(io.kamax.mxisd.as.processor.command.InviteCommandProcessor) LookupCommandProcessor(io.kamax.mxisd.as.processor.command.LookupCommandProcessor) PingCommandProcessor(io.kamax.mxisd.as.processor.command.PingCommandProcessor) CommandProcessor(io.kamax.mxisd.as.processor.command.CommandProcessor) io.kamax.matrix.hs._MatrixRoom(io.kamax.matrix.hs._MatrixRoom) io.kamax.matrix._MatrixID(io.kamax.matrix._MatrixID)

Example 2 with io.kamax.matrix.hs._MatrixRoom

use of io.kamax.matrix.hs._MatrixRoom in project mxisd by kamax-io.

the class InviteCommandProcessor method process.

@Override
public void process(Mxisd m, _MatrixClient client, _MatrixRoom room, CommandLine cmdLine) {
    if (cmdLine.getArgs().length < 2) {
        room.sendNotice(buildHelp());
    } else {
        String arg = cmdLine.getArgList().get(1);
        String response;
        if (StringUtils.equals("list", arg)) {
            StrBuilder b = new StrBuilder();
            List<IThreePidInviteReply> invites = m.getInvite().listInvites();
            if (invites.isEmpty()) {
                b.appendln("No invites!");
                response = b.toString();
            } else {
                b.appendln("Invites:");
                for (IThreePidInviteReply invite : invites) {
                    b.appendNewLine().append("ID: ").append(invite.getId());
                    b.appendNewLine().append("Room: ").append(invite.getInvite().getRoomId());
                    b.appendNewLine().append("Medium: ").append(invite.getInvite().getMedium());
                    b.appendNewLine().append("Address: ").append(invite.getInvite().getAddress());
                    b.appendNewLine();
                }
                response = b.appendNewLine().append("Total: " + invites.size()).toString();
            }
        } else if (StringUtils.equals("show", arg)) {
            if (cmdLine.getArgList().size() < 3) {
                response = buildHelp();
            } else {
                String id = cmdLine.getArgList().get(2);
                IThreePidInviteReply invite = m.getInvite().getInvite(id);
                StrBuilder b = new StrBuilder();
                b.appendln("Details for Invitation #" + id);
                b.appendNewLine().append("Room: ").append(invite.getInvite().getRoomId());
                b.appendNewLine().append("Sender: ").append(invite.getInvite().getSender().toString());
                b.appendNewLine().append("Medium: ").append(invite.getInvite().getMedium());
                b.appendNewLine().append("Address: ").append(invite.getInvite().getAddress());
                b.appendNewLine().append("Display name: ").append(invite.getDisplayName());
                b.appendNewLine().appendNewLine().append("Properties:");
                invite.getInvite().getProperties().forEach((k, v) -> {
                    b.appendNewLine().append("\t").append(k).append("=").append(v);
                });
                b.appendNewLine();
                response = b.toString();
            }
        } else if (StringUtils.equals("revoke", arg)) {
            if (cmdLine.getArgList().size() < 3) {
                response = buildHelp();
            } else {
                m.getInvite().expireInvite(cmdLine.getArgList().get(2));
                response = "OK";
            }
        } else {
            response = buildError("Unknown invite action: " + arg, true);
        }
        room.sendNotice(response);
    }
}
Also used : StrBuilder(org.apache.commons.lang.text.StrBuilder) StringUtils(org.apache.commons.lang.StringUtils) List(java.util.List) io.kamax.matrix.client._MatrixClient(io.kamax.matrix.client._MatrixClient) io.kamax.matrix.hs._MatrixRoom(io.kamax.matrix.hs._MatrixRoom) IThreePidInviteReply(io.kamax.mxisd.invitation.IThreePidInviteReply) CommandLine(org.apache.commons.cli.CommandLine) Mxisd(io.kamax.mxisd.Mxisd) IThreePidInviteReply(io.kamax.mxisd.invitation.IThreePidInviteReply) StrBuilder(org.apache.commons.lang.text.StrBuilder)

Aggregations

io.kamax.matrix.hs._MatrixRoom (io.kamax.matrix.hs._MatrixRoom)2 io.kamax.matrix._MatrixID (io.kamax.matrix._MatrixID)1 io.kamax.matrix.client._MatrixClient (io.kamax.matrix.client._MatrixClient)1 MatrixJsonRoomMessageEvent (io.kamax.matrix.json.event.MatrixJsonRoomMessageEvent)1 Mxisd (io.kamax.mxisd.Mxisd)1 CommandProcessor (io.kamax.mxisd.as.processor.command.CommandProcessor)1 InviteCommandProcessor (io.kamax.mxisd.as.processor.command.InviteCommandProcessor)1 LookupCommandProcessor (io.kamax.mxisd.as.processor.command.LookupCommandProcessor)1 PingCommandProcessor (io.kamax.mxisd.as.processor.command.PingCommandProcessor)1 IThreePidInviteReply (io.kamax.mxisd.invitation.IThreePidInviteReply)1 List (java.util.List)1 CommandLine (org.apache.commons.cli.CommandLine)1 StringUtils (org.apache.commons.lang.StringUtils)1 StrBuilder (org.apache.commons.lang.text.StrBuilder)1