From d40627874c8b11da86b51d3d9ce7f409815c2c1a Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Tue, 12 Aug 2014 19:27:08 -0600 Subject: [PATCH 1/2] Restructure for Go --- Makefile | 13 +++++++------ wirc.cgi.go => src/wirc.cgi/wirc.cgi.go | 0 wirc.go => src/wirc/wirc.go | 0 3 files changed, 7 insertions(+), 6 deletions(-) rename wirc.cgi.go => src/wirc.cgi/wirc.cgi.go (100%) rename wirc.go => src/wirc/wirc.go (100%) diff --git a/Makefile b/Makefile index 60b62a3..9072141 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,9 @@ -all: irc.cgi irc +all: wirc.cgi wirc -%: %.go - go build $< +export GOPATH = $(CURDIR) -irc.cgi: irc.cgi.go - go build irc.cgi.go - chmod +s irc.cgi +%: + go build $@ + +wirc.cgi: + go build $@ diff --git a/wirc.cgi.go b/src/wirc.cgi/wirc.cgi.go similarity index 100% rename from wirc.cgi.go rename to src/wirc.cgi/wirc.cgi.go diff --git a/wirc.go b/src/wirc/wirc.go similarity index 100% rename from wirc.go rename to src/wirc/wirc.go From a2bafa2e46d15ed9d3732474029ae66daf78cf61 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Tue, 12 Aug 2014 21:41:54 -0600 Subject: [PATCH 2/2] Maybe handle multiple servers? --- src/wirc.cgi/wirc.cgi.go | 17 ++++++++++++----- src/wirc/wirc.go | 20 ++++++++++++++------ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/wirc.cgi/wirc.cgi.go b/src/wirc.cgi/wirc.cgi.go index 3dbafed..b86efed 100644 --- a/src/wirc.cgi/wirc.cgi.go +++ b/src/wirc.cgi/wirc.cgi.go @@ -18,7 +18,7 @@ type Handler struct { cgi.Handler } -var BaseDir string +var ServerDir string func ReadString(fn string) string { octets, err := ioutil.ReadFile(fn) @@ -29,7 +29,7 @@ func ReadString(fn string) string { } func tail(w http.ResponseWriter, pos int) { - f, err := os.Open(path.Join(BaseDir, "log")) + f, err := os.Open(path.Join(ServerDir, "log")) if err != nil { log.Fatal(err) } @@ -61,7 +61,7 @@ func tail(w http.ResponseWriter, pos int) { } func handleCommand(w http.ResponseWriter, text string, target string) { - fn := path.Join(BaseDir, fmt.Sprintf("outq/cgi.%d", time.Now().Unix())) + fn := path.Join(ServerDir, fmt.Sprintf("outq/cgi.%d", time.Now().Unix())) f, err := os.Create(fn) if err != nil { fmt.Fprintln(w, "NO") @@ -84,7 +84,15 @@ func handleCommand(w http.ResponseWriter, text string, target string) { func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - authtok := ReadString(path.Join(BaseDir, "authtok")) + // XXX: I'm not happy with this irc.basedir file + BaseDir := ReadString("irc.basedir") + ServerDir = path.Join(BaseDir, r.FormValue("server")) + + if m, _ := path.Match(path.Join(BaseDir, "*"), ServerDir); ! m { + ServerDir = path.Join(BaseDir, "default") + } + + authtok := ReadString(path.Join(ServerDir, "authtok")) if r.FormValue("auth") != authtok { w.Header().Set("Content-Type", "text/plain") fmt.Fprintln(w, "NO") @@ -102,7 +110,6 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } func main() { - BaseDir = ReadString("irc.basedir") h := Handler{} if err := cgi.Serve(h); err != nil { log.Fatal(err) diff --git a/src/wirc/wirc.go b/src/wirc/wirc.go index ceba86b..f61b979 100644 --- a/src/wirc/wirc.go +++ b/src/wirc/wirc.go @@ -22,6 +22,8 @@ type Message struct { } var running bool = true +var nick string +var gecos string var logq chan Message func isChannel(s string) bool { @@ -165,6 +167,9 @@ func dispatch(outq chan<- string, m Message) { switch m.Command { case "PING": outq <- "PONG :" + m.Text + case "433": + nick = nick + "_" + outq <- fmt.Sprintf("NICK %s", nick) } } @@ -211,10 +216,11 @@ func usage() { func main() { dotls := flag.Bool("notls", true, "Disable TLS security") outqdir := flag.String("outq", "outq", "Output queue directory") + flag.StringVar(&gecos, "gecos", "Bob The Merry Slug", "Gecos entry (full name)") flag.Parse() - if flag.NArg() != 1 { - fmt.Fprintln(os.Stderr, "Error: must specify host") + if flag.NArg() != 2 { + fmt.Fprintln(os.Stderr, "Error: must specify nickname and host") os.Exit(69) } @@ -223,8 +229,11 @@ func main() { log.Fatal(err) } defer dir.Close() + + nick := flag.Arg(0) + host := flag.Arg(1) - conn, err := connect(flag.Arg(0), *dotls) + conn, err := connect(host, *dotls) if err != nil { log.Fatal(err) } @@ -237,8 +246,8 @@ func main() { go writeLoop(conn, outq) go monitorDirectory(*outqdir, dir, outq) - outq <- "NICK neale" - outq <- "USER neale neale neale :neale" + outq <- fmt.Sprintf("NICK %s", nick) + outq <- fmt.Sprintf("USER %s %s %s: %s", nick, nick, nick, gecos) for v := range inq { p, err := parse(v) if err != nil { @@ -251,5 +260,4 @@ func main() { close(outq) close(logq) - close(inq) }