From 3a66d0477dd5c8e349b2c008a305ac4050c46ff7 Mon Sep 17 00:00:00 2001 From: FelixSFD Date: Sat, 31 Mar 2018 13:22:42 +0200 Subject: [PATCH 01/10] #162: + added SOBoticsWorkshopChatRoom --- .../sobotics/guttenberg/clients/Client.java | 6 +- ...ingFacility.java => SEBoticsChatRoom.java} | 2 +- .../roomdata/SOBoticsWorkshopChatRoom.java | 59 +++++++++++++++++++ 3 files changed, 64 insertions(+), 3 deletions(-) rename src/main/java/org/sobotics/guttenberg/roomdata/{SOGuttenbergTestingFacility.java => SEBoticsChatRoom.java} (96%) create mode 100644 src/main/java/org/sobotics/guttenberg/roomdata/SOBoticsWorkshopChatRoom.java diff --git a/src/main/java/org/sobotics/guttenberg/clients/Client.java b/src/main/java/org/sobotics/guttenberg/clients/Client.java index b3c5fac..f0448f3 100644 --- a/src/main/java/org/sobotics/guttenberg/clients/Client.java +++ b/src/main/java/org/sobotics/guttenberg/clients/Client.java @@ -14,8 +14,9 @@ import org.sobotics.redunda.PingService; import org.sobotics.guttenberg.commands.Status; import org.sobotics.guttenberg.roomdata.BotRoom; +import org.sobotics.guttenberg.roomdata.SEBoticsChatRoom; import org.sobotics.guttenberg.roomdata.SOBoticsChatRoom; -import org.sobotics.guttenberg.roomdata.SOGuttenbergTestingFacility; +import org.sobotics.guttenberg.roomdata.SOBoticsWorkshopChatRoom; import org.sobotics.guttenberg.services.RunnerService; import org.sobotics.guttenberg.utils.FilePathUtils; import org.sobotics.guttenberg.utils.StatusUtils; @@ -57,7 +58,8 @@ public static void main(String[] args) { List rooms = new ArrayList<>(); rooms.add(new SOBoticsChatRoom()); - rooms.add(new SOGuttenbergTestingFacility()); + rooms.add(new SOBoticsWorkshopChatRoom()); + rooms.add(new SEBoticsChatRoom()); //get current version Properties guttenbergProperties = new Properties(); diff --git a/src/main/java/org/sobotics/guttenberg/roomdata/SOGuttenbergTestingFacility.java b/src/main/java/org/sobotics/guttenberg/roomdata/SEBoticsChatRoom.java similarity index 96% rename from src/main/java/org/sobotics/guttenberg/roomdata/SOGuttenbergTestingFacility.java rename to src/main/java/org/sobotics/guttenberg/roomdata/SEBoticsChatRoom.java index 20f2a03..2be7c80 100644 --- a/src/main/java/org/sobotics/guttenberg/roomdata/SOGuttenbergTestingFacility.java +++ b/src/main/java/org/sobotics/guttenberg/roomdata/SEBoticsChatRoom.java @@ -14,7 +14,7 @@ import fr.tunaki.stackoverflow.chat.event.MessageReplyEvent; import fr.tunaki.stackoverflow.chat.event.UserMentionedEvent; -public class SOGuttenbergTestingFacility implements BotRoom { +public class SEBoticsChatRoom implements BotRoom { @Override public int getRoomId() { diff --git a/src/main/java/org/sobotics/guttenberg/roomdata/SOBoticsWorkshopChatRoom.java b/src/main/java/org/sobotics/guttenberg/roomdata/SOBoticsWorkshopChatRoom.java new file mode 100644 index 0000000..fe92e78 --- /dev/null +++ b/src/main/java/org/sobotics/guttenberg/roomdata/SOBoticsWorkshopChatRoom.java @@ -0,0 +1,59 @@ +package org.sobotics.guttenberg.roomdata; + +import java.util.function.Consumer; + +import org.sobotics.guttenberg.commandlists.SoBoticsCommandsList; +import org.sobotics.guttenberg.printers.PostPrinter; +import org.sobotics.guttenberg.printers.SoBoticsPostPrinter; +import org.sobotics.guttenberg.services.RunnerService; +import org.sobotics.guttenberg.utils.PostUtils; + +import fr.tunaki.stackoverflow.chat.ChatHost; +import fr.tunaki.stackoverflow.chat.Room; +import fr.tunaki.stackoverflow.chat.event.MessagePostedEvent; +import fr.tunaki.stackoverflow.chat.event.MessageReplyEvent; +import fr.tunaki.stackoverflow.chat.event.UserMentionedEvent; + +public class SOBoticsWorkshopChatRoom implements BotRoom { + + @Override + public int getRoomId() { + return 167908; + } + + @Override + public ChatHost getHost() { + return ChatHost.STACK_OVERFLOW; + } + + @Override + public boolean getIsProductionRoom() { + return false; + } + + @Override + public Consumer getMention(Room room, RunnerService instance) { + return event->new SoBoticsCommandsList().mention(room, event, true, instance); + } + + @Override + public Consumer getMessage(Room room, RunnerService instance) { + return event->new SoBoticsCommandsList().globalCommand(room, event, instance); + } + + @Override + public Consumer getReply(Room room) { + return event-> PostUtils.reply(room, event, true); + } + + @Override + public PostPrinter getPostPrinter() { + return new SoBoticsPostPrinter(); + } + + @Override + public boolean getIsLogged() { + return true; + } + +} From 3b0cfed096355643ca7031d63543af7b2c9f584e Mon Sep 17 00:00:00 2001 From: FelixSFD Date: Sat, 31 Mar 2018 20:41:18 +0200 Subject: [PATCH 02/10] #163: + improved log-output --- .../sobotics/guttenberg/clients/Client.java | 25 +++++++++-- .../guttenberg/clients/Guttenberg.java | 14 +++--- .../sobotics/guttenberg/clients/Updater.java | 13 +++--- .../sobotics/guttenberg/commands/Alive.java | 2 +- .../sobotics/guttenberg/commands/Check.java | 11 +++-- .../guttenberg/commands/Feedback.java | 4 +- .../sobotics/guttenberg/commands/Kill.java | 7 ++- .../sobotics/guttenberg/commands/Reboot.java | 15 +++++-- .../sobotics/guttenberg/commands/Update.java | 12 +++-- .../guttenberg/entities/PostMatch.java | 8 ++++ .../guttenberg/finders/NewAnswersFinder.java | 2 +- .../guttenberg/finders/PlagFinder.java | 23 +++++----- .../finders/RelatedAnswersFinder.java | 16 +++---- .../printers/SoBoticsPostPrinter.java | 10 ++--- .../reasons/ExactParagraphMatch.java | 10 +++-- .../guttenberg/reasons/StringSimilarity.java | 9 ++-- .../guttenberg/services/RunnerService.java | 1 + .../guttenberg/utils/FilePathUtils.java | 1 + .../sobotics/guttenberg/utils/PostUtils.java | 44 +++++++------------ 19 files changed, 134 insertions(+), 93 deletions(-) diff --git a/src/main/java/org/sobotics/guttenberg/clients/Client.java b/src/main/java/org/sobotics/guttenberg/clients/Client.java index b3c5fac..cd3cc32 100644 --- a/src/main/java/org/sobotics/guttenberg/clients/Client.java +++ b/src/main/java/org/sobotics/guttenberg/clients/Client.java @@ -8,6 +8,8 @@ import java.util.List; import java.util.Properties; +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sobotics.redunda.DataService; @@ -35,8 +37,22 @@ private Client(){ } public static void main(String[] args) { - LOGGER.info("Hello, World!"); - LOGGER.info("Load properties..."); + Properties loggerProperties = new Properties(); + try{ + loggerProperties.load(new FileInputStream(FilePathUtils.loggerPropertiesFile)); + + String levelStr = loggerProperties.getProperty("level"); + Level newLevel = Level.toLevel(levelStr, Level.ERROR); + LogManager.getRootLogger().setLevel(newLevel); + } + catch (Throwable e){ + LOGGER.error("Could not load logger.properties! Using default log-level ERROR.", e); + } + + LOGGER.info("============================"); + LOGGER.info("=== Launching Guttenberg ==="); + LOGGER.info("============================"); + LOGGER.info("Loading properties..."); Properties prop = new Properties(); @@ -52,7 +68,7 @@ public static void main(String[] args) { Guttenberg.setLoginProperties(prop); - LOGGER.info("Initialize chat..."); + LOGGER.info("Initializing chat..."); StackExchangeClient seClient = new StackExchangeClient(prop.getProperty("email"), prop.getProperty("password")); List rooms = new ArrayList<>(); @@ -66,6 +82,7 @@ public static void main(String[] args) { InputStream is = Status.class.getResourceAsStream("/guttenberg.properties"); guttenbergProperties.load(is); version = guttenbergProperties.getProperty("version", "0.0.0"); + LOGGER.info("Running on version " + version); } catch (IOException e){ LOGGER.error("Could not load properties", e); @@ -100,7 +117,7 @@ public static void main(String[] args) { redunda.start(); - LOGGER.info("Launch Guttenberg..."); + LOGGER.debug("Initialize RunnerService..."); RunnerService runner = new RunnerService(seClient, rooms); diff --git a/src/main/java/org/sobotics/guttenberg/clients/Guttenberg.java b/src/main/java/org/sobotics/guttenberg/clients/Guttenberg.java index 94041fb..b935619 100644 --- a/src/main/java/org/sobotics/guttenberg/clients/Guttenberg.java +++ b/src/main/java/org/sobotics/guttenberg/clients/Guttenberg.java @@ -44,7 +44,7 @@ public void secureExecute() { try { execute(); } catch (Throwable e) { - LOGGER.error("Error throws in execute()", e); + LOGGER.error("Error thrown in execute()", e); } } @@ -95,23 +95,23 @@ public void execute() throws Throwable { return; } - LOGGER.info("Add the answers to the PlagFinders..."); + LOGGER.debug("Add the answers to the PlagFinders..."); //add relatedAnswers to the PlagFinders for (PlagFinder finder : plagFinders) { Integer targetId = finder.getTargetAnswerId(); - //System.out.println("TargetID: "+targetId); + LOGGER.trace("Check targetID: " + targetId); for (Post relatedItem : relatedAnswersUnsorted) { - //System.out.println(relatedItem); + LOGGER.trace("Related item: " + relatedItem); if (relatedItem.getAnswerID() != null && relatedItem.getAnswerID() != targetId) { finder.relatedAnswers.add(relatedItem); - //System.out.println("Added answer: "+relatedItem); + LOGGER.trace("Added answer: " + relatedItem); } } } - LOGGER.info("There are "+plagFinders.size()+" PlagFinders"); - LOGGER.info("Find the duplicates..."); + LOGGER.debug("There are "+plagFinders.size()+" PlagFinders"); + LOGGER.debug("Find the duplicates..."); //Let PlagFinders find the best match List allMatches = new ArrayList(); for (PlagFinder finder : plagFinders) { diff --git a/src/main/java/org/sobotics/guttenberg/clients/Updater.java b/src/main/java/org/sobotics/guttenberg/clients/Updater.java index 3585a4a..a7f535d 100644 --- a/src/main/java/org/sobotics/guttenberg/clients/Updater.java +++ b/src/main/java/org/sobotics/guttenberg/clients/Updater.java @@ -34,7 +34,7 @@ public Updater() { LOGGER.error("Could not load properties", e); } - LOGGER.info("Loaded properties"); + LOGGER.debug("Loaded properties"); String versionString = guttenbergProperties.getProperty("version", "0.0.0"); this.currentVersion = new Version(versionString); @@ -49,24 +49,23 @@ public Updater() { for (File file : files) { if (file.isFile()) { String name = file.getName(); - //LOGGER.info("File: "+name); + LOGGER.debug("File: "+name); Matcher matcher = pattern.matcher(name); matcher.find(); - //LOGGER.info("Init matcher"); + LOGGER.debug("Init matcher"); String v = ""; try { v = matcher.group(1); } catch (Exception e) { - //LOGGER.error("ERROR", e); + LOGGER.warn("Filename " + name + " didn't match the pattern.", e); } - //LOGGER.info("Matched"); if (v != null && v.length() > 0) { - + LOGGER.debug("Matched"); Version version = new Version(v); - + LOGGER.debug("Found version " + version.get()); if (this.currentVersion.compareTo(version) == -1) { //higher than current version if (this.newVersion.compareTo(version) == -1) { diff --git a/src/main/java/org/sobotics/guttenberg/commands/Alive.java b/src/main/java/org/sobotics/guttenberg/commands/Alive.java index 3992fcf..4146c49 100644 --- a/src/main/java/org/sobotics/guttenberg/commands/Alive.java +++ b/src/main/java/org/sobotics/guttenberg/commands/Alive.java @@ -30,7 +30,7 @@ public boolean validate() { @Override public void execute(Room room, RunnerService instance) { - LOGGER.info("Someone wants to know, if I'm alive"); + LOGGER.warn("Someone wants to know, if I'm alive"); room.send("The instance "+PingService.location+ " is running.\nStandby: "+PingService.standby.toString()); } diff --git a/src/main/java/org/sobotics/guttenberg/commands/Check.java b/src/main/java/org/sobotics/guttenberg/commands/Check.java index 825e5a8..250d6f6 100644 --- a/src/main/java/org/sobotics/guttenberg/commands/Check.java +++ b/src/main/java/org/sobotics/guttenberg/commands/Check.java @@ -40,11 +40,7 @@ public boolean validate() { @Override public void execute(Room room, RunnerService instance) { String word = CommandUtils.extractData(message.getPlainContent()).trim(); - Integer returnValue = 0; - - if(word.contains(" ")){ - String parts[] = word.split(" "); - } + if(word.contains("/")) { word = CommandUtils.getAnswerId(word); @@ -65,10 +61,13 @@ public void execute(Room room, RunnerService instance) { try { JsonObject answer = ApiUtils.getAnswerDetailsById(answerId, "stackoverflow", prop.getProperty("apikey", "")); JsonObject target = answer.get("items").getAsJsonArray().get(0).getAsJsonObject(); + LOGGER.trace("Checking answer: " + target.toString()); PlagFinder finder = new PlagFinder(target); finder.collectData(); List matches = finder.matchesForReasons(true); + LOGGER.trace("Found " + matches.size() + " PostMatches"); + if (matches.size() > 0) { //sort the matches Collections.sort(matches, @@ -91,7 +90,7 @@ public void execute(Room room, RunnerService instance) { room.replyTo(message.getId(), "No similar posts found."); } } catch (IOException e) { - LOGGER.error("ERROR", e); + LOGGER.error("Error while executing the \"check\"-command!", e); } } diff --git a/src/main/java/org/sobotics/guttenberg/commands/Feedback.java b/src/main/java/org/sobotics/guttenberg/commands/Feedback.java index c296853..7092004 100644 --- a/src/main/java/org/sobotics/guttenberg/commands/Feedback.java +++ b/src/main/java/org/sobotics/guttenberg/commands/Feedback.java @@ -57,12 +57,14 @@ public void execute(Room room, RunnerService instance) { try { reportId = Integer.parseInt(word); } catch (Exception e) { - LOGGER.info("Invalid report-ID", e); + LOGGER.warn("Invalid report-ID", e); } if (reportId == -1) return; + LOGGER.debug("Sending feedback " + type + " for report " + reportId); + try { if (type.equalsIgnoreCase("tp") || type.equalsIgnoreCase("k")) { if (!isSELink) { diff --git a/src/main/java/org/sobotics/guttenberg/commands/Kill.java b/src/main/java/org/sobotics/guttenberg/commands/Kill.java index 21e0ab8..5b7567f 100644 --- a/src/main/java/org/sobotics/guttenberg/commands/Kill.java +++ b/src/main/java/org/sobotics/guttenberg/commands/Kill.java @@ -1,5 +1,7 @@ package org.sobotics.guttenberg.commands; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.sobotics.guttenberg.services.RunnerService; import org.sobotics.guttenberg.utils.CommandUtils; @@ -9,6 +11,8 @@ public class Kill implements SpecialCommand { + private static final Logger LOGGER = LoggerFactory.getLogger(Kill.class); + private static final String CMD = "kill"; private final Message message; @@ -26,11 +30,12 @@ public void execute(Room room, RunnerService instance) { User user = message.getUser(); if (!user.isModerator() && !user.isRoomOwner()) { + LOGGER.warn("User " + user.getName() + " tried to kill the bot!"); room.replyTo(message.getId(), "Sorry, but only room-owners and moderators can use this command (@FelixSFD)"); return; } - System.out.println("KILLED BY "+user.getName()); + LOGGER.error("KILLED BY "+user.getName()); System.exit(0); } diff --git a/src/main/java/org/sobotics/guttenberg/commands/Reboot.java b/src/main/java/org/sobotics/guttenberg/commands/Reboot.java index b71f648..f8d5dbb 100644 --- a/src/main/java/org/sobotics/guttenberg/commands/Reboot.java +++ b/src/main/java/org/sobotics/guttenberg/commands/Reboot.java @@ -5,6 +5,8 @@ import java.io.InputStream; import org.sobotics.guttenberg.utils.CommandUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.sobotics.guttenberg.services.RunnerService; import fr.tunaki.stackoverflow.chat.Message; @@ -16,6 +18,9 @@ * @author owen */ public class Reboot implements SpecialCommand { + + private static final Logger LOGGER = LoggerFactory.getLogger(Reboot.class); + private static final String CMD = "reboot"; private final Message message; @@ -33,12 +38,13 @@ public void execute(Room room, RunnerService instance) { User user = message.getUser(); if (!user.isModerator() && !user.isRoomOwner()) { + LOGGER.warn("User " + user.getName() + " tried to reboot the bot!"); room.replyTo(message.getId(), "Sorry, but only room-owners and moderators can use this command (@FelixSFD)"); return; } - System.out.println("REBOOT COMMAND"); + LOGGER.warn("REBOOT COMMAND executed by " + user.getName()); String[] args = message.getPlainContent().split(" "); if (args.length >= 3) { String rebootType = args[2]; @@ -71,19 +77,22 @@ private void softReboot(Room room, RunnerService instance) { } private void hardReboot(Room room) { + LOGGER.warn("Hard reboot..."); try { Properties properties = new Properties(); InputStream is = Status.class.getResourceAsStream("/guttenberg.properties"); properties.load(is); String versionString = (String)properties.get("version"); + + LOGGER.info("Booting version " + versionString); Runtime.getRuntime().exec("nohup java -cp guttenberg-" + versionString + ".jar org.sobotics.guttenberg.clients.Client"); System.exit(0); } catch (IOException e) { - e.printStackTrace(); - room.replyTo(message.getId(), "**Reboot failed:** '" + e.getMessage() + "'."); + LOGGER.error("Hard reboot failed!", e); + room.replyTo(message.getId(), "**Reboot failed!** (cc @FelixSFD)"); } } diff --git a/src/main/java/org/sobotics/guttenberg/commands/Update.java b/src/main/java/org/sobotics/guttenberg/commands/Update.java index dc9a794..8f989b6 100644 --- a/src/main/java/org/sobotics/guttenberg/commands/Update.java +++ b/src/main/java/org/sobotics/guttenberg/commands/Update.java @@ -1,5 +1,7 @@ package org.sobotics.guttenberg.commands; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.sobotics.guttenberg.clients.Updater; import org.sobotics.guttenberg.services.RunnerService; import org.sobotics.guttenberg.utils.CommandUtils; @@ -8,6 +10,8 @@ import fr.tunaki.stackoverflow.chat.Room; public class Update implements SpecialCommand { + private static final Logger LOGGER = LoggerFactory.getLogger(Update.class); + private static final String CMD = "update"; private final Message message; @@ -22,15 +26,15 @@ public boolean validate() { @Override public void execute(Room room, RunnerService instance) { - System.out.println("Load updater..."); + LOGGER.info("Load updater..."); Updater updater = new Updater(); - System.out.println("Check for updates..."); + LOGGER.info("Check for updates..."); boolean update = false; try { update = updater.updateIfAvailable(); } catch (Exception e) { - System.out.println(e.getMessage()); + LOGGER.error("Update failed!", e); room.replyTo(message.getId(), "Update failed!"); } @@ -44,7 +48,7 @@ public void execute(Room room, RunnerService instance) { try { wait(10); } catch (InterruptedException e) { - e.printStackTrace(); + LOGGER.error("Error while waiting for shutdown!", e); } System.exit(0); } diff --git a/src/main/java/org/sobotics/guttenberg/entities/PostMatch.java b/src/main/java/org/sobotics/guttenberg/entities/PostMatch.java index 6dcac07..84a3d10 100644 --- a/src/main/java/org/sobotics/guttenberg/entities/PostMatch.java +++ b/src/main/java/org/sobotics/guttenberg/entities/PostMatch.java @@ -47,13 +47,16 @@ public void addReason(String reason, double score) { } public void addReasonToCopyPastorString(String reason, double score) { + LOGGER.trace("Adding reason \"" + reason + "\" (Score: " + score + ") to string for CopyPastor"); if (!reasons.contains(reason)) { + LOGGER.trace("Reason doesn't exist in string yet"); double roundedScore = Math.round(score*100.0)/100.0; if (copyPastorReasonString.length() > 0) this.copyPastorReasonString += ","; this.copyPastorReasonString += reason + ":" + roundedScore; + LOGGER.trace("New copyPastorReasonString: " + this.copyPastorReasonString); } } @@ -107,6 +110,11 @@ public boolean isValidMatch() { if (this.isRepost()) minimumTimeSpanChecked = targetCreation.getEpochSecond() > originalCreation.getEpochSecond(); + LOGGER.trace("minimumTimeSpanChecked: " + minimumTimeSpanChecked); + LOGGER.trace("Minimum length: " + minimumLength); + LOGGER.trace("Length one: " + lengthOne); + LOGGER.trace("Length two: " + lengthTwo); + return lengthOne >= minimumLength && lengthTwo >= minimumLength && minimumTimeSpanChecked; } diff --git a/src/main/java/org/sobotics/guttenberg/finders/NewAnswersFinder.java b/src/main/java/org/sobotics/guttenberg/finders/NewAnswersFinder.java index ea35634..19a320e 100644 --- a/src/main/java/org/sobotics/guttenberg/finders/NewAnswersFinder.java +++ b/src/main/java/org/sobotics/guttenberg/finders/NewAnswersFinder.java @@ -50,7 +50,7 @@ public static List findRecentAnswers() { //fetched answers JsonArray items = apiResult.get("items").getAsJsonArray(); - //System.out.println(items); + LOGGER.trace("New answers:\n" + items.toString()); LOGGER.info("findRecentAnswers() done with "+items.size()+" items"); List posts = new ArrayList(); diff --git a/src/main/java/org/sobotics/guttenberg/finders/PlagFinder.java b/src/main/java/org/sobotics/guttenberg/finders/PlagFinder.java index c7668cf..eb9d204 100644 --- a/src/main/java/org/sobotics/guttenberg/finders/PlagFinder.java +++ b/src/main/java/org/sobotics/guttenberg/finders/PlagFinder.java @@ -59,13 +59,13 @@ public PlagFinder(Post target, List related) { public void collectData() { this.relatedAnswers = new ArrayList(); this.fetchRelatedAnswers(); - LOGGER.info("RelatedAnswers: "+this.relatedAnswers.size()); + LOGGER.debug("Number of related answers: "+this.relatedAnswers.size()); } private void fetchRelatedAnswers() { int targetId = this.targetAnswer.getQuestionID(); int targetAnswerId = this.targetAnswer.getAnswerID(); - LOGGER.info("Target: "+targetId); + LOGGER.debug("Fetching related answers to: "+targetId); Properties prop = new Properties(); try{ @@ -79,18 +79,19 @@ private void fetchRelatedAnswers() { try { JsonObject relatedQuestions = ApiUtils.getRelatedQuestionsById(targetId, "stackoverflow", prop.getProperty("apikey", "")); JsonObject linkedQuestions = ApiUtils.getLinkedQuestionsById(targetId, "stackoverflow", prop.getProperty("apikey", "")); - //System.out.println("Answer: "+relatedQuestions); + LOGGER.trace("Related questions: "+relatedQuestions); + LOGGER.trace("Linked questions: "+linkedQuestions); String relatedIds = targetId+";"; for (JsonElement question : relatedQuestions.get("items").getAsJsonArray()) { int id = question.getAsJsonObject().get("question_id").getAsInt(); - //System.out.println("Add: "+id); + LOGGER.trace("Add: "+id); relatedIds += id+";"; } for (JsonElement question : linkedQuestions.get("items").getAsJsonArray()) { int id = question.getAsJsonObject().get("question_id").getAsInt(); - //System.out.println("Add: "+id); + LOGGER.trace("Add: "+id); relatedIds += id+";"; } @@ -98,14 +99,14 @@ private void fetchRelatedAnswers() { relatedIds = relatedIds.substring(0, relatedIds.length()-1); - LOGGER.info("Related question IDs: "+relatedIds); - LOGGER.info("Fetching all answers..."); + LOGGER.debug("Related question IDs: "+relatedIds); + LOGGER.debug("Fetching all answers..."); JsonObject relatedAnswers = ApiService.defaultService.getAnswersToQuestionsByIdString(relatedIds); - //JsonObject relatedAnswers = ApiUtils.getAnswersToQuestionsByIdString(relatedIds, "stackoverflow", prop.getProperty("apikey", "")); - //System.out.println(relatedAnswers); + for (JsonElement answer : relatedAnswers.get("items").getAsJsonArray()) { JsonObject answerObject = answer.getAsJsonObject(); + LOGGER.trace("Related answer: " + answer); Post answerPost = PostUtils.getPost(answerObject); int answerId = answerPost.getAnswerID(); @@ -154,7 +155,7 @@ public List matchesForReasons(boolean ignoringScores) { ReasonList reasonList = new SOBoticsReasonList(this.targetAnswer, this.relatedAnswers); for (Reason reason : reasonList.reasons(ignoringScores)) { - //LOGGER.info("Checking reason "+reason.description()+"\nIgnoring score: "+ignoringScores); + LOGGER.debug("Checking reasons. Ignoring score: "+ignoringScores); //check if the reason applies if (reason.check()) { //if yes, add (new) posts to the list @@ -176,6 +177,7 @@ public List matchesForReasons(boolean ignoringScores) { if (existingMatch.getOriginal().getAnswerID() == id) { //if it exists, add the new reason alreadyExists = true; + LOGGER.trace("Adding reason " + reason.description(i) + " with score " + scores.get(n)); existingMatch.addReason(reason.description(i), scores.get(n)); existingMatch.addReasonToCopyPastorString(reason.description(i, false), scores.get(n)); @@ -190,6 +192,7 @@ public List matchesForReasons(boolean ignoringScores) { if (!alreadyExists) { PostMatch newMatch = new PostMatch(this.targetAnswer, post); newMatch.addReason(reason.description(i), scores.get(n)); + LOGGER.trace("Adding PostMatch " + newMatch); matches.add(newMatch); } diff --git a/src/main/java/org/sobotics/guttenberg/finders/RelatedAnswersFinder.java b/src/main/java/org/sobotics/guttenberg/finders/RelatedAnswersFinder.java index 46aa708..c36d1cf 100644 --- a/src/main/java/org/sobotics/guttenberg/finders/RelatedAnswersFinder.java +++ b/src/main/java/org/sobotics/guttenberg/finders/RelatedAnswersFinder.java @@ -55,25 +55,25 @@ public List fetchRelatedAnswers() { LOGGER.error("Could not load login.properties", e); } - LOGGER.info("Fetch the linked/related questions..."); + LOGGER.debug("Fetching the linked/related questions..."); try { JsonObject relatedQuestions = ApiService.defaultService.getRelatedQuestionsByIds(idString); - LOGGER.info("Related done"); + LOGGER.debug("Related done"); JsonObject linkedQuestions = ApiService.defaultService.getLinkedQuestionsByIds(idString); - LOGGER.info("linked done"); + LOGGER.debug("linked done"); String relatedIds = ""; for (JsonElement question : relatedQuestions.get("items").getAsJsonArray()) { int id = question.getAsJsonObject().get("question_id").getAsInt(); - //System.out.println("Add: "+id); + LOGGER.trace("Add: "+id); relatedIds += id+";"; } for (JsonElement question : linkedQuestions.get("items").getAsJsonArray()) { int id = question.getAsJsonObject().get("question_id").getAsInt(); - //System.out.println("Add: "+id); + LOGGER.trace("Add: "+id); relatedIds += id+";"; } @@ -85,9 +85,9 @@ public List fetchRelatedAnswers() { int i = 1; while (i <= 2) { - LOGGER.info("Fetch page "+i); + LOGGER.debug("Fetch page "+i); JsonObject relatedAnswers = ApiService.defaultService.getAnswersToQuestionsByIdString(relatedIds, i); - //System.out.println(relatedAnswers); + LOGGER.trace("Related answers:\n" + relatedAnswers); for (JsonElement answer : relatedAnswers.get("items").getAsJsonArray()) { JsonObject answerObject = answer.getAsJsonObject(); @@ -108,7 +108,7 @@ public List fetchRelatedAnswers() { relatedPosts.add(post); } - LOGGER.info("Collected "+relatedFinal.size()+" answers"); + LOGGER.debug("Collected "+relatedFinal.size()+" answers"); return relatedPosts; } else { diff --git a/src/main/java/org/sobotics/guttenberg/printers/SoBoticsPostPrinter.java b/src/main/java/org/sobotics/guttenberg/printers/SoBoticsPostPrinter.java index d69a20e..d59a0c5 100644 --- a/src/main/java/org/sobotics/guttenberg/printers/SoBoticsPostPrinter.java +++ b/src/main/java/org/sobotics/guttenberg/printers/SoBoticsPostPrinter.java @@ -1,19 +1,13 @@ package org.sobotics.guttenberg.printers; import java.io.IOException; -import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.sobotics.guttenberg.clients.Guttenberg; -import org.sobotics.guttenberg.entities.Post; import org.sobotics.guttenberg.entities.PostMatch; -import org.sobotics.guttenberg.utils.JsonUtils; import org.sobotics.guttenberg.utils.PostUtils; import org.sobotics.guttenberg.utils.PrintUtils; -import com.google.gson.JsonObject; - /** * Created by bhargav.h on 20-Oct-16. */ @@ -36,6 +30,8 @@ public String print(PostMatch match) { reasonsList += reason+"; "; } + LOGGER.debug("PostPrinter reasons: " + reasonsList); + String plagOrRepost = match.isRepost() ? "repost" : "plagiarism"; double roundedTotalScore = Math.round(match.getTotalScore()*100.0)/100.0; @@ -44,7 +40,7 @@ public String print(PostMatch match) { reportLink = PostUtils.storeReport(match); } catch (IOException e) { - LOGGER.warn(e.getMessage()); + LOGGER.error("Error while sending the report to CopyPastor!", e); } diff --git a/src/main/java/org/sobotics/guttenberg/reasons/ExactParagraphMatch.java b/src/main/java/org/sobotics/guttenberg/reasons/ExactParagraphMatch.java index c175374..5105516 100644 --- a/src/main/java/org/sobotics/guttenberg/reasons/ExactParagraphMatch.java +++ b/src/main/java/org/sobotics/guttenberg/reasons/ExactParagraphMatch.java @@ -36,6 +36,7 @@ public ExactParagraphMatch(Post target, List originalPosts) { @Override public boolean check() { + LOGGER.trace("Checking for " + LABEL); Properties prop = new Properties(); try { prop.load(new FileInputStream(FilePathUtils.generalPropertiesFile)); @@ -49,9 +50,10 @@ public boolean check() { boolean matched = false; List targetCodePs = PostUtils.getCodeParagraphs(this.target.getCleanBodyMarkdown()); - //System.out.println(targetCodePs); + LOGGER.trace("Target code-only paragraphs: " + targetCodePs.size()); for (Post original : this.originals) { List originalCodePs = PostUtils.getCodeParagraphs(original.getCleanBodyMarkdown()); + LOGGER.trace("Original code-only paragraphs: " + targetCodePs.size()); //Loop through targetCodePs for (String targetCode : targetCodePs) { @@ -59,7 +61,7 @@ public boolean check() { for (String originalCode : originalCodePs) { double similarity = jw.similarity(targetCode, originalCode); if (similarity > 0.97 && originalCode.length() >= minimumLength && targetCode.length() >= minimumLength) { - System.out.println("Exact match: "+similarity+"\n"+targetCode); + LOGGER.debug("Exact match: "+similarity+"\n"+targetCode); if (this.score < 0) this.score = 0; @@ -86,7 +88,9 @@ public String description(int index) { public String description(int index, boolean includingScore) { if (includingScore) { double roundedScore = Math.round(this.scoreList.get(index)*100.0)/100.0; - return score() >= 0 ? LABEL + " "+roundedScore : LABEL; + String descriptionStr = score() >= 0 ? LABEL + " "+roundedScore : LABEL; + LOGGER.trace("Description: " + descriptionStr); + return descriptionStr; } else { return LABEL; } diff --git a/src/main/java/org/sobotics/guttenberg/reasons/StringSimilarity.java b/src/main/java/org/sobotics/guttenberg/reasons/StringSimilarity.java index 5abc40f..22ee94a 100644 --- a/src/main/java/org/sobotics/guttenberg/reasons/StringSimilarity.java +++ b/src/main/java/org/sobotics/guttenberg/reasons/StringSimilarity.java @@ -41,6 +41,7 @@ public StringSimilarity(Post target, List originalPosts, boolean ignoringS } public static double similarityOf(Post targetPost, Post originalPost) { + LOGGER.debug("Checking StringSimilarity of " + targetPost.getAnswerID() + " and " + originalPost.getAnswerID()); String targetBodyMarkdown = targetPost.getCleanBodyMarkdown(); String targetCodeOnly = targetPost.getCodeOnly(); String targetPlaintext = targetPost.getPlaintext(); @@ -83,7 +84,7 @@ public static double similarityOf(Post targetPost, Post originalPost) { double jwQuotes = originalQuotes != null ? ( jw.similarity(originalQuotes, targetQuotes) * quantifierQuotes) : 0; - //LOGGER.info("bodyMarkdown: "+jwBodyMarkdown+"; codeOnly: "+jwCodeOnly+"; plaintext: "+jwPlaintext); + LOGGER.debug("bodyMarkdown: "+jwBodyMarkdown+"; codeOnly: "+jwCodeOnly+"; plaintext: "+jwPlaintext); double usedScores = (jwBodyMarkdown > 0 ? quantifierBodyMarkdown : 0) + (jwCodeOnly > 0 ? quantifierCodeOnly : 0) @@ -94,7 +95,7 @@ public static double similarityOf(Post targetPost, Post originalPost) { + (jwPlaintext > 0 ? jwPlaintext : 0) + (jwQuotes > 0 ? jwQuotes : 0)) / usedScores; - //LOGGER.info("Score: "+jaroWinklerScore); + LOGGER.debug("Score: "+jaroWinklerScore); if (jwBodyMarkdown > 0.9) { return jwBodyMarkdown; @@ -148,7 +149,9 @@ public String description(int index) { public String description(int index, boolean includingScore) { if (includingScore) { double roundedScore = Math.round(this.scoreList.get(index)*100.0)/100.0; - return score() >= 0 ? LABEL + " "+roundedScore : LABEL; + String descriptionStr = score() >= 0 ? LABEL + " "+roundedScore : LABEL; + LOGGER.trace("Description: " + descriptionStr); + return descriptionStr; } else { return LABEL; } diff --git a/src/main/java/org/sobotics/guttenberg/services/RunnerService.java b/src/main/java/org/sobotics/guttenberg/services/RunnerService.java index 7004e0c..3a3e0db 100644 --- a/src/main/java/org/sobotics/guttenberg/services/RunnerService.java +++ b/src/main/java/org/sobotics/guttenberg/services/RunnerService.java @@ -128,6 +128,7 @@ public List getChatRooms() { @Override public void standbyStatusChanged(boolean newStatus) { + LOGGER.info("New standby status: " + newStatus); if (newStatus == false) { StatusUtils.lastExecutionFinished = Instant.now(); StatusUtils.lastSucceededExecutionStarted = Instant.now().minusSeconds(30); diff --git a/src/main/java/org/sobotics/guttenberg/utils/FilePathUtils.java b/src/main/java/org/sobotics/guttenberg/utils/FilePathUtils.java index d227eb2..45b68d8 100644 --- a/src/main/java/org/sobotics/guttenberg/utils/FilePathUtils.java +++ b/src/main/java/org/sobotics/guttenberg/utils/FilePathUtils.java @@ -3,6 +3,7 @@ public class FilePathUtils { public static String generalPropertiesFile = "./properties/general.properties"; public static String loginPropertiesFile = "./properties/login.properties"; + public static String loggerPropertiesFile = "./properties/logger.properties"; public static String optedUsersFile = "./data/OptedInUsersList.txt"; public static String blacklistedUsersFile = "./data/BlacklistedUsersList.txt"; } diff --git a/src/main/java/org/sobotics/guttenberg/utils/PostUtils.java b/src/main/java/org/sobotics/guttenberg/utils/PostUtils.java index 94ae26e..4aa2c11 100644 --- a/src/main/java/org/sobotics/guttenberg/utils/PostUtils.java +++ b/src/main/java/org/sobotics/guttenberg/utils/PostUtils.java @@ -19,14 +19,13 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import fr.tunaki.stackoverflow.chat.ChatHost; import fr.tunaki.stackoverflow.chat.Message; import fr.tunaki.stackoverflow.chat.Room; import fr.tunaki.stackoverflow.chat.event.PingMessageEvent; public class PostUtils { - private static final Logger LOGGER = LoggerFactory.getLogger(Guttenberg.class); + private static final Logger LOGGER = LoggerFactory.getLogger(PostUtils.class); private PostUtils() { super(); @@ -58,7 +57,8 @@ public static Post getPost(JsonObject answer) { np.setTags(tags); } catch (Throwable e) { - // LOGGER.warn("No tags found"); + //disabled, since our requests are causing a bug: https://stackapps.com/q/7213/43403 + //LOGGER.warn("No tags found", e); } SOUser answerer = new SOUser(); @@ -69,7 +69,7 @@ public static Post getPost(JsonObject answer) { answerer.setUserType(answererJSON.get("user_type").getAsString()); answerer.setUserId(answererJSON.get("user_id").getAsInt()); } catch (NullPointerException e) { - + LOGGER.debug("Could not load user", e); } np.setAnswerer(answerer); @@ -133,7 +133,7 @@ public static void reply(Room room, PingMessageEvent event, boolean isReply) { Message message = event.getMessage(); Message parentMessage = room.getMessage(event.getParentMessageId()); long parentMessageId = parentMessage.getId(); - System.out.println(message.getContent()); + LOGGER.debug("Received reply: " + message.getContent()); if(CheckUtils.checkIfUserIsBlacklisted(event.getUserId(), room.getHost().getBaseUrl())) { LOGGER.info("Blacklisted user " + event.getUserName() + " replied to the bot."); @@ -146,7 +146,7 @@ public static void reply(Room room, PingMessageEvent event, boolean isReply) { boolean isReport = true; if (!parentMessage.getPlainContent().startsWith("[ [")) { if (parentMessage.getPlainContent().startsWith("---")) { - LOGGER.info("This post has already been handled"); + LOGGER.debug("This post has already been handled"); return; } @@ -201,13 +201,14 @@ public static Integer getIdFromLink(String link) { try { return Integer.parseInt(suid); } catch (NumberFormatException e) { - LOGGER.error(suid + " can't be parsed"); + LOGGER.error(suid + " can't be parsed", e); return null; } } public static String storeReport(PostMatch match) throws IOException { + LOGGER.debug("Sending match to CopyPastor..."); Properties prop = Guttenberg.getLoginProperties(); String targetUsername = ""; @@ -248,6 +249,10 @@ public static String storeReport(PostMatch match) throws IOException { "score", ""+match.getTotalScore(), "reasons", match.getCopyPastorReasonString()); + LOGGER.debug("Sending data to CopyPastor"); + LOGGER.debug("Score: " + match.getTotalScore()); + LOGGER.debug("Reasons: " + match.getCopyPastorReasonString()); + return prop.getProperty("copypastor_url", "http://localhost:5000") + "/posts/" + output.get("post_id").getAsString(); } @@ -293,7 +298,7 @@ public static String storeReport(Post target, Post original) throws IOException * Sends the feedback to CopyPastor * */ public static void checkPingForFeedback(Room room, PingMessageEvent ping) { - LOGGER.info("Checking message for feedback"); + LOGGER.debug("Checking message for feedback"); int reportId = -1; Message reportMsg = null; Message pingMsg = ping.getMessage(); @@ -308,6 +313,7 @@ public static void checkPingForFeedback(Room room, PingMessageEvent ping) { if (reportMsg != null) { reportId = PostUtils.getReportIdFromChatMessage(reportMsg); + LOGGER.trace("ReportId in message: " + reportId); } if (reportId == -1) @@ -364,6 +370,7 @@ public static int getReportIdFromChatMessage(Message message) { * @param feedback tp or fp * */ public static void storeFeedback(Room room, PingMessageEvent ping, int reportId, String feedback) throws IOException { + LOGGER.debug("Sending feedback " + feedback + " for report " + reportId); Properties prop = Guttenberg.getLoginProperties(); String url = prop.getProperty("copypastor_url", "http://localhost:5000")+"/feedback/create"; @@ -375,6 +382,8 @@ public static void storeFeedback(Room room, PingMessageEvent ping, int reportId, "link", ping.getMessage().getUser().getProfileLink() ); + LOGGER.trace("Response form CopyPastor:\n" + output); + String status = output.get("status").getAsString(); if (!status.equalsIgnoreCase("success")) { String statusMsg = output.get("message").getAsString(); @@ -387,23 +396,4 @@ public static void storeFeedback(Room room, PingMessageEvent ping, int reportId, LOGGER.error(statusMsg); } // if } // storeFeedback - - /** - * Ugly workaround to get ChatHost.getName(), which is not public - * https://github.com/Tunaki/chatexchange/issues/5 - * @Deprecated in 1.0; ChatHost.getBaseUrl() now returns the chat-URL - * */ - @Deprecated - public static String getChatHostAsString(ChatHost host) { - switch (host) { - case STACK_OVERFLOW: - return "stackoverflow.com"; - case STACK_EXCHANGE: - return "stackexchange.com"; - case META_STACK_EXCHANGE: - return "meta.stackexchange.com"; - default: - return "stackoverflow.com"; - } - } } // class From a520b739ae7b139818b7ef3aaf7ca7b6b243f340 Mon Sep 17 00:00:00 2001 From: FelixSFD Date: Sat, 31 Mar 2018 20:46:44 +0200 Subject: [PATCH 03/10] #163: - removed System.out.println() --- .../org/sobotics/guttenberg/finders/RelatedAnswersFinder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/sobotics/guttenberg/finders/RelatedAnswersFinder.java b/src/main/java/org/sobotics/guttenberg/finders/RelatedAnswersFinder.java index c36d1cf..909fdc7 100644 --- a/src/main/java/org/sobotics/guttenberg/finders/RelatedAnswersFinder.java +++ b/src/main/java/org/sobotics/guttenberg/finders/RelatedAnswersFinder.java @@ -41,7 +41,7 @@ public List fetchRelatedAnswers() { idString += n++ == 0 ? id : ";"+id; } - System.out.println(idString); + LOGGER.debug("Related IDs: " + idString); if (idString.length() < 2) return new ArrayList(); From cfc250934a10a16c182b0fb7f7c5d95d547425ec Mon Sep 17 00:00:00 2001 From: FelixSFD Date: Sun, 1 Apr 2018 09:59:28 +0200 Subject: [PATCH 04/10] #163: - printing time is not required when using LOGGER --- src/main/java/org/sobotics/guttenberg/clients/Client.java | 2 +- .../java/org/sobotics/guttenberg/clients/Guttenberg.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/sobotics/guttenberg/clients/Client.java b/src/main/java/org/sobotics/guttenberg/clients/Client.java index cd3cc32..6ceea28 100644 --- a/src/main/java/org/sobotics/guttenberg/clients/Client.java +++ b/src/main/java/org/sobotics/guttenberg/clients/Client.java @@ -124,7 +124,7 @@ public static void main(String[] args) { runner.start(); StatusUtils.startupDate = Instant.now(); - LOGGER.info(StatusUtils.startupDate + " - Successfully launched Guttenberg!"); + LOGGER.info("Successfully launched Guttenberg!"); } } diff --git a/src/main/java/org/sobotics/guttenberg/clients/Guttenberg.java b/src/main/java/org/sobotics/guttenberg/clients/Guttenberg.java index b935619..0f85148 100644 --- a/src/main/java/org/sobotics/guttenberg/clients/Guttenberg.java +++ b/src/main/java/org/sobotics/guttenberg/clients/Guttenberg.java @@ -51,13 +51,13 @@ public void secureExecute() { public void execute() throws Throwable { boolean standbyMode = PingService.standby.get(); if (standbyMode == true) { - LOGGER.info("STANDBY - " + Instant.now()); + LOGGER.info("STANDBY - Abort execute()"); return; } Instant startTime = Instant.now(); Properties props = new Properties(); - LOGGER.info("Executing at - "+startTime); + LOGGER.info("Starting Guttenberg.execute() ..."); try { props.load(new FileInputStream(FilePathUtils.generalPropertiesFile)); @@ -138,7 +138,7 @@ public void execute() throws Throwable { StatusUtils.lastSucceededExecutionStarted = startTime; StatusUtils.lastExecutionFinished = Instant.now(); - LOGGER.info("Finished at - "+StatusUtils.lastExecutionFinished); + LOGGER.info("Guttenberg.execute() finished"); } public static Properties getLoginProperties() { From 0d65464235abe6137de999edbca9d297570ea0cb Mon Sep 17 00:00:00 2001 From: FelixSFD Date: Sun, 1 Apr 2018 11:24:59 +0200 Subject: [PATCH 05/10] #163: + added log-command to set the logging-level from chat --- .../commandlists/SoBoticsCommandsList.java | 1 + .../guttenberg/commands/LogLevel.java | 115 ++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 src/main/java/org/sobotics/guttenberg/commands/LogLevel.java diff --git a/src/main/java/org/sobotics/guttenberg/commandlists/SoBoticsCommandsList.java b/src/main/java/org/sobotics/guttenberg/commandlists/SoBoticsCommandsList.java index 2aaa36c..f27e704 100644 --- a/src/main/java/org/sobotics/guttenberg/commandlists/SoBoticsCommandsList.java +++ b/src/main/java/org/sobotics/guttenberg/commandlists/SoBoticsCommandsList.java @@ -45,6 +45,7 @@ public void mention(Room room, PingMessageEvent event, boolean isReply, RunnerSe new Check(message), new ClearHelp(message), new Feedback(message, event, room), + new LogLevel(message), //new OptIn(message), //new OptOut(message), new Quota(message), diff --git a/src/main/java/org/sobotics/guttenberg/commands/LogLevel.java b/src/main/java/org/sobotics/guttenberg/commands/LogLevel.java new file mode 100644 index 0000000..033fca1 --- /dev/null +++ b/src/main/java/org/sobotics/guttenberg/commands/LogLevel.java @@ -0,0 +1,115 @@ +package org.sobotics.guttenberg.commands; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.util.Properties; + +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sobotics.guttenberg.services.RunnerService; +import org.sobotics.guttenberg.utils.CommandUtils; +import org.sobotics.guttenberg.utils.FilePathUtils; +import org.sobotics.redunda.PingService; + +import fr.tunaki.stackoverflow.chat.Message; +import fr.tunaki.stackoverflow.chat.Room; +import fr.tunaki.stackoverflow.chat.User; + +public class LogLevel implements SpecialCommand { + private static final Logger LOGGER = LoggerFactory.getLogger(LogLevel.class); + + private static final String CMD = "log"; + private final Message message; + + public LogLevel(Message message) { + this.message = message; + } + + @Override + public boolean validate() { + return CommandUtils.checkForCommand(message.getPlainContent(), CMD); + } + + @Override + public void execute(Room room, RunnerService instance) { + User user = message.getUser(); + + if (!user.isModerator() && !user.isRoomOwner()) { + LOGGER.warn("User " + user.getName() + " tried to change the logging-level!"); + room.replyTo(message.getId(), "You are not allowed to change the logging-level."); + return; + } + + String allowedLevels = "error|warn|info|debug|trace"; + String levelString = "warn"; + String instanceParam = null; + String paramString = CommandUtils.extractData(message.getPlainContent()); + String[] params = paramString.split(" "); + + LOGGER.debug(params.length + " parameters"); + + if (params.length == 1) { + if (params[0].matches(allowedLevels)) { + //No instance given + levelString = params[0]; + LOGGER.debug("No instance specified. Changing level on all instances..."); + } else { + //Not a valid level + room.replyTo(message.getId(), params[0] + " is not a valid logging-level!"); + LOGGER.debug(params[0] + " is not a valid logging-level!"); + return; + } + } else if (params.length == 2) { + //when two arguments are passed, the first one is the instance-name + instanceParam = params[0]; + + if (params[1].matches(allowedLevels)) { + levelString = params[1]; + } else { + //Not a valid level + room.replyTo(message.getId(), params[1] + " is not a valid logging-level!"); + return; + } + } else { + room.replyTo(message.getId(), "Invalid number of arguments!"); + return; + } + + LOGGER.debug("Level " + levelString + " found."); + + if (instanceParam == null || instanceParam.equalsIgnoreCase(PingService.location)) { + //the logging-level for this instance should be changed + Level newLevel = Level.toLevel(levelString); + LogManager.getRootLogger().setLevel(newLevel); + + try { + Properties loggingProp = new Properties(); + loggingProp.load(new FileInputStream(FilePathUtils.loggerPropertiesFile)); + loggingProp.setProperty("level", levelString.toLowerCase()); + loggingProp.store(new FileOutputStream(FilePathUtils.loggerPropertiesFile), null); + } catch (Exception e) { + LOGGER.error("Error while saving the new log-level to the logging.properties", e); + } + + LOGGER.info("Logging-level successfully changed to " + levelString); + room.send("Instance *" + PingService.location + "* is now using the logging-level **" + levelString.toUpperCase() + "**"); + } + } + + @Override + public String description() { + return "Sets the logging-level. Usage: log "; + } + + @Override + public String name() { + return CMD; + } + + @Override + public boolean availableInStandby() { + return true; + } +} From 2ef209545cc6ada1caa98f8dbecf1a11f8600102 Mon Sep 17 00:00:00 2001 From: FelixSFD Date: Sun, 1 Apr 2018 20:35:32 +0200 Subject: [PATCH 06/10] #157: + feedback-command can now parse CP-urls --- .../guttenberg/commands/Feedback.java | 4 +-- .../guttenberg/utils/CommandUtils.java | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/sobotics/guttenberg/commands/Feedback.java b/src/main/java/org/sobotics/guttenberg/commands/Feedback.java index c296853..21113ff 100644 --- a/src/main/java/org/sobotics/guttenberg/commands/Feedback.java +++ b/src/main/java/org/sobotics/guttenberg/commands/Feedback.java @@ -55,9 +55,9 @@ public void execute(Room room, RunnerService instance) { } try { - reportId = Integer.parseInt(word); + reportId = CommandUtils.getPostIdFromUrl(word); } catch (Exception e) { - LOGGER.info("Invalid report-ID", e); + LOGGER.error("Report-URL could not be parsed!", e); } if (reportId == -1) diff --git a/src/main/java/org/sobotics/guttenberg/utils/CommandUtils.java b/src/main/java/org/sobotics/guttenberg/utils/CommandUtils.java index 869b14b..2102ffa 100644 --- a/src/main/java/org/sobotics/guttenberg/utils/CommandUtils.java +++ b/src/main/java/org/sobotics/guttenberg/utils/CommandUtils.java @@ -4,6 +4,8 @@ import java.io.IOException; import java.util.Arrays; import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -66,5 +68,38 @@ else if (parts[1].equals("q") || parts[1].equals("questions")){ } return word; } + + /** + * Extracts the postId from a CopyPastor-URL + * @param input Input string + * @returns Post-ID + * @throws NumberFormatException if ID could not be read + * @throws IllegalArgumentException if the ID is <= 0 + * */ + public static int getPostIdFromUrl(String input) throws NumberFormatException, IllegalArgumentException { + int postId = -1; + LOGGER.debug("Checking string for postId: " + input); + + try { + postId = Integer.parseInt(input); + } catch (NumberFormatException e) { + LOGGER.debug("Couldn't parse input to integer. Trying RegEx..."); + Pattern regex = Pattern.compile(".*\\/posts\\/(\\d*)"); + Matcher matcher = regex.matcher(input); + + if (matcher.matches()) { + postId = Integer.parseInt(matcher.group(1)); + } else { + throw new NumberFormatException("Could not extract postId from string '" + input + "'! RegEx didn't match."); + } + } + + if (postId > 0) { + LOGGER.debug("Found postId: " + postId); + return postId; + } else { + throw new IllegalArgumentException("Invalid postId!"); + } + } } From b02f08bfe6281fcea047a886b1782b2074fce7c0 Mon Sep 17 00:00:00 2001 From: FelixSFD Date: Tue, 3 Apr 2018 20:57:36 +0200 Subject: [PATCH 07/10] #163: - print the values before sending them --- .../java/org/sobotics/guttenberg/utils/PostUtils.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/sobotics/guttenberg/utils/PostUtils.java b/src/main/java/org/sobotics/guttenberg/utils/PostUtils.java index 4aa2c11..9ddecaf 100644 --- a/src/main/java/org/sobotics/guttenberg/utils/PostUtils.java +++ b/src/main/java/org/sobotics/guttenberg/utils/PostUtils.java @@ -232,6 +232,12 @@ public static String storeReport(PostMatch match) throws IOException { } String url = prop.getProperty("copypastor_url", "http://localhost:5000")+"/posts/create"; + + LOGGER.debug("Sending data to CopyPastor"); + LOGGER.debug("CopyPastorURL: " + url); + LOGGER.debug("Score: " + match.getTotalScore()); + LOGGER.debug("Reasons: " + match.getCopyPastorReasonString()); + JsonObject output = JsonUtils.post(url, "key", prop.getProperty("copypastor_key", "no_key"), "url_one","//stackoverflow.com/a/"+target.getAnswerID(), @@ -249,9 +255,7 @@ public static String storeReport(PostMatch match) throws IOException { "score", ""+match.getTotalScore(), "reasons", match.getCopyPastorReasonString()); - LOGGER.debug("Sending data to CopyPastor"); - LOGGER.debug("Score: " + match.getTotalScore()); - LOGGER.debug("Reasons: " + match.getCopyPastorReasonString()); + LOGGER.debug("Data sent to CopyPastor"); return prop.getProperty("copypastor_url", "http://localhost:5000") + "/posts/" + output.get("post_id").getAsString(); } From 096edb1db9fabcdc9a39921305727515842a7dc9 Mon Sep 17 00:00:00 2001 From: FelixSFD Date: Thu, 5 Apr 2018 17:29:33 +0200 Subject: [PATCH 08/10] #162: - don't join SEBotics anymore --- src/main/java/org/sobotics/guttenberg/clients/Client.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/sobotics/guttenberg/clients/Client.java b/src/main/java/org/sobotics/guttenberg/clients/Client.java index 7dfd747..2037a49 100644 --- a/src/main/java/org/sobotics/guttenberg/clients/Client.java +++ b/src/main/java/org/sobotics/guttenberg/clients/Client.java @@ -75,7 +75,7 @@ public static void main(String[] args) { List rooms = new ArrayList<>(); rooms.add(new SOBoticsChatRoom()); rooms.add(new SOBoticsWorkshopChatRoom()); - rooms.add(new SEBoticsChatRoom()); + //rooms.add(new SEBoticsChatRoom()); //get current version Properties guttenbergProperties = new Properties(); From 6f0ace8f0df266682a2f3480cbd1ec28ffbf57f4 Mon Sep 17 00:00:00 2001 From: FelixSFD Date: Sat, 7 Apr 2018 10:28:04 +0200 Subject: [PATCH 09/10] #157: - only allow feedback for the CP-instance, Guttenberg is currently using --- .../guttenberg/utils/CommandUtils.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/sobotics/guttenberg/utils/CommandUtils.java b/src/main/java/org/sobotics/guttenberg/utils/CommandUtils.java index 2102ffa..247cd93 100644 --- a/src/main/java/org/sobotics/guttenberg/utils/CommandUtils.java +++ b/src/main/java/org/sobotics/guttenberg/utils/CommandUtils.java @@ -83,8 +83,34 @@ public static int getPostIdFromUrl(String input) throws NumberFormatException, I try { postId = Integer.parseInt(input); } catch (NumberFormatException e) { + String pattern = ".*\\/posts\\/(\\d*)"; + Properties props = new Properties(); + + try { + props.load(new FileInputStream(FilePathUtils.loginPropertiesFile)); + + String cpUrl = props.getProperty("copypastor_url"); + + if (cpUrl != null) { + LOGGER.debug("CopyPastor-URL: " + cpUrl); + + cpUrl = Pattern.quote(cpUrl); + + LOGGER.debug("Escaped CopyPastor-URL for RegEx: " + cpUrl); + + pattern = cpUrl + "\\/posts\\/(\\d*)"; + + LOGGER.debug("RegEx pattern: " + pattern); + } else { + LOGGER.warn("CopyPastor-URL is null"); + } + } catch (IOException ioE) { + LOGGER.warn("Could not load general properties", ioE); + } + + LOGGER.debug("Couldn't parse input to integer. Trying RegEx..."); - Pattern regex = Pattern.compile(".*\\/posts\\/(\\d*)"); + Pattern regex = Pattern.compile(pattern); Matcher matcher = regex.matcher(input); if (matcher.matches()) { From 09f7399492c187e9742182e26666a9cd7b523876 Mon Sep 17 00:00:00 2001 From: FelixSFD Date: Wed, 11 Apr 2018 11:29:29 +0200 Subject: [PATCH 10/10] - bump version to 1.2.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a2bc1ea..a7a64ec 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ de.felixsfd.stackoverflow guttenberg - 1.1.0 + 1.2.0