2019-11-26 10:50:38 +01:00
package moe.kageru.spektacles
import arrow.core.Option
import arrow.core.toOption
import java.time.LocalDateTime
class ParsedLine ( val time : LocalDateTime ,
val user : Option < String > ,
2019-11-26 13:01:29 +01:00
val userMode : UserMode ,
2019-11-26 10:50:38 +01:00
val message : String ,
val isSystemMessage : Boolean ,
private val originalMessage : String
) {
override fun toString ( ) = originalMessage
2019-11-26 22:02:39 +01:00
fun getMention ( ) : Option < String > = mentionRegex . find ( message ) . toOption ( ) . map { it . value . toLowerCase ( ) }
2019-11-26 10:50:38 +01:00
companion object {
// 18 04 26 19:40:47 <kageru_> Hi
private val lineRegex = Regex ( """ (?<year>\d\d)\s(?<month>\d\d)\s(?<day>\d\d)\s(?<hour>\d\d):(?<minute>\d\d):(?<second>\d\d)\s(<(?<mode>[~&@%+])?(?<author>\w+)>\t)?(?<message>.*) """ )
2019-11-26 22:02:39 +01:00
private val mentionRegex = Regex ( " (?<=@) \\ w+ " )
2019-11-26 10:50:38 +01:00
fun parse ( line : String ) : ParsedLine ? = lineRegex
. matchEntire ( line )
?. let {
ParsedLine (
LocalDateTime . of (
it . groups [ " year " ] !! . value . toInt ( ) ,
it . groups [ " month " ] !! . value . toInt ( ) ,
it . groups [ " day " ] !! . value . toInt ( ) ,
it . groups [ " hour " ] !! . value . toInt ( ) ,
it . groups [ " minute " ] !! . value . toInt ( ) ,
it . groups [ " second " ] !! . value . toInt ( )
) ,
it . groups [ " author " ] ?. value . toOption ( ) ,
2019-11-26 13:01:29 +01:00
it . groups [ " mode " ] ?. value ?. let { UserMode . values ( ) . first { mode -> mode . icon == it } } ?: UserMode . NONE ,
2019-11-26 10:50:38 +01:00
it . groups [ " message " ] !! . value ,
it . groups [ " author " ] == null ,
it . value
)
}
}
}
enum class UserMode ( val icon : String ) {
ADMIN ( " ~ " ) ,
BOT ( " & " ) ,
OP ( " @ " ) ,
HOP ( " % " ) ,
2019-11-26 13:01:29 +01:00
VOICE ( " + " ) ,
NONE ( " " )
}
typealias ParsedLines = Sequence < ParsedLine >
fun Lines . parse ( ) : ParsedLines {
return map { ParsedLine . parse ( it ) }
2019-11-26 22:02:39 +01:00
. filterNotNull ( )
2019-11-26 13:01:29 +01:00
}