irc

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2025 License: BSD-3-Clause Imports: 3 Imported by: 0

README

irc Go Reference

dead simple go module for reading and communicating with IRC server via io.Writer compatible interface.

getting started
package main

import (
	"bufio"
	"codeberg.org/Yonle/irc"
	"fmt"
	"net"
)

var botnick = "ircbot"

func main() {
	conn, err := net.Dial("tcp", "irc.example.chat:6667")
	if err != nil {
		fmt.Println(err)
		return
	}

	irc.RegisterUser(conn, "ircbot", "IRC Bot")
	irc.Nick(conn, botnick)

	b := bufio.NewScanner(conn)

	for b.Scan() {
		line := b.Text()

		fmt.Println(line)

		if irc.Pong(conn, line) {
			continue
		}

		if irc.Welcome(line) {
			irc.Join(conn, "#example", "#chat")
			continue
		}

		if irc.NicknameIsUsed(line) {
			botnick = "ircbot_"
			irc.Nick(conn, botnick)
			continue
		}

		user, cmd, flags, fullmsg := irc.ParseLine(line)

		// echo
		if cmd == "PRIVMSG" {
			to := flags[0]

			// if to is equals to bot's nick, it's a pm.
			if to == botnick {
				// redirect target to sender
				to = user.Nick
			}

			fmt.Printf("%s <%s> %s", to, user.Nick, fullmsg)
			irc.Chat(conn, flags[0], fullmsg)
		}
	}
}
documentation

press this: Go Reference

also check these out:
  • irc/commands: dead simple command handler & registerer so you could implement new commands easily in your bot.
  • irc/irctext: dead simple IRC text formatter (bold, colored, underlined, strikethrough IRC text).
  • irc/ctcp: dead simple ctcp parser, and maker.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Action added in v1.0.13

func Action(w io.Writer, to string, msg ...string)

This will send action message(s) (similar to /me <msg>) to a channel / individual. Example:

irc.Action(conn, "#chat", "Hello world", "This is the second message")
irc.Action(conn, "user", "Hey there!")
irc.Action(conn, "#chan1,#chan2,user1,user2", "Hey there!")

func Chat

func Chat(w io.Writer, to string, msg ...string)

This will send message(s) to a channel / individual. Example:

irc.Chat(conn, "#chat", "Hello world", "This is the second message")
irc.Chat(conn, "user", "Hey there!")
irc.Chat(conn, "#chan1,#chan2,user1,user2", "Hey there!")

func CutIRCPrefixMSG

func CutIRCPrefixMSG(msg string) (after string, found bool)

func Invite added in v1.0.13

func Invite(w io.Writer, channel string, nick ...string)

This will send invite to nick(s) Example:

irc.Invite(conn, "#chat", "User")
irc.Invite(conn, "#chat", "User1", "User2")

func Join

func Join(w io.Writer, channel ...string)

This will send a command to join channel(s). Example:

irc.Join(conn, "#chat", "#yonle")
irc.Join(conn, "#secret tralalala") // assuming this channel's key is "tralalala"

func Kick added in v1.0.13

func Kick(w io.Writer, channel, reason string, nick ...string)

This will kick member(s) in a channel. Example:

irc.Kick(conn, "#chat", "A reason", "User")
irc.Kick(conn, "#chat", "A reason", "User1", "User2")

func Mode added in v1.0.13

func Mode(w io.Writer, mode ...string)

This will send MODE command(s) to a channel / individual. Example:

irc.Mode(conn, "#chat +o User")
irc.Mode(conn, "+s") // user mode
irc.Mode(conn, "#chat2 +s User", "#chat3 +s User")

func Nick

func Nick(w io.Writer, nick string)

This will send `NICK` to server.

func NicknameIsUsed

func NicknameIsUsed(line string) (used bool)

This will check whenever an nickname has already been used Example:

for buf.Scan() {
	line := buf.Text()
	if irc.NicknameIsUsed(line) {
		irc.Nick(conn, "NewNick")
		continue
	}
}

func Notice added in v1.0.13

func Notice(w io.Writer, to string, msg ...string)

This will send notice(s) to a channel / individual. Example:

irc.Notice(conn, "#chat", "Hello world", "This is the second message")
irc.Notice(conn, "user", "Hey there!")
irc.Notice(conn, "#chan1,#chan2,user1,user2", "Hey there!")

func Part added in v1.0.3

func Part(w io.Writer, reason string, channel ...string)

This is for leaving / parting channel. Example:

irc.Part(conn, "bye-bye~", "#chat", "#yonle")

func Pong

func Pong(w io.Writer, line string) (ponged bool)

This will automaticallg reply to PING messages from server. Example:

for buf.Scan() {
	line := buf.Text()
	if irc.Pong(line) {
		continue
	}
}

func Quit added in v1.0.3

func Quit(w io.Writer, reason string)

This is for sending `QUIT` message to server. Example:

irc.Quit(conn, "Bot power off")

func RegisterUser

func RegisterUser(w io.Writer, username, realname string)

This will send `USER` param to server. `hostname“

func Send

func Send(w io.Writer, cmd ...string) (err error)

This will send command(s) directly to IRC server. Example:

irc.Send(conn, "OPER nene abcd1234")

func Welcome

func Welcome(line string) (welcomed bool)

This will check whenever server has welcomed client. Example:

for buf.Scan() {
	line := buf.Text()
	if irc.Welcome(line) {
		irc.Join(conn, "#chat") // join channel once we're greeted.
		continue
	}
}

Types

type User

type User struct {
	Nick  string
	Ident string
	Host  string
	Mask  string
}

func ParseLine

func ParseLine(line string) (u User, cmd string, args []string, fullmsg string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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