birc

package module
v0.0.0-...-0586431 Latest Latest
Warning

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

Go to latest
Published: May 27, 2017 License: MIT Imports: 6 Imported by: 0

README

BitterBot IRC Client

Bitter IRC is a streamlined IRC library specifically designed for Twitch's IRC servers. To see an example application go here.

Example

import "github.com/jpiontek/bitter-irc"

// Your OAuth Key
oauthKey := "my_oauth_key"
// Your account username
username := "fred_bot"
// The channel you'd like to listen to
channelName := "awesome_streamer"
// Use TLS
tls := true

// Create a new channel by supplying the necessary info and the Logger digester.
channel := birc.NewTwitchChannel(channelName, username, oauthKey, tls, birc.Logger)

// Connect estblishes the underlying TCP connection
err := channel.Connect()
if err != nil {
  // Handle error
}

// Authenticate will send the proper credentials and join the channel.
err := channel.Authenticate()
if err != nil {
  // Handle error
}

// Listen begins listening on the channel and handling messages. It is blocking,
// so you may want to wrap it in a go routine if you intend to continue executing
// on the current thread.
err := channel.Listen()
if err != nil {
 // Channel eventually had an error.
}

Digesters

Digesters are simply functions used to handle incoming IRC messages. They have the signature:

type Digester func(m Message, c ChannelWriter)

You can pass in any number of digesters to the NewTwitchChannel function. They MUST be threadsafe as they will be called by multiple go routines. An example of the Logger digester:

func Logger(m Message, w ChannelWriter) {
	if m.Username != "" && m.Content != "" {
		fmt.Printf("\n%s %s: %s", m.Time.Format(timeFormat), m.Username, m.Content)
	}
}

You can see the Logger digester just prints a formatted string to stdout if the message has a username and some sort of content.

The ChannelWriter is an interface that represents a channel you can write to via the Send function.

// Simply checks if the content of someone's message is !command. If so then
// the digester replies in the channel with "Executing command!".
if m.Content == "!command" {
  w.Send("Executing command!")
}

The ChannelWriter also supports SendMessage. You can send any message struct via this function.

message := &birc.Message{
  Command: "PONG",
  Content: "tmi.twitch.tv",
}

err := w.SendMessage(message)

The ChannelWriter also supports retrieving the Channel's configuration.

config := w.GetConfig()

The Message struct passed into each digester:

type Message struct {
  Name     string
  Username string
  Content  string
  Command  string
  Host     string
  Params   []string
  Time     time.Time
}

Documentation

Index

Constants

View Source
const (
	// DefaultTwitchPort is Twitch's default IRC port
	DefaultTwitchPort = "6667"
	// DefaultTwitchTlsPort is Twitch's default IRC port for TLS connections
	DefaultTwitchTlsPort = "443"
	// DefaultTwitchURI is Twitch's default IRC server
	DefaultTwitchURI = "irc.chat.twitch.tv"
	// DefaultTwitchServer is a helper with the DefaultTwitchURI and
	// DefaultTwitchPort combined.
	DefaultTwitchServer = DefaultTwitchURI + ":" + DefaultTwitchPort
	// DefaultTwitchTlsServer is the default TLS server and port
	DefaultTwitchTlsServer = DefaultTwitchURI + ":" + DefaultTwitchTlsPort
)

Variables

This section is empty.

Functions

func Logger

func Logger(m Message, c ChannelWriter)

Logger is a digester that simply echoes out user's messages to stdout.

Types

type Channel

type Channel struct {
	Config    *Config
	Digesters []Digester
	// contains filtered or unexported fields
}

Channel represents a connected and active IRC channel.

func NewTwitchChannel

func NewTwitchChannel(channelName, username, token string, tls bool, digesters ...Digester) *Channel

NewTwitchChannel creates an IRC channel with Twitch's default server and port.

func (*Channel) Authenticate

func (c *Channel) Authenticate() error

Authenticate sends the PASS and NICK to authenticate against the server. It also sends the JOIN message in order to join the specified channel in the configuration.

func (*Channel) Connect

func (c *Channel) Connect() error

Connect establishes a connection to an IRC server.

func (*Channel) Disconnect

func (c *Channel) Disconnect()

Disconnect ends the current listener and closes the TCP connection.

func (*Channel) GetConfig

func (c *Channel) GetConfig() Config

GetConfig returns the Channel's configuration.

func (*Channel) Listen

func (c *Channel) Listen() error

Listen enters a loop and starts decoding IRC messages from the connected channel. Decoded messages are pushed to the digesters to be handled.

func (*Channel) Reconnect

func (c *Channel) Reconnect() error

Reconnect reconnects and reauthenticates the Channel.

func (*Channel) Send

func (c *Channel) Send(content string) error

Send writes a message to the channel.

func (*Channel) SendMessage

func (c *Channel) SendMessage(message *Message) error

SendMessage sends the supplied message to the Channel.

func (*Channel) SetWriter

func (c *Channel) SetWriter(e Encoder)

SetWriter sets the channel's underlying writer. This is not threadsafe.

type ChannelWriter

type ChannelWriter interface {
	Send(content string) error
	SendMessage(message *Message) error
	GetConfig() Config
}

ChannelWriter represents a writer capable of sending messages to a channel.

type Config

type Config struct {
	ChannelName string
	Server      string
	Username    string
	OAuthToken  string
	// contains filtered or unexported fields
}

Config contains fields required to connect to the IRC server.

type Decoder

type Decoder interface {
	Decode() (*sirc.Message, error)
}

Decoder represents a struct capable of decoding incoming IRC messages.

type Digester

type Digester func(m Message, c ChannelWriter)

Digester is a handler function for parsing and reacting to IRC chat messages. All digesters MUST be thread safe, they will be called from multiple go routines.

func CustomLogger

func CustomLogger(w io.Writer) Digester

CustomLogger will write the incoming messages to the supplied io.Writer.

type Encoder

type Encoder interface {
	Encode(m *sirc.Message) error
}

Encoder represents a struct capable of encoding an IRC message.

type Message

type Message struct {
	Name     string
	Username string
	Content  string
	Command  string
	Host     string
	Params   []string
	Time     time.Time
}

Message is a decoded IRC message.

func PongMessage

func PongMessage() *Message

PongMessage returns a Message struct containing a PONG message, which should be used as a response to Twitch's PING message

Jump to

Keyboard shortcuts

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