]> git.openfl.eu Git - twitch-chat.git/commitdiff
generate tags from prototype
authorFl_GUI <flor.guilini@hotmail.com>
Sun, 12 May 2024 16:37:03 +0000 (18:37 +0200)
committerFl_GUI <flor.guilini@hotmail.com>
Sun, 12 May 2024 16:37:03 +0000 (18:37 +0200)
twitch/core/messages/messages.go
twitch/core/messages/privmsg.go [deleted file]
twitch/core/privmsg/privmsg.go [new file with mode: 0644]
twitch/core/privmsg/privmsgtags.go [new file with mode: 0755]
x/corechatclient/main.go
x/generate/tagprototype/main.go [new file with mode: 0644]

index 6605b727727ef5a8fae9a26b58924293d1b18110..b56fb6ce39f7cf9f49a2738fb08ad47c00295048 100644 (file)
@@ -7,3 +7,13 @@ type Message irc.Message
 func (m Message) String() string {
        return irc.Message(m).String()
 }
+
+// Returns the tag of the corresponding tag key, or nil if not found
+func (m Message) GetTag(key string) *irc.Tag {
+       for _, tag := range m.Tags {
+               if tag.Key == key {
+                       return &tag
+               }
+       }
+       return nil
+}
diff --git a/twitch/core/messages/privmsg.go b/twitch/core/messages/privmsg.go
deleted file mode 100644 (file)
index 3807571..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-package messages
-
-import (
-       "twitchchat/twitch/core/commands"
-)
-
-type PrivMsg Message
-
-func IsPrivateMessage(m Message) bool {
-       return string(m.Command) == commands.PrivMsg
-}
-
-func (p PrivMsg) User() string {
-       return p.Prefix.Name
-}
-
-func (p PrivMsg) Text() string {
-       return p.Params.Trailing
-}
diff --git a/twitch/core/privmsg/privmsg.go b/twitch/core/privmsg/privmsg.go
new file mode 100644 (file)
index 0000000..aec04ea
--- /dev/null
@@ -0,0 +1,27 @@
+package privmsg
+
+import (
+       "twitchchat/irc"
+       "twitchchat/twitch/core/commands"
+       "twitchchat/twitch/core/messages"
+)
+
+type PrivMsg messages.Message
+
+//go:generate tagprototype -out "privmsgtags.go" -type PrivMsgTag -prototype "@badge-info=<badge-info>;badges=<badges>;bits=<bits>client-nonce=<nonce>;color=<color>;display-name=<display-name>;emotes=<emotes>;first-msg=<first-msg>;flags=<flags>;id=<msg-id>;mod=<mod>;room-id=<room-id>;subscriber=<subscriber>;tmi-sent-ts=<timestamp>;turbo=<turbo>;user-id=<user-id>;user-type=<user-type>;reply-parent-msg-id=<reply-parent-msg-id>;reply-parent-user-id=<reply-parent-user-id>;reply-parent-user-login=<reply-parent-user-login>;reply-parent-display-name=<reply-parent-display-name>;reply-parent-msg-body=<reply-parent-msg-body>;reply-thread-parent-msg-id=<reply-thread-parent-msg-id>;reply-thread-parent-user-login=<reply-thread-parent-user-login>;vip=<vip>" -package privmsg
+
+func IsPrivateMessage(m messages.Message) bool {
+       return string(m.Command) == commands.PrivMsg
+}
+
+func (p PrivMsg) User() string {
+       return p.Prefix.Name
+}
+
+func (p PrivMsg) Text() string {
+       return p.Params.Trailing
+}
+
+func (p PrivMsg) GetTag(key PrivMsgTag) *irc.Tag {
+       return messages.Message(p).GetTag(string(key))
+}
diff --git a/twitch/core/privmsg/privmsgtags.go b/twitch/core/privmsg/privmsgtags.go
new file mode 100755 (executable)
index 0000000..f9e3571
--- /dev/null
@@ -0,0 +1,32 @@
+//Generated by twitch-chat/x/generate/tagprototype
+
+package privmsg
+
+type PrivMsgTag string
+
+const (
+       BadgeInfo                  PrivMsgTag = "badge-info"
+       Badges                     PrivMsgTag = "badges"
+       Bits                       PrivMsgTag = "bits"
+       Color                      PrivMsgTag = "color"
+       DisplayName                PrivMsgTag = "display-name"
+       Emotes                     PrivMsgTag = "emotes"
+       FirstMsg                   PrivMsgTag = "first-msg"
+       Flags                      PrivMsgTag = "flags"
+       Id                         PrivMsgTag = "id"
+       Mod                        PrivMsgTag = "mod"
+       RoomId                     PrivMsgTag = "room-id"
+       Subscriber                 PrivMsgTag = "subscriber"
+       TmiSentTs                  PrivMsgTag = "tmi-sent-ts"
+       Turbo                      PrivMsgTag = "turbo"
+       UserId                     PrivMsgTag = "user-id"
+       UserType                   PrivMsgTag = "user-type"
+       ReplyParentMsgId           PrivMsgTag = "reply-parent-msg-id"
+       ReplyParentUserId          PrivMsgTag = "reply-parent-user-id"
+       ReplyParentUserLogin       PrivMsgTag = "reply-parent-user-login"
+       ReplyParentDisplayName     PrivMsgTag = "reply-parent-display-name"
+       ReplyParentMsgBody         PrivMsgTag = "reply-parent-msg-body"
+       ReplyThreadParentMsgId     PrivMsgTag = "reply-thread-parent-msg-id"
+       ReplyThreadParentUserLogin PrivMsgTag = "reply-thread-parent-user-login"
+       Vip                        PrivMsgTag = "vip"
+)
index 1bb5f34c6d8006faf9dc997e762d498d7163a902..71a06cb6b90650d4ca916eec42e78c251324ee2e 100644 (file)
@@ -7,6 +7,7 @@ import (
        "strings"
        "twitchchat/twitch/core"
        "twitchchat/twitch/core/messages"
+       "twitchchat/twitch/core/privmsg"
 )
 
 var channel = flag.String("channel", "fl_gui", "channel to read")
@@ -50,8 +51,8 @@ func main() {
        var format string
        rmax := NewRunningMax(50)
        for m := range msgs {
-               if messages.IsPrivateMessage(m) {
-                       priv := messages.PrivMsg(m)
+               if privmsg.IsPrivateMessage(m) {
+                       priv := privmsg.PrivMsg(m)
                        u := priv.User()
                        mx := rmax.Push(len(u))
                        format = fmt.Sprintf("%%%ds: %%s\n", mx)
diff --git a/x/generate/tagprototype/main.go b/x/generate/tagprototype/main.go
new file mode 100644 (file)
index 0000000..c449df9
--- /dev/null
@@ -0,0 +1,77 @@
+package main
+
+import (
+       "flag"
+       "fmt"
+       "os"
+       "strings"
+       "unicode"
+)
+
+var prototypeString = flag.String("prototype", "", "The prototype of tags in the form of\n@tag-name-1=<tag-value-1>;tag-name-2=<tag-value-2>;...")
+var outFile = flag.String("out", "", "File to write to")
+var ofType = flag.String("type", "", "Type to wrap tag values in")
+var packageName = flag.String("package", "", "package for the file")
+
+func extractTagsFromPrototype() []string {
+       // trim leading "@", and split into key=<value>
+       parts := strings.Split((*prototypeString)[1:], ";")
+       res := make([]string, len(parts))
+       for i, p := range parts {
+               res[i] = p[0:strings.Index(p, "=")]
+       }
+
+       return res
+}
+
+var bob = strings.Builder{}
+
+func tagToName(t string) string {
+       bob.Reset()
+       var turnNextUpper = true
+       for _, r := range t {
+               if turnNextUpper {
+                       bob.WriteRune(unicode.ToUpper(r))
+                       turnNextUpper = false
+               } else if r == '-' {
+                       turnNextUpper = true
+               } else {
+                       bob.WriteRune(r)
+               }
+       }
+
+       return bob.String()
+}
+
+func main() {
+       flag.Parse()
+
+       // required arguments
+       if prototypeString == nil || outFile == nil || packageName == nil {
+               flag.Usage()
+               return
+       }
+
+       f, err := os.OpenFile(*outFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0755)
+       if err != nil {
+               fmt.Fprintln(os.Stderr, err)
+       }
+
+       fmt.Fprintf(f, "//Generated by twitch-chat/x/generate/tagprototype\n")
+       fmt.Fprintf(f, "\n")
+       fmt.Fprintf(f, "package %s\n", *packageName)
+       fmt.Fprintf(f, "\n")
+
+       var typ = "string"
+       if *ofType != "" {
+               fmt.Fprintf(f, "type %s string\n", *ofType)
+               fmt.Fprintf(f, "\n")
+               typ = *ofType
+       }
+
+       fmt.Fprintf(f, "const (\n")
+       for _, t := range extractTagsFromPrototype() {
+               fmt.Fprintf(f, "\t%s %s = %#v\n", tagToName(t), typ, t)
+       }
+       fmt.Fprintf(f, ")\n")
+}