Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Daniele Ricci <[email protected]>
  • Loading branch information
daniele-athome committed Aug 3, 2018
0 parents commit 1b8a1a4
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea
/*.iml
/target
45 changes: 45 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.kontalk</groupId>
<artifactId>databot</artifactId>
<version>1</version>

<modules>
<module>../konbot</module>
</modules>

<name>Databot</name>
<description>A data bot for Kontalk</description>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.kontalk</groupId>
<artifactId>konbot</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
</dependencies>

</project>
61 changes: 61 additions & 0 deletions src/main/java/org/kontalk/databot/Databot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Databot
* Copyright (C) 2018 Kontalk Devteam <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.kontalk.databot;

import org.apache.commons.lang3.ArrayUtils;
import org.kontalk.konbot.Konbot;
import org.kontalk.konbot.shell.BotShell;


public class Databot extends Konbot {

private final String datasetFile;
private final String serverSpec;
private final String personalKeyFile;
private final String personalKeyPassphrase;

public Databot(String[] args) {
super(null);
datasetFile = args[0];
serverSpec = args[1];
personalKeyFile = args[2];
personalKeyPassphrase = args[3];
}

public void run() {
try {
BotShell sh = new BotShell();
sh.init();
sh.run(ArrayUtils.addAll(new String[]{"server"}, serverSpec.split(" ")));
sh.run("personalkey", personalKeyFile, personalKeyPassphrase);
sh.run("connect");
sh.run("httpserver");
sh.run("databot", datasetFile);
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}

public static void main(String[] args) {
new Databot(args).run();
}

}
109 changes: 109 additions & 0 deletions src/main/java/org/kontalk/databot/commands/DatabotCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Konbot
* Copyright (C) 2018 Kontalk Devteam <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.kontalk.databot.commands;

import org.bouncycastle.openpgp.PGPException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.chat2.Chat;
import org.jivesoftware.smack.chat2.ChatManager;
import org.jivesoftware.smack.chat2.IncomingChatMessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jxmpp.jid.EntityBareJid;
import org.kontalk.konbot.client.XMPPTCPConnection;
import org.kontalk.konbot.shell.HelpableCommand;
import org.kontalk.konbot.shell.ShellCommand;
import org.kontalk.konbot.shell.ShellSession;
import org.kontalk.konbot.shell.commands.AbstractCommand;
import org.kontalk.konbot.shell.commands.ConnectCommand;
import org.kontalk.konbot.shell.commands.HttpServerCommand;
import org.kontalk.konbot.util.MessageUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Random;


@SuppressWarnings("unused")
public class DatabotCommand extends AbstractCommand implements HelpableCommand {

@Override
public String name() {
return "databot";
}

@Override
public String description() {
return "Databot that sends random messages from a dataset";
}

@Override
public void run(String[] args, ShellSession session) {
if (args.length < 2) {
help();
return;
}

String datasetFile = args[1];
// load dataset and put in session
try (InputStream in = new FileInputStream(datasetFile)) {
Properties dataset = new Properties();
dataset.load(in);
session.put("databot.dataset", dataset);
}
catch (IOException e) {
throw new IllegalArgumentException("Unable to load dataset: " + e);
}

// TODO register a random timer for sending a roster broadcast with a random element from the dataset

// register a message listener and reply with a random element from the dataset
XMPPTCPConnection conn = ConnectCommand.connection(session);
ChatManager.getInstanceFor(conn).addIncomingListener((entityBareJid, message, chat) -> {
String text = randomData(dataset(session));
Message reply = new Message(entityBareJid, Message.Type.chat);
reply.setBody(text);
try {
chat.send(MessageUtils.signMessage(reply));
}
catch (Exception e) {
println("Unable to send message: " + e);
e.printStackTrace(out);
}
});
}

private String randomData(Properties dataset) {
Random generator = new Random();
// inefficient, but works
Object[] values = dataset.values().toArray();
return (String) values[generator.nextInt(values.length)];
}

public static Properties dataset(ShellSession session) {
return (Properties) session.get("databot.dataset");
}

@Override
public void help() {
println("Usage: "+name()+" <dataset.properties>");
}
}

0 comments on commit 1b8a1a4

Please sign in to comment.