Maybe handle multiple servers?

This commit is contained in:
Neale Pickett 2014-08-12 21:41:54 -06:00
parent d40627874c
commit a2bafa2e46
2 changed files with 26 additions and 11 deletions

View File

@ -18,7 +18,7 @@ type Handler struct {
cgi.Handler cgi.Handler
} }
var BaseDir string var ServerDir string
func ReadString(fn string) string { func ReadString(fn string) string {
octets, err := ioutil.ReadFile(fn) octets, err := ioutil.ReadFile(fn)
@ -29,7 +29,7 @@ func ReadString(fn string) string {
} }
func tail(w http.ResponseWriter, pos int) { 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -61,7 +61,7 @@ func tail(w http.ResponseWriter, pos int) {
} }
func handleCommand(w http.ResponseWriter, text string, target string) { 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) f, err := os.Create(fn)
if err != nil { if err != nil {
fmt.Fprintln(w, "NO") 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) { 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 { if r.FormValue("auth") != authtok {
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "NO") fmt.Fprintln(w, "NO")
@ -102,7 +110,6 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
func main() { func main() {
BaseDir = ReadString("irc.basedir")
h := Handler{} h := Handler{}
if err := cgi.Serve(h); err != nil { if err := cgi.Serve(h); err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -22,6 +22,8 @@ type Message struct {
} }
var running bool = true var running bool = true
var nick string
var gecos string
var logq chan Message var logq chan Message
func isChannel(s string) bool { func isChannel(s string) bool {
@ -165,6 +167,9 @@ func dispatch(outq chan<- string, m Message) {
switch m.Command { switch m.Command {
case "PING": case "PING":
outq <- "PONG :" + m.Text outq <- "PONG :" + m.Text
case "433":
nick = nick + "_"
outq <- fmt.Sprintf("NICK %s", nick)
} }
} }
@ -211,10 +216,11 @@ func usage() {
func main() { func main() {
dotls := flag.Bool("notls", true, "Disable TLS security") dotls := flag.Bool("notls", true, "Disable TLS security")
outqdir := flag.String("outq", "outq", "Output queue directory") outqdir := flag.String("outq", "outq", "Output queue directory")
flag.StringVar(&gecos, "gecos", "Bob The Merry Slug", "Gecos entry (full name)")
flag.Parse() flag.Parse()
if flag.NArg() != 1 { if flag.NArg() != 2 {
fmt.Fprintln(os.Stderr, "Error: must specify host") fmt.Fprintln(os.Stderr, "Error: must specify nickname and host")
os.Exit(69) os.Exit(69)
} }
@ -224,7 +230,10 @@ func main() {
} }
defer dir.Close() defer dir.Close()
conn, err := connect(flag.Arg(0), *dotls) nick := flag.Arg(0)
host := flag.Arg(1)
conn, err := connect(host, *dotls)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -237,8 +246,8 @@ func main() {
go writeLoop(conn, outq) go writeLoop(conn, outq)
go monitorDirectory(*outqdir, dir, outq) go monitorDirectory(*outqdir, dir, outq)
outq <- "NICK neale" outq <- fmt.Sprintf("NICK %s", nick)
outq <- "USER neale neale neale :neale" outq <- fmt.Sprintf("USER %s %s %s: %s", nick, nick, nick, gecos)
for v := range inq { for v := range inq {
p, err := parse(v) p, err := parse(v)
if err != nil { if err != nil {
@ -251,5 +260,4 @@ func main() {
close(outq) close(outq)
close(logq) close(logq)
close(inq)
} }