carousel

package
v0.0.0-...-dfb818a Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2021 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDisconnected = errors.New("Client disconnected.")

	ClientCommandTable = map[string]ClientCommandHook{
		irc.USER: (*Client).user,
		irc.NICK: (*Client).nick,
		irc.PASS: (*Client).pass,
		irc.QUIT: (*Client).quit,
		irc.CAP:  (*Client).cap,
	}
)
View Source
var NetworkCommandTable = map[string]NetworkCommandHook{
	irc.PING:         (*Network).ping,
	irc.JOIN:         (*Network).join,
	irc.RPL_WELCOME:  (*Network).rpl_welcome,
	irc.RPL_YOURHOST: (*Network).rpl_welcome,
	irc.RPL_CREATED:  (*Network).rpl_welcome,
	irc.RPL_MYINFO:   (*Network).rpl_welcome,
	irc.RPL_BOUNCE:   (*Network).rpl_welcome,
	irc.RPL_NAMREPLY: (*Network).rpl_namreply,
}

NetworkCommandTable maps IRC to commands to hooks. Whenever the client receives a message, it looks up the command in the CommandTable and runs the corresponding function.

Functions

This section is empty.

Types

type Channel

type Channel struct {
	Name  string   `json:",omitempty"`
	Nicks []string `json:",omitempty"`
}

func NewChannel

func NewChannel(name string) (*Channel, error)

func (*Channel) AddNick

func (c *Channel) AddNick(nick string)

AddNick adds a given nick to the channel.

func (*Channel) AddNicks

func (c *Channel) AddNicks(nicks []string)

AddNicks adds a list of nicks to the channel.

func (*Channel) RemoveNick

func (c *Channel) RemoveNick(nick string)

RemoveNick removes a given nick form the channel.

type Client

type Client struct {
	Connection net.Conn          `json:",omitempty"`
	Buffer     chan *irc.Message `json:",omitempty"`
	Ident      *Identity         `json:",omitempty"`

	Encoder *irc.Encoder  `json:",omitempty"`
	Decoder *irc.Decoder  `json:",omitempty"`
	Reader  *bufio.Reader `json:",omitempty"`
}

Client represents the actual Connection between the User and the Server. Unlike Network which represents both the Connection and metadata, Client is seperate from User so it can run independetly before identifying itself.

func NewClient

func NewClient(config ClientConfig) (*Client, error)

New takes in a ClientConfig and returns a new Client object.

func (*Client) BatchSend

func (c *Client) BatchSend(messages []*irc.Message) error

func (*Client) Disconnect

func (c *Client) Disconnect()

Disconnect closes the client connection.

func (*Client) Heartbeat

func (c *Client) Heartbeat(ctx context.Context) error

heartbeat sends a ping message every 30 seonds to the client. It takes a done channel as a replacement to context. TODO: Close the `done` channel if the client doesn't reply with a PONG.

func (*Client) Listen

func (c *Client) Listen(ctx context.Context) error

Listen reads, sanitizes, and forwards all messages sent from the Client directed towards the Network. In its current state, this blocking process should only exit if...

  • the reader throws an error
  • the Client disconnects

func (*Client) LogEntry

func (c *Client) LogEntry() *log.Entry

func (*Client) LogFields

func (c *Client) LogFields() log.Fields

func (*Client) MaybeRun

func (c *Client) MaybeRun(msg *irc.Message) (bool, error)

func (*Client) Ping

func (c *Client) Ping(nickname string)

Ping sends a simple PING message to the client. See RFC 2812 § 3.7.2 for more details.

func (*Client) Receive

func (c *Client) Receive() (*irc.Message, error)

func (*Client) Send

func (c *Client) Send(msg *irc.Message) error

type ClientCommandHook

type ClientCommandHook func(c *Client, msg *irc.Message) (bool, error)

type ClientConfig

type ClientConfig struct {
	Connection net.Conn
}

type Connection

type Connection interface {
	Send(msg *irc.Message) error
	Receive() (*irc.Message, error)
	BatchSend(message []*irc.Message) error
}

type HTTPConfig

type HTTPConfig struct {
	Advertise string
}

type HTTPHandler

type HTTPHandler func(resp http.ResponseWriter, req *http.Request)

type HTTPServer

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

func NewHTTPServer

func NewHTTPServer(s *Server, c *HTTPConfig) (*HTTPServer, error)

NewHTTPServer configures a new multiplexer and listener and returns the running HTTPServer.

type HTTPWrapper

type HTTPWrapper func(resp http.ResponseWriter, req *http.Request) (interface{}, error)

type Identity

type Identity struct {
	Username string `json:"username"`
	Nickname string `json:"nickname"`
	Realname string `json:"realname"`
	Password string `json:"password"`
}

Identity represnts the necessary information to authenticate with a Network. See RFC 2812 § 3.1

func (*Identity) CanAuthenticate

func (i *Identity) CanAuthenticate() bool

func (*Identity) HasNetwork

func (i *Identity) HasNetwork() bool

HasNetwork returns true if the username is username/network pair.

func (*Identity) ParsedNetwork

func (i *Identity) ParsedNetwork() string

ParsedNetwork returns the string following a '/' in the username. This method is naive and assumes that a username will only contain a single slash, and the substring following the delimeter is the desired network.

func (*Identity) ParsedUsername

func (i *Identity) ParsedUsername() string

ParsedUsername returns the string preceding a '/' in the username. This method is naive and assumes the desired username is the first substring after splitting by a forward slash delimeter. This method should be used instead of accessing the field directly.

func (*Identity) Wait

func (i *Identity) Wait(duration time.Duration) error

Wait returns if the identity is populated (has a username and password). If the ident cannot be authenticated by some given timeout duration, it returns an error.

type Network

type Network struct {
	Config   *NetworkConfig
	Name     string     `json:"name"`
	URI      string     `json:"uri"`
	Ident    *Identity  `json:"ident,omitempty"`
	Channels []*Channel `json:",omitempty"`

	Buffer        chan *irc.Message `json:"-,omitempty"`
	Connection    *irc.Conn         `json:"-,omitempty"`
	ClientReplies []*irc.Message    `json:"-,omitempty"`
}

Network represents an IRC network. Each network has a URI, and, because Users own the Network object, each Network stores the User's Identity as well.

func NewNetwork

func NewNetwork(config *NetworkConfig) (*Network, error)

New takes in a Network Config and returns a new Network object. In this case, all configs are manadatory, but, in its current state, New doesn't throw any errors.

func (*Network) BatchSend

func (n *Network) BatchSend(messages []*irc.Message) error

func (*Network) Listen

func (n *Network) Listen() error

Listen attempts to connect and listen if the Network isn't already connected. If the Network's Connection is nil, Listen reads parses, and forwards all messages from the Network to the Client. In it's current state, this blocking function should exit if the Network encounters an error when receiving messages.

func (*Network) LogEntry

func (n *Network) LogEntry() *log.Entry

func (*Network) MaybeRun

func (n *Network) MaybeRun(msg *irc.Message) (bool, error)

CommandTable maps IRC to commands to hooks. Whenever the network receives a message, it looks up the command in the CommandTable and runs the corresponding function.

func (*Network) Receive

func (n *Network) Receive() (*irc.Message, error)

func (*Network) Send

func (n *Network) Send(msg *irc.Message) error

type NetworkCommandHook

type NetworkCommandHook func(n *Network, msg *irc.Message) (bool, error)

type NetworkConfig

type NetworkConfig struct {
	Name  string
	URI   string
	Ident *Identity
}

type NetworkUpdateRequest

type NetworkUpdateRequest struct {
	User    string
	Network *Network
}

type NetworkUpdateResponse

type NetworkUpdateResponse struct {
	Name string
}

type Router

type Router struct {
	ServerURI string
	Client    *Client
	Network   *Network
}

func (*Router) Route

func (r *Router) Route(ctx context.Context) error

Route passes messages from the given Network buffer to the Client buffer, and visa versa. Route also calls heartbeat to periodically ping the Client's Connection. If the Client doesn' respond to the Ping or encounters an error when sending to either the Client or Network, Route returns.

type Server

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

Server is the configuration for all of Carousel. It maintains a list of all Users, as well general server information (ie. URI).

func NewServer

func NewServer(config *ServerConfig) (*Server, error)

func (Server) GetUser

func (s Server) GetUser(username string) (*User, error)

getUser searches the server's users and retrieves the user matching the given username. It returns an error if the user does not exist. This function is only a helper until a better User storage solution is implemented.

func (*Server) Serve

func (s *Server) Serve()

Serve attaches a tcp listener to the specificed URI, and starts the main event loop. Serve blocks for the lifetime of the parent process and should only return if the TCP listener closes or errors (even if there are no active connections).

type ServerConfig

type ServerConfig struct {
	URI             string
	SSLEnabled      bool
	CertificatePath string
}

type User

type User struct {
	Config   *UserConfig
	Username string     `json:"username"`
	Password string     `json:"password"`
	Networks []*Network `json:"network,omitempty"`
	Client   *Client    `json:",omitempty"`
}

User represents the individual Users's account and network config. Currently, a user can only connect to a single network, and each user owns their own router to pass messages.

func NewUser

func NewUser(config *UserConfig) (*User, error)

func (*User) Authorize

func (u *User) Authorize(ident Identity) error

Authorized compares the given password with the password hash stored in the config. The user's password isn't stored in plaintext (for very obvious reasons, so we have to hash and salt the supplied password before comparing)

func (*User) Network

func (u *User) Network(name string) *Network

Network returns the network object with matching name, and returns nil if no match is found.

func (*User) NetworkOrDefault

func (u *User) NetworkOrDefault(name string) (n *Network)

NetworkOrDefault, like Network, returns the network with matching name or returns nil if no match if found. If the given string is empty, however, it returns the default network (for now, the default is the first in a non-empty network list.

type UserConfig

type UserConfig struct {
	Username string
	Password string
	Networks []*Network
}

type UserUpdateRequest

type UserUpdateRequest struct {
	Username string
	Password string
}

type UserUpdateResponse

type UserUpdateResponse struct {
	Username string
}

Jump to

Keyboard shortcuts

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