Search in sources :

Example 1 with Mxisd

use of io.kamax.mxisd.Mxisd in project mxisd by kamax-io.

the class BuiltInNotificationHandlerSupplier method acceptPhone.

private void acceptPhone(String handler, Mxisd mxisd) {
    if (StringUtils.equals(PhoneNotificationHandler.ID, handler)) {
        Object o = mxisd.getConfig().getThreepid().getMedium().get(ThreePidMedium.PhoneNumber.getId());
        if (Objects.nonNull(o)) {
            PhoneConfig cfg;
            try {
                cfg = GsonUtil.get().fromJson(GsonUtil.makeObj(o), PhoneConfig.class);
            } catch (JsonSyntaxException e) {
                throw new ConfigurationException("Invalid configuration for threepid msisdn notification");
            }
            List<PhoneGenerator> generators = StreamSupport.stream(ServiceLoader.load(PhoneGeneratorSupplier.class).spliterator(), false).map(s -> s.apply(cfg, mxisd)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
            List<PhoneConnector> connectors = StreamSupport.stream(ServiceLoader.load(PhoneConnectorSupplier.class).spliterator(), false).map(s -> s.apply(cfg, mxisd)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
            NotificationHandlers.register(() -> new PhoneNotificationHandler(cfg, generators, connectors));
        }
    }
}
Also used : ThreePidMedium(io.kamax.matrix.ThreePidMedium) GsonUtil(io.kamax.matrix.json.GsonUtil) PhoneNotificationHandler(io.kamax.mxisd.threepid.notification.phone.PhoneNotificationHandler) ConfigurationException(io.kamax.mxisd.exception.ConfigurationException) StringUtils(org.apache.commons.lang3.StringUtils) EmailConfig(io.kamax.mxisd.config.threepid.medium.EmailConfig) Mxisd(io.kamax.mxisd.Mxisd) StreamSupport(java.util.stream.StreamSupport) PhoneConfig(io.kamax.mxisd.config.threepid.medium.PhoneConfig) EmailGeneratorSupplier(io.kamax.mxisd.threepid.generator.email.EmailGeneratorSupplier) NotificationHandlerSupplier(io.kamax.mxisd.notification.NotificationHandlerSupplier) EmailRawNotificationHandler(io.kamax.mxisd.threepid.notification.email.EmailRawNotificationHandler) JsonSyntaxException(com.google.gson.JsonSyntaxException) PhoneGeneratorSupplier(io.kamax.mxisd.threepid.generator.phone.PhoneGeneratorSupplier) ServiceLoader(java.util.ServiceLoader) EmailGenerator(io.kamax.mxisd.threepid.generator.email.EmailGenerator) Collectors(java.util.stream.Collectors) EmailConnector(io.kamax.mxisd.threepid.connector.email.EmailConnector) Objects(java.util.Objects) EmailSendGridConfig(io.kamax.mxisd.config.threepid.connector.EmailSendGridConfig) PhoneConnectorSupplier(io.kamax.mxisd.threepid.connector.phone.PhoneConnectorSupplier) List(java.util.List) PhoneGenerator(io.kamax.mxisd.threepid.generator.phone.PhoneGenerator) NotificationHandlers(io.kamax.mxisd.notification.NotificationHandlers) EmailConnectorSupplier(io.kamax.mxisd.threepid.connector.email.EmailConnectorSupplier) EmailSendGridNotificationHandler(io.kamax.mxisd.threepid.notification.email.EmailSendGridNotificationHandler) Optional(java.util.Optional) PhoneConnector(io.kamax.mxisd.threepid.connector.phone.PhoneConnector) JsonSyntaxException(com.google.gson.JsonSyntaxException) Optional(java.util.Optional) PhoneConfig(io.kamax.mxisd.config.threepid.medium.PhoneConfig) ConfigurationException(io.kamax.mxisd.exception.ConfigurationException) PhoneConnector(io.kamax.mxisd.threepid.connector.phone.PhoneConnector) PhoneNotificationHandler(io.kamax.mxisd.threepid.notification.phone.PhoneNotificationHandler) PhoneGenerator(io.kamax.mxisd.threepid.generator.phone.PhoneGenerator)

Example 2 with Mxisd

use of io.kamax.mxisd.Mxisd in project mxisd by kamax-io.

the class BuiltInNotificationHandlerSupplier method acceptEmail.

private void acceptEmail(String handler, Mxisd mxisd) {
    if (StringUtils.equals(EmailRawNotificationHandler.ID, handler)) {
        Object o = mxisd.getConfig().getThreepid().getMedium().get(ThreePidMedium.Email.getId());
        if (Objects.nonNull(o)) {
            EmailConfig emailCfg;
            try {
                emailCfg = GsonUtil.get().fromJson(GsonUtil.makeObj(o), EmailConfig.class);
            } catch (JsonSyntaxException e) {
                throw new ConfigurationException("Invalid configuration for threepid email notification");
            }
            if (StringUtils.isBlank(emailCfg.getGenerator())) {
                throw new ConfigurationException("notification.email.generator");
            }
            if (StringUtils.isBlank(emailCfg.getConnector())) {
                throw new ConfigurationException("notification.email.connector");
            }
            List<EmailGenerator> generators = StreamSupport.stream(ServiceLoader.load(EmailGeneratorSupplier.class).spliterator(), false).map(s -> s.apply(emailCfg, mxisd)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
            List<EmailConnector> connectors = StreamSupport.stream(ServiceLoader.load(EmailConnectorSupplier.class).spliterator(), false).map(s -> s.apply(emailCfg, mxisd)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
            NotificationHandlers.register(() -> new EmailRawNotificationHandler(emailCfg, generators, connectors));
        }
    }
    if (StringUtils.equals(EmailSendGridNotificationHandler.ID, handler)) {
        Object cfgJson = mxisd.getConfig().getNotification().getHandlers().get(EmailSendGridNotificationHandler.ID);
        if (Objects.nonNull(cfgJson)) {
            EmailSendGridConfig cfg;
            try {
                cfg = GsonUtil.get().fromJson(GsonUtil.get().toJson(cfgJson), EmailSendGridConfig.class);
            } catch (JsonSyntaxException e) {
                throw new ConfigurationException("Invalid configuration for threepid email sendgrid handler");
            }
            NotificationHandlers.register(() -> new EmailSendGridNotificationHandler(mxisd.getConfig(), cfg));
        }
    }
}
Also used : ThreePidMedium(io.kamax.matrix.ThreePidMedium) GsonUtil(io.kamax.matrix.json.GsonUtil) PhoneNotificationHandler(io.kamax.mxisd.threepid.notification.phone.PhoneNotificationHandler) ConfigurationException(io.kamax.mxisd.exception.ConfigurationException) StringUtils(org.apache.commons.lang3.StringUtils) EmailConfig(io.kamax.mxisd.config.threepid.medium.EmailConfig) Mxisd(io.kamax.mxisd.Mxisd) StreamSupport(java.util.stream.StreamSupport) PhoneConfig(io.kamax.mxisd.config.threepid.medium.PhoneConfig) EmailGeneratorSupplier(io.kamax.mxisd.threepid.generator.email.EmailGeneratorSupplier) NotificationHandlerSupplier(io.kamax.mxisd.notification.NotificationHandlerSupplier) EmailRawNotificationHandler(io.kamax.mxisd.threepid.notification.email.EmailRawNotificationHandler) JsonSyntaxException(com.google.gson.JsonSyntaxException) PhoneGeneratorSupplier(io.kamax.mxisd.threepid.generator.phone.PhoneGeneratorSupplier) ServiceLoader(java.util.ServiceLoader) EmailGenerator(io.kamax.mxisd.threepid.generator.email.EmailGenerator) Collectors(java.util.stream.Collectors) EmailConnector(io.kamax.mxisd.threepid.connector.email.EmailConnector) Objects(java.util.Objects) EmailSendGridConfig(io.kamax.mxisd.config.threepid.connector.EmailSendGridConfig) PhoneConnectorSupplier(io.kamax.mxisd.threepid.connector.phone.PhoneConnectorSupplier) List(java.util.List) PhoneGenerator(io.kamax.mxisd.threepid.generator.phone.PhoneGenerator) NotificationHandlers(io.kamax.mxisd.notification.NotificationHandlers) EmailConnectorSupplier(io.kamax.mxisd.threepid.connector.email.EmailConnectorSupplier) EmailSendGridNotificationHandler(io.kamax.mxisd.threepid.notification.email.EmailSendGridNotificationHandler) Optional(java.util.Optional) PhoneConnector(io.kamax.mxisd.threepid.connector.phone.PhoneConnector) EmailGenerator(io.kamax.mxisd.threepid.generator.email.EmailGenerator) Optional(java.util.Optional) EmailConnector(io.kamax.mxisd.threepid.connector.email.EmailConnector) EmailConfig(io.kamax.mxisd.config.threepid.medium.EmailConfig) JsonSyntaxException(com.google.gson.JsonSyntaxException) ConfigurationException(io.kamax.mxisd.exception.ConfigurationException) EmailSendGridNotificationHandler(io.kamax.mxisd.threepid.notification.email.EmailSendGridNotificationHandler) EmailSendGridConfig(io.kamax.mxisd.config.threepid.connector.EmailSendGridConfig) EmailRawNotificationHandler(io.kamax.mxisd.threepid.notification.email.EmailRawNotificationHandler)

Example 3 with Mxisd

use of io.kamax.mxisd.Mxisd in project mxisd by kamax-io.

the class AuthManagerTest method beforeClass.

// FIXME we should be able to easily build the class ourselves
// FIXME use constants
@BeforeClass
public static void beforeClass() {
    MxisdConfig cfg = new MxisdConfig();
    cfg.getMatrix().setDomain("localhost");
    cfg.getKey().setPath(":memory:");
    cfg.getStorage().getProvider().getSqlite().setDatabase(":memory:");
    MemoryThreePid mem3pid = new MemoryThreePid();
    mem3pid.setMedium("email");
    mem3pid.setAddress("john@localhost");
    MemoryIdentityConfig validCfg = new MemoryIdentityConfig();
    validCfg.setUsername("john");
    validCfg.setPassword("doe");
    validCfg.getThreepids().add(mem3pid);
    MemoryIdentityConfig illegalUser = new MemoryIdentityConfig();
    illegalUser.setUsername("JANE");
    illegalUser.setPassword("doe");
    cfg.getMemory().setEnabled(true);
    cfg.getMemory().getIdentities().add(validCfg);
    cfg.getMemory().getIdentities().add(illegalUser);
    Mxisd m = new Mxisd(cfg);
    m.start();
    mgr = m.getAuth();
}
Also used : MxisdConfig(io.kamax.mxisd.config.MxisdConfig) MemoryIdentityConfig(io.kamax.mxisd.config.memory.MemoryIdentityConfig) Mxisd(io.kamax.mxisd.Mxisd) MemoryThreePid(io.kamax.mxisd.config.memory.MemoryThreePid) BeforeClass(org.junit.BeforeClass)

Example 4 with Mxisd

use of io.kamax.mxisd.Mxisd in project mxisd by kamax-io.

the class MxisdTest method before.

@Before
public void before() {
    MxisdConfig cfg = MxisdConfig.forDomain("localhost").inMemory();
    MemoryThreePid mem3pid = new MemoryThreePid();
    mem3pid.setMedium("email");
    mem3pid.setAddress("john@localhost");
    MemoryIdentityConfig memCfg = new MemoryIdentityConfig();
    memCfg.setUsername("john");
    memCfg.getThreepids().add(mem3pid);
    cfg.getMemory().setEnabled(true);
    cfg.getMemory().getIdentities().add(memCfg);
    m = new Mxisd(cfg);
    m.start();
}
Also used : MxisdConfig(io.kamax.mxisd.config.MxisdConfig) MemoryIdentityConfig(io.kamax.mxisd.config.memory.MemoryIdentityConfig) Mxisd(io.kamax.mxisd.Mxisd) MemoryThreePid(io.kamax.mxisd.config.memory.MemoryThreePid) Before(org.junit.Before)

Example 5 with Mxisd

use of io.kamax.mxisd.Mxisd 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

Mxisd (io.kamax.mxisd.Mxisd)8 MxisdConfig (io.kamax.mxisd.config.MxisdConfig)4 EmailConfig (io.kamax.mxisd.config.threepid.medium.EmailConfig)4 GsonUtil (io.kamax.matrix.json.GsonUtil)3 List (java.util.List)3 Optional (java.util.Optional)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 ThreePidMedium (io.kamax.matrix.ThreePidMedium)2 MemoryIdentityConfig (io.kamax.mxisd.config.memory.MemoryIdentityConfig)2 MemoryThreePid (io.kamax.mxisd.config.memory.MemoryThreePid)2 EmailSendGridConfig (io.kamax.mxisd.config.threepid.connector.EmailSendGridConfig)2 PhoneConfig (io.kamax.mxisd.config.threepid.medium.PhoneConfig)2 ConfigurationException (io.kamax.mxisd.exception.ConfigurationException)2 NotificationHandlerSupplier (io.kamax.mxisd.notification.NotificationHandlerSupplier)2 NotificationHandlers (io.kamax.mxisd.notification.NotificationHandlers)2 EmailConnector (io.kamax.mxisd.threepid.connector.email.EmailConnector)2 EmailConnectorSupplier (io.kamax.mxisd.threepid.connector.email.EmailConnectorSupplier)2 PhoneConnector (io.kamax.mxisd.threepid.connector.phone.PhoneConnector)2 PhoneConnectorSupplier (io.kamax.mxisd.threepid.connector.phone.PhoneConnectorSupplier)2 EmailGenerator (io.kamax.mxisd.threepid.generator.email.EmailGenerator)2