Skip to content

Commit

Permalink
Merge pull request #86 from SOBotics/develop
Browse files Browse the repository at this point in the history
merged release to the wrong branch again
  • Loading branch information
FelixSFD authored Mar 25, 2017
2 parents b59b210 + 537125f commit 84b5d9a
Show file tree
Hide file tree
Showing 25 changed files with 329 additions and 14 deletions.
1 change: 1 addition & 0 deletions data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/OptedInUsersList.txt
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.felixsfd.stackoverflow</groupId>
<artifactId>guttenberg</artifactId>
<version>0.5.4</version>
<version>0.6.101</version>
<build>
<resources>
<resource>
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.felixsfd.stackoverflow</groupId>
<artifactId>guttenberg</artifactId>
<version>0.6.0</version>
<version>0.7.0</version>

<dependencies>
<dependency>
Expand Down Expand Up @@ -45,6 +45,11 @@
<artifactId>java-string-similarity</artifactId>
<version>0.21</version>
</dependency>
<dependency>
<groupId>org.sobotics</groupId>
<artifactId>redunda-lib</artifactId>
<version>0.3.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/org/sobotics/guttenberg/clients/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sobotics.redunda.DataService;
import org.sobotics.redunda.PingService;
import org.sobotics.guttenberg.commands.Status;
import org.sobotics.guttenberg.roomdata.BotRoom;
import org.sobotics.guttenberg.roomdata.SOBoticsChatRoom;
import org.sobotics.guttenberg.roomdata.SOGuttenbergTestingFacility;
Expand Down Expand Up @@ -49,6 +53,45 @@ public static void main(String[] args) {
rooms.add(new SOBoticsChatRoom());
rooms.add(new SOGuttenbergTestingFacility());

//get current version
Properties guttenbergProperties = new Properties();
String version = "0.0.0";
try{
InputStream is = Status.class.getResourceAsStream("/guttenberg.properties");
guttenbergProperties.load(is);
version = guttenbergProperties.getProperty("version", "0.0.0");
}
catch (IOException e){
LOGGER.error("Could not load properties", e);
}

LOGGER.info("Connecting to Redunda...");
PingService redunda = new PingService(prop.getProperty("redunda_apikey", ""), version);
String productionInstance = prop.getProperty("production_instance", "false");


//track files for synchronization
DataService redundaData = redunda.buildDataService();
redundaData.trackFile(FilePathUtils.optedUsersFile);
redundaData.trackFile(FilePathUtils.generalPropertiesFile);

if (productionInstance.equals("false")) {
redunda.setDebugging(true);
LOGGER.info("Set Redunda debugging to true");
} else {
//not debugging
LOGGER.info("Start synchronization...");
redundaData.syncAndStart();
LOGGER.info("Synchronization finished!");
}

//first check manually, so that RunnerService will know the status before posting the welcome message
boolean isOnStandby = redunda.checkStandbyStatus();
if (isOnStandby)
LOGGER.info("Launching in standby...");

redunda.start();


LOGGER.info("Launch Guttenberg...");

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/sobotics/guttenberg/clients/Guttenberg.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sobotics.guttenberg.finders.NewAnswersFinder;
import org.sobotics.redunda.PingService;
import org.sobotics.guttenberg.entities.OptedInUser;
import org.sobotics.guttenberg.entities.Post;
import org.sobotics.guttenberg.finders.PlagFinder;
Expand Down Expand Up @@ -48,6 +49,12 @@ public void secureExecute() {
}

public void execute() throws Throwable {
boolean standbyMode = PingService.standby.get();
if (standbyMode == true) {
LOGGER.info("STANDBY - " + Instant.now());
return;
}

Instant startTime = Instant.now();
Properties props = new Properties();
LOGGER.info("Executing at - "+startTime);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package org.sobotics.guttenberg.commandlists;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sobotics.redunda.PingService;
import org.sobotics.guttenberg.commands.*;
import org.sobotics.guttenberg.services.RunnerService;
import org.sobotics.guttenberg.utils.FilePathUtils;

import fr.tunaki.stackoverflow.chat.Message;
import fr.tunaki.stackoverflow.chat.Room;
import fr.tunaki.stackoverflow.chat.User;
import fr.tunaki.stackoverflow.chat.event.MessagePostedEvent;
import fr.tunaki.stackoverflow.chat.event.PingMessageEvent;

/**
Expand Down Expand Up @@ -43,7 +51,73 @@ public void mention(Room room, PingMessageEvent event, boolean isReply, RunnerSe

for(SpecialCommand command: commands){
if(command.validate()){
command.execute(room, instance);
boolean standbyMode = PingService.standby.get();
if (standbyMode == true) {
if (command.availableInStandby() == true) {
command.execute(room, instance);
}
} else {
command.execute(room, instance);
}
}
}
}

public void globalCommand(Room room, MessagePostedEvent event, RunnerService instance) {
//only ROs should execute global commands!
try {
User user = event.getUser().get();
if (!user.isModerator() && !user.isRoomOwner())
return;
} catch (Throwable e) {
LOGGER.warn("Could not verify privileges of that user. Don't execute the command.", e);
return;
}


Message message = event.getMessage();
LOGGER.info("Message: "+message.getContent());

int cp = Character.codePointAt(message.getPlainContent(), 0);

if (PingService.standby.get() == false && (cp == 128642 || (cp>=128644 && cp<=128650))) {
room.send("[🚃](http://bit.ly/2nRi9kX)");
return;
}

//return immediately, if @gut is part of the message!
String username = "";

Properties prop = new Properties();

try{
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
username = prop.getProperty("username").substring(0,3).toLowerCase();
}
catch (IOException e){
LOGGER.error("Could not load login.properties", e);
username = "gut";
}

boolean containsUsername = message.getPlainContent().toLowerCase().contains("@"+username);
//LOGGER.info("containsUsername: "+containsUsername);
if (containsUsername == true)
return;

List<SpecialCommand> commands = new ArrayList<>(Arrays.asList(
new Alive(message)
));

for(SpecialCommand command: commands){
if(command.validate()){
boolean standbyMode = PingService.standby.get();
if (standbyMode == true) {
if (command.availableInStandby() == true) {
command.execute(room, instance);
}
} else {
command.execute(room, instance);
}
}
}
}
Expand Down
28 changes: 26 additions & 2 deletions src/main/java/org/sobotics/guttenberg/commands/Alive.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package org.sobotics.guttenberg.commands;

import org.sobotics.guttenberg.utils.CommandUtils;
import org.sobotics.guttenberg.utils.FilePathUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sobotics.redunda.PingService;
import org.sobotics.guttenberg.services.RunnerService;

import fr.tunaki.stackoverflow.chat.Message;
Expand All @@ -11,7 +20,7 @@
*/
public class Alive implements SpecialCommand {


private static final Logger LOGGER = LoggerFactory.getLogger(Alive.class);
private final Message message;

public Alive(Message message) {
Expand All @@ -25,7 +34,17 @@ public boolean validate() {

@Override
public void execute(Room room, RunnerService instance) {
room.replyTo(message.getId(), "Not sure. Ask someone else.");
Properties prop = new Properties();

try{
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
}
catch (IOException e){
LOGGER.error("Error: ", e);
room.replyTo(message.getId(), "Maybe. But something strange is going on!");
return;
}
room.send("The instance "+prop.getProperty("location", "undefined")+ " is running.\nStandby: "+PingService.standby.toString());
}

@Override
Expand All @@ -37,4 +56,9 @@ public String description() {
public String name() {
return "alive";
}

@Override
public boolean availableInStandby() {
return true;
}
}
5 changes: 5 additions & 0 deletions src/main/java/org/sobotics/guttenberg/commands/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@ public String name() {
return "check";
}

@Override
public boolean availableInStandby() {
return false;
}

}
5 changes: 5 additions & 0 deletions src/main/java/org/sobotics/guttenberg/commands/ClearHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ public String name() {
return "clear";
}

@Override
public boolean availableInStandby() {
return false;
}

}
5 changes: 5 additions & 0 deletions src/main/java/org/sobotics/guttenberg/commands/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,10 @@ public String description() {
return "Returns the list of commands associated with this bot";
}

@Override
public boolean availableInStandby() {
return false;
}


}
51 changes: 51 additions & 0 deletions src/main/java/org/sobotics/guttenberg/commands/Kill.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.sobotics.guttenberg.commands;

import org.sobotics.guttenberg.services.RunnerService;
import org.sobotics.guttenberg.utils.CommandUtils;

import fr.tunaki.stackoverflow.chat.Message;
import fr.tunaki.stackoverflow.chat.Room;
import fr.tunaki.stackoverflow.chat.User;

public class Kill implements SpecialCommand {

private final Message message;

public Kill(Message message) {
this.message = message;
}

@Override
public boolean validate() {
return CommandUtils.checkForCommand(message.getPlainContent(), "kill");
}

@Override
public void execute(Room room, RunnerService instance) {
User user = message.getUser();

if (!user.isModerator() && !user.isRoomOwner()) {
room.replyTo(message.getId(), "Sorry, but only room-owners and moderators can use this command");
return;
}

System.out.println("KILLED BY "+user.getName());
System.exit(0);
}

@Override
public String description() {
return "Kills the currently active instance";
}

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

@Override
public boolean availableInStandby() {
return false;
}

}
5 changes: 5 additions & 0 deletions src/main/java/org/sobotics/guttenberg/commands/OptIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,9 @@ public String name() {
return "opt-in";
}

@Override
public boolean availableInStandby() {
return false;
}

}
5 changes: 5 additions & 0 deletions src/main/java/org/sobotics/guttenberg/commands/OptOut.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ public String name() {
return "opt-out";
}

@Override
public boolean availableInStandby() {
return false;
}

}
Loading

0 comments on commit 84b5d9a

Please sign in to comment.