GithubHelp home page GithubHelp logo

goirc's Introduction

goIRC

A go implementation of connecting to an IRC server. To run this in the command line do the following.

Install

go get github.com/nexes/goIRC

Example

package main

import (
  "bufio"
  "fmt"
  "os"
  "strings"

  "github.com/nexes/goIRC/pkg/irc"
)

func main() {
  var currentRoom string
  client := irc.NewClient("yourNick", "yourPassIfYouHaveOne", "irc.freenode.net")

  // listen to the callbacks you're interested in

  client.HandleEventFunc(irc.EventConnect, func(event irc.EventType) {
    fmt.Printf("connection made message: %s\n", event.Message)
  })

  client.HandleEventFunc(irc.EventDisconnect, func(event irc.EventType) {
    fmt.Println("disconnect event")
  })

  client.HandleEventFunc(irc.EventError, func(event irc.EventType) {
    fmt.Printf("Error code %d: msg: %s\n", event.Code, event.Err.Error())
  })

  client.HandleEventFunc(irc.EventMessage, func(event irc.EventType) {
    // received a message
    fmt.Printf("[%s]: %s - %s\n", event.Room, event.Nick, event.Message)
  })

  client.HandleEventFunc(irc.EventChannelMessage, func(event irc.EventType) {
    switch event.Code {
    case irc.RPL_FORWARDJOIN:
      // sometimes the room will forward to a different named room, e.g #programming -> ##programming
      fmt.Printf("Room forwared to %s. message: %s\n", event.Room, event.Message)
      currentRoom = event.Room
    }
  })

  client.HandleEventFunc(irc.EventRoomMessage, func(event irc.EventType) {
    switch event.Code {
    case irc.RPL_ROOMJOIN:
      fmt.Printf("\t%s Joined %s\n", event.Nick, event.Room)
    case irc.RPL_ROOMQUIT:
      fmt.Printf("\t%s Quit %s\n", event.Nick, event.Room)
    }
  })

  // capture user input, send commands or write to a room
  go func() {
  loop:
    for {
      stdin := bufio.NewReader(os.Stdin)
      input, err := stdin.ReadString('\n')
      if err != nil {
        fmt.Println("error reading stdio ", err)
      }

      args := strings.Split(strings.TrimSpace(input), " ")

      switch strings.ToLower(args[0]) {
      case "/join":
        client.Command(irc.Command{
          Action: "join",
          Args:   args[1:],
        })
      case "/part":
        client.Command(irc.Command{
          Action: "part",
          Args:   args[1:],
        })
      case "/quit":
        client.StopConnection()
        break loop

      default:
        message := strings.Join(args, " ")
        client.WriteToTarget(currentRoom, message)
      }
    }
  }()

  // start the connection, this block here
  client.StartConnection()
}

Todo

  • Better documentation for the api
  • ssl/tls
  • complete response codes

LICENSE (MIT)

Copyright (c) 2016-2019 Joe Berria

goirc's People

Contributors

nexes avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.