From d2f98102ef213b5fc8f2327a3fc3a5da5c436876 Mon Sep 17 00:00:00 2001 From: IllusionMan1212 Date: Wed, 20 Oct 2021 16:21:26 +0300 Subject: [PATCH] refactor parser --- client.go | 1 + constants.go | 13 +++++-- parser.go | 101 +++++++++++++++------------------------------------ 3 files changed, 40 insertions(+), 75 deletions(-) diff --git a/client.go b/client.go index 9ec2486..b05467e 100644 --- a/client.go +++ b/client.go @@ -57,6 +57,7 @@ func NewClient(host string, port string) *Client { } func (c Client) handleCommand(message IRCMessage) { + fmt.Printf("Tags: %s\n", message.Tags) fmt.Printf("Source: %s\n", message.Source) fmt.Printf("Command: %s\n", message.Command) fmt.Printf("Params: %s\n", message.Parameters) diff --git a/constants.go b/constants.go index 6b436eb..0880297 100644 --- a/constants.go +++ b/constants.go @@ -78,10 +78,15 @@ const ( /* --- IRC Message --- */ type IRCMessage struct { - Tags map[string]string // 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) + Tags []Tag // 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 Tag struct { + key string + value string } /* --- Channel Types --- */ diff --git a/parser.go b/parser.go index e37c274..d3879ba 100644 --- a/parser.go +++ b/parser.go @@ -17,91 +17,50 @@ package main import ( - "fmt" "strings" ) +func parseTags(rawTags string) []Tag { + // TODO: + // TOOD: some tags don't have an "=" and they're basically considered "true" + return nil +} + func parseIRCMessage(line string) IRCMessage { ircMessage := &IRCMessage{} - switch line[0] { - case '@': - substrs := strings.SplitN(line, " ", 4) - - tagsSlice := make(map[string]string) - - tags := strings.Split(substrs[0][1:], ";") - - for _, tag := range tags { - splitTag := strings.Split(tag, "=") - - // TOOD: some tags don't have an "=" and they're basically considered "true" - tagsSlice[splitTag[0]] = splitTag[1] - } - - ircMessage := IRCMessage{ - Tags: tagsSlice, - Source: substrs[1][1:], - Command: substrs[2], - } - - ircMessage.Tags = tagsSlice - ircMessage.Source = substrs[1][1:] - ircMessage.Command = substrs[2] - - fmt.Printf("%s\n", ircMessage.Tags) - fmt.Printf("%s\n", ircMessage.Source) - fmt.Printf("%s\n", ircMessage.Command) - fmt.Printf("%s\n", ircMessage.Parameters) - // TODO: - - break - case ':': - substrs := strings.SplitN(line, " ", 3) - - parameters := make([]string, 0) - - // if parameters exist - if len(substrs) > 2 { - params := strings.SplitN(substrs[2], " :", 2) - - parameters = strings.Split(params[0], " ") - // TODO: parameters can be empty. this is denoted by a ":" - - // if there's a final space-included param - if len(params) > 1 { - parameters = append(parameters, params[1]) - } - } - - ircMessage.Source = substrs[0][1:] - ircMessage.Command = substrs[1] - ircMessage.Parameters = parameters - - break - default: + if line[0] == '@' { substrs := strings.SplitN(line, " ", 2) + tags := parseTags(substrs[0][1:]) + ircMessage.Tags = tags - parameters := make([]string, 0) + line = substrs[1] + } - // if parameters exist - if len(substrs) > 1 { - params := strings.SplitN(substrs[1], " :", 2) + if line[0] == ':' { + substrs := strings.SplitN(line, " ", 2) + ircMessage.Source = substrs[0] - parameters = strings.Split(params[0], " ") - // TODO: parameters can be empty. this is denoted by a ":" + line = substrs[1] + } - // if there's a trailing parameter - if len(params) > 1 { - parameters = append(parameters, params[1]) - } + substrs := strings.SplitN(line, " ", 2) + + ircMessage.Command = substrs[0] + + // if parameters exist + if len(substrs) > 1 { + params := strings.SplitN(substrs[1], " :", 2) + + parameters := strings.Split(params[0], " ") + // TODO: parameters can be empty. this is denoted by a ":" + + // if there's a trailing parameter + if len(params) > 1 { + parameters = append(parameters, params[1]) } - ircMessage.Source = "" - ircMessage.Command = substrs[0] ircMessage.Parameters = parameters - - break } return *ircMessage