client: Initial code. Very untested, likely very broken. What is test coverage
This commit is contained in:
parent
b19577fca9
commit
c04ab544b3
149
client/client.go
Normal file
149
client/client.go
Normal file
@ -0,0 +1,149 @@
|
||||
// written by Daniel Oaks <daniel@danieloaks.net>
|
||||
// released under the ISC license
|
||||
|
||||
package gircclient
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/DanielOaks/girc-go/eventmgr"
|
||||
"github.com/DanielOaks/girc-go/ircmsg"
|
||||
)
|
||||
|
||||
// ServerConnection is a connection to a single server.
|
||||
type ServerConnection struct {
|
||||
Name string
|
||||
Connected bool
|
||||
|
||||
// internal stuff
|
||||
connection net.Conn
|
||||
eventsIn eventmgr.EventManager
|
||||
eventsOut eventmgr.EventManager
|
||||
|
||||
// details users must supply before connection
|
||||
Nick string
|
||||
InitialUser string
|
||||
InitialRealName string
|
||||
}
|
||||
|
||||
// Connect connects to the given address.
|
||||
func (sc *ServerConnection) Connect(address string, ssl bool) {
|
||||
var conn net.Conn
|
||||
var err error
|
||||
|
||||
if ssl {
|
||||
conn, err = tls.Dial("tcp", address, nil)
|
||||
} else {
|
||||
conn, err = net.Dial("tcp", address)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Connection error:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
sc.connection = conn
|
||||
|
||||
sc.Send(nil, "", "NICK", sc.Nick)
|
||||
sc.Send(nil, "", "USER", sc.InitialUser, "0", "*", sc.InitialRealName)
|
||||
|
||||
go sc.receiveLoop()
|
||||
}
|
||||
|
||||
// receiveLoop runs a loop of receiving and dispatching new messages.
|
||||
func (sc *ServerConnection) receiveLoop() {
|
||||
reader := bufio.NewReader(sc.connection)
|
||||
|
||||
for sc.Connected {
|
||||
line, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
sc.Connected = false
|
||||
fmt.Println("Reading error:", err)
|
||||
break
|
||||
}
|
||||
line = strings.Trim(line, "\r\n")
|
||||
|
||||
// dispatch raw
|
||||
rawInfo := eventmgr.NewInfoMap()
|
||||
rawInfo["direction"] = "in"
|
||||
rawInfo["data"] = line
|
||||
|
||||
sc.dispatchRawIn(rawInfo)
|
||||
|
||||
// dispatch events
|
||||
message, err := ircmsg.ParseLine(line)
|
||||
info := eventmgr.NewInfoMap()
|
||||
info["tags"] = message.Tags
|
||||
info["prefix"] = message.Prefix
|
||||
info["command"] = message.Command
|
||||
info["params"] = message.Params
|
||||
|
||||
sc.dispatchIn(message.Command, info)
|
||||
}
|
||||
|
||||
sc.connection.Close()
|
||||
}
|
||||
|
||||
// RegisterEvent registers a new handler for the given event.
|
||||
//
|
||||
// The standard directions are "in" and "out".
|
||||
//
|
||||
// 'name' can either be the name of an event, "all", or "raw". Note that "all"
|
||||
// will not catch "raw" events, but will catch all others.
|
||||
func (sc *ServerConnection) RegisterEvent(direction string, name string, handler eventmgr.HandlerFn, priority int) {
|
||||
if direction == "in" || direction == "both" {
|
||||
sc.eventsIn.Attach(name, handler, priority)
|
||||
}
|
||||
if direction == "out" || direction == "both" {
|
||||
sc.eventsOut.Attach(name, handler, priority)
|
||||
}
|
||||
}
|
||||
|
||||
// Shutdown exits from the server.
|
||||
func (sc *ServerConnection) Shutdown(message string) {
|
||||
sc.Send(nil, sc.Nick, "QUIT", message)
|
||||
}
|
||||
|
||||
// Send sends an IRC message to the server.
|
||||
func (sc *ServerConnection) Send(tags *map[string]ircmsg.TagValue, prefix string, command string, params ...string) {
|
||||
ircmsg := ircmsg.MakeMessage(tags, prefix, command, params...)
|
||||
line, err := ircmsg.Line()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Fprintf(sc.connection, line)
|
||||
|
||||
// dispatch raw event
|
||||
info := eventmgr.NewInfoMap()
|
||||
info["direction"] = "out"
|
||||
info["data"] = line
|
||||
sc.dispatchRawOut(info)
|
||||
}
|
||||
|
||||
// dispatchRawIn dispatches raw inbound messages.
|
||||
func (sc *ServerConnection) dispatchRawIn(info eventmgr.InfoMap) {
|
||||
sc.eventsIn.Dispatch("raw", info)
|
||||
}
|
||||
|
||||
// dispatchIn dispatches inbound messages.
|
||||
func (sc *ServerConnection) dispatchIn(name string, info eventmgr.InfoMap) {
|
||||
sc.eventsIn.Dispatch(name, info)
|
||||
sc.eventsIn.Dispatch("all", info)
|
||||
}
|
||||
|
||||
// dispatchRawOut dispatches raw outbound messages.
|
||||
func (sc *ServerConnection) dispatchRawOut(info eventmgr.InfoMap) {
|
||||
sc.eventsOut.Dispatch("raw", info)
|
||||
}
|
||||
|
||||
// dispatchOut dispatches outbound messages.
|
||||
func (sc *ServerConnection) dispatchOut(name string, info eventmgr.InfoMap) {
|
||||
sc.eventsOut.Dispatch(name, info)
|
||||
sc.eventsOut.Dispatch("all", info)
|
||||
}
|
14
client/doc.go
Normal file
14
client/doc.go
Normal file
@ -0,0 +1,14 @@
|
||||
// written by Daniel Oaks <daniel@danieloaks.net>
|
||||
// released under the ISC license
|
||||
|
||||
/*
|
||||
Package gircclient is an IRC client library.
|
||||
|
||||
It uses the other various gIRC-Go libraries to provide a clean, consistent
|
||||
interface for connecting to and interacting with IRC servers.
|
||||
|
||||
The Reactor is the primary handler of all new clients.
|
||||
|
||||
This package is in planning/pre-alpha and the API will change substantially.
|
||||
*/
|
||||
package gircclient
|
65
client/reactor.go
Normal file
65
client/reactor.go
Normal file
@ -0,0 +1,65 @@
|
||||
// written by Daniel Oaks <daniel@danieloaks.net>
|
||||
// released under the ISC license
|
||||
|
||||
package gircclient
|
||||
|
||||
import "github.com/DanielOaks/girc-go/eventmgr"
|
||||
|
||||
// eventRegistration holds events that have not yet been registered.
|
||||
type eventRegistration struct {
|
||||
Direction string
|
||||
Name string
|
||||
Handler eventmgr.HandlerFn
|
||||
Priority int
|
||||
}
|
||||
|
||||
// Reactor is the start-point for gircclient. It creates and manages
|
||||
// ServerConnections.
|
||||
type Reactor struct {
|
||||
ServerConnections map[string]ServerConnection
|
||||
eventsToRegister []eventRegistration
|
||||
}
|
||||
|
||||
// NewReactor returns a new, empty Reactor.
|
||||
func NewReactor() Reactor {
|
||||
var newReactor Reactor
|
||||
|
||||
newReactor.eventsToRegister = make([]eventRegistration, 0)
|
||||
|
||||
return newReactor
|
||||
}
|
||||
|
||||
// CreateServer creates a ServerConnection and returns it.
|
||||
func (r *Reactor) CreateServer(name string) ServerConnection {
|
||||
var sc ServerConnection
|
||||
|
||||
r.ServerConnections[name] = sc
|
||||
|
||||
for _, e := range r.eventsToRegister {
|
||||
sc.RegisterEvent(e.Direction, e.Name, e.Handler, e.Priority)
|
||||
}
|
||||
|
||||
return sc
|
||||
}
|
||||
|
||||
// Shutdown shuts down all ServerConnections.
|
||||
func (r *Reactor) Shutdown(message string) {
|
||||
for _, sc := range r.ServerConnections {
|
||||
sc.Shutdown(message)
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterEvent registers an event with all current and new ServerConnections.
|
||||
func (r *Reactor) RegisterEvent(direction string, name string, handler eventmgr.HandlerFn, priority int) {
|
||||
for _, sc := range r.ServerConnections {
|
||||
sc.RegisterEvent(direction, name, handler, priority)
|
||||
}
|
||||
|
||||
// for future servers
|
||||
var newEvent eventRegistration
|
||||
newEvent.Direction = direction
|
||||
newEvent.Name = name
|
||||
newEvent.Handler = handler
|
||||
newEvent.Priority = priority
|
||||
r.eventsToRegister = append(r.eventsToRegister, newEvent)
|
||||
}
|
Loading…
Reference in New Issue
Block a user