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.
|
|
|
|
|
|
2021-11-14 06:19:46 +02:00
|
|
|
package parser
|
2021-10-20 03:13:57 +03:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2021-11-14 06:19:46 +02:00
|
|
|
type IRCMessage struct {
|
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
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-11-14 06:19:46 +02: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
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-14 06:19:46 +02:00
|
|
|
return ircMessage
|
2021-10-20 03:13:57 +03:00
|
|
|
}
|