Browse Source

Initial Commit

pull/1/head
James Mills 4 years ago
commit
781659baaa
Signed by: prologic GPG Key ID: AC4C014F1440EBD6
  1. 30
      .drone.yml
  2. 3
      .gitignore
  3. 28
      Dockerfile
  4. 22
      LICENSE
  5. 33
      Makefile
  6. 21
      README.md
  7. 142
      main.go
  8. 17
      version.go

30
.drone.yml

@ -0,0 +1,30 @@
workspace:
base: /go
path: src/github.com/prologic/irccat
pipeline:
build:
image: golang
commands:
- go get -d
- go build .
docker:
image: plugins/docker
repo: r.mills.io/prologic/irccat
registry: r.mills.io
secrets: [ docker_username, docker_password ]
notify:
image: drillster/drone-email
host: mail.mills.io
from: drone@mills.io
skip_verify: true
when:
status: [ changed, failure ]
secrets:
registry_username:
external: true
registry_password:
external: true

3
.gitignore

@ -0,0 +1,3 @@
*~*
*.bak
irccat

28
Dockerfile

@ -0,0 +1,28 @@
# Build
FROM golang:alpine AS build
ARG TAG
ARG BUILD
ENV APP irccat
ENV REPO prologic/$APP
RUN apk add --update git make build-base && \
rm -rf /var/cache/apk/*
WORKDIR /go/src/github.com/$REPO
COPY . /go/src/github.com/$REPO
RUN make TAG=$TAG BUILD=$BUILD build
# Runtime
FROM alpine
ENV APP irccat
ENV REPO prologic/$APP
LABEL irccat.app main
COPY --from=build /go/src/github.com/${REPO}/${APP} /${APP}
ENTRYPOINT ["/irccat"]
CMD [""]

22
LICENSE

@ -0,0 +1,22 @@
Copyright (C) 2017 James Mills
irccat is covered by the MIT license::
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

33
Makefile

@ -0,0 +1,33 @@
.PHONY: dev build image test deps clean
CGO_ENABLED=0
COMMIT=`git rev-parse --short HEAD`
APP=irccat
PACKAGE=
REPO?=prologic/$(APP)
TAG?=latest
BUILD?=-dev
all: dev
dev: build
@./$(APP)
deps:
@go get ./...
build: clean deps
@echo " -> Building $(TAG)$(BUILD)"
@go build -tags "netgo static_build" -installsuffix netgo \
-ldflags "-w -X github.com/$(REPO)/${PACKAGE}.GitCommit=$(COMMIT) -X github.com/$(REPO)/${PACKAGE}.Build=$(BUILD)" .
@echo "Built $$(./$(APP) -v)"
image:
@docker build --build-arg TAG=$(TAG) --build-arg BUILD=$(BUILD) -t $(REPO):$(TAG) .
@echo "Image created: $(REPO):$(TAG)"
test:
@go test -v -cover -race $(TEST_ARGS)
clean:
@rm -rf $(APP)

21
README.md

@ -0,0 +1,21 @@
# irccat - cat for IRC
`irccat` is a simple tool that reads from standard input (*stdin*) and
posts the read message to a target IRC channel on a specified server.
Useful for simple command-line notifications, part of a pipeline, monotiring
or anything you can pipe to `ircat`. Example:
```#!bash
echo 'Hello World!' | irccat irc.freenode.net gonuts
```
## Installation
```#!bash
$ go get github.com/prologic/irccat
```
## License
`irccat` is licensed under the MIT License.

142
main.go

@ -0,0 +1,142 @@
package main
import (
"bufio"
"crypto/tls"
"fmt"
"os"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/namsral/flag"
"github.com/thoj/go-ircevent"
)
const (
username = "irccat"
realname = "IRC Cat, cat for IRC"
)
type Addr struct {
Host string
Port int
UseTLS bool
}
func (a *Addr) String() string {
return fmt.Sprintf("%s:%d", a.Host, a.Port)
}
func ParseAddr(s string) (addr *Addr, err error) {
addr = &Addr{}
parts := strings.Split(s, ":")
fmt.Printf("%v", parts)
if len(parts) != 2 {
return nil, fmt.Errorf("malformed address: %s", s)
}
addr.Host = parts[0]
if parts[1][0] == '+' {
port, err := strconv.Atoi(parts[1][1:])
if err != nil {
return nil, fmt.Errorf("invalid port: %s", parts[1])
}
addr.Port = port
addr.UseTLS = true
} else {
port, err := strconv.Atoi(parts[1])
if err != nil {
return nil, fmt.Errorf("invalid port: %s", parts[1])
}
addr.Port = port
}
if addr.Port < 1 || addr.Port > 65535 {
return nil, fmt.Errorf("invalid port: %d", addr.Port)
}
return addr, nil
}
func main() {
var (
err error
version bool
config string
debug bool
nickname string
)
flag.BoolVar(&version, "v", false, "display version information")
flag.StringVar(&config, "c", "", "config file")
flag.BoolVar(&debug, "d", false, "debug logging")
flag.StringVar(&nickname, "n", "irccat", "nick to use")
flag.Parse()
if debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
if version {
fmt.Printf("irccat v%s", FullVersion())
os.Exit(0)
}
if flag.NArg() < 2 {
log.Fatalf("Ussage: %s <address>[:port] <channel>", os.Args[0])
}
addr, err := ParseAddr(flag.Arg(0))
if err != nil {
log.Fatalf("error parsing addr: %s", err)
}
channel := flag.Arg(1)
if channel[0] != '#' {
log.Warnf("assuming channel #%s", channel)
channel = "#" + channel
}
reader := bufio.NewReader(os.Stdin)
line, isPrefix, err := reader.ReadLine()
if isPrefix {
log.Warn("message may be too long")
}
message := string(line)
conn := irc.IRC(nickname, username)
conn.RealName = realname
conn.VerboseCallbackHandler = debug
conn.Debug = debug
conn.UseTLS = addr.UseTLS
conn.KeepAlive = 30 * time.Second
conn.TLSConfig = &tls.Config{InsecureSkipVerify: true}
conn.AddCallback("001", func(e *irc.Event) {
log.Info("Connected!")
conn.Join(channel)
conn.Privmsg(channel, message)
conn.Quit()
})
err = conn.Connect(addr.String())
if err != nil {
fmt.Printf("Err %s", err)
return
}
conn.Loop()
}

17
version.go

@ -0,0 +1,17 @@
package main
var (
// Version release version
Version = "0.0.1"
// Build will be overwritten automatically by the build system
Build = "-dev"
// GitCommit will be overwritten automatically by the build system
GitCommit = "HEAD"
)
// FullVersion display the full version and build
func FullVersion() string {
return Version + Build + " (" + GitCommit + ")"
}
Loading…
Cancel
Save