From b05bfd0b8bd2830ec6481f77a2c74ca2a9099fee Mon Sep 17 00:00:00 2001 From: Stefano Date: Wed, 17 Nov 2021 21:40:28 +0100 Subject: [PATCH] Add url.go for handling irc file urls --- url.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 url.go diff --git a/url.go b/url.go new file mode 100644 index 0000000..6d4da9e --- /dev/null +++ b/url.go @@ -0,0 +1,52 @@ +package main + +import ( + "errors" + "fmt" + "strconv" + "strings" +) + +type IRCFileURL struct { + Network string + Channel string + UserName string + Slot int +} + +const ircFileURLFields = 4 + +func parseSlot(slotStr string) (int, error) { + if !strings.HasPrefix(slotStr, "#") { + return -1, errors.New("invalid slot") + } + return strconv.Atoi(strings.TrimPrefix(slotStr, "#")) +} + +// url has the following format: irc://network/channel/bot/#slot +func parseIRCFileURl(url string) (*IRCFileURL, error) { + if !strings.HasPrefix(url, "irc://") { + return nil, errors.New("not an IRC url") + } + + fields := strings.Split(strings.TrimPrefix(url, "irc://"), "/") + if len(fields) != ircFileURLFields { + return nil, errors.New("invalid IRC url") + } + + slot, err := parseSlot(fields[3]) + if err != nil { + return nil, err + } + + return &IRCFileURL{ + Network: fields[0], + Channel: fields[1], + UserName: fields[2], + Slot: slot, + }, nil +} + +func (url *IRCFileURL) String() string { + return fmt.Sprintf("irc://%s/%s/%s/#%d", url.Network, url.Channel, url.UserName, url.Slot) +}