irc

package module
v0.0.0-...-07452bf Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2021 License: BSD-3-Clause Imports: 18 Imported by: 5

README

Description

Event based irc client library.

Features

  • Event based. Register Callbacks for the events you need to handle.
  • Handles basic irc demands for you
    • Standard CTCP
    • Reconnections on errors
    • Detect stoned servers

Install

$ go get github.com/thoj/go-ircevent

Example

See examples/simple/simple.go and irc_test.go

Events for callbacks

  • 001 Welcome
  • PING
  • CTCP Unknown CTCP
  • CTCP_VERSION Version request (Handled internaly)
  • CTCP_USERINFO
  • CTCP_CLIENTINFO
  • CTCP_TIME
  • CTCP_PING
  • CTCP_ACTION (/me)
  • PRIVMSG
  • MODE
  • JOIN

+Many more

AddCallback Example

ircobj.AddCallback("PRIVMSG", func(event *irc.Event) {
	//event.Message() contains the message
	//event.Nick Contains the sender
	//event.Arguments[0] Contains the channel
});

Please note: Callbacks are run in the main thread. If a callback needs a long time to execute please run it in a new thread.

Example:

    ircobj.AddCallback("PRIVMSG", func(event *irc.Event) {
	go func(event *irc.Event) {
                    //event.Message() contains the message
                    //event.Nick Contains the sender
                    //event.Arguments[0] Contains the channel
	}(event)
    });

Commands

ircobj := irc.IRC("<nick>", "<user>") //Create new ircobj
//Set options
ircobj.UseTLS = true //default is false
//ircobj.TLSOptions //set ssl options
ircobj.Password = "[server password]"
//Commands
ircobj.Connect("irc.someserver.com:6667") //Connect to server
ircobj.SendRaw("<string>") //sends string to server. Adds \r\n
ircobj.SendRawf("<formatstring>", ...) //sends formatted string to server.n
ircobj.Join("<#channel> [password]") 
ircobj.Nick("newnick") 
ircobj.Privmsg("<nickname | #channel>", "msg") // sends a message to either a certain nick or a channel
ircobj.Privmsgf(<nickname | #channel>, "<formatstring>", ...)
ircobj.Notice("<nickname | #channel>", "msg")
ircobj.Noticef("<nickname | #channel>", "<formatstring>", ...)

Documentation

Index

Constants

View Source
const CAP_TIMEOUT = time.Second * 15
View Source
const (
	VERSION = "go-ircevent v2.1"
)

Variables

View Source
var ErrDisconnected = errors.New("Disconnect Called")

Functions

This section is empty.

Types

type Channel

type Channel struct {
	sync.Mutex
	// We should extract topic by handling the correct Event at some point in the future!
	Topic string
	// We should extract channel modes by handling the correct Event at some point in the future!
	Mode string
	// leaving exposed for now, will be unexported in the future!
	Users map[string]User
	// contains filtered or unexported fields
}

Channel stores the channel information for a channel this Connection is a member of

func (*Channel) GetUser

func (ch *Channel) GetUser(name string) (User, bool)

func (*Channel) IterUsers

func (ch *Channel) IterUsers(call func(string, User))

type Connection

type Connection struct {
	sync.Mutex
	sync.WaitGroup
	Debug            bool
	Error            chan error
	WebIRC           string
	Password         string
	UseTLS           bool
	UseSASL          bool
	RequestCaps      []string
	AcknowledgedCaps []string
	SASLLogin        string
	SASLPassword     string
	SASLMech         string
	TLSConfig        *tls.Config
	Version          string
	Timeout          time.Duration
	CallbackTimeout  time.Duration
	PingFreq         time.Duration
	KeepAlive        time.Duration
	Server           string
	Encoding         encoding.Encoding

	RealName string // The real name we want to display.

	QuitMessage string

	VerboseCallbackHandler bool
	Log                    *log.Logger

	Channels map[string]*Channel
	// contains filtered or unexported fields
}

func IRC

func IRC(nick, user string) *Connection

Create a connection with the (publicly visible) nickname and username. The nickname is later used to address the user. Returns nil if nick or user are empty.

func (*Connection) Action

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

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{})

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

func (*Connection) AddCallback

func (irc *Connection) AddCallback(eventcode string, callback func(*Event)) int

Register a callback to a connection and event code. A callback is a function which takes only an Event pointer as parameter. Valid event codes are all IRC/CTCP commands and error/response codes. To register a callback for all events pass "*" as the event code. This function returns the ID of the registered callback for later management.

func (*Connection) ClearCallback

func (irc *Connection) ClearCallback(eventcode string) bool

Remove all callbacks from a given event code. It returns true if given event code is found and cleared.

func (*Connection) Connect

func (irc *Connection) Connect(server string) 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) Disconnect

func (irc *Connection) Disconnect()

A disconnect sends all buffered messages (if possible), stops all goroutines and then closes the socket.

func (*Connection) ErrorChan

func (irc *Connection) ErrorChan() chan error

func (*Connection) GetChannel

func (irc *Connection) GetChannel(name string) (*Channel, bool)

GetChannel gets a channel by name that the Connection is on

func (*Connection) GetFeature

func (irc *Connection) GetFeature(name string) (*Feature, bool)

GetFeature returns an ISUPPORT feature by name, and a boolean indicating if the value was present or not. One should be sure to lock/unlock the feature when accessing or modifying it.

func (*Connection) GetNick

func (irc *Connection) GetNick() string

Determine nick currently used with the connection.

func (*Connection) IterChannels

func (irc *Connection) IterChannels(call func(string, *Channel))

IterChannels allows for calling code to provide a callable that is ran for each channel the Connection is on

func (*Connection) IterFeatures

func (irc *Connection) IterFeatures(call func(string, *Feature))

IterFeatures allows for one to iterate over the features present and perform actions with/upon it with the call function passed in. There is no need to lock the feature, this handles locking for the caller.

func (*Connection) Join

func (irc *Connection) Join(channel string)

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

func (*Connection) Kick

func (irc *Connection) Kick(user, channel, msg string)

Kick <user> from <channel> with <msg>. For no message, pass empty string ("")

func (*Connection) Loop

func (irc *Connection) Loop()

Main loop to control the connection.

func (*Connection) Mode

func (irc *Connection) Mode(target string, modestring ...string)

Set different modes for a target (channel or nickname). RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.2.3

func (*Connection) MultiKick

func (irc *Connection) MultiKick(users []string, channel string, msg string)

Kick all <users> from <channel> with <msg>. For no message, pass empty string ("")

func (*Connection) Nick

func (irc *Connection) Nick(n string)

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

func (*Connection) NickLength

func (irc *Connection) NickLength() uint

NickLength provides structured access to the NICKLEN in ISUPPORT

func (*Connection) Notice

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

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{})

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)

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

func (*Connection) PrefixModes

func (irc *Connection) PrefixModes() *PrefixModes

PrefixModes provides structured access to the PREFIX in ISUPPORT

func (*Connection) Privmsg

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

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{})

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() error

Reconnect to a server using the current connection.

func (*Connection) RemoveCallback

func (irc *Connection) RemoveCallback(eventcode string, i int) bool

Remove callback i (ID) from the given event code. This functions returns true upon success, false if any error occurs.

func (*Connection) ReplaceCallback

func (irc *Connection) ReplaceCallback(eventcode string, i int, callback func(*Event))

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

func (*Connection) RunCallbacks

func (irc *Connection) RunCallbacks(event *Event)

Execute all callbacks associated with a given event.

func (*Connection) SendRaw

func (irc *Connection) SendRaw(message string)

Send raw string.

func (*Connection) SendRawf

func (irc *Connection) SendRawf(format string, a ...interface{})

Send raw formated string.

func (*Connection) SetupFeatureDetect

func (irc *Connection) SetupFeatureDetect()

SetupFeatureDetect allows for the connection to have FeatureDetection enabled on it

func (*Connection) SetupNickTrack

func (irc *Connection) SetupNickTrack()

SetupNickTrack enables stateful tracking of IRC Users and Channels on this Connection

func (*Connection) Who

func (irc *Connection) Who(nick string)

Query information about a given nickname in the server. RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.5.1

func (*Connection) Whois

func (irc *Connection) Whois(nick string)

Query information about a particular nickname. RFC 1459: https://tools.ietf.org/html/rfc1459#section-4.5.2

type Event

type Event struct {
	Code       string
	Raw        string
	Nick       string //<nick>
	Host       string //<nick>!<usr>@<host>
	Source     string //<host>
	User       string //<usr>
	Arguments  []string
	Tags       map[string]string
	Connection *Connection
	Ctx        context.Context
}

A struct to represent an event.

func (*Event) Message

func (e *Event) Message() string

Retrieve the last message from Event arguments. This function leaves the arguments untouched and returns an empty string if there are none.

func (*Event) MessageWithoutFormat

func (e *Event) MessageWithoutFormat() string

Retrieve the last message from Event arguments, but without IRC formatting (color. This function leaves the arguments untouched and returns an empty string if there are none.

type Feature

type Feature struct {
	sync.Mutex
	Enabled bool
	Value   string
	// contains filtered or unexported fields
}

Feature is a single Feature as exposed by ISUPPORT

type Features

type Features struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Features exposes the supported features from RPL_ISUPPORT

type PrefixModes

type PrefixModes struct {
	Modes   map[rune]rune
	Display map[rune]rune
}

PrefixModes presents a forwards and backwards map of mode character to display character

type SASLResult

type SASLResult struct {
	Failed bool
	Err    error
}

type User

type User struct {
	// FIXME: If we support Host we should set up a WHO handler
	// Careful though - some IRCd's (looking at unreal) silently treat WHO as WHOX
	// and the resultant fields we expect to parse out could be out of order!
	Host string
	Mode string
}

User stores information on an IRC user encountered by this Connection

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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