irc

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: MIT Imports: 13 Imported by: 1

README

About

Event driven IRC library for go

Features

  • Easy to use
  • Async event handlers
  • Nick reclaim
  • Reconnect on disconnect

Handlers

An event handler receives a pointer to the message that was sent to the client from the server. See godoc github.com/osm/irc Message for a complete description of the Message struct.

Example:

c.Handle("PRIVMSG", func(m *Message) {
	fmt.Println(m.Raw)
})

Complete example

package main

import (
	"fmt"
	"log"
	"os"
	"sync"

	"github.com/osm/irc"
)

func main() {
	// Create a new IRC client
	c := irc.NewClient(
		irc.WithAddr("localhost:6667"),
		irc.WithNick("foo"),
		irc.WithUser("bar"),
		irc.WithRealName("foo bar"),
		irc.WithChannel("#dev"),
		irc.WithDebug(),
		irc.WithLogger(log.New(os.Stdout, "IRC: ", log.LstdFlags)))

	// Setup an event handler for PRIVMSG
	c.Handle("PRIVMSG", func(m *irc.Message) {
		if m.Params == "#dev :hello" {
			c.Privmsg(m.ParamsArray[0], "world")
		}
	})

	// Create a wait group
	var wg sync.WaitGroup
	wg.Add(1)

	// Connect is blocking, so we need to run it in a goroutine
	go func() {
		if err := c.Connect(); err != nil {
			fmt.Println(err)
		}

		wg.Done()
	}()

	// Wait for the client to end
	wg.Wait()
}

Docs

godoc github.com/osm/irc

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client contains the IRC client

func NewClient

func NewClient(opts ...Option) *Client

NewClient creates a new IRC client

func (*Client) Connect

func (c *Client) Connect() error

Connect connects to the IRC server

func (*Client) GetNick

func (c *Client) GetNick() string

GetNick returns the current nick

func (*Client) Handle

func (c *Client) Handle(event string, fn func(m *Message))

Handle registers a new event handler

func (*Client) Mode

func (c *Client) Mode(channel, mode, target string) error

Mode sets mode on a channel for a nick

func (*Client) Nick

func (c *Client) Nick(nick string) error

Nick sets the nick

func (*Client) Notice

func (c *Client) Notice(target, message string) error

Notice sends a notice

func (*Client) Noticef

func (c *Client) Noticef(target, format string, args ...interface{}) error

Noticef sends a notice and accepts a format string as message argument

func (*Client) Privmsg

func (c *Client) Privmsg(target, message string) error

Privmsg sends a message to a channel or nick

func (*Client) Privmsgf

func (c *Client) Privmsgf(target, format string, args ...interface{}) error

Privmsgf sends a privmsg and accepts a format string as message argument

func (*Client) Quit

func (c *Client) Quit(message string)

Quit sends a QUIT message to the server and terminates the connection

func (*Client) ReclaimNick

func (c *Client) ReclaimNick()

ReclaimNick tries to reclaim the nick

func (*Client) Sendf

func (c *Client) Sendf(format string, args ...interface{}) error

Sendf sends a message to the server and appends CR-LF at the end of the string

func (*Client) Whois

func (c *Client) Whois(nick string) error

Whois sends a WHOIS request

type Message

type Message struct {
	// Raw contains the unparsed message
	Raw string

	// Command contains a three digit number or a string
	Command string

	// Params is filled with all parameters that the message contains
	Params string

	// ParamsArray is equal to Params, but are splitted on space for easier manipulation
	ParamsArray []string

	// Name is an optional field, if it contains data it holds either the server name or a nick
	Name string

	// User is an optional parameter that contains the user if the message originates from a client
	User string

	// Host is also an optional parameter that contains the host if the message originates from a client
	Host string
}

Message represents the RFC1459 definition of an IRC message See the full definition in section 2.3.1 in the RFC

type Option

type Option func(*Client)

Option should be implemented by all client options

func WithAddr

func WithAddr(addr string) Option

WithAddr sets the address of the IRC server, this can be omitted if you supply a connection with WithConn

func WithChannel

func WithChannel(ch string) Option

WithChannel sets the channel that the client should join on connect, this can be called mupltiple times

func WithConn

func WithConn(conn net.Conn) Option

WithConn sets the client connection, this can be omitted if you supply an address with WithAddr

func WithDebug

func WithDebug() Option

WithDebug sets the debug flag, set this if you want to log the communication

func WithLogger

func WithLogger(logger *log.Logger) Option

WithLogger sets the logger

func WithNick

func WithNick(n string) Option

WithNick sets the nick for the client

func WithPostConnectMessage added in v1.2.0

func WithPostConnectMessage(t, m string) Option

func WithPostConnectMode added in v1.3.0

func WithPostConnectMode(m string) Option

func WithRealName

func WithRealName(r string) Option

WithRealName sets the real name for the client

func WithUser

func WithUser(u string) Option

WithUser sets the user for the client

func WithVersion

func WithVersion(v string) Option

WithVersion sets the CTCP VERSION reply string

Jump to

Keyboard shortcuts

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