Files
gorc/parser/parser.go
T

106 lines
2.7 KiB
Go
Raw Normal View History

2021-10-20 03:13:57 +03:00
// gorc project
2021-11-14 06:31:51 +02:00
// Copyright (C) 2021 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.
package parser
2021-10-20 03:13:57 +03:00
import (
"regexp"
2021-10-20 03:13:57 +03:00
"strings"
)
type IRCMessage struct {
Tags Tags // starts with @ | Optional
Source string // starts with : | Optional
Command string // can either be a string or a numeric value | Required
Parameters []string // Optional (Dependant on command)
}
type Tags map[string]string
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
}
return tags
2021-10-20 16:21:26 +03:00
}
func ParseIRCMessage(line string) IRCMessage {
ircMessage := IRCMessage{}
multipleSpacesRegex := regexp.MustCompile("[^\\S\\t]+")
trailingParamRegex := regexp.MustCompile("[^\\S\\t]+:")
whitespaceRegex := regexp.MustCompile("^[^\\S\\t]+$")
if len(line) <= 0 {
return IRCMessage{}
}
2021-10-20 03:13:57 +03:00
2021-10-20 16:21:26 +03:00
if line[0] == '@' {
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] == ':' {
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
substrs := multipleSpacesRegex.Split(line, 2)
2021-10-20 03:13:57 +03: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
// 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
parameters := multipleSpacesRegex.Split(params[0], -1)
2021-10-20 03:13:57 +03:00
2021-10-20 16:21:26 +03:00
// if there's a trailing parameter
// this parses empty params as well
2021-10-20 16:21:26 +03:00
if len(params) > 1 {
parameters = append(parameters, params[1])
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
if len(parameters) == 1 && parameters[0][0] == ':' {
parameters[0] = parameters[0][1:]
}
2021-10-20 03:13:57 +03:00
ircMessage.Parameters = parameters
}
return ircMessage
2021-10-20 03:13:57 +03:00
}