gochat

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

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

Go to latest
Published: Nov 4, 2015 License: BSD-3-Clause, ISC, MIT Imports: 12 Imported by: 1

README

GoChat

Build Status GoDoc

A simple modular IRC library written in Go.

Requirements

Golang, and a working internet connection. An optional dependency for the URL Title module is the golang html package, obtained with go get golang.org/x/net/html.

#Setup A simple bot that joins a test channel then leaves can be written as such:

func main() {
    bot, err := gochat.NewBot("irc.rizon.net:6666", "go-bot", "")
    if err != nil {
        //Handle errors
    }
    c := bot.JoinChan("#test-channel")
    c.Part()
    bot.Quit()
}

In addition, if you'd like to control verbosity settings, run either gochat.LogVerbose(), gochat.LogWarn(), or gochat.LogErr() prior to creating any bots. Otherwise, the default logging level will be set to Warning.

#Default Modules These modules are built into the bot and can be easily imported and loaded with the LoadDefaultModules() function. The currently available modules are:

  • Ping-Pong: The bot says "Pong!" on a user saying ".ping"
  • URL Title Getting: The bot will display the title of a URL whenever a user types on into the chat
  • Sedding: The bot will revise your previous message when you say "s/[old phrase]/[new phrase]/"
  • Channel Joining: The bot will join a new channel on ".join [channel]". Note that this require operator status to run
  • Quoting: The bot will display a somewhat recent quote from a user on ".quote [nick]"
  • Cute Pics: The bot will provide a link to a random cute picture obtained from /c/ on ".cute"
  • Bot Report: The bot will report in on ".bots"
  • Nick Ignore: The bot will ignore or unignore a nick on ".ignore [nick]" and ".unignore [nick]" respectively
  • Say: The bot will say [msg] on ".say [msg]"

To load in the default modules you can write code as such:

import (
    //Other imports
    github.com/Luminarys/gochat/modules
)

func main() {
    //Bot initialization above
    gcModules.LoadDefaultModules(bot)
}

#Modules The core behind GoChat is the Module interface. Modules must implement a IsValid function which will check whether or not a message should be acted upon, and a ParseMessage function which will parse an input and return an output.

A simple ping-pong module can be written as such:

type PingMod struct {
}

func (p *PingMod) IsValid(msg *Message, c *Channel) bool {
    //Returns true if the message is equal to ".ping" and false otherwise
    return msg.Text == ".ping"
}

func (p *PingMod) ParseMessage(msg *Message, c *Channel) string {
    //Return "Pong!" to be printed
    return "Pong!"
}

func main() {
    //Declare bot stuff
    bot.AddModule(&{PingMod}) 
}

Now, whenever a user types ".ping" into a channel, the bot will respond with "Pong!".

#Users Gochat utilizes a permissions system based on iotas, where users have a mode of:

  • Normal
  • Voice
  • Halfop
  • Operator
  • Admin
  • Owner These should be used to compare user permissions, e.g. if you want to ensure that a user is operator or higher to execute a command, you would check that (your channel).Users[(user nick)].CMode >= gochat.Halfop

#TODO

  • Improve connection handling/irc library in general.
  • Add in more useful modules
  • Use configuration files or flags
  • Better persistence and log handling using a proper DB

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	LTrace   *log.Logger
	LWarning *log.Logger
	LError   *log.Logger
)

Functions

func LogErr

func LogErr()

func LogInit

func LogInit(
	traceHandle io.Writer,
	warningHandle io.Writer,
	errorHandle io.Writer)

func LogVerbose

func LogVerbose()

func LogWarn

func LogWarn()

Types

type Bot

type Bot struct {
	Server   string
	Nick     string
	Channels map[string]*Channel
	Modules  []Module
	Conn     *connection
}

func NewBot

func NewBot(server, nick, pass string) (*Bot, error)

Creates a new bot for a server, and returns it once it is ready

func (*Bot) AddModule

func (bot *Bot) AddModule(mod Module)

Loads a module into the bot

func (*Bot) Auth

func (bot *Bot) Auth(pass string)

Sends the password to authenticate

func (*Bot) Broadcast

func (bot *Bot) Broadcast(message string)

Broadcasts a message to all chans

func (*Bot) JoinChan

func (bot *Bot) JoinChan(chanName string) *Channel

Joins a channel, returning the channel once it is ready

func (*Bot) NewChannel

func (bot *Bot) NewChannel(channel string) *Channel

Creates and joins a new channel

func (*Bot) Notice

func (bot *Bot) Notice(who, text string)

Sends a NOTICE to a channel or user

func (*Bot) PM

func (bot *Bot) PM(who, text string)

Sends a PM to a channel or user

func (*Bot) Part

func (bot *Bot) Part(channel string)

Leaves a channel and destroys it

func (*Bot) Quit

func (bot *Bot) Quit()

Disconnects and destroys the bot

func (*Bot) Register

func (bot *Bot) Register(pass string, email string)

Registers the bot

type Channel

type Channel struct {
	Name    string
	Buffer  []*Message
	Bot     *Bot
	Ignored map[string]bool
	Ready   bool
	Users   map[string]*User
	Me      *User
	Modules []Module
}

Representation of a channel or a user query

func (*Channel) AddModule

func (c *Channel) AddModule(mod Module)

Loads a module into the chan

func (*Channel) DumpLogs

func (c *Channel) DumpLogs()

Dumps current logs into a file and wipes the Buffer

func (*Channel) HandleLogs

func (c *Channel) HandleLogs()

Dumps logs every 24 hours to ensure that the buffer doesn't get too big

func (*Channel) HandleMessage

func (c *Channel) HandleMessage(msg *Message)

Handles a message in a channel.

func (*Channel) IgnoreNick

func (c *Channel) IgnoreNick(nick string)

Ignores a nick, preferrably for a bot, but also potentially for spammers

func (*Channel) Part

func (c *Channel) Part()

Leaves a channel and destroys the channel struct

func (*Channel) Say

func (c *Channel) Say(message string)

Broadcasts a message on a channel.

func (*Channel) UnignoreNick

func (c *Channel) UnignoreNick(nick string)

Unignores a nick in the channel

func (*Channel) UpdateUsers

func (c *Channel) UpdateUsers()

type Message

type Message struct {
	Nick      string
	User      string
	Host      string
	Time      time.Time
	Cmd       string
	Text      string
	Arguments []string
}

func ParseMessage

func ParseMessage(msg string) (m *Message, err error)

Code shamelessly grabbed from go-ircevent

type Mode

type Mode int
const (
	Normal Mode = iota
	Voice
	Halfop
	Operator
	Admin
	Owner
)

Declare our modes, in order of precedence, i.e. a mode with higher value outranks one with lower value

type Module

type Module interface {
	//Takes a message and evaluates whether or not the Module should act upon it
	IsValid(msg *Message, c *Channel) bool
	//Takes a message, and returns the result. If there is no result, "" should be returned
	ParseMessage(msg *Message, c *Channel) string
}

type NullWriter

type NullWriter int

func (NullWriter) Write

func (NullWriter) Write([]byte) (int, error)

type PingResp

type PingResp struct {
}

Responds PONG to PING requests

func (*PingResp) IsValid

func (m *PingResp) IsValid(msg *Message, c *Channel) bool

func (*PingResp) ParseMessage

func (m *PingResp) ParseMessage(msg *Message, c *Channel) string

type User

type User struct {
	Nick  string
	CMode Mode
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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