intel2noise.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "strings"
  6. "github.com/nxadm/tail"
  7. )
  8. // ParseChat Parses a string, returns the payload of the chat
  9. // *sigh*
  10. // This took effing forever to get right.
  11. // "Lets start with Regex, cannot go wrong".
  12. // You have this string:
  13. // ��[ 2022.02.09 10:05:43 ] Kaysee Guru > EX-GBT clr nd
  14. // Cool. Looks easy.
  15. // .*\[ (.*?) (.*?) \] (.*?) > (.*)
  16. // regex101.com says its good
  17. // And no matter what I do just does not work for some magical reason.
  18. //
  19. // Lets do this with splitting by space and compare it with that. Do a bunch of ifs, it works in test aaaaaand....
  20. // It doesn't work.
  21. //
  22. // By now I have a somewhat reliable version, but it fails to look for the word MOTD.
  23. // I should prolly look into runes()
  24. // This is terrible
  25. func ParseChat(chatline string) (LineDate string, LineTime string, LineUser string, Payload []string) {
  26. logLine := chatline[3:]
  27. //var LineDate string
  28. //var LineTime string
  29. //var LineUser string
  30. var LinePayload string
  31. //var Payload []string
  32. var splitpos int
  33. splitpos = strings.Index(logLine, ">")
  34. LineDate = logLine[4:24]
  35. LineTime = logLine[24:42]
  36. LineUser = logLine[46 : splitpos-2]
  37. LinePayload = logLine[splitpos+4:]
  38. LinePayload = shorten(LinePayload)
  39. Payload = strings.Fields(LinePayload)
  40. //fmt.Println(logLine)
  41. //fmt.Println(LineDate)
  42. //fmt.Println(LineTime)
  43. //fmt.Println(LineUser)
  44. //fmt.Println(splitpos)
  45. //fmt.Println(LinePayload)
  46. //fmt.Println(Payload)
  47. return
  48. }
  49. func shorten(payload string) (logLine string) {
  50. runes := []rune(payload)
  51. for pos, char := range runes {
  52. if pos%2 == 0 && char != 13 {
  53. logLine = logLine + string(char)
  54. }
  55. }
  56. return
  57. }
  58. // CompareElements Compares a log msg with systems we wish to track
  59. func CompareElements(alerts []string, payload []string) bool {
  60. for _, word := range payload {
  61. for _, alert := range alerts {
  62. if strings.EqualFold(word, alert) {
  63. //fmt.Println("Found (EF): ", alert, "in", word)
  64. return true
  65. } //else {
  66. // fmt.Println(word, "!=", alert)
  67. //}
  68. // In the odd case somebody links "C-V6DQ*"
  69. if len(word) > 6 {
  70. //alert = alert[:len(word)]
  71. word = word[:6]
  72. }
  73. // In case of partials like "C-V"
  74. if len(word) < 6 && len(word) >= 3 {
  75. //word = word[:5]
  76. alert = alert[:len(word)]
  77. }
  78. if strings.EqualFold(word, alert) {
  79. //fmt.Println("Found: ", alert, "in", word)
  80. return true
  81. }
  82. }
  83. }
  84. return false
  85. }
  86. // playBeep Play a beep
  87. // Honestly, I tried to play alert.mp3, but after two executions I got a segfault and thought 0x07 it is!
  88. func playBeep() {
  89. fmt.Print("\a")
  90. }
  91. func main() {
  92. // Deal with parameters first.
  93. LogLocation := flag.String("l", "", "Location of the log-file to track.")
  94. LogSystemsRaw := flag.String("s", "jita,Perimeter,New Caldari,Sobaseki", "comma-seperated list of systems")
  95. //LogAlarm := flag.String("a", "alert.mp3", "Audio file to play when an alert needs to be triggered")
  96. flag.Parse()
  97. // Split the CSV string into different parts
  98. LogSystems := strings.Split(*LogSystemsRaw, ",")
  99. // Do the main loop and start parsing the logfiles
  100. t, err := tail.TailFile(*LogLocation, tail.Config{Follow: true})
  101. if err != nil {
  102. panic(err)
  103. }
  104. for line := range t.Lines {
  105. // Cast a line to a text, trim the trash
  106. logLine := string(line.Text)
  107. logLine = strings.Trim(logLine, "\n")
  108. // Figure out if this is a message we want to deal with
  109. // > is critical in this regard because it is the start of the message
  110. // and everything behind it until it hits ] is the username
  111. splitpos := strings.Index(logLine, ">")
  112. if -1 == splitpos {
  113. continue
  114. }
  115. // Somehow I cannot filter out this stuff.
  116. // EVE System > Channel MOTD
  117. LineDate, LineTime, LineUser, Payload := ParseChat(logLine)
  118. if CompareElements(LogSystems, Payload) {
  119. playBeep()
  120. fmt.Println("On", LineDate, "at", LineTime, "", LineUser, "posted", Payload)
  121. }
  122. }
  123. }