Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit af041bb

Browse files
committed
Merge branch 'develop' into feature/102-gui-task-rework
# Conflicts: # .gitignore
2 parents f3bcc6a + 8497176 commit af041bb

File tree

9 files changed

+55
-5
lines changed

9 files changed

+55
-5
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,7 @@ project/plugins/project/
3636
/wiki/
3737

3838
# Plugin Data
39-
data/
39+
/data/
40+
41+
# Log Output
42+
/log/

src/main/scala/org/codeoverflow/chatoverflow/ChatOverflow.scala

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.codeoverflow.chatoverflow
22

3+
import java.io.File
34
import java.security.Policy
45

56
import org.codeoverflow.chatoverflow.api.APIVersion
@@ -49,6 +50,9 @@ class ChatOverflow(val pluginFolderPath: String,
4950

5051
logger debug "Initialization started."
5152

53+
logger debug "Ensuring that all required directories exist."
54+
createDirectories()
55+
5256
logger debug "Enabling framework security policy."
5357
enableFrameworkSecurity()
5458

@@ -99,6 +103,11 @@ class ChatOverflow(val pluginFolderPath: String,
99103
System.setSecurityManager(new SecurityManager)
100104
}
101105

106+
private def createDirectories(): Unit = {
107+
Set(pluginFolderPath, configFolderPath, Launcher.pluginDataPath)
108+
.foreach(path => new File(path).mkdir())
109+
}
110+
102111
/**
103112
* Saves all settings and credentials to the corresponding files in the config folder.
104113
*/

src/main/scala/org/codeoverflow/chatoverflow/Launcher.scala

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.codeoverflow.chatoverflow
22

3+
import java.time.OffsetDateTime
4+
import java.time.format.DateTimeFormatter
5+
6+
import org.apache.log4j.{FileAppender, Level, Logger, PatternLayout}
37
import org.codeoverflow.chatoverflow.ui.CLI.parse
48
import org.codeoverflow.chatoverflow.ui.web.Server
59

@@ -19,6 +23,24 @@ object Launcher extends WithLogger {
1923
def main(args: Array[String]): Unit = {
2024
parse(args) { config =>
2125

26+
// Enable logging to log files if specified
27+
if (config.logFileOutput) {
28+
val fileName = s"log/${
29+
OffsetDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd--HH-mm-ss-SSS"))
30+
}.log"
31+
32+
// Try to retrieve the console appender layout or create a default one
33+
val pattern = if (logger.getAppender("CA") != null) {
34+
logger.getAppender("CA").getLayout
35+
} else new PatternLayout("%-4r %d{HH:mm:ss.SSS} [%t] %-5p %c{2} %x - %m%n")
36+
37+
// This is the pattern from our basic console logger
38+
val appender = new FileAppender(pattern, fileName, true)
39+
appender.setThreshold(Level.ALL)
40+
41+
Logger.getRootLogger.addAppender(appender)
42+
}
43+
2244
// Set globally available plugin data path
2345
this.pluginDataPath = config.pluginDataPath
2446

@@ -56,6 +78,8 @@ object Launcher extends WithLogger {
5678
logger warn "Unable to run startup plugins. No/wrong password supplied."
5779
}
5880
}
81+
82+
Runtime.getRuntime.addShutdownHook(new Thread(() => exit()))
5983
}
6084
}
6185

@@ -98,6 +122,5 @@ object Launcher extends WithLogger {
98122
}
99123

100124
logger info "Bye Bye. Stay minzig!"
101-
System.exit(0)
102125
}
103126
}

src/main/scala/org/codeoverflow/chatoverflow/requirement/service/twitch/chat/TwitchChatConnectListener.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class TwitchChatConnectListener(fn: (Boolean, String) => Unit) extends ListenerA
1414
override def onEvent(event: Event): Unit = {
1515
event match {
1616
case _: ConnectEvent => fn(true, "")
17-
case e: ConnectAttemptFailedEvent => fn(false, "couldn't connect to irc chat server")
17+
case _: ConnectAttemptFailedEvent => fn(false, "couldn't connect to irc chat server")
1818
case e: NoticeEvent =>
1919
if (e.getNotice.contains("authentication failed")) {
2020
fn(false, "authentication failed")

src/main/scala/org/codeoverflow/chatoverflow/requirement/service/twitch/chat/TwitchChatConnector.scala

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class TwitchChatConnector(override val sourceIdentifier: String) extends Connect
5353

5454
def isJoined(channel: String): Boolean = channels.contains(channel)
5555

56+
def getUsername: String = bot.getNick
57+
5658
private def getConfig: Configuration = {
5759

5860
if (credentials.isDefined) {

src/main/scala/org/codeoverflow/chatoverflow/requirement/service/twitch/chat/impl/TwitchChatInputImpl.scala

+8-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class TwitchChatInputImpl extends EventInputImpl[TwitchEvent, chat.TwitchChatCon
3030
private val emoticonRegex = """(\d+)-(\d+)""".r
3131

3232
private var currentChannel: Option[String] = None
33+
private var ignoreOwnMessages = false
3334

3435
private val onMessageFn = onMessage _
3536
private val onUnknownFn = onUnknown _
@@ -41,7 +42,9 @@ class TwitchChatInputImpl extends EventInputImpl[TwitchEvent, chat.TwitchChatCon
4142
}
4243

4344
private def onMessage(event: MessageEvent): Unit = {
44-
if (currentChannel.isDefined && event.getChannelSource == currentChannel.get) {
45+
if (currentChannel.isDefined && event.getChannelSource == currentChannel.get
46+
&& (!ignoreOwnMessages || event.getUser.getNick.toLowerCase != getUsername)) {
47+
4548
val message = event.getMessage
4649
val color = if (event.getV3Tags.get("color").contains("#")) event.getV3Tags.get("color") else ""
4750
val subscriber = event.getV3Tags.get("subscriber") == "1"
@@ -103,6 +106,10 @@ class TwitchChatInputImpl extends EventInputImpl[TwitchEvent, chat.TwitchChatCon
103106
if (!sourceConnector.get.isJoined(currentChannel.get)) sourceConnector.get.joinChannel(currentChannel.get)
104107
}
105108

109+
override def getUsername: String = sourceConnector.get.getUsername
110+
111+
override def ignoreOwnMessages(ignore: Boolean): Unit = this.ignoreOwnMessages = ignore
112+
106113
/**
107114
* Stops the input, called before source connector will shutdown
108115
*

src/main/scala/org/codeoverflow/chatoverflow/requirement/service/twitch/chat/impl/TwitchChatOutputImpl.scala

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class TwitchChatOutputImpl extends OutputImpl[chat.TwitchChatConnector] with Twi
2929
if (!sourceConnector.get.isJoined(currentChannel.get)) sourceConnector.get.joinChannel(currentChannel.get)
3030
}
3131

32+
override def getUsername: String = sourceConnector.get.getUsername
33+
3234
/**
3335
* Stops the output, called before source connector will shutdown
3436
*

src/main/scala/org/codeoverflow/chatoverflow/ui/CLI.scala

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ object CLI {
3737
opt[Unit]('o', "enablePluginOutput").action((_, c) =>
3838
c.copy(pluginLogOutputOnConsole = true)).text("set this flag to enable plugin log output on console")
3939

40+
opt[Unit]('f', "enableLogFile").action((_, c) =>
41+
c.copy(logFileOutput = true)).text("set this flag to enable logging to log files")
42+
4043
help("help").hidden().text("prints this usage text")
4144

4245
note("\nFor more information, please visit http://codeoverflow.org/")
@@ -65,6 +68,7 @@ object CLI {
6568
pluginDataPath: String = "data",
6669
webServerPort: Int = 2400,
6770
pluginLogOutputOnConsole: Boolean = false,
71+
logFileOutput: Boolean = false,
6872
loginPassword: Array[Char] = Array[Char](),
6973
startupPlugins: Seq[String] = Seq[String]())
7074

src/main/scala/org/codeoverflow/chatoverflow/ui/web/rest/config/ConfigController.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ConfigController(implicit val swagger: Swagger) extends JsonServlet with C
3838
// Give enough time to return success. Then bye bye
3939
new Thread(() => {
4040
Thread.sleep(500)
41-
Launcher.exit()
41+
System.exit(0) // Shutdown hook takes care to stop everything (registered on startup by the Launcher)
4242
}).start()
4343
ResultMessage(success = true)
4444
}

0 commit comments

Comments
 (0)