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 ¶
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 ¶
IsErrorCommand checks if given command reports an error. TODO: replace with map
func JoinMessage ¶
JoinMessage builds IRC message in a way that it satisfy IRC JOIN command structure.
Types ¶
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 NewClientWithOpts ¶
func NewClientWithOpts(conn net.Conn, user *User, options *ClientOpts) *Client
NewClientWithOpts ...
func (*Client) Encode ¶
Encode writes the IRC encoding of irc.Message to the stream. Its wrapper for irc.Encoder.Encode method.
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.
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 ¶
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 Request ¶
Request represents an IRC request received by a client. Its handy wrapper around irc.Message object.
func (*Request) Receivers ¶
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 (*ServeMux) Handle ¶
Handle registers the handler for the given IRC command. If a handler already exists for pattern, Handle panics.
func (*ServeMux) NotFound ¶
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.