From: Fl_GUI Date: Mon, 13 May 2024 20:11:09 +0000 (+0200) Subject: add code for all messages X-Git-Url: https://git.openfl.eu/?a=commitdiff_plain;h=f1e23c2f2b5b0806f025433936d96504d6643574;p=twitch-chat.git add code for all messages --- diff --git a/twitch/core/clearchat/clearchat.go b/twitch/core/clearchat/clearchat.go new file mode 100644 index 0000000..f1dc74f --- /dev/null +++ b/twitch/core/clearchat/clearchat.go @@ -0,0 +1,25 @@ +package clearchat + +import ( + "twitchchat/irc" + "twitchchat/twitch/core/commands" + "twitchchat/twitch/core/messages" +) + +//go:generate tagprototype -out clearchattags.go -package clearchat -prototype @ban-duration=;room-id=;target-user-id=;tmi-sent-ts= -type ClearChatTag + +type ClearChat struct { + messages.Message +} + +func IsClearChat(m messages.Message) bool { + return string(m.Command) == commands.ClearChat +} + +func (c ClearChat) GetTag(key ClearChatTag) *irc.Tag { + return c.Message.GetTag(string(key)) +} + +func (c ClearChat) User() string { + return c.Params.Trailing +} diff --git a/twitch/core/clearchat/clearchattags.go b/twitch/core/clearchat/clearchattags.go new file mode 100755 index 0000000..6d1e9ae --- /dev/null +++ b/twitch/core/clearchat/clearchattags.go @@ -0,0 +1,12 @@ +//Generated by twitch-chat/x/generate/tagprototype + +package clearchat + +type ClearChatTag string + +const ( + BanDuration ClearChatTag = "ban-duration" + RoomId ClearChatTag = "room-id" + TargetUserId ClearChatTag = "target-user-id" + TmiSentTs ClearChatTag = "tmi-sent-ts" +) diff --git a/twitch/core/clearmsg/clearmsg.go b/twitch/core/clearmsg/clearmsg.go new file mode 100644 index 0000000..ae64d6f --- /dev/null +++ b/twitch/core/clearmsg/clearmsg.go @@ -0,0 +1,18 @@ +package clearmsg + +import ( + "twitchchat/twitch/core/commands" + "twitchchat/twitch/core/messages" +) + +type ClearMsg struct { + messages.Message +} + +func IsClearMsg(m messages.Message) bool { + return string(m.Command) == commands.ClearMsg +} + +func (c ClearMsg) ChatMessage() string { + return c.Params.Trailing +} diff --git a/twitch/core/commands/commands.go b/twitch/core/commands/commands.go index 5769dff..f39e851 100644 --- a/twitch/core/commands/commands.go +++ b/twitch/core/commands/commands.go @@ -18,7 +18,7 @@ const ( // twitch specific IRC messages. These are all receive only ClearChat = "CLEARCHAT" ClearMsg = "CLEARMSG" - GlobalUserStare = "GLOBALUSERSTATE" + GlobalUserState = "GLOBALUSERSTATE" HostTarget = "HOSTTARGET" Reconnect = "RECONNECT" RoomState = "ROOMSTATE" diff --git a/twitch/core/globaluserstate/globaluserstate.go b/twitch/core/globaluserstate/globaluserstate.go new file mode 100644 index 0000000..8c64a35 --- /dev/null +++ b/twitch/core/globaluserstate/globaluserstate.go @@ -0,0 +1,12 @@ +package globaluserstate + +import ( + "twitchchat/twitch/core/commands" + "twitchchat/twitch/core/messages" +) + +//go:generate tagprototype -out globaluserstatetags.go -package globaluserstate -type GlobalUserStateTag -prototype @badge-info=;badges=;color=;display-name=;emote-sets=;turbo=;user-id=;user-type= + +func IsGlobalUserState(m messages.Message) bool { + return m.Command == commands.GlobalUserState +} diff --git a/twitch/core/globaluserstate/globaluserstatetags.go b/twitch/core/globaluserstate/globaluserstatetags.go new file mode 100755 index 0000000..e8861de --- /dev/null +++ b/twitch/core/globaluserstate/globaluserstatetags.go @@ -0,0 +1,16 @@ +//Generated by twitch-chat/x/generate/tagprototype + +package globaluserstate + +type GlobalUserStateTag string + +const ( + BadgeInfo GlobalUserStateTag = "badge-info" + Badges GlobalUserStateTag = "badges" + Color GlobalUserStateTag = "color" + DisplayName GlobalUserStateTag = "display-name" + EmoteSets GlobalUserStateTag = "emote-sets" + Turbo GlobalUserStateTag = "turbo" + UserId GlobalUserStateTag = "user-id" + UserType GlobalUserStateTag = "user-type" +) diff --git a/twitch/core/hosttarget/hosttarget.go b/twitch/core/hosttarget/hosttarget.go new file mode 100644 index 0000000..b174960 --- /dev/null +++ b/twitch/core/hosttarget/hosttarget.go @@ -0,0 +1,36 @@ +package hosttarget + +import ( + "strconv" + "strings" + "twitchchat/twitch/core/commands" + "twitchchat/twitch/core/messages" +) + +type HostTarget struct { + messages.Message +} + +func IsHostTarget(m messages.Message) bool { + return m.Command == commands.HostTarget +} + +func (h HostTarget) ChannelTarget() string { + parts := strings.Split(h.Params.Trailing, " ") + if len(parts) != 2 { + return "" + } + return parts[0] +} + +func (h HostTarget) NumberOfViewers() int { + parts := strings.Split(h.Params.Trailing, " ") + if len(parts) != 2 { + return 0 + } + if i, err := strconv.Atoi(parts[1]); err != nil { + return 0 + } else { + return i + } +} diff --git a/twitch/core/messages/messages.go b/twitch/core/messages/messages.go index b56fb6c..cdfd06e 100644 --- a/twitch/core/messages/messages.go +++ b/twitch/core/messages/messages.go @@ -17,3 +17,13 @@ func (m Message) GetTag(key string) *irc.Tag { } return nil } + +func (m Message) Channel() string { + if len(m.Params.Params) == 0 { + return "" + } + if m.Params.Params[0] != "#" { + return "" + } + return m.Params.Params[0][1:] +} diff --git a/twitch/core/notice/notice.go b/twitch/core/notice/notice.go new file mode 100644 index 0000000..49afc93 --- /dev/null +++ b/twitch/core/notice/notice.go @@ -0,0 +1,25 @@ +package notice + +import ( + "twitchchat/irc" + "twitchchat/twitch/core/commands" + "twitchchat/twitch/core/messages" +) + +//go:generate tagprototype -out noticetags.go -package notice -type NoticeTag -prototype @msg-id=;target-user-id= + +type Notice struct { + messages.Message +} + +func IsNotice(m messages.Message) bool { + return m.Command == commands.Notice +} + +func (n Notice) GetTag(tag NoticeTag) *irc.Tag { + return n.Message.GetTag(string(tag)) +} + +func (n Notice) NoticeMessage() string { + return n.Params.Trailing +} diff --git a/twitch/core/notice/noticetags.go b/twitch/core/notice/noticetags.go new file mode 100755 index 0000000..c1de781 --- /dev/null +++ b/twitch/core/notice/noticetags.go @@ -0,0 +1,10 @@ +//Generated by twitch-chat/x/generate/tagprototype + +package notice + +type NoticeTag string + +const ( + MsgId NoticeTag = "msg-id" + TargetUserId NoticeTag = "target-user-id" +) diff --git a/twitch/core/privmsg/privmsg.go b/twitch/core/privmsg/privmsg.go index aec04ea..496c30e 100644 --- a/twitch/core/privmsg/privmsg.go +++ b/twitch/core/privmsg/privmsg.go @@ -6,10 +6,12 @@ import ( "twitchchat/twitch/core/messages" ) -type PrivMsg messages.Message - //go:generate tagprototype -out "privmsgtags.go" -type PrivMsgTag -prototype "@badge-info=;badges=;bits=client-nonce=;color=;display-name=;emotes=;first-msg=;flags=;id=;mod=;room-id=;subscriber=;tmi-sent-ts=;turbo=;user-id=;user-type=;reply-parent-msg-id=;reply-parent-user-id=;reply-parent-user-login=;reply-parent-display-name=;reply-parent-msg-body=;reply-thread-parent-msg-id=;reply-thread-parent-user-login=;vip=" -package privmsg +type PrivMsg struct { + messages.Message +} + func IsPrivateMessage(m messages.Message) bool { return string(m.Command) == commands.PrivMsg } @@ -23,5 +25,5 @@ func (p PrivMsg) Text() string { } func (p PrivMsg) GetTag(key PrivMsgTag) *irc.Tag { - return messages.Message(p).GetTag(string(key)) + return p.Message.GetTag(string(key)) } diff --git a/twitch/core/roomstate/roomstate.go b/twitch/core/roomstate/roomstate.go new file mode 100644 index 0000000..6817a70 --- /dev/null +++ b/twitch/core/roomstate/roomstate.go @@ -0,0 +1,21 @@ +package roomstate + +import ( + "twitchchat/irc" + "twitchchat/twitch/core/commands" + "twitchchat/twitch/core/messages" +) + +//go:generate tagprototype -out roomstatetags.go -package roomstate -type RoomStateTag -prototype @emote-only=;followers-only=;r9k=;rituals=;room-id=;slow=;subs-only= + +type RoomState struct { + messages.Message +} + +func IsRoomState(m messages.Message) bool { + return m.Command == commands.RoomState +} + +func (r RoomState) GetTag(tag RoomStateTag) *irc.Tag { + return r.Message.GetTag(string(tag)) +} diff --git a/twitch/core/roomstate/roomstatetags.go b/twitch/core/roomstate/roomstatetags.go new file mode 100755 index 0000000..8e5e16d --- /dev/null +++ b/twitch/core/roomstate/roomstatetags.go @@ -0,0 +1,15 @@ +//Generated by twitch-chat/x/generate/tagprototype + +package roomstate + +type RoomStateTag string + +const ( + EmoteOnly RoomStateTag = "emote-only" + FollowersOnly RoomStateTag = "followers-only" + R9k RoomStateTag = "r9k" + Rituals RoomStateTag = "rituals" + RoomId RoomStateTag = "room-id" + Slow RoomStateTag = "slow" + SubsOnly RoomStateTag = "subs-only" +) diff --git a/twitch/core/usernotice/usernotice.go b/twitch/core/usernotice/usernotice.go new file mode 100644 index 0000000..e6cc1b8 --- /dev/null +++ b/twitch/core/usernotice/usernotice.go @@ -0,0 +1,20 @@ +package usernotice + +import ( + "twitchchat/twitch/core/commands" + "twitchchat/twitch/core/messages" +) + +//go:generate tagprototype -out usernoticetags.go -package usernotice -type UserNoticeTag -prototype @badge-info=;badges=;color=;display-name=;emotes=;id=;login=;mod=;msg-id=;room-id=;subscriber=;system-msg=;tmi-sent-ts=;turbo=;user-id=;user-type= + +type UserNotice struct { + messages.Message +} + +func IsUserNotice(m messages.Message) bool { + return m.Command == commands.UserNotice +} + +func (u UserNotice) UserMessage() string { + return u.Params.Trailing +} diff --git a/twitch/core/usernotice/usernoticetags.go b/twitch/core/usernotice/usernoticetags.go new file mode 100755 index 0000000..94b5380 --- /dev/null +++ b/twitch/core/usernotice/usernoticetags.go @@ -0,0 +1,24 @@ +//Generated by twitch-chat/x/generate/tagprototype + +package usernotice + +type UserNoticeTag string + +const ( + BadgeInfo UserNoticeTag = "badge-info" + Badges UserNoticeTag = "badges" + Color UserNoticeTag = "color" + DisplayName UserNoticeTag = "display-name" + Emotes UserNoticeTag = "emotes" + Id UserNoticeTag = "id" + Login UserNoticeTag = "login" + Mod UserNoticeTag = "mod" + MsgId UserNoticeTag = "msg-id" + RoomId UserNoticeTag = "room-id" + Subscriber UserNoticeTag = "subscriber" + SystemMsg UserNoticeTag = "system-msg" + TmiSentTs UserNoticeTag = "tmi-sent-ts" + Turbo UserNoticeTag = "turbo" + UserId UserNoticeTag = "user-id" + UserType UserNoticeTag = "user-type" +) diff --git a/twitch/core/userstate/userstate.go b/twitch/core/userstate/userstate.go new file mode 100644 index 0000000..32160c8 --- /dev/null +++ b/twitch/core/userstate/userstate.go @@ -0,0 +1,21 @@ +package userstate + +import ( + "twitchchat/irc" + "twitchchat/twitch/core/commands" + "twitchchat/twitch/core/messages" +) + +//go:generate tagprototype -out userstatetags.go -package userstate -type UserStateTag -prototype @badge-info=;badges=staff/1;color=#0D4200;display-name=ronni;emote-sets=0,33,50,237,793,2126,3517,4578,5569,9400,10337,12239;mod=1;subscriber=1;turbo=1;user-type=staff :tmi.twitch.tv USERSTATE #dallas + +type UserState struct { + messages.Message +} + +func IsUserState(m messages.Message) bool { + return m.Command == commands.UserState +} + +func (u UserState) GetTag(tag UserStateTag) *irc.Tag { + return u.Message.GetTag(string(tag)) +} diff --git a/twitch/core/userstate/userstatetags.go b/twitch/core/userstate/userstatetags.go new file mode 100755 index 0000000..2dec248 --- /dev/null +++ b/twitch/core/userstate/userstatetags.go @@ -0,0 +1,17 @@ +//Generated by twitch-chat/x/generate/tagprototype + +package userstate + +type UserStateTag string + +const ( + BadgeInfo UserStateTag = "badge-info" + Badges UserStateTag = "badges" + Color UserStateTag = "color" + DisplayName UserStateTag = "display-name" + EmoteSets UserStateTag = "emote-sets" + Mod UserStateTag = "mod" + Subscriber UserStateTag = "subscriber" + Turbo UserStateTag = "turbo" + UserType UserStateTag = "user-type" +) diff --git a/twitch/core/whisper/whisper.go b/twitch/core/whisper/whisper.go new file mode 100644 index 0000000..3fd0197 --- /dev/null +++ b/twitch/core/whisper/whisper.go @@ -0,0 +1,31 @@ +package whisper + +import ( + "twitchchat/twitch/core/commands" + "twitchchat/twitch/core/messages" +) + +//go:generate tagprototype -out whispertags.go -package whisper -type WhisperTag -prototype @badges=;color=;display-name=;emotes=;message-id=;thread-id=;turbo=;user-id=;user-type= + +type Whisper struct { + messages.Message +} + +func IsWhisper(m messages.Message) bool { + return m.Command == commands.Whisper +} + +func (w Whisper) ToUser() string { + return w.Prefix.User +} + +func (w Whisper) FromUser() string { + if len(w.Params.Params) != 1 { + return "" + } + return w.Params.Params[0] +} + +func (w Whisper) WhisperMessage() string { + return w.Params.Trailing +} diff --git a/twitch/core/whisper/whispertags.go b/twitch/core/whisper/whispertags.go new file mode 100755 index 0000000..159018e --- /dev/null +++ b/twitch/core/whisper/whispertags.go @@ -0,0 +1,17 @@ +//Generated by twitch-chat/x/generate/tagprototype + +package whisper + +type WhisperTag string + +const ( + Badges WhisperTag = "badges" + Color WhisperTag = "color" + DisplayName WhisperTag = "display-name" + Emotes WhisperTag = "emotes" + MessageId WhisperTag = "message-id" + ThreadId WhisperTag = "thread-id" + Turbo WhisperTag = "turbo" + UserId WhisperTag = "user-id" + UserType WhisperTag = "user-type" +) diff --git a/x/corechatclient/main.go b/x/corechatclient/main.go index 71a06cb..8112318 100644 --- a/x/corechatclient/main.go +++ b/x/corechatclient/main.go @@ -52,7 +52,7 @@ func main() { rmax := NewRunningMax(50) for m := range msgs { if privmsg.IsPrivateMessage(m) { - priv := privmsg.PrivMsg(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 index c449df9..1bde237 100644 --- a/x/generate/tagprototype/main.go +++ b/x/generate/tagprototype/main.go @@ -52,7 +52,7 @@ func main() { return } - f, err := os.OpenFile(*outFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0755) + f, err := os.OpenFile(*outFile, os.O_WRONLY|os.O_CREATE, 0755) if err != nil { fmt.Fprintln(os.Stderr, err) }