goirc

repository·master·Indexed 19 days ago

https://github.com/fluffle/goirc

A Go-based IRC client framework for building IRC bots and clients. It features a channel-based communication structure, an event-driven API using HandleFunc, and optional state tracking for nicks and channels. The library provides high-level methods for common IRC commands like Join, Part, and Privmsg, as well as a Raw method for sending custom commands.

Tokens
1.5K
Snippets
12
Records
13
Agent score
18%

What's inside goirc

  1. Enable state tracking for nicks and channels

    master

    GoIRC can automatically track the state of all nicks in the channels the client is present in. This feature is disabled by default to save resources.

    To enable it, call EnableStateTracking(). To disable it, call DisableStateTracking().

    Warning: Do not enable or disable state tracking while already connected to an IRC server, as this will likely result in an inconsistent state and produce warnings to STDERR.

    c.EnableStateTracking()
    // ... later
    c.DisableStateTracking()
  2. Create an IRC client with GoIRC

    master

    You can create a client in two ways: using a quick helper for simple connections or manually configuring a Config object for more control.

    Simple Client

    Use irc.SimpleClient("nick") for a basic setup.

    Configured Client

    Use irc.NewConfig("nick") to customize settings such as SSL, server address, and nickname generation logic, then pass the config to irc.Client(cfg).

    // Simple client
    c := irc.SimpleClient("nick")
    
    // Configured client
    cfg := irc.NewConfig("nick")
    cfg.SSL = true
    cfg.SSLConfig = &tls.Config{ServerName: "irc.freenode.net"}
    cfg.Server = "irc.freenode.net:7000"
    cfg.NewNick = func(n string) string { return n + "^" }
    c = irc.Client(cfg)
  3. Handle IRC events with HandleFunc

    master

    GoIRC uses an event-driven model. You can register handlers for specific events using the HandleFunc method on the client instance. Common events include irc.CONNECTED and irc.DISCONNECTED.

    Handlers receive a *irc.Conn (the connection object) and a *irc.Line (the raw IRC message line).

    c.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, line *irc.Line) {
        conn.Join("#channel")
    })
    
    quit := make(chan bool)
    c.HandleFunc(irc.DISCONNECTED, func(conn *irc.Conn, line *irc.Line) {
        quit <- true
    })
  4. Connect to an IRC server

    master

    There are two primary ways to initiate a connection:

    1. Connect(): Uses the server address defined in the client's configuration.
    2. ConnectTo(server): Connects to a specific server address provided as an argument, overriding the configured server.

    Note: If using a SimpleClient, you must set the Server field in the configuration before calling Connect().

    // Using configured server
    if err := c.Connect(); err != nil {
        // handle error
    }
    
    // Using specific server address
    if err := c.ConnectTo("irc.freenode.net"); err != nil {
        // handle error
    }
  5. Send IRC commands

    master
    Commands sent to the server (such as PRIVMSG) are implemented as methods on the *irc.Conn struct. You can find the available command implementations in the client/commands.go file of the repository.
  6. Initialize an IRC client with SimpleClient()

    master

    Use irc.SimpleClient(nickname, username) to create a new IRC client instance. This is a high-level constructor that sets up a client with the provided nickname and username.

    To enable tracking of user presence and channel membership, call c.EnableStateTracking() on the returned client.

    c := irc.SimpleClient("GoTest", "gotest")
    c.EnableStateTracking()
  7. Manage connection lifecycle with Quit() and Close()

    master

    To gracefully exit an IRC session, use c.Quit(reason). To abruptly terminate the connection, use c.Close().

    // Graceful quit with a reason
    c.Quit("Goodbye!")
    
    // Immediate close
    c.Close()
  8. Send IRC commands with Join(), Part(), and Privmsg()

    master

    The client provides methods to interact with the IRC server:

    • c.Join(channel): Joins a specific channel.
    • c.Part(channel): Parts from a specific channel.
    • c.Privmsg(target, message): Sends a private message to a user or a message to a channel.
    c.Join("#go-nuts")
    c.Part("#go-nuts")
    c.Privmsg("#go-nuts", "Hello world!")
  9. Configure client behavior via Config()

    master

    The c.Config() method returns access to the client's configuration settings. For example, you can toggle the Flood boolean to control how the client handles rapid message sending.

    c.Config().Flood = true
  10. Use StateTracker to check user presence

    master

    If EnableStateTracking() has been called, you can use c.StateTracker().IsOn(channel, username) to check if a specific user is currently present in a specific channel. It returns a boolean indicating presence.

    // Returns true if username is in channelname
    _, userIsOn := c.StateTracker().IsOn("#channel", "nickname")