minimalirc

package module
v0.0.0-...-5e4a42c Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2014 License: MIT Imports: 11 Imported by: 0

README

minimalirc

Yet another minimal IRC library written in go. Supports SSL.

Nearly totally untested. Written for ircstatus.

Documentation: http://godoc.org/github.com/kd5pbo/minimalirc

Contrived Example

The below is a contrived example in contrivedExample.go.read which builds and runs quite nicely (after a quick rename).

package main

import (
	"fmt"
	"github.com/kd5pbo/minimalirc"
	"regexp"
)

func main() {

	/* Make an IRC struct */
	irc := minimalirc.New("irc.freenode.net", 7000, true, "", "jbond",
		"james", "James Bond")

	/* Set some settings */
	irc.IdNick = "jbond"
	irc.IdPass = "iloveturtles"
	irc.Channel = "#mi5"
	irc.Pongs = true

	/* Connect to the server */
	if err := irc.Connect(); nil != err {
		fmt.Printf("Failed to connect to server: %v\n", err)
		return
	}
	fmt.Printf("Connected to server.\n")

	secretMessageRE := regexp.MustCompile(
		`:(\S+) PRIVMSG #mi5 :SECRET MESSAGE: (.*)`)
	/* Wait for a secret message */
	for {
		line, ok := <-irc.C
		if !ok {
			err := <-irc.E
			fmt.Printf("Error reading from server: %v\n", err)
			return
		}
		if g := secretMessageRE.FindStringSubmatch(line); 3 == len(g) {
			fmt.Printf("Secret message from %v: %v\n", g[1], g[2])
		}
	}

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IRC

type IRC struct {
	C <-chan string /* Messages from the server are sent here */
	E <-chan error  /* Receives an error before close(C) */
	S net.Conn      /* Represents the connection to the server */

	Msglen  int    /* Size of an IRC message */
	Default string /* Default target for privmsgs */

	/* Configs and defauls.  These may be changed at any time. */
	Host          string /* Host to which to connect */
	Port          uint16 /* Port to which to connect */
	Ssl           bool   /* True to use SSL/TLS */
	Hostname      string /* Hostname to verify on server's certificate */
	Nick          string /* For NICK */
	Username      string /* For USER */
	Realname      string /* For USER */
	IdNick        string /* To auth to NickServ */
	IdPass        string /* To auth to NickServ */
	Channel       string /* For JOIN */
	Chanpass      string /* For JOIN */
	Txp           string /* Prefix for logging sent messages */
	Rxp           string /* Prefix for logging received messages */
	Pongs         bool   /* Automatic ping responses */
	RandomNumbers bool   /* Append random numbers to the nick */
	QuitMessage   string /* Message to send when the client QUITs */
	// contains filtered or unexported fields
}

IRC represents a connection to an IRC server via OpenSSL

func New

func New(host string, port uint16, ssl bool, hostname string,
	nick, username, realname string) *IRC

New allocates, initializes, and returns a pointer to a new IRC struct. hostname will be ignored if ssl is false, or assumed to be the same as host if it is the empty string and ssl is true.

func (*IRC) Auth

func (i *IRC) Auth() error

Auth authenticates to NickServ with the values in i. If either i.IdNick or i.IdPass are the empty string, this is a no-op.

func (*IRC) Connect

func (i *IRC) Connect() error

Connect connects to the server, and calls Handshake(). After connect returns, messages sent by the IRC server will be available on i.C. If i.Rxp is set, received messages from the server will be logged via log.Printf prefixed by i.Rxp, separated by a space. If an error is encountered reading messages from the IRC server, i.C will be closed and the error will be sent on i.E. i.S represents the connection to the server.

func (*IRC) Handshake

func (i *IRC) Handshake() error

Handshake is a shorthand for ID, Auth, and Join, in that order, using the values in i.

func (*IRC) ID

func (i *IRC) ID() error

ID sets the nick and user from the values in i, and sends a NICK command without any parameters (to get an easy-to-parse response with the nick as the server knows it). If i.Nick, i.Username or i.Realname are the empty string, this is a no-op.

func (*IRC) Join

func (i *IRC) Join(channel, pass string) error

Join joins the channel with the optional password (which may be the empty string). If the channel is the empty string, the value from i.Channel and i.Chanpass will be used. If channel and i.Channel are both the empty string, this is a no-op.

func (*IRC) PrintfLine

func (i *IRC) PrintfLine(f string, args ...interface{}) error

PrintfLine sends the formatted string to the IRC server. The message should be a raw IRC protocol message (like WHOIS or CAP). It is not wrapped in PRIVMSG or anything else. For PRIVMSGs, see Privmsg .If i.Txp is not the empty string, successfully sent lines will be logged via log.Printf() prefixed by i.Txp, separated by a space. Note that all the functions used to send protocol messages use PrintfLine.

func (*IRC) Privmsg

func (i *IRC) Privmsg(msg, target string) error

Privmsg sends a PRIVMSG to the target, which may be a nick or a channel. If the target is an empty string, the message will be sent to i.Target, unless that is also an empty string, in which case nothing is sent.

func (*IRC) PrivmsgSize

func (i *IRC) PrivmsgSize(target string) int

PrivmsgSize returns the length of the message that can be shoved into a PRIVMSG to the target. i.Msglen may be changed to override the default size of an IRC message (467 bytes, determined experimentally on freenode, 510 should be it, though). See Privmsg for the meaning of target.

func (*IRC) Quit

func (i *IRC) Quit(msg string) error

Quit sends a QUIT command to the IRC server, with the optional msg as the quit message and closes the connection if the send succeeds. If msg is the empty string, i.QuitMessage will be used, unless it's also the empty string, in which case no message is sent with the QUIT command.

func (*IRC) SNick

func (i *IRC) SNick() string

Nick returns a guess as to what the server thinks the nick is. This is handy for servers that truncate nicks when RandomNumbers is true. This is, however, only a guess (albiet a good one). It should be called after setting the nick with Nick() or Handshake(). Note this is based on passive inspection of received messagess, which requires reading due to the read channel being unbuffered. */

Jump to

Keyboard shortcuts

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