irc

package module
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 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).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chat

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

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

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

func CutIRCPrefixMSG

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

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