Files

151 lines
3.5 KiB
Go

// gorc project
// Copyright (C) 2022 IllusionMan1212
//
// 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
import (
"regexp"
"strings"
"github.com/illusionman1212/gorc/irc"
)
func escapeCharacters(char rune) string {
switch char {
case 's':
return " "
case 'n':
return "\n"
case 'r':
return "\r"
case 'b':
return "\b"
case 't':
return "\t"
case 'f':
return "\f"
case ':':
return ";"
case '\\':
fallthrough
case '\'':
fallthrough
case '"':
fallthrough
default:
return string(char)
}
}
func parseTags(rawTags string) irc.MessageTags {
tags := make(irc.MessageTags, 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 {
rawValue := str[1]
for idx := 0; idx < len(rawValue); {
// if we have a backslash at the end of the string
// we need to ignore it
if idx == len(rawValue)-1 && rawValue[idx] == '\\' {
idx += 2
continue
}
if rawValue[idx] == '\\' {
value += escapeCharacters(rune(rawValue[idx+1]))
idx += 2
continue
}
value += string(rawValue[idx])
idx++
}
}
tags[key] = value
}
return tags
}
func ParseIRCMessage(line string) (irc.Message, bool) {
ircMessage := irc.Message{}
multipleSpacesRegex := regexp.MustCompile(`[^\S\t]+`)
trailingParamRegex := regexp.MustCompile(`[^\S\t]+:`)
whitespaceRegex := regexp.MustCompile(`^[^\S\t]+$`)
if len(line) <= 0 {
// TODO: do something here ???
// log.Println("empty message")
return irc.Message{}, false
}
if line[0] == '@' {
substrs := multipleSpacesRegex.Split(line, 2)
tags := parseTags(substrs[0][1:])
ircMessage.Tags = tags
line = substrs[1]
}
if line[0] == ':' {
substrs := multipleSpacesRegex.Split(line, 2)
ircMessage.Source = substrs[0][1:]
line = substrs[1]
}
substrs := multipleSpacesRegex.Split(line, 2)
// TODO: return error(??) if the irc message is malformed (i.e doesn't contain command)
ircMessage.Command = substrs[0]
// 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)
parameters := multipleSpacesRegex.Split(params[0], -1)
finalParams := make([]string, 0)
// if we have 1 param and it starts with a colon
if len(params) == 1 && params[0][0] == ':' {
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...)
}
ircMessage.Parameters = finalParams
}
ircMessage.SetTimestamp()
return ircMessage, true
}