irc

package
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2023 License: BSD-3-Clause Imports: 12 Imported by: 4

Documentation

Index

Constants

View Source
const IP = "irc.chat.twitch.tv"

IP for the IRC server

Variables

View Source
var (
	// ErrShardIDOutOfBounds returned when an invalid shard id is provided
	ErrShardIDOutOfBounds = errors.New("shard id out of bounds")
	// ErrShardedMessageSend returned when a sharded connection tries to send a message
	ErrShardedMessageSend = errors.New("messages can not be sent on a sharded connection")
	// ErrNotConnected returned when the connection is closed
	ErrNotConnected = errors.New("connection is closed")
	// ErrNotAuthenticated returned when authentication is required but not set
	ErrNotAuthenticated = errors.New("not authenticated")
	// ErrAlreadyConnected returned when a connection tries to connect but is already running
	ErrAlreadyConnected = errors.New("client is already connected")
	// ErrPingTimeout returned when the server takes too long to respond to a ping message
	ErrPingTimeout = errors.New("server took too long to respond to ping")
	// ErrPartialMessage returned when a raw message is not complete
	ErrPartialMessage = errors.New("parseError: partial message")
	// ErrNoCommand returned when a raw message has no command
	ErrNoCommand = errors.New("parseError: no command")
)

Functions

This section is empty.

Types

type ChannelUserState

type ChannelUserState struct {
	UserState
	IsBroadcaster bool
	IsModerator   bool
	IsVIP         bool
	IsSubscriber  bool
}

ChannelUserState is the state of the authenticated user in a channels chatroom

func NewChannelUserState

func NewChannelUserState(msg Message) (state ChannelUserState)

NewChannelUserState parses the state of an authenticated user in a channels chatroom

type ChatBan

type ChatBan struct {
	IRCMessage  Message
	ChannelName string
	ChannelID   int64
	TargetName  string
	TargetID    int64

	CreatedAt time.Time
	// contains filtered or unexported fields
}

ChatBan is a timeout or permanent ban that was issued on a user in a chatroom

func NewChatBan

func NewChatBan(msg Message) ChatBan

NewChatBan parses a ban or timeout in a channels chatroom

func (ChatBan) Duration

func (ban ChatBan) Duration() time.Duration

Duration returns the duration of a temporary ban

func (ChatBan) Expiration

func (ban ChatBan) Expiration() time.Time

Expiration returns the time that a temporary ban will expire

func (ChatBan) IsTemporary

func (ban ChatBan) IsTemporary() bool

IsTemporary returns true if a ban is set to expire after a set amount of time

type ChatMessage

type ChatMessage struct {
	IRCMessage Message
	Sender     ChatSender
	ID         string
	Channel    string
	ChannelID  int64
	Text       string
	IsCheer    bool
	IsAction   bool
	CreatedAt  time.Time
}

ChatMessage is a message sent in a channels chatroom

func NewChatMessage

func NewChatMessage(msg Message) ChatMessage

NewChatMessage parses a message sent in a channels chatroom

type ChatMessageDelete

type ChatMessageDelete struct {
	IRCMessage       Message
	ChannelName      string
	TargetID         string
	TargetSenderName string
	Text             string
	CreatedAt        time.Time
}

ChatMessageDelete is a message that was deleted from the chatroom

func NewChatMessageDelete

func NewChatMessageDelete(msg Message) ChatMessageDelete

NewChatMessageDelete parses a notice that a message was deleted

type ChatSender

type ChatSender struct {
	ID            int64
	Username      string
	DisplayName   string
	Color         string
	Badges        map[string]string
	BadgeInfo     map[string]string
	IsBroadcaster bool
	IsModerator   bool
	IsVIP         bool
	IsSubscriber  bool
	Type          string
}

ChatSender is a user that sent a message in a channels chatroom

func NewChatSender

func NewChatSender(msg Message) ChatSender

NewChatSender parses the sender for a message in a channels chatroom

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a sharded connection to the Twitch IRC service

func New

func New() *Client

New IRC Client

The client uses a sharding system to allow applications to listen to large numbers of Twitch chatrooms with minimized backlogs of message handling. The client will separate channels into groups of 100 by default.

See: https://dev.twitch.tv/docs/irc

func (*Client) Close

func (client *Client) Close()

Close disconnect all active shards

func (*Client) GetChannel

func (client *Client) GetChannel(channel string) (RoomState, bool)

GetChannel returns true if any shard is listening to the provided channel along with its RoomState

func (*Client) GetNextShard

func (client *Client) GetNextShard() (*Conn, error)

GetNextShard returns the first shard that can join channels

func (*Client) GetShard

func (client *Client) GetShard(id int) (*Conn, error)

GetShard retrieves or creates a shard based on the provided id

func (*Client) Join

func (client *Client) Join(channels ...string) error

Join attempts to join a channel on an available shard

func (*Client) Leave

func (client *Client) Leave(channels ...string) error

Leave attempts to leave a channel

func (*Client) OnShardChannelBan

func (client *Client) OnShardChannelBan(f func(int, ChatBan))

OnShardChannelBan event called after a user was banned or timed out in a channels chatoom

func (*Client) OnShardChannelJoin

func (client *Client) OnShardChannelJoin(f func(int, string, string))

OnShardChannelJoin event called after a user joins a chatroom

func (*Client) OnShardChannelLeave

func (client *Client) OnShardChannelLeave(f func(int, string, string))

OnShardChannelLeave event called after a user leaves a chatroom

func (*Client) OnShardChannelMessageDelete

func (client *Client) OnShardChannelMessageDelete(f func(int, ChatMessageDelete))

OnShardChannelMessageDelete event called after a message was deleted in a channels chatroom

func (*Client) OnShardChannelUpdate

func (client *Client) OnShardChannelUpdate(f func(int, RoomState))

OnShardChannelUpdate event called after a chatrooms state has been modified

func (*Client) OnShardChannelUserNotice

func (client *Client) OnShardChannelUserNotice(f func(int, UserNotice))

OnShardChannelUserNotice event called after a generic user event occurrs in a channels chatroom

func (*Client) OnShardDisconnect

func (client *Client) OnShardDisconnect(f func(int))

OnShardDisconnect event called after a shards connection is closed

func (*Client) OnShardLatencyUpdate

func (client *Client) OnShardLatencyUpdate(f func(int, time.Duration))

OnShardLatencyUpdate event called after a shards latency is updated

func (*Client) OnShardMessage

func (client *Client) OnShardMessage(f func(int, ChatMessage))

OnShardMessage event called after a shard receives a chat message

func (*Client) OnShardRawMessage

func (client *Client) OnShardRawMessage(f func(int, Message))

OnShardRawMessage event called after a shard receives an IRC message

func (*Client) OnShardReconnect

func (client *Client) OnShardReconnect(f func(int))

OnShardReconnect event called after a shards connection is reopened

func (*Client) OnShardServerNotice

func (client *Client) OnShardServerNotice(f func(int, ServerNotice))

OnShardServerNotice event called when the IRC server sends a notice message

func (*Client) SetMaxChannelsPerShard

func (client *Client) SetMaxChannelsPerShard(max int)

SetMaxChannelsPerShard sets the maximum number of channels a shard can listen to at a time

Default: 100

type Command

type Command string

Command is the command sent in an IRCMessage

const (
	// CMDPrivMessage is a PRIVMSG command
	CMDPrivMessage Command = "PRIVMSG"
	// CMDClearChat is a CLEARCHAT command
	CMDClearChat Command = "CLEARCHAT"
	// CMDClearMessage is a CLEARMSG command
	CMDClearMessage Command = "CLEARMSG"
	// CMDHostTarget is a HOSTTARGET command
	CMDHostTarget Command = "HOSTTARGET"
	// CMDNotice is a NOTICE command
	CMDNotice Command = "NOTICE"
	// CMDReconnect is a RECONNECT command
	CMDReconnect Command = "RECONNECT"
	// CMDRoomState is a ROOMSTATE command
	CMDRoomState Command = "ROOMSTATE"
	// CMDUserNotice is a USERNOTICE command
	CMDUserNotice Command = "USERNOTICE"
	// CMDUserState is a USERSTATE command
	CMDUserState Command = "USERSTATE"
	// CMDGlobalUserState is a GLOBALUSERSTATE command
	CMDGlobalUserState Command = "GLOBALUSERSTATE"
	// CMDJoin is a JOIN command
	CMDJoin Command = "JOIN"
	// CMDPart is a PART command
	CMDPart Command = "PART"
	// CMDPing is a PING command
	CMDPing Command = "PING"
	// CMDPong is a PONG command
	CMDPong Command = "PONG"
	// CMDReady is a 376 command
	CMDReady Command = "376"
)

type Conn

type Conn struct {
	UserState GlobalUserState
	Username  string
	// contains filtered or unexported fields
}

Conn is a single connection to the Twitch IRC service

func (*Conn) Close

func (conn *Conn) Close()

Close disconnects from the IRC server

func (*Conn) Connect

func (conn *Conn) Connect() error

Connect attempts to open a connection to the IRC server

func (*Conn) GetChannel

func (conn *Conn) GetChannel(channel string) (RoomState, bool)

GetChannel returns true if this connection is listening to the provided channel along with its RoomState

func (*Conn) IsConnected

func (conn *Conn) IsConnected() bool

IsConnected returns true if the connection is active

func (*Conn) IsShard

func (conn *Conn) IsShard() bool

IsShard returns true if the connection was created by a shard manager.

func (*Conn) Join

func (conn *Conn) Join(channels ...string) error

Join attempts to join a channel

func (*Conn) Leave

func (conn *Conn) Leave(channels ...string) error

Leave attempts to leave a channel

func (*Conn) OnChannelBan

func (conn *Conn) OnChannelBan(f func(ChatBan))

OnChannelBan event called after a user was banned or timed out in a channels chatoom

func (*Conn) OnChannelJoin

func (conn *Conn) OnChannelJoin(f func(string, string))

OnChannelJoin event called after a user joins a chatroom

func (*Conn) OnChannelLeave

func (conn *Conn) OnChannelLeave(f func(string, string))

OnChannelLeave event called after a user leeaves a chatroom

func (*Conn) OnChannelMessageDelete

func (conn *Conn) OnChannelMessageDelete(f func(ChatMessageDelete))

OnChannelMessageDelete event called after a message was deleted in a channels chatroom

func (*Conn) OnChannelUpdate

func (conn *Conn) OnChannelUpdate(f func(RoomState))

OnChannelUpdate event called after a chatrooms state has been modified

func (*Conn) OnChannelUserNotice

func (conn *Conn) OnChannelUserNotice(f func(UserNotice))

OnChannelUserNotice event called after a generic user event occurrs in a channels chatroom

func (*Conn) OnDisconnect

func (conn *Conn) OnDisconnect(f func())

OnDisconnect event called when the connection was closed

func (*Conn) OnLatencyUpdate

func (conn *Conn) OnLatencyUpdate(f func(time.Duration))

OnLatencyUpdate event called after the latency to server has been updated

func (*Conn) OnMessage

func (conn *Conn) OnMessage(f func(ChatMessage))

OnMessage event called after a message is received

func (*Conn) OnRawMessage

func (conn *Conn) OnRawMessage(f func(Message))

OnRawMessage event called after a raw IRC message has been handled

func (*Conn) OnReconnect

func (conn *Conn) OnReconnect(f func())

OnReconnect event called when the connection is reopened

func (*Conn) OnServerNotice

func (conn *Conn) OnServerNotice(f func(ServerNotice))

OnServerNotice event called when the IRC server sends a notice message

func (*Conn) Ping

func (conn *Conn) Ping() (time.Duration, error)

Ping sends a ping message to the IRC server

This operation will block, giving the server up to 5 seconds to respond after correcting for latency before failing

func (*Conn) Reconnect

func (conn *Conn) Reconnect() error

Reconnect closes and reopens the IRC connection

Equivalent to Connect if the connection is not already open

func (*Conn) Say

func (conn *Conn) Say(channel string, message string) error

Say sends a message in the provided channel if authenticated

If using a shards, you must create a single connection and use it as a writer

func (*Conn) Sayf

func (conn *Conn) Sayf(channel string, format string, a ...interface{}) error

Sayf sends a formatted message in the provided channel if authenticated

If using a shards, you must create a single connection and use it as a writer

func (*Conn) SendRaw

func (conn *Conn) SendRaw(raw ...string) error

SendRaw writes the provided messages to the IRC server

Will attempt to connect to the IRC server if it is not already connected

func (*Conn) SetLogin

func (conn *Conn) SetLogin(username, token string) error

SetLogin sets the username and authentication token for the connection

Will return an error if the connection is already open

type GlobalUserState

type GlobalUserState struct {
	UserState
}

GlobalUserState is the state of the authenticated user that does not change across channels

func NewGlobalUserState

func NewGlobalUserState(msg Message) (state GlobalUserState)

NewGlobalUserState parses the global state of an authenticated user

type IClient

type IClient interface {
	SetMaxChannelsPerShard(int)
	GetNextShard() (*Conn, error)
	GetShard(int) (*Conn, error)
	GetChannel(string) (RoomState, bool)

	Join(...string) error
	Leave(...string) error
	Close()

	OnShardMessage(func(int, ChatMessage))
	OnShardServerNotice(func(int, ServerNotice))
	OnShardLatencyUpdate(func(int, time.Duration))
	OnShardChannelJoin(func(int, string, string))
	OnShardChannelLeave(func(int, string, string))
	OnShardChannelUpdate(func(int, RoomState))
	OnShardChannelUserNotice(func(int, UserNotice))
	OnShardChannelMessageDelete(func(int, ChatMessageDelete))
	OnShardChannelBan(func(int, ChatBan))
	OnShardRawMessage(func(int, Message))
	OnShardReconnect(func(int))
	OnShardDisconnect(func(int))
}

IClient is a generic IRC shard provider

type IConn

type IConn interface {
	SetLogin(string, string) error
	IsShard() bool
	IsConnected() bool
	GetChannel(string) (RoomState, bool)

	Connect() error
	SendRaw(...string) error
	Ping() (time.Duration, error)
	Join(...string) error
	Say(string, string) error
	Sayf(string, string, ...interface{}) error
	Leave(...string) error
	Reconnect() error
	Close()

	OnServerNotice(func(ServerNotice))
	OnLatencyUpdate(func(time.Duration))
	OnChannelJoin(func(string, string))
	OnChannelLeave(func(string, string))
	OnChannelUpdate(func(RoomState))
	OnChannelUserNotice(func(UserNotice))
	OnChannelMessageDelete(func(ChatMessageDelete))
	OnChannelBan(func(ChatBan))
	OnMessage(func(ChatMessage))
	OnRawMessage(func(Message))
	OnReconnect(func())
	OnDisconnect(func())
}

IConn is a generic IRC connection

type IMessageParser

type IMessageParser interface {
	Parse() error
	// contains filtered or unexported methods
}

IMessageParser is a generic parser for an IRC message

type Message

type Message struct {
	Raw     string
	Command Command
	Sender  Source
	Tags    map[string]string
	Params  []string
	Text    string
}

Message is an IRC message received by the socket

func NewParsedMessage

func NewParsedMessage(raw string) (Message, error)

NewParsedMessage parses raw data from the IRC server and returns an IRCMessage

func (*Message) Parse

func (msg *Message) Parse() error

Parse takes the raw data in an IRCMessage and stores it accordingly

This is done automatically when running NewParsedMessage but can be run again at any time

type RoomState

type RoomState struct {
	UserState ChannelUserState
	ID        int64
	Name      string
	// contains filtered or unexported fields
}

RoomState is the current state of a channels chatroom

func NewRoomState

func NewRoomState(msg Message, state *RoomState) *RoomState

NewRoomState parses the state of a channels chatroom and stores it to the provided pointer

func (RoomState) IsEmoteOnly

func (state RoomState) IsEmoteOnly() bool

IsEmoteOnly returns true if users without VIP or moderator privileges are only permitted to send emotes

func (RoomState) IsFollowersOnly

func (state RoomState) IsFollowersOnly() (enabled bool, duration time.Duration)

IsFollowersOnly returns true if users must be following for a set duration before sending messages

func (RoomState) IsR9KModeEnabled

func (state RoomState) IsR9KModeEnabled() bool

IsR9KModeEnabled returns true if messages must contain more than 9 unique characters to be sent successfully

func (RoomState) IsRitualsEnabled

func (state RoomState) IsRitualsEnabled() bool

IsRitualsEnabled returns true if users are able to signal to the chatroom that they are new to the community

func (RoomState) IsSlowModeEnabled

func (state RoomState) IsSlowModeEnabled() (enabled bool, duration time.Duration)

IsSlowModeEnabled returns true if non-moderators must wait a set duration between sending messages

func (RoomState) IsSubscribersOnly

func (state RoomState) IsSubscribersOnly() bool

IsSubscribersOnly returns true if users without VIP or moderator privileges must be subscribed to send messages

type ServerNotice

type ServerNotice struct {
	IRCMessage Message
	Channel    string
	Message    string
	Type       string
}

ServerNotice is a message sent from the IRC server with general notices

func NewServerNotice

func NewServerNotice(msg Message) ServerNotice

NewServerNotice parses a notice message sent from the IRC server

type Source

type Source struct {
	Nickname string
	Username string
	Host     string
}

Source is basic info about in IRC message

type UserNotice

type UserNotice struct {
	IRCMessage Message
	Sender     ChatSender
	ID         string
	Message    string
	Type       string
	CreatedAt  time.Time
}

UserNotice is a generic user based event in a channels chatroom

func NewUserNotice

func NewUserNotice(msg Message) UserNotice

NewUserNotice parses a generic user based event in a channels chatroom

type UserState

type UserState struct {
	ID          int64
	DisplayName string
	Color       string
	Badges      map[string]string
	BadgeInfo   map[string]string
	EmoteSets   []int64
	Type        string
}

UserState is a generic state of an authenticated user

func NewUserState

func NewUserState(msg Message) (state UserState)

NewUserState parses a generic state of an authenticated user

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL