Logging and reading outq

This commit is contained in:
Neale Pickett 2014-07-21 05:03:53 +00:00
parent 3ef3cf3b0b
commit 5c178b38f9
1 changed files with 66 additions and 14 deletions

80
irc.go
View File

@ -9,23 +9,30 @@ import (
"net" "net"
"os" "os"
"strings" "strings"
"time"
) )
var running bool = true
type Message struct { type Message struct {
Command string Command string
FullSender string FullSender string
Sender string Sender string
Forum string Forum string
Args []string Args []string
Text string Text string
} }
func (m Message) String() string { func (m Message) String() string {
a := append([]string{m.FullSender}, m.Args...) a := append([]string{m.FullSender}, m.Args...)
args :=strings.Join(a, " ") args := strings.Join(a, " ")
return fmt.Sprintf("%s %s %s %s %s", m.Command, m.Sender, m.Forum, args, m.Text) return fmt.Sprintf("%s %s %s %s %s", m.Command, m.Sender, m.Forum, args, m.Text)
} }
func Log(m Message) {
fmt.Printf("%d %s\n", time.Now().Unix(), m.String())
}
func nuhost(s string) (string, string, string) { func nuhost(s string) (string, string, string) {
var parts []string var parts []string
@ -81,7 +88,7 @@ func parse(v string) (Message, error) {
lhs = v lhs = v
m.Text = "" m.Text = ""
} }
m.FullSender = "." m.FullSender = "."
m.Forum = "." m.Forum = "."
m.Sender = "." m.Sender = "."
@ -90,15 +97,15 @@ func parse(v string) (Message, error) {
if parts[0][0] == ':' { if parts[0][0] == ':' {
m.FullSender = parts[0][1:] m.FullSender = parts[0][1:]
parts = parts[1:] parts = parts[1:]
n, u, _ := nuhost(m.FullSender) n, u, _ := nuhost(m.FullSender)
if u != "" { if u != "" {
m.Sender = n m.Sender = n
} }
} }
m.Command = strings.ToUpper(parts[0]) m.Command = strings.ToUpper(parts[0])
switch (m.Command) { switch m.Command {
case "PRIVMSG", "NOTICE": case "PRIVMSG", "NOTICE":
n, u, _ := nuhost(parts[1]) n, u, _ := nuhost(parts[1])
if u == "" { if u == "" {
@ -126,32 +133,74 @@ func parse(v string) (Message, error) {
m.FullSender = parts[1] m.FullSender = parts[1]
m.Forum = m.FullSender m.Forum = m.FullSender
} }
return m, nil return m, nil
} }
func dispatch(outq chan<- string, m Message) { func dispatch(outq chan<- string, m Message) {
log.Print(m.String()) Log(m)
switch (m.Command) { switch m.Command {
case "PING": case "PING":
outq <- "PONG :" + m.Text outq <- "PONG :" + m.Text
} }
} }
func handleInfile(path string, outq chan<- string) {
f, err := os.Open(path)
if (err != nil) {
return
}
defer f.Close()
os.Remove(path)
inf := bufio.NewScanner(f)
for inf.Scan() {
outq <- inf.Text()
}
}
func monitorDirectory(dirname string, dir *os.File, outq chan<- string) {
latest := time.Unix(0, 0)
for running {
fi, err := dir.Stat()
if err != nil {
break
}
current := fi.ModTime()
if current.After(latest) {
latest = current
dn, _ := dir.Readdirnames(0)
for _, fn := range dn {
path := dirname + string(os.PathSeparator) + fn
handleInfile(path, outq)
}
_, _ = dir.Seek(0, 0)
}
time.Sleep(500 * time.Millisecond)
}
}
func usage() { func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] HOST:PORT\n", os.Args[0]) fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] HOST:PORT\n", os.Args[0])
flag.PrintDefaults() flag.PrintDefaults()
} }
func main() { func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
dotls := flag.Bool("notls", true, "Disable TLS security") dotls := flag.Bool("notls", true, "Disable TLS security")
outqdir := flag.String("outq", "outq", "Output queue directory")
flag.Parse() flag.Parse()
if flag.NArg() != 1 { if flag.NArg() != 1 {
fmt.Fprintln(os.Stderr, "Error: must specify host") fmt.Fprintln(os.Stderr, "Error: must specify host")
os.Exit(69) os.Exit(69)
} }
dir, err := os.Open(*outqdir)
if err != nil {
log.Fatal(err)
}
defer dir.Close()
conn, err := connect(flag.Arg(0), *dotls) conn, err := connect(flag.Arg(0), *dotls)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -161,6 +210,7 @@ func main() {
outq := make(chan string) outq := make(chan string)
go readLoop(conn, inq) go readLoop(conn, inq)
go writeLoop(conn, outq) go writeLoop(conn, outq)
go monitorDirectory(*outqdir, dir, outq)
outq <- "NICK neale" outq <- "NICK neale"
outq <- "USER neale neale neale :neale" outq <- "USER neale neale neale :neale"
@ -171,6 +221,8 @@ func main() {
} }
dispatch(outq, p) dispatch(outq, p)
} }
running = false
close(outq) close(outq)
} }