chatsrv

package module
v0.0.0-...-54e4640 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2018 License: MIT Imports: 14 Imported by: 0

README

This is a simple chat server written in Go. It is a work in progress, and is mainly meant as a toy to help me learn Go.

To use:

  • go get github.com/n0ot/chatsrv
  • go install github.com/n0ot/chatsrv/chatsrv_cmd
  • Create a directory ".chatsrv" in your home directory, and copy chatsrv_cmd/example.conf to $HOME/.chatsrv/conf; modify it to your liking.
  • Create a file in $HOME/.chatsrv, called motd, with any text you want to be displayed when users connect.
  • Run chatsrv_cmd

To connect, use netcat:

nc localhost 36362

If you want to use tls, set useTls = true in the configuration, and point certFile and keyFile at your certificate and private key. Ncat is an improved version of netcat that supports ssl. To connect to a host with ssl, use

ncat --ssl localhost 36362

In order to prevent man-in-the-middle attacks, the certificate should be verified. If your certificate was signed by a trusted CA, you can just run

ncat --ssl-verify chatsrv.example.com 36362

Otherwise, you'll need to give the certificate (not the private key) to people who want to connect, and they'll need to type

ncat --ssl-verify --ssl-trustfile yourcert.pem chatsrv.example.com 36362

When chatting with ncat, input text and messages from the server can get mingled, and you will see your message twice--the copy echoed from your keyboard, and the response from the server. This renders ncat almost useless for actual conversations. If you want a better experience, get a mud client. Chatsrv has been tested with MUSHclient (requires stunnel or an ncat pipe for TLS) and TinyFugue.

Commands are:

  • /users: Says who's on the server
  • /rooms: Lists the rooms on the server
  • /whois [nick]: Displays information about a user; displays information about yourself if nick is omitted.
  • /create <roomname> [<topic> [<roompass>]]: Create a room. If roompass is set, the room will be private until it is destroyed. Rooms are destroyed when everyone leaves.
  • /join <room> [<roompass>]: Joins a room. Use roompass if the room is private.
  • /leave [<reason>]: Leaves a room.
  • /nick <NewNick>: Changes your nick
  • /me <action>: Emotes an action; try /me sits down
  • /quit: Quit from the server.

Documentation

Overview

Package chatsrv implements a simple chat server.

Index

Constants

View Source
const (
	InputModeLines = InputMode(iota)
	InputModeBytes
	InputModeRunes
)
View Source
const SendBuffSize = 10 // Buffer size of channel for sending data to clients

Variables

This section is empty.

Functions

func NewServer

func NewServer(config *ServerConfig) *server

NewServer creates a new server with the specified configuration

Types

type Client

type Client struct {
	Send chan []byte // Bytes sent here will be written to the client
	Recv chan []byte // Bytes received by client will be sent here.
	// contains filtered or unexported fields
}

Represents a client on the server. It is the ClientHandler's responsibility to make sure nothing sends on client.Send after the client is stopped, as client.Send will be closed.

func NewClient

func NewClient(rw io.ReadWriteCloser, inputMode InputMode, clientHandler ClientHandler) (*Client, error)

NewClient initializes a new client, and starts the initial client handler. When the initial ClientHandler stops, the client will be disconnected.

func (*Client) FriendlyName

func (client *Client) FriendlyName() string

FriendlyName Gets the client's friendly name. This is how the client will be identified when converted to a string.

func (*Client) GetVar

func (client *Client) GetVar(varName string) interface{}

GetVar gets a client's custom variable. This method is thread safe.

func (*Client) InputMode

func (client *Client) InputMode() InputMode

func (*Client) SetFriendlyName

func (client *Client) SetFriendlyName(friendlyName string)

SetFriendlyName specifies how the client should identify itself when being converted to a string. This replaces the default, which is the client's UUID. Set this to "" to restore the default.

func (*Client) SetInputMode

func (client *Client) SetInputMode(inputMode InputMode) error

func (*Client) SetVar

func (client *Client) SetVar(varName string, value interface{})

SetVar sets a client's custom variable. This method is thread safe.

func (*Client) Stop

func (client *Client) Stop(reason string)

Stop stops a client, closing it's ReadWriteCloser. Stop is idempotent; calling Stop more than once will have no effect. The send channel will not be closed until the initial ClientHandler returns.

func (*Client) Stopped

func (client *Client) Stopped() bool

Stopped returns true if the client was stopped.

func (*Client) StoppedReason

func (client *Client) StoppedReason() string

StoppedReason returns the reason the client was stopped. Returns "" if the client is still running, or if no reason was set.

func (*Client) String

func (client *Client) String() string

func (*Client) UnsetVar

func (client *Client) UnsetVar(varName string)

UnsetVar unsets a variable This method is thread safe.

func (*Client) Uuid

func (client *Client) Uuid() uuid.UUID

func (*Client) VarExists

func (client *Client) VarExists(varName string) bool

VarExists checks to see if a variable has been set This method is thread safe.

type ClientHandler

type ClientHandler interface {
	Handle(*Client) string
}

ClientHandler provides the server's end of the conversation with a client. For example, it might echo text they type back to them, or patch them into a chat.

ClientHandlers can call other ClientHandlers, but will be blocked until the called handler returns. Receive from client.Recv, and respond to client.Send. When client.Stop(reason string) is called, client.Send is closed. It is therefore a good idea to make sure client.Stopped() is false when regaining control from a called ClientHandler to prevent panics.

type ClientHandlerFunc

type ClientHandlerFunc func(*Client) string

ClientHandlerFunc is an adapter to use an ordinary function as a ClientHandler. If f is a func(*Client) string, ClientHandlerFunc(f) is a ClientHandler whose Handle() method calls f.

func (ClientHandlerFunc) Handle

func (f ClientHandlerFunc) Handle(client *Client) string

Handle calls f(client)

type InputMode

type InputMode byte

type ServerConfig

type ServerConfig struct {
	ServerName          string
	BindAddr            string
	CertFile            string
	KeyFile             string
	UseTls              bool
	Motd                string
	MessageLineLimit    int
	MessagePasteTimeout time.Duration
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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