2021-10-20 03:13:57 +03:00
|
|
|
// gorc project
|
2022-01-24 04:24:58 +02:00
|
|
|
// Copyright (C) 2022 IllusionMan1212
|
2021-10-20 03:13:57 +03:00
|
|
|
//
|
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation; either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
|
// with this program; if not, see https://www.gnu.org/licenses.
|
|
|
|
|
|
2021-11-14 06:19:46 +02:00
|
|
|
package parser
|
2021-10-20 03:13:57 +03:00
|
|
|
|
|
|
|
|
import (
|
2022-02-19 16:24:39 +02:00
|
|
|
"fmt"
|
2022-01-02 02:27:26 +02:00
|
|
|
"regexp"
|
2021-10-20 03:13:57 +03:00
|
|
|
"strings"
|
2022-02-19 16:24:39 +02:00
|
|
|
"time"
|
2021-10-20 03:13:57 +03:00
|
|
|
)
|
|
|
|
|
|
2021-11-14 06:19:46 +02:00
|
|
|
type IRCMessage struct {
|
2022-02-19 16:24:39 +02:00
|
|
|
Timestamp string
|
2021-12-09 05:14:20 +02:00
|
|
|
Tags Tags // starts with @ | Optional
|
2021-11-14 06:19:46 +02:00
|
|
|
Source string // starts with : | Optional
|
|
|
|
|
Command string // can either be a string or a numeric value | Required
|
|
|
|
|
Parameters []string // Optional (Dependant on command)
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-09 05:14:20 +02:00
|
|
|
type Tags map[string]string
|
|
|
|
|
|
2022-02-19 16:24:39 +02:00
|
|
|
func (m *IRCMessage) setTimestamp() {
|
|
|
|
|
// TODO: get timestamp from time tag
|
|
|
|
|
now := time.Now()
|
2023-02-19 00:58:42 +02:00
|
|
|
m.Timestamp = fmt.Sprintf("[%02d:%02d:%02d.%03d]", now.Hour(), now.Minute(), now.Second(), now.Nanosecond()/1000000)
|
2022-02-19 16:24:39 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-09 05:14:20 +02:00
|
|
|
func parseTags(rawTags string) Tags {
|
|
|
|
|
tags := make(Tags, 0)
|
|
|
|
|
rawTagsSlice := strings.Split(rawTags, ";")
|
|
|
|
|
|
|
|
|
|
for _, tag := range rawTagsSlice {
|
|
|
|
|
str := strings.Split(tag, "=")
|
|
|
|
|
|
|
|
|
|
key := str[0]
|
|
|
|
|
value := ""
|
|
|
|
|
|
|
|
|
|
// we need to make sure a "value" exists before accessing str[1]
|
|
|
|
|
if len(str) > 1 {
|
|
|
|
|
value = str[1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tags[key] = value
|
|
|
|
|
}
|
2021-11-14 06:19:46 +02:00
|
|
|
|
2021-12-09 05:14:20 +02:00
|
|
|
return tags
|
2021-10-20 16:21:26 +03:00
|
|
|
}
|
|
|
|
|
|
2022-01-09 04:30:22 +02:00
|
|
|
func ParseIRCMessage(line string) (IRCMessage, bool) {
|
2021-11-14 06:19:46 +02:00
|
|
|
ircMessage := IRCMessage{}
|
2022-01-02 02:27:26 +02:00
|
|
|
multipleSpacesRegex := regexp.MustCompile("[^\\S\\t]+")
|
|
|
|
|
trailingParamRegex := regexp.MustCompile("[^\\S\\t]+:")
|
|
|
|
|
whitespaceRegex := regexp.MustCompile("^[^\\S\\t]+$")
|
2021-11-14 06:19:46 +02:00
|
|
|
|
|
|
|
|
if len(line) <= 0 {
|
2022-01-09 04:30:22 +02:00
|
|
|
// TODO: do something here ???
|
|
|
|
|
// log.Println("empty message")
|
|
|
|
|
return IRCMessage{}, false
|
2021-11-14 06:19:46 +02:00
|
|
|
}
|
2021-10-20 03:13:57 +03:00
|
|
|
|
2021-10-20 16:21:26 +03:00
|
|
|
if line[0] == '@' {
|
2022-01-02 02:27:26 +02:00
|
|
|
substrs := multipleSpacesRegex.Split(line, 2)
|
2021-10-20 16:21:26 +03:00
|
|
|
tags := parseTags(substrs[0][1:])
|
|
|
|
|
ircMessage.Tags = tags
|
2021-10-20 03:13:57 +03:00
|
|
|
|
2021-10-20 16:21:26 +03:00
|
|
|
line = substrs[1]
|
|
|
|
|
}
|
2021-10-20 03:13:57 +03:00
|
|
|
|
2021-10-20 16:21:26 +03:00
|
|
|
if line[0] == ':' {
|
2022-01-02 02:27:26 +02:00
|
|
|
substrs := multipleSpacesRegex.Split(line, 2)
|
|
|
|
|
ircMessage.Source = substrs[0][1:]
|
2021-10-20 03:13:57 +03:00
|
|
|
|
2021-10-20 16:21:26 +03:00
|
|
|
line = substrs[1]
|
|
|
|
|
}
|
2021-10-20 03:13:57 +03:00
|
|
|
|
2022-01-02 02:27:26 +02:00
|
|
|
substrs := multipleSpacesRegex.Split(line, 2)
|
2021-10-20 03:13:57 +03:00
|
|
|
|
2022-01-02 02:27:26 +02:00
|
|
|
// TODO: return error(??) if the irc message is malformed (i.e doesn't contain command)
|
2021-10-20 16:21:26 +03:00
|
|
|
ircMessage.Command = substrs[0]
|
2021-10-20 03:13:57 +03:00
|
|
|
|
2022-01-02 02:27:26 +02:00
|
|
|
// if parameters exist and it's not an empty string and it's not whitespace without ":"
|
|
|
|
|
if len(substrs) > 1 && len(substrs[1]) != 0 && !whitespaceRegex.MatchString(substrs[1]) {
|
|
|
|
|
params := trailingParamRegex.Split(substrs[1], 2)
|
2021-10-20 03:13:57 +03:00
|
|
|
|
2022-01-02 02:27:26 +02:00
|
|
|
parameters := multipleSpacesRegex.Split(params[0], -1)
|
2022-02-19 12:22:03 +02:00
|
|
|
finalParams := make([]string, 0)
|
2021-10-20 03:13:57 +03:00
|
|
|
|
2022-01-03 23:59:52 +02:00
|
|
|
// if we have 1 param and it starts with a colon
|
2022-01-09 04:30:22 +02:00
|
|
|
if len(params) == 1 && params[0][0] == ':' {
|
2022-02-19 12:22:03 +02:00
|
|
|
finalParams = append(finalParams, params[0][1:])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if there's regular param(s) with trailing param
|
|
|
|
|
// this parses empty params as well
|
|
|
|
|
if len(params) > 1 {
|
|
|
|
|
finalParams = append(parameters, params[1])
|
|
|
|
|
} else {
|
|
|
|
|
// if we only have regular param(s)
|
|
|
|
|
finalParams = append(finalParams, parameters...)
|
2022-01-03 23:59:52 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-19 12:22:03 +02:00
|
|
|
ircMessage.Parameters = finalParams
|
2021-10-20 03:13:57 +03:00
|
|
|
}
|
|
|
|
|
|
2022-02-19 16:24:39 +02:00
|
|
|
ircMessage.setTimestamp()
|
|
|
|
|
|
2022-01-09 04:30:22 +02:00
|
|
|
return ircMessage, true
|
2021-10-20 03:13:57 +03:00
|
|
|
}
|