relay

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

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

Go to latest
Published: Sep 27, 2015 License: Apache-2.0 Imports: 7 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Relay

Relay is Go framework to create IRC clients. Has API heavily inspired by net/http library.

Documentation

Overview

Package relay provides IRC client implementations.

package main

import (
	"crypto/tls"
	"log"
	"os"

	"github.com/piotrkowalczuk/relay"
	"github.com/piotrkowalczuk/relay/action"
	"github.com/piotrkowalczuk/relay/freenode"
	"github.com/sorcix/irc"
)

func main() {
	conn, err := tls.Dial("tcp", freenode.Addr, &tls.Config{})
	if err != nil {
		log.Fatal(err)
	}

	rel := relay.NewClientWithOpts(
		conn,
		&relay.User{
			Nick:     "bot",
			RealName: "Mr Bot",
			Password: "password",
			Mode:     irc.UserModeOperator,
		},
		&relay.ClientOpts{
			Logger: log.New(os.Stderr, "", log.LstdFlags),
		},
	)

	sm := relay.NewServeMux()
	sm.Handle(irc.PRIVMSG, relay.HandleFunc(func(mw *relay.MessagesWriter, r *relay.Request){
		mw.WriteParams(r.Receivers()...)
		mw.Write([]byte("Reply"))
	})

	rel.Handle(handler)
	rel.ListenAndReply()

	for {
		select {
		case <-rel.Registered():
			err := rel.Join(relay.NewChannel("#go-nuts", ""))
			if err != nil {
				log.Fatal(err)
			}
		case err := <-rel.Err():
			log.Println("ERROR: ", err)
		}
	}
}

This is a complete program that connects to FreeNode and expose one handler for all private messages.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrJoinNotEnoughChannels is returned by Join if it
	// will be called without single channel as an argument.
	ErrJoinNotEnoughChannels = errors.New("relay: join not enough channels")
)

Functions

func IsErrorCommand

func IsErrorCommand(c string) bool

IsErrorCommand checks if given command reports an error. TODO: replace with map

func JoinMessage

func JoinMessage(channels ...*Channel) *irc.Message

JoinMessage builds IRC message in a way that it satisfy IRC JOIN command structure.

Types

type Channel

type Channel struct {
	Name string
	Key  string
}

Channel ...

func NewChannel

func NewChannel(name, key string) *Channel

NewChannel ...

func (*Channel) String

func (c *Channel) String() string

String implements fmt.Stringer interface.

type Client

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

Client represents connection and logic that corresponds to single IRC server. It can be used to make requests.

func NewClient

func NewClient(conn net.Conn, user *User) *Client

NewClient ...

func NewClientWithOpts

func NewClientWithOpts(conn net.Conn, user *User, options *ClientOpts) *Client

NewClientWithOpts ...

func (*Client) Encode

func (c *Client) Encode(message *irc.Message) error

Encode writes the IRC encoding of irc.Message to the stream. Its wrapper for irc.Encoder.Encode method.

func (*Client) Err

func (c *Client) Err() <-chan error

Err returns channel

func (*Client) Handle

func (c *Client) Handle(h Handler)

Handle sets handler for the client.

func (*Client) IsRegistered

func (c *Client) IsRegistered() bool

IsRegistered ...

func (*Client) Join

func (c *Client) Join(channels ...*Channel) error

Join sends JOIN message to the IRC server with given channels.

func (*Client) ListenAndReply

func (c *Client) ListenAndReply()

ListenAndReply listens for IRC messages on TCP connection and handle them with provided handler. If no handler is passed it panics.

func (*Client) Registered

func (c *Client) Registered() <-chan bool

Registered ...

type ClientOpts

type ClientOpts struct {
	Logger              StdLogger
	RequestInterceptor  func(*irc.Message) error
	ResponseInterceptor func(*irc.Message) error
}

ClientOpts represent optional values that can be pass to the client.

type ErrorMessage

type ErrorMessage struct {
	*irc.Message
}

ErrorMessage ...

func (*ErrorMessage) Error

func (em *ErrorMessage) Error() string

Error implements error interface.

type Handler

type Handler interface {
	ServeIRC(MessageWriter, *Request)
}

Handler is a basic wrapper for ServeIRC method.

type MessageWriter

type MessageWriter interface {
	// Params returns the param slice that will be sent.
	// Changing the params after a call to Write has no effect.
	Params() *Params
	// Write writes the data to the message trailing as part of an IRC reply.
	// If WriteCommand has not yet been called, Write calls WriteCommand(irc.PRIVMSG)
	// before writing the data.
	Write([]byte) (int, error)
	// WriteCommand sets IRC message command type, irc.PRIVMSG by default.
	WriteCommand(string)
	WriteParams(...string)
}

MessageWriter interface is used by a Handler to construct an IRC message.

type Params

type Params []string

Params ...

func (*Params) Del

func (p *Params) Del(param string)

Del ...

func (*Params) Exists

func (p *Params) Exists(param string) bool

Exists ...

func (*Params) Set

func (p *Params) Set(params ...string)

Set ...

type Request

type Request struct {
	*irc.Message
	RemoteAddr net.Addr
	LocalAddr  net.Addr
}

Request represents an IRC request received by a client. Its handy wrapper around irc.Message object.

func (*Request) Receivers

func (r *Request) Receivers() []string

Receivers returns slice of possible recipients. It can be list of channels or a user that sends a message.

PRIVMSG and NOTICE are the only messages available which actually perform delivery of a text message from one client to another - the rest just make it possible and try to ensure it happens in a reliable and structured manner.

type ServeMux

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

ServeMux is an IRC request multiplexer. It matches the IRC command of each incoming request against a list of registered and calls the handler matches the given command. TODO(piotr): mutexes!

func NewServeMux

func NewServeMux() *ServeMux

NewServeMux allocates and returns a new ServeMux.

func (*ServeMux) Handle

func (sm *ServeMux) Handle(command string, handler Handler)

Handle registers the handler for the given IRC command. If a handler already exists for pattern, Handle panics.

func (*ServeMux) NotFound

func (sm *ServeMux) NotFound(h Handler)

NotFound sets configurable Handler which is called when no matching Handlers is found.

func (*ServeMux) ServeIRC

func (sm *ServeMux) ServeIRC(ew MessageWriter, r *Request)

ServeIRC dispatches the request to the handler command matches the incoming message command.

type StdLogger

type StdLogger interface {
	Print(v ...interface{})
	Printf(format string, v ...interface{})
	Println(v ...interface{})
}

StdLogger ...

type User

type User struct {
	Nick     string
	Password string
	Mode     rune
	RealName string
}

User ...

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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