mirror of
https://github.com/Luzifer/tinyirc.git
synced 2024-11-09 15:40:05 +00:00
Initial version
This commit is contained in:
commit
ec7aa452dc
4 changed files with 201 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
.env
|
||||
tinyirc
|
10
go.mod
Normal file
10
go.mod
Normal file
|
@ -0,0 +1,10 @@
|
|||
module github.com/Luzifer/tinyirc
|
||||
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/Luzifer/rconfig/v2 v2.2.1
|
||||
github.com/go-irc/irc v2.1.0+incompatible
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
)
|
20
go.sum
Normal file
20
go.sum
Normal file
|
@ -0,0 +1,20 @@
|
|||
github.com/Luzifer/rconfig/v2 v2.2.1 h1:zcDdLQlnlzwcBJ8E0WFzOkQE1pCMn3EbX0dFYkeTczg=
|
||||
github.com/Luzifer/rconfig/v2 v2.2.1/go.mod h1:OKIX0/JRZrPJ/ZXXWklQEFXA6tBfWaljZbW37w+sqBw=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-irc/irc v2.1.0+incompatible h1:pg7pMVq5OYQbqTxceByD/EN8VIsba7DtKn49rsCnG8Y=
|
||||
github.com/go-irc/irc v2.1.0+incompatible/go.mod h1:jJILTRy8s/qOvusiKifAEfhQMVwft1ZwQaVJnnzmyX4=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
|
||||
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19 h1:WB265cn5OpO+hK3pikC9hpP1zI/KTwmyMFKloW9eOVc=
|
||||
gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19/go.mod h1:o4V0GXN9/CAmCsvJ0oXYZvrZOe7syiDZSN1GWGZTGzc=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
169
main.go
Normal file
169
main.go
Normal file
|
@ -0,0 +1,169 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-irc/irc"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/Luzifer/rconfig/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
cfg = struct {
|
||||
Join []string `flag:"join,j" default:"" description:"Channels to join (specify multiple times for multiple channels)"`
|
||||
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
||||
Nick string `flag:"nick" default:"" description:"Nick to choose after connecting (defaults to user)"`
|
||||
Port int64 `flag:"port" default:"6667" description:"Port to connect to"`
|
||||
Quiet bool `flag:"quiet,q" default:"false" description:"Do not print messages to stdout"`
|
||||
SendBurst int `flag:"send-burst" default:"0" description:"Number of messages to be sent in a burst"`
|
||||
SendLimit time.Duration `flag:"send-limit" default:"0" description:"How long to wait between two messages"`
|
||||
Server string `flag:"server,s" default:"" description:"IRC Server to connect to"`
|
||||
ServerPass string `flag:"server-pass,p" default:"" description:"Password to authenticate"`
|
||||
TLS bool `flag:"tls" default:"false" description:"Use TLS connection"`
|
||||
User string `flag:"user,u" default:"tinyirc" description:"User to use to connect to the server"`
|
||||
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
||||
}{}
|
||||
|
||||
connWaitOnce = new(sync.Once)
|
||||
connEstablished = new(sync.WaitGroup)
|
||||
done bool
|
||||
|
||||
version = "dev"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rconfig.AutoEnv(true)
|
||||
if err := rconfig.ParseAndValidate(&cfg); err != nil {
|
||||
log.Fatalf("Unable to parse commandline options: %s", err)
|
||||
}
|
||||
|
||||
if cfg.VersionAndExit {
|
||||
fmt.Printf("tinyirc %s\n", version)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if l, err := log.ParseLevel(cfg.LogLevel); err != nil {
|
||||
log.WithError(err).Fatal("Unable to parse log level")
|
||||
} else {
|
||||
log.SetLevel(l)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
connEstablished.Add(1)
|
||||
|
||||
client, conn, err := connect()
|
||||
if err != nil {
|
||||
log.WithError(err).Fatal("Unable to connect")
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
go func() {
|
||||
if err := client.Run(); err != nil && !done {
|
||||
log.WithError(err).Fatal("IRC client reported error")
|
||||
}
|
||||
}()
|
||||
|
||||
connEstablished.Wait()
|
||||
|
||||
defer client.WriteMessage(&irc.Message{Command: "QUIT"})
|
||||
|
||||
for _, c := range cfg.Join {
|
||||
logger := log.WithField("channel", c)
|
||||
logger.Debug("Joining channel")
|
||||
if err = client.WriteMessage(&irc.Message{
|
||||
Command: "JOIN",
|
||||
Params: []string{c},
|
||||
}); err != nil {
|
||||
logger.WithError(err).Error("Unable to join channel")
|
||||
}
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
logger := log.WithField("line", line)
|
||||
|
||||
logger.Debug("Processing line")
|
||||
msg, err := irc.ParseMessage(line)
|
||||
if err != nil {
|
||||
logger.WithError(err).Error("Unable to parse line")
|
||||
continue
|
||||
}
|
||||
|
||||
if err = client.WriteMessage(msg); err != nil {
|
||||
logger.WithError(err).Error("Unable to send message")
|
||||
}
|
||||
}
|
||||
|
||||
done = true
|
||||
}
|
||||
|
||||
func connect() (*irc.Client, net.Conn, error) {
|
||||
var (
|
||||
conn net.Conn
|
||||
err error
|
||||
)
|
||||
|
||||
for f, r := range map[string]bool{
|
||||
"server": cfg.Server != "",
|
||||
"user": cfg.User != "",
|
||||
} {
|
||||
if !r {
|
||||
return nil, nil, errors.Errorf("missing configuration: %s", f)
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.TLS {
|
||||
conn, err = tls.Dial("tcp", fmt.Sprintf("%s:%d", cfg.Server, cfg.Port), nil)
|
||||
} else {
|
||||
conn, err = net.Dial("tcp", fmt.Sprintf("%s:%d", cfg.Server, cfg.Port))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(err, "creating tcp connection")
|
||||
}
|
||||
|
||||
nick := cfg.Nick
|
||||
if nick == "" {
|
||||
nick = cfg.User
|
||||
}
|
||||
|
||||
return irc.NewClient(conn, irc.ClientConfig{
|
||||
Nick: nick,
|
||||
Pass: cfg.ServerPass,
|
||||
User: cfg.User,
|
||||
|
||||
SendBurst: cfg.SendBurst,
|
||||
SendLimit: cfg.SendLimit,
|
||||
|
||||
Handler: irc.HandlerFunc(printMessage),
|
||||
}), conn, nil
|
||||
}
|
||||
|
||||
func printMessage(c *irc.Client, m *irc.Message) {
|
||||
if m.Command == "001" {
|
||||
connWaitOnce.Do(connEstablished.Done)
|
||||
}
|
||||
|
||||
if _, err := strconv.Atoi(m.Command); err == nil {
|
||||
// Numeric command, connection setup, do not print
|
||||
return
|
||||
}
|
||||
|
||||
if cfg.Quiet {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(strings.TrimSpace(m.String()))
|
||||
}
|
Loading…
Reference in a new issue