hbot

package
v0.0.0-...-6d08d74 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2021 License: GPL-2.0 Imports: 11 Imported by: 1

Documentation

Overview

Package hbot is IRCv3 enabled framework for writing IRC bots

Index

Examples

Constants

View Source
const CapAccountNotify = "account-notify"

CapAccountNotify is account-notify CAP

View Source
const CapAccountTag = "account-tag"

CapAccountTag is account-tag CAP

View Source
const CapAwayNotify = "away-notify"

CapAwayNotify is away-notify CAP

View Source
const CapCapNotify = "cap-notify"

CapCapNotify is cap-notify CAP

View Source
const CapChghost = "chghost"

CapChghost is chghost CAP

View Source
const CapExtendedJoin = "extended-join"

CapExtendedJoin is extended-join CAP

View Source
const CapIdentifyMsg = "identify-msg"

CapIdentifyMsg is identify-msg CAP

View Source
const CapInviteNotify = "invite-notify"

CapInviteNotify is invite-notify CAP

View Source
const CapMessageTags = "message-tags"

CapMessageTags is message-tags CAP

View Source
const CapMultiPrefix = "multi-prefix"

CapMultiPrefix is multi-prefix CAP

View Source
const CapSASL = "sasl"

CapSASL is SASL CAP

View Source
const CapServerTime = "server-time"

CapServerTime is server-time CAP

View Source
const CapSetName = "setname"

CapSetName is setname CAP

View Source
const CapTLS = "tls"

CapTLS is tls CAP

View Source
const CapUserhostInNames = "userhost-in-names"

CapUserhostInNames is userhost-in-names CAP

View Source
const CommonBotUserPrefix = "botsan-"

CommonBotUserPrefix may be optionally used as a user prefix or realname to identify as a bot. e.g. `/mode #CHANNEL +q $~x:*!~botsan-*@*` or `/mode #CHANNEL +q $~r:botsan-*`.

Variables

This section is empty.

Functions

func DiscardLogf

func DiscardLogf(format string, args ...interface{})

Types

type Bot

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

Bot implements an irc bot to be connected to a given server

func NewBot

func NewBot(conf *Config) *Bot

NewBot creates a new instance of Bot

func (*Bot) Action

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

Action sends an action to 'who' (user or channel)

func (*Bot) AddTrigger

func (bot *Bot) AddTrigger(h Handler)

AddTrigger adds a trigger to the bot's handlers

func (*Bot) CapStatus

func (bot *Bot) CapStatus(cap string) (enabled, present bool)

CapStatus returns whether the server capability is enabled and present

func (*Bot) ChMode

func (bot *Bot) ChMode(user, channel, mode string)

ChMode is used to change users modes in a channel operator = "+o" deop = "-o" ban = "+b"

func (*Bot) Close

func (bot *Bot) Close()

Close closes the bot

func (*Bot) Join

func (bot *Bot) Join(ch string)

Join a channel

func (*Bot) Joined

func (bot *Bot) Joined() <-chan struct{}

Joined returns a channel that closes after channels are joined

func (*Bot) Msg

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

Msg sends a message to 'who' (user or channel)

func (*Bot) MsgMaxSize

func (bot *Bot) MsgMaxSize(who string) int

MsgMaxSize returns maximum number of bytes that fit into one message. Useful, for example, if you want to generate a wall of emojis that fit into one message, or you want to cap some output to one message

func (*Bot) Nick

func (bot *Bot) Nick() string

Nick returns the bot's nick.

func (*Bot) Notice

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

Notice sends a NOTICE message to 'who' (user or channel)

func (*Bot) NoticeMaxSize

func (bot *Bot) NoticeMaxSize(who string) int

NoticeMaxSize returns maximum number of bytes that fit into one message. Useful, for example, if you want to generate a wall of emojis that fit into one message, or you want to cap some output to one message

func (*Bot) Part

func (bot *Bot) Part(ch, msg string)

Part a channel

func (*Bot) Prefix

func (bot *Bot) Prefix() Prefix

Prefix returns the bot's prefix.

func (*Bot) Reply

func (bot *Bot) Reply(m *Message, text string)

Reply sends a message to where the message came from (user or channel)

func (*Bot) ReplyMaxSize

func (bot *Bot) ReplyMaxSize(m *Message) int

ReplyMaxSize is just like MsgMaxSize but calculates message size for the reply target

func (*Bot) Run

func (bot *Bot) Run()

Run starts the bot and connects to the server. Blocks until we disconnect from the server.

func (*Bot) Send

func (bot *Bot) Send(command string)

Send any command to the server

func (*Bot) SetNick

func (bot *Bot) SetNick(nick string)

SetNick sets the bots nick on the irc server. This does not alter *Bot.Nick, so be vary of that

func (*Bot) Topic

func (bot *Bot) Topic(c, topic string)

Topic sets the channel 'c' topic (requires bot has proper permissions)

type Config

type Config struct {
	Host            string
	Nick            string
	Realname        string
	User            string
	Password        string
	Channels        []string
	SASL            bool
	MsgSafetyBuffer bool                                         // Set it if long messages get truncated on the receiving end
	Dial            func(network, addr string) (net.Conn, error) // An optional custom function for connecting to the server
	ThrottleDelay   time.Duration                                // Duration to wait between sending of messages to avoid being kicked by the server for flooding (default 200ms)
	PingTimeout     time.Duration                                // Maximum time between incoming data
	UseTLS          TLSKnob
	TLSConfig       tls.Config
	Logger          Logger
}

type Handler

type Handler interface {
	Handle(*Bot, *Message)
}

Handler is used to subscribe and react to events on the bot Server

type Logger

type Logger struct {
	Verbosef func(format string, args ...interface{})
	Errorf   func(format string, args ...interface{})
}

type Message

type Message struct {
	Tags
	Prefix
	Command string
	Params  []string
}

Message represents an IRC protocol message. See RFC1459 section 2.3.1.

<message>  ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
<prefix>   ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
<command>  ::= <letter> { <letter> } | <number> <number> <number>
<SPACE>    ::= ' ' { ' ' }
<params>   ::= <SPACE> [ ':' <trailing> | <middle> <params> ]

<middle>   ::= <Any *non-empty* sequence of octets not including SPACE
               or NUL or CR or LF, the first of which may not be ':'>
<trailing> ::= <Any, possibly *empty*, sequence of octets not including
               NUL or CR or LF>

<crlf>     ::= CR LF

func ParseMessage

func ParseMessage(raw string) (m *Message)

ParseMessage takes a string and attempts to create a Message struct. Returns nil if the Message is invalid.

Example
message := ParseMessage("JOIN #help")

fmt.Println(message.Params[0])
Output:

#help

func (*Message) Bytes

func (m *Message) Bytes() []byte

Bytes returns a []byte representation of this message.

As noted in rfc2812 section 2.3, messages should not exceed 512 characters in length. This method forces that limit by discarding any characters exceeding the length limit.

func (*Message) Len

func (m *Message) Len() (length int)

Len calculates the length of the string representation of this message.

func (*Message) Param

func (m *Message) Param(i int) string

Param returns the i'th parameter. Returns the empty string if the requested parameter does not exist.

func (*Message) String

func (m *Message) String() string

String returns a string representation of this message.

As noted in rfc2812 section 2.3, messages should not exceed 512 characters in length. This method forces that limit by discarding any characters exceeding the length limit.

Example
message := &Message{
	Prefix: Prefix{
		Name: "sorcix",
		User: "sorcix",
		Host: "myhostname",
	},
	Command: "PRIVMSG",
	Params:  []string{"This is an example!"},
}

fmt.Println(message.String())
Output:

:sorcix!sorcix@myhostname PRIVMSG :This is an example!

func (*Message) Trailing

func (m *Message) Trailing() string

Trailing returns the last parameter. Returns the empty string if there are no parameters.

type Prefix

type Prefix struct {
	Name string // Nick- or servername
	User string // Username
	Host string // Hostname
}

Prefix represents the prefix (sender) of an IRC message. See RFC1459 section 2.3.1.

<servername> | <nick> [ '!' <user> ] [ '@' <host> ]

func ParsePrefix

func ParsePrefix(raw string) (p Prefix)

ParsePrefix takes a string and attempts to create a Prefix struct.

func (*Prefix) Bytes

func (p *Prefix) Bytes() []byte

Bytes returns a []byte representation of this prefix.

func (*Prefix) Empty

func (p *Prefix) Empty() bool

Empty determines whether the prefix is empty.

func (*Prefix) IsHostmask

func (p *Prefix) IsHostmask() bool

IsHostmask returns true if this prefix looks like a user hostmask.

func (*Prefix) IsServer

func (p *Prefix) IsServer() bool

IsServer returns true if this prefix looks like a server name.

func (*Prefix) Len

func (p *Prefix) Len() (length int)

Len calculates the length of the string representation of this prefix.

func (*Prefix) String

func (p *Prefix) String() (s string)

String returns a string representation of this prefix.

type TLSKnob

type TLSKnob int
const (
	AutoTLS TLSKnob = iota
	YesTLS
	NoTLS
)

type Tags

type Tags map[string]string

Tags represents (optional) tags added to the start of each message See IRCv3.2 Message Tags (http://ircv3.net/specs/core/message-tags-3.2.html)

<message> ::= ['@' <tags> <SPACE>] [':' <prefix> <SPACE> ] <command> <params> <crlf> <tags> ::= <tag> [';' <tag>]* <tag> ::= <key> ['=' <escaped value>] <key> ::= [ <vendor> '/' ] <sequence of letters, digits, hyphens (`-`)> <escaped value> ::= <sequence of any characters except NUL, CR, LF, semicolon (`;`) and SPACE> <vendor> ::= <host>

func ParseTags

func ParseTags(raw string) (t Tags)

ParseTags takes a string and attempts to create a Tags struct

func (Tags) Bytes

func (t Tags) Bytes() []byte

Bytes returns the []byte representation of this collection of message tags

func (Tags) GetTag

func (t Tags) GetTag(key string) (string, bool)

GetTag checks whether a tag with the given key exists. The boolean return value indicates whether a value was found

func (Tags) String

func (t Tags) String() (s string)

String returns the string representation of all set message tags

type Trigger

type Trigger struct {
	// Returns true if this trigger applies to the passed in message
	Condition func(*Bot, *Message) bool

	// The action to perform if Condition is true
	Action func(*Bot, *Message)
}

Trigger is a Handler which is guarded by a condition. DO NOT alter *Message in your triggers or you'll have strange things happen.

func (Trigger) Handle

func (t Trigger) Handle(bot *Bot, m *Message)

Handle executes the trigger action if the condition is satisfied

Jump to

Keyboard shortcuts

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