summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/openslx/satserver/util/MatrixClient.java56
-rw-r--r--src/main/java/org/openslx/taskmanager/tasks/MatrixNotification.java80
2 files changed, 136 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/satserver/util/MatrixClient.java b/src/main/java/org/openslx/satserver/util/MatrixClient.java
new file mode 100644
index 0000000..356d08e
--- /dev/null
+++ b/src/main/java/org/openslx/satserver/util/MatrixClient.java
@@ -0,0 +1,56 @@
+package org.openslx.satserver.util;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import com.cosium.matrix_communication_client.MatrixResources;
+import com.cosium.matrix_communication_client.MatrixResourcesFactory.AuthenticationBuilder;
+import com.cosium.matrix_communication_client.MatrixResourcesFactory.FinalBuilder;
+import com.cosium.matrix_communication_client.Message;
+import com.cosium.matrix_communication_client.Message.Builder;
+import com.cosium.matrix_communication_client.RoomResource;
+
+public class MatrixClient
+{
+
+ private static final Logger LOGGER = LogManager.getLogger( MatrixClient.class );
+
+ private static final Map<String, MatrixResources> connections = new HashMap<>();
+
+ public static void sendMessage( String serverAddress, String channel, String user, String password, String message, String htmlMessage, List<String> outErrors )
+ {
+ String key = serverAddress + "::" + user;
+ MatrixResources matrix;
+ synchronized ( connections ) {
+ matrix = connections.get( key );
+ if ( matrix == null ) {
+ AuthenticationBuilder b1 = MatrixResources.factory()
+ .builder()
+ .https()
+ .hostname( serverAddress )
+ .defaultPort();
+ FinalBuilder b2;
+ if ( !Util.isEmpty( user ) ) {
+ b2 = b1.usernamePassword( user, password );
+ } else {
+ b2 = b1.accessToken( password );
+ }
+ matrix = b2.build();
+ connections.put( key, matrix );
+ }
+ }
+ RoomResource room = matrix.rooms().byId( channel );
+ if ( room != null ) {
+ Builder m = Message.builder().body( message );
+ if ( !Util.isEmpty( htmlMessage ) ) {
+ m.formattedBody( htmlMessage );
+ }
+ room.sendMessage( m.build() );
+ }
+ }
+
+}
diff --git a/src/main/java/org/openslx/taskmanager/tasks/MatrixNotification.java b/src/main/java/org/openslx/taskmanager/tasks/MatrixNotification.java
new file mode 100644
index 0000000..ef21ad7
--- /dev/null
+++ b/src/main/java/org/openslx/taskmanager/tasks/MatrixNotification.java
@@ -0,0 +1,80 @@
+package org.openslx.taskmanager.tasks;
+
+import java.util.ArrayList;
+
+import org.openslx.satserver.util.MatrixClient;
+import org.openslx.satserver.util.Util;
+import org.openslx.taskmanager.api.AbstractTask;
+
+import com.google.gson.annotations.Expose;
+
+public class MatrixNotification extends AbstractTask
+{
+
+ @Expose
+ private String serverAddress;
+
+ @Expose
+ private String room;
+
+ @Expose
+ private String message;
+
+ @Expose
+ private String htmlMessage;
+
+ @Expose
+ private String user;
+
+ @Expose
+ private String password;
+
+ private Output status = new Output();
+
+ @Override
+ protected boolean initTask()
+ {
+ this.setStatusObject( status );
+ if ( Util.isEmpty( serverAddress ) ) {
+ status.add( "serverAddress empty" );
+ return false;
+ }
+ if ( Util.isEmpty( room ) ) {
+ status.add( "room empty" );
+ return false;
+ }
+ if ( Util.isEmpty( message ) ) {
+ status.add( "message empty" );
+ return false;
+ }
+ if ( Util.isEmpty( password ) ) {
+ status.add( "password empty" );
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ protected boolean execute()
+ {
+ ArrayList<String> errors = new ArrayList<>( 0 );
+ MatrixClient.sendMessage( serverAddress, room, user, password, message, htmlMessage, errors );
+ return true;
+ }
+
+ public static class Output
+ {
+ private String messages;
+
+ public void add( String message )
+ {
+ this.messages += message + "\n";
+ }
+
+ public String getMessages()
+ {
+ return this.messages;
+ }
+ }
+
+}