refactor parser

This commit is contained in:
2021-10-20 16:21:26 +03:00
parent 8f56877d69
commit d2f98102ef
3 changed files with 40 additions and 75 deletions
+1
View File
@@ -57,6 +57,7 @@ func NewClient(host string, port string) *Client {
} }
func (c Client) handleCommand(message IRCMessage) { func (c Client) handleCommand(message IRCMessage) {
fmt.Printf("Tags: %s\n", message.Tags)
fmt.Printf("Source: %s\n", message.Source) fmt.Printf("Source: %s\n", message.Source)
fmt.Printf("Command: %s\n", message.Command) fmt.Printf("Command: %s\n", message.Command)
fmt.Printf("Params: %s\n", message.Parameters) fmt.Printf("Params: %s\n", message.Parameters)
+6 -1
View File
@@ -78,12 +78,17 @@ const (
/* --- IRC Message --- */ /* --- IRC Message --- */
type IRCMessage struct { type IRCMessage struct {
Tags map[string]string // starts with @ | Optional Tags []Tag // starts with @ | Optional
Source string // starts with : | Optional Source string // starts with : | Optional
Command string // can either be a string or a numeric value | Required Command string // can either be a string or a numeric value | Required
Parameters []string // Optional (Dependant on command) Parameters []string // Optional (Dependant on command)
} }
type Tag struct {
key string
value string
}
/* --- Channel Types --- */ /* --- Channel Types --- */
// These are the channel prefixes for different types of channels // These are the channel prefixes for different types of channels
const ( const (
+18 -59
View File
@@ -17,91 +17,50 @@
package main package main
import ( import (
"fmt"
"strings" "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 { func parseIRCMessage(line string) IRCMessage {
ircMessage := &IRCMessage{} ircMessage := &IRCMessage{}
switch line[0] { if line[0] == '@' {
case '@': substrs := strings.SplitN(line, " ", 2)
substrs := strings.SplitN(line, " ", 4) tags := parseTags(substrs[0][1:])
ircMessage.Tags = tags
tagsSlice := make(map[string]string) line = substrs[1]
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{ if line[0] == ':' {
Tags: tagsSlice, substrs := strings.SplitN(line, " ", 2)
Source: substrs[1][1:], ircMessage.Source = substrs[0]
Command: substrs[2],
line = substrs[1]
} }
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:
substrs := strings.SplitN(line, " ", 2) substrs := strings.SplitN(line, " ", 2)
parameters := make([]string, 0) ircMessage.Command = substrs[0]
// if parameters exist // if parameters exist
if len(substrs) > 1 { if len(substrs) > 1 {
params := strings.SplitN(substrs[1], " :", 2) params := strings.SplitN(substrs[1], " :", 2)
parameters = strings.Split(params[0], " ") parameters := strings.Split(params[0], " ")
// TODO: parameters can be empty. this is denoted by a ":" // TODO: parameters can be empty. this is denoted by a ":"
// if there's a trailing parameter // if there's a trailing parameter
if len(params) > 1 { if len(params) > 1 {
parameters = append(parameters, params[1]) parameters = append(parameters, params[1])
} }
}
ircMessage.Source = ""
ircMessage.Command = substrs[0]
ircMessage.Parameters = parameters ircMessage.Parameters = parameters
break
} }
return *ircMessage return *ircMessage