ircmsg

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2020 License: MIT Imports: 2 Imported by: 1

README

Go IRC message parser package

GoDoc

Message

The Message and Prefix types provide translation to and from IRC message format.

// Parse the IRC-encoded data and stores the result in a new struct.
message := ircmsg.ParseMessage(raw)

// Returns the IRC encoding of the message.
raw = message.String()

Warning

We are at v0.0.X, the API is subject to change

Original source

https://github.com/sorcix/irc

Documentation

Overview

Package ircmsg parses raw irc messages.

The Message and Prefix structs provide translation to and from raw IRC messages:

// Parse the IRC-encoded data and store the result in a new struct:
message := ircmsg.ParseMessage(raw)

// Translate back to a raw IRC message string:
raw = message.String()

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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) 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 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

Jump to

Keyboard shortcuts

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