ircevent

package
v0.0.0-...-f5e0f87 Latest Latest
Warning

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

Go to latest
Published: May 10, 2021 License: ISC, BSD-3-Clause Imports: 16 Imported by: 0

README

Description

This is an event-based IRC client library. It is a fork of thoj/go-ircevent.

Features

  • Event-based: register callbacks for IRC commands
  • Handles reconnections
  • Supports SASL
  • Supports requesting IRCv3 capabilities

Example

See examples/simple.go for a working example, but this illustrates the API:

irc := ircevent.Connection{
	Server:      "testnet.oragono.io:6697",
	UseTLS:      true,
	Nick:        "ircevent-test",
	Debug:       true,
	RequestCaps: []string{"server-time", "message-tags"},
}

irc.AddCallback("001", func(e ircmsg.Message) { irc.Join("#ircevent-test") })

irc.AddCallback("PRIVMSG", func(event ircmsg.Message) {
	// event.Prefix is the source;
	// event.Params[0] is the target (the channel or nickname the message was sent to)
	// and event.Params[1] is the message itself
});

err := irc.Connect()
if err != nil {
	log.Fatal(err)
}
irc.Loop()

The read loop will wait for all callbacks to complete before moving on to the next message. If your callback needs to trigger a long-running task, you should spin off a new goroutine for it.

Commands

These commands can be used from inside callbacks, or externally:

irc.Connect("irc.someserver.com:6667") //Connect to server
irc.Send(command, params...)
irc.SendWithTags(tags, command, params...)
irc.Join(channel)
irc.Privmsg(target, message)
irc.Privmsgf(target, formatString, params...)

Documentation

Index

Constants

View Source
const (
	Version = "goshuirc/irc-go"

	CAPTimeout = time.Second * 15
)
View Source
const (
	RPL_WELCOME         = "001"
	RPL_ISUPPORT        = "005"
	RPL_ENDOFMOTD       = "376"
	ERR_NOMOTD          = "422"
	ERR_NICKNAMEINUSE   = "433"
	ERR_UNAVAILRESOURCE = "437"
	// SASL
	RPL_LOGGEDIN    = "900"
	RPL_LOGGEDOUT   = "901"
	ERR_NICKLOCKED  = "902"
	RPL_SASLSUCCESS = "903"
	ERR_SASLFAIL    = "904"
	ERR_SASLALREADY = "907"
)

Variables

View Source
var (
	ClientDisconnected = errors.New("Could not send because client is disconnected")
	ServerTimedOut     = errors.New("Server did not respond in time")
	ServerDisconnected = errors.New("Disconnected by server")
	SASLFailed         = errors.New("SASL setup timed out. Does the server support SASL?")

	CapabilityNotNegotiated = errors.New("The IRCv3 capability required for this was not negotiated")
)

Functions

func ExtractNick

func ExtractNick(source string) string

func SplitNUH

func SplitNUH(source string) (nick, user, host string)

Types

type Batch

type Batch struct {
	ircmsg.Message
	Items []*Batch
}

Batch represents an IRCv3 batch, or a line within one. There are two cases: 1. (Batch).Command == "BATCH". This indicates the start of an IRCv3 batch; the embedded Message is the initial BATCH command, which may contain tags that pertain to the batch as a whole. (Batch).Items contains zero or more *Batch elements, pointing to the contents of the batch in order. 2. (Batch).Command != "BATCH". This is an ordinary IRC line; its tags, command, and parameters are available as members of the embedded Message. In the context of labeled-response, there is a third case: a `nil` value of *Batch indicates that the server failed to respond in time.

type BatchCallback

type BatchCallback func(*Batch) bool

type Callback

type Callback func(ircmsg.Message)

type CallbackID

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

Tuple type for uniquely identifying callbacks

type Connection

type Connection struct {
	// config data, user-settable
	Server          string
	TLSConfig       *tls.Config
	Nick            string
	User            string
	RealName        string   // IRC realname/gecos
	WebIRC          []string // parameters for the WEBIRC command
	Password        string   // server password (PASS command)
	RequestCaps     []string // IRCv3 capabilities to request (failure is non-fatal)
	SASLLogin       string   // SASL credentials to log in with (failure is fatal)
	SASLPassword    string
	SASLMech        string
	QuitMessage     string
	Version         string
	Timeout         time.Duration
	KeepAlive       time.Duration
	ReconnectFreq   time.Duration
	MaxLineLen      int // maximum line length, not including tags
	UseTLS          bool
	UseSASL         bool
	EnableCTCP      bool
	Debug           bool
	AllowPanic      bool // if set, don't recover() from panics in callbacks
	AllowTruncation bool // if set, truncate lines exceeding MaxLineLen and send them

	Log *log.Logger
	// contains filtered or unexported fields
}

func (*Connection) AcknowledgedCaps

func (irc *Connection) AcknowledgedCaps() (result map[string]string)

Return IRCv3 CAPs actually enabled on the connection, together with their values if applicable. The resulting map is shared, so do not modify it.

func (*Connection) Action

func (irc *Connection) Action(target, message string) error

Send (action) message to a target (channel or nickname). No clear RFC on this one...

func (*Connection) Actionf

func (irc *Connection) Actionf(target, format string, a ...interface{}) error

Send formatted (action) message to a target (channel or nickname).

func (*Connection) AddBatchCallback

func (irc *Connection) AddBatchCallback(callback func(*Batch) bool) CallbackID

AddBatchCallback adds a callback for handling BATCH'ed server responses. All available BATCH callbacks will be invoked in an undefined order, stopping at the first one to return a value of true (indicating successful processing). If no batch callback returns true, the batch will be "flattened" (i.e., its messages will be processed individually by the normal event handlers). Batch callbacks can be removed as usual with RemoveCallback.

func (*Connection) AddCallback

func (irc *Connection) AddCallback(command string, callback func(ircmsg.Message)) CallbackID

Register a callback to handle an IRC command (or numeric). A callback is a function which takes only an ircmsg.Message object as parameter. Valid commands are all IRC commands, including numerics. This function returns the ID of the registered callback for later management.

func (*Connection) AddConnectCallback

func (irc *Connection) AddConnectCallback(callback func(ircmsg.Message)) (id CallbackID)

Convenience function to add a callback that will be called once the connection is completed (this is traditionally referred to as "connection registration").

func (*Connection) ClearCallback

func (irc *Connection) ClearCallback(command string)

Remove all callbacks from a given event code.

func (*Connection) Connect

func (irc *Connection) Connect() (err error)

Connect to a given server using the current connection configuration. This function also takes care of identification if a password is provided. RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.1

func (*Connection) Connected

func (irc *Connection) Connected() bool

Returns true if the connection is connected to an IRC server.

func (*Connection) CurrentNick

func (irc *Connection) CurrentNick() string

Determine nick currently used with the connection.

func (*Connection) HandleBatch

func (irc *Connection) HandleBatch(batch *Batch)

HandleBatch handles a *Batch using available handlers, "flattening" it if no handler succeeds. This can be used in a batch or labeled-response callback to process inner batches.

func (*Connection) HandleMessage

func (irc *Connection) HandleMessage(event ircmsg.Message)

HandleMessage handles an IRC line using the available handlers. This can be used in a batch or labeled-response callback to process an individual line.

func (*Connection) ISupport

func (irc *Connection) ISupport() (result map[string]string)

Returns the 005 RPL_ISUPPORT tokens sent by the server when the connection was initiated, parsed into key-value form as a map. The resulting map is shared, so do not modify it.

func (*Connection) Join

func (irc *Connection) Join(channel string) error

Use the connection to join a given channel. RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.2.1

func (*Connection) Loop

func (irc *Connection) Loop()

Main loop to control the connection.

func (*Connection) Notice

func (irc *Connection) Notice(target, message string) error

Send a notification to a nickname. This is similar to Privmsg but must not receive replies. RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.4.2

func (*Connection) Noticef

func (irc *Connection) Noticef(target, format string, a ...interface{}) error

Send a formated notification to a nickname. RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.4.2

func (*Connection) Part

func (irc *Connection) Part(channel string) error

Leave a given channel. RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.2.2

func (*Connection) PreferredNick

func (irc *Connection) PreferredNick() string

Returns the expected or desired nickname for the connection; if the real nickname is different, the client will periodically attempt to change to this one.

func (*Connection) Privmsg

func (irc *Connection) Privmsg(target, message string) error

Send (private) message to a target (channel or nickname). RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.4.1

func (*Connection) Privmsgf

func (irc *Connection) Privmsgf(target, format string, a ...interface{}) error

Send formated string to specified target (channel or nickname).

func (*Connection) Quit

func (irc *Connection) Quit()

Quit the current connection and disconnect from the server RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.1.6

func (*Connection) Reconnect

func (irc *Connection) Reconnect()

Reconnect forces the client to reconnect to the server. TODO try to ensure buffered messages are sent?

func (*Connection) RemoveCallback

func (irc *Connection) RemoveCallback(id CallbackID)

Remove callback i (ID) from the given event code.

func (*Connection) ReplaceCallback

func (irc *Connection) ReplaceCallback(id CallbackID, callback func(ircmsg.Message)) bool

Replace callback i (ID) associated with a given event code with a new callback function.

func (*Connection) Send

func (irc *Connection) Send(command string, params ...string) error

Send an IRC message without tags.

func (*Connection) SendIRCMessage

func (irc *Connection) SendIRCMessage(msg ircmsg.Message) error

Send a built ircmsg.Message.

func (*Connection) SendRaw

func (irc *Connection) SendRaw(message string) error

Send a raw string.

func (*Connection) SendWithLabel

func (irc *Connection) SendWithLabel(callback func(*Batch), tags map[string]string, command string, params ...string) error

SendWithLabel sends an IRC message using the IRCv3 labeled-response specification. Instead of being processed by normal event handlers, the server response to the command will be collected into a *Batch and passed to the provided callback. If the server fails to respond correctly, the callback will be invoked with `nil` as the argument.

func (*Connection) SendWithTags

func (irc *Connection) SendWithTags(tags map[string]string, command string, params ...string) error

Send an IRC message with tags.

func (*Connection) SetNick

func (irc *Connection) SetNick(n string)

Set (new) nickname. RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.1.2

type LabelCallback

type LabelCallback func(*Batch)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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