ircslack

package
v0.0.0-...-03f8f56 Latest Latest
Warning

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

Go to latest
Published: May 20, 2024 License: BSD-3-Clause Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ChannelPrefixPublicChannel  = "#"
	ChannelPrefixPrivateChannel = "@"
	ChannelPrefixMpIM           = "&"
	// NOTE: a thread is not a channel type
	ChannelPrefixThread = "+"
)

Constants for public, private, and multi-party conversation prefixes. Channel threads are prefixed with "+" but they are not conversation types so they do not belong here. A thread is just a message whose destination is within another message in a public, private, or multi-party conversation.

View Source
const (
	ProjectAuthor       = "Andrea Barberio"
	ProjectAuthorEmail  = "insomniac@slackware.it"
	ProjectURL          = "https://github.com/insomniacslk/irc-slack"
	MaxSlackAPIAttempts = 3
)

Project constants

Variables

View Source
var IrcCommandHandlers = map[string]IrcCommandHandler{
	"CAP":     IrcCapHandler,
	"NICK":    IrcNickHandler,
	"USER":    IrcUserHandler,
	"PING":    IrcPingHandler,
	"PRIVMSG": IrcPrivMsgHandler,
	"QUIT":    IrcQuitHandler,
	"MODE":    IrcModeHandler,
	"PASS":    IrcPassHandler,
	"WHOIS":   IrcWhoisHandler,
	"WHO":     IrcWhoHandler,
	"JOIN":    IrcJoinHandler,
	"PART":    IrcPartHandler,
	"TOPIC":   IrcTopicHandler,
	"NAMES":   IrcNamesHandler,
}

IrcCommandHandlers maps each IRC command to its handler function

View Source
var IrcNumericsSafeToChunk = []int{

	352,

	353,
}

IrcNumericsSafeToChunk is a list of IRC numeric replies that are safe to chunk. As per RFC2182, the maximum message size is 512, including newlines. Sending longer lines breaks some clients like ZNC. See https://github.com/insomniacslk/irc-slack/issues/38 for background. This list is meant to grow if we find more IRC numerics that are safe to split. Being safe to split doesn't mean that it *will* be split. The actual behaviour depends on the IrcContext.ChunkSize value.

View Source
var (
	UserContexts = map[net.Addr]*IrcContext{}
)

Maps of user contexts and nicknames

Functions

func ChannelMembers

func ChannelMembers(ctx *IrcContext, channelID string) ([]slack.User, error)

ChannelMembers returns a list of users in the given conversation.

func ExpandText

func ExpandText(text string) string

ExpandText expands and unquotes text and URLs from Slack's messages. Slack quotes the text and URLS, and the latter are enclosed in < and >. It also translates potential URLs into actual URLs (e.g. when you type "example.com"), so you will get something like <http://example.com|example.com>. This function tries to detect them and unquote and expand them for a better visualization on IRC.

func HasChannelPrefix

func HasChannelPrefix(name string) bool

HasChannelPrefix returns true if the channel name starts with one of the supproted channel prefixes.

func IrcAfterLoggingIn

func IrcAfterLoggingIn(ctx *IrcContext, rtm *slack.RTM) error

IrcAfterLoggingIn is called once the user has successfully logged on IRC

func IrcCapHandler

func IrcCapHandler(ctx *IrcContext, prefix, cmd string, args []string, trailing string)

IrcCapHandler is called when a CAP command is sent

func IrcJoinHandler

func IrcJoinHandler(ctx *IrcContext, prefix, cmd string, args []string, trailing string)

IrcJoinHandler is called when a JOIN command is sent

func IrcModeHandler

func IrcModeHandler(ctx *IrcContext, prefix, cmd string, args []string, trailing string)

IrcModeHandler is called when a MODE command is sent

func IrcNamesHandler

func IrcNamesHandler(ctx *IrcContext, prefix, cmd string, args []string, trailing string)

IrcNamesHandler is called when a NAMES command is sent

func IrcNickHandler

func IrcNickHandler(ctx *IrcContext, prefix, cmd string, args []string, trailing string)

IrcNickHandler is called when a NICK command is sent

func IrcPartHandler

func IrcPartHandler(ctx *IrcContext, prefix, cmd string, args []string, trailing string)

IrcPartHandler is called when a PART command is sent

func IrcPassHandler

func IrcPassHandler(ctx *IrcContext, prefix, cmd string, args []string, trailing string)

IrcPassHandler is called when a PASS command is sent

func IrcPingHandler

func IrcPingHandler(ctx *IrcContext, prefix, cmd string, args []string, trailing string)

IrcPingHandler is called when a PING command is sent

func IrcPrivMsgHandler

func IrcPrivMsgHandler(ctx *IrcContext, prefix, cmd string, args []string, trailing string)

IrcPrivMsgHandler is called when a PRIVMSG command is sent

func IrcQuitHandler

func IrcQuitHandler(ctx *IrcContext, prefix, cmd string, args []string, trailing string)

IrcQuitHandler is called when a QUIT command is sent

func IrcSendChanInfoAfterJoin

func IrcSendChanInfoAfterJoin(ctx *IrcContext, ch *Channel, members []slack.User)

IrcSendChanInfoAfterJoin sends channel information to the user about a joined channel.

func IrcSendChanInfoAfterJoinCustom

func IrcSendChanInfoAfterJoinCustom(ctx *IrcContext, chanName, chanID, topic string, members []slack.User)

IrcSendChanInfoAfterJoinCustom sends channel information to the user about a joined channel. It can be used as an alternative to IrcSendChanInfoAfterJoin when you need to specify custom chan name, id, and topic.

func IrcTopicHandler

func IrcTopicHandler(ctx *IrcContext, prefix, cmd string, args []string, trailing string)

IrcTopicHandler is called when a TOPIC command is sent

func IrcUserHandler

func IrcUserHandler(ctx *IrcContext, prefix, cmd string, args []string, trailing string)

IrcUserHandler is called when a USER command is sent

func IrcWhoHandler

func IrcWhoHandler(ctx *IrcContext, prefix, cmd string, args []string, trailing string)

IrcWhoHandler is called when a WHO command is sent

func IrcWhoisHandler

func IrcWhoisHandler(ctx *IrcContext, prefix, cmd string, args []string, trailing string)

IrcWhoisHandler is called when a WHOIS command is sent

func SendIrcNumeric

func SendIrcNumeric(ctx *IrcContext, code int, args, desc string) error

SendIrcNumeric sends a numeric code message to the recipient

func SplitReply

func SplitReply(preamble, msg string, chunksize int) []string

SplitReply will split a reply message if necessary. See IrcNumericSafeToChunk for background on why splitting. The function will return a list of chunks to be sent separately. The first argument is the entire message to be split. The second argument is the chunk size to use to determine whether the message should be split. Any value equal or above 512 will cause splitting. Any other value will return the unmodified string as only item of the list.

func StripChannelPrefix

func StripChannelPrefix(name string) string

StripChannelPrefix returns a channel name without its channel prefix. If no channel prefix is present, the string is returned unchanged.

func SupportedChannelPrefixes

func SupportedChannelPrefixes() []string

SupportedChannelPrefixes returns a list of supported channel prefixes.

func WordWrap

func WordWrap(allWords []string, maxLen int) []string

WordWrap wraps the given words up to the maximum specified length. If a single word is longer than the max length, it is truncated.

Types

type Channel

type Channel slack.Channel

Channel wraps a Slack conversation with a few utility functions.

func (*Channel) IRCName

func (c *Channel) IRCName() string

IRCName returns the channel name as it would appear on IRC. Examples: * #channel for public groups * @channel for private groups * &Gxxxx|nick1-nick2-nick3 for multi-party IMs

func (*Channel) IsMP

func (c *Channel) IsMP() bool

IsMP returns true if it is a multi-party conversation.

func (*Channel) IsPrivateChannel

func (c *Channel) IsPrivateChannel() bool

IsPrivateChannel returns true if the channel is private.

func (*Channel) IsPublicChannel

func (c *Channel) IsPublicChannel() bool

IsPublicChannel returns true if the channel is public.

func (*Channel) SlackName

func (c *Channel) SlackName() string

SlackName returns the slack.Channel.Name field.

type Channels

type Channels struct {
	Pagination int
	// contains filtered or unexported fields
}

Channels wraps the channel list with convenient operations and cache.

func NewChannels

func NewChannels(pagination int) *Channels

NewChannels creates a new Channels object.

func (*Channels) AsMap

func (c *Channels) AsMap() map[string]Channel

AsMap returns the channels as a map of name -> channel. The map is copied to avoid data races

func (*Channels) ByID

func (c *Channels) ByID(id string) *Channel

ByID retrieves a channel by its Slack ID.

func (*Channels) ByName

func (c *Channels) ByName(name string) *Channel

ByName retrieves a channel by its Slack or IRC name.

func (*Channels) Count

func (c *Channels) Count() int

Count returns the number of channels. This method must be called after `Fetch`.

func (*Channels) Fetch

func (c *Channels) Fetch(client *slack.Client) error

Fetch retrieves all the channels on a given Slack team. The Slack client has to be valid and connected.

func (*Channels) FetchByIDs

func (c *Channels) FetchByIDs(client *slack.Client, skipCache bool, channelIDs ...string) ([]Channel, error)

FetchByIDs fetches the channels with the specified IDs and updates the internal channel mapping.

type FileHandler

type FileHandler struct {
	SlackAPIKey          string
	FileDownloadLocation string
	ProxyPrefix          string
}

FileHandler downloads files from slack

func (*FileHandler) Download

func (handler *FileHandler) Download(file slack.File) string

Download downloads url contents to a local file and returns a url to either the file on slack's server or a downloaded file

type IrcCommandHandler

type IrcCommandHandler func(*IrcContext, string, string, []string, string)

IrcCommandHandler is the prototype that every IRC command handler has to implement

type IrcContext

type IrcContext struct {
	Conn net.Conn
	User *slack.User
	// TODO make RealName a function
	RealName       string
	OrigName       string
	SlackClient    *slack.Client
	SlackRTM       *slack.RTM
	SlackAPIKey    string
	SlackDebug     bool
	SlackConnected bool
	ServerName     string
	Channels       *Channels
	Users          *Users
	ChunkSize      int

	FileHandler *FileHandler
	// contains filtered or unexported fields
}

IrcContext holds the client context information

func (*IrcContext) ExpandUserIds

func (ic *IrcContext) ExpandUserIds(text string) string

ExpandUserIds will convert slack user tags with user's nicknames

func (IrcContext) GetConversationInfo

func (ic IrcContext) GetConversationInfo(conversation string) (*slack.Channel, error)

GetConversationInfo is cached version of slack.GetConversationInfo

func (*IrcContext) GetThreadOpener

func (ic *IrcContext) GetThreadOpener(channel string, threadTimestamp string) (slack.Message, error)

GetThreadOpener returns text of the first message in a thread that provided message belongs to

func (*IrcContext) GetUserInfo

func (ic *IrcContext) GetUserInfo(userID string) *slack.User

GetUserInfo returns a slack.User instance from a given user ID, or nil if no user with that ID was found

func (*IrcContext) GetUserInfoByName

func (ic *IrcContext) GetUserInfoByName(username string) *slack.User

GetUserInfoByName returns a slack.User instance from a given user name, or nil if no user with that name was found

func (IrcContext) Mask

func (ic IrcContext) Mask() string

Mask returns the IRC mask for the current user

func (*IrcContext) Nick

func (ic *IrcContext) Nick() string

Nick returns the nickname of the user, if known

func (*IrcContext) PostTextMessage

func (ic *IrcContext) PostTextMessage(target, text, targetTs string)

PostTextMessage batches all messages that should be posted to slack

func (*IrcContext) SendUnknownError

func (ic *IrcContext) SendUnknownError(fmtstr string, args ...interface{})

SendUnknownError sends an IRC 400 (ERR_UNKNOWNERROR) message to the client and prints a warning about it.

func (*IrcContext) Start

func (ic *IrcContext) Start()

Start handles batching of messages to slack

func (IrcContext) UserID

func (ic IrcContext) UserID() string

UserID returns the user's Slack ID

func (*IrcContext) UserName

func (ic *IrcContext) UserName() string

UserName returns the user's name. Currently this is equivalent to the user's Slack ID

type Server

type Server struct {
	Name                 string
	LocalAddr            net.Addr
	Listener             net.Listener
	SlackAPIKey          string
	SlackDebug           bool
	ChunkSize            int
	FileDownloadLocation string
	FileProxyPrefix      string
	Pagination           int
	TLSConfig            *tls.Config
}

Server is the server object that exposes the Slack API with an IRC interface.

func (*Server) HandleMsg

func (s *Server) HandleMsg(conn net.Conn, msg string)

HandleMsg handles raw IRC messages

func (Server) HandleRequest

func (s Server) HandleRequest(conn net.Conn)

HandleRequest handle IRC client connections

func (Server) Start

func (s Server) Start() error

Start runs the IRC server

type SlackPostMessage

type SlackPostMessage struct {
	Target   string
	TargetTs string
	Text     string
}

SlackPostMessage represents a message sent to slack api

type Users

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

Users wraps the user list with convenient operations and cache.

func NewUsers

func NewUsers(pagination int) *Users

NewUsers creates a new Users object.

func (*Users) ByID

func (u *Users) ByID(id string) *slack.User

ByID retrieves a user by its Slack ID.

func (*Users) ByName

func (u *Users) ByName(name string) *slack.User

ByName retrieves a user by its Slack name.

func (*Users) Count

func (u *Users) Count() int

Count returns the number of users. This method must be called after `Fetch`.

func (*Users) Fetch

func (u *Users) Fetch(client *slack.Client) ([]slack.User, error)

Fetch retrieves all the users on a given Slack team. The Slack client has to be valid and connected.

func (*Users) FetchByIDs

func (u *Users) FetchByIDs(client *slack.Client, skipCache bool, userIDs ...string) ([]slack.User, error)

FetchByIDs fetches the users with the specified IDs and updates the internal user mapping.

func (*Users) IDsToNames

func (u *Users) IDsToNames(userIDs ...string) []string

IDsToNames returns a list of user names from the given IDs. The returned list could be shorter if there are invalid user IDs. Warning: this method is probably only useful for NAMES commands where a non-exact mapping is acceptable.

Jump to

Keyboard shortcuts

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