Files
gorc/parser/parser.go
T

96 lines
2.2 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 (
"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{}
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 := strings.SplitN(line, " ", 2)
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 := strings.SplitN(line, " ", 2)
ircMessage.Source = substrs[0]
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
substrs := strings.SplitN(line, " ", 2)
2021-10-20 03:13:57 +03:00
2021-10-20 16:21:26 +03:00
ircMessage.Command = substrs[0]
2021-10-20 03:13:57 +03:00
2021-10-20 16:21:26 +03:00
// if parameters exist
if len(substrs) > 1 {
params := strings.SplitN(substrs[1], " :", 2)
2021-10-20 03:13:57 +03:00
2021-10-20 16:21:26 +03:00
parameters := strings.Split(params[0], " ")
// TODO: parameters can be empty. this is denoted by a ":"
2021-10-20 03:13:57 +03:00
2021-10-20 16:21:26 +03:00
// if there's a trailing parameter
if len(params) > 1 {
parameters = append(parameters, params[1])
2021-10-20 03:13:57 +03:00
}
ircMessage.Parameters = parameters
}
return ircMessage
2021-10-20 03:13:57 +03:00
}