2020-02-04 09:46:22 +00:00
|
|
|
package jounce
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"gopkg.in/irc.v3"
|
|
|
|
)
|
|
|
|
|
2020-02-06 14:50:46 +00:00
|
|
|
type Logger interface {
|
|
|
|
Print(v ...interface{})
|
|
|
|
Printf(format string, v ...interface{})
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:03:07 +00:00
|
|
|
type Upstream struct {
|
|
|
|
Addr string
|
|
|
|
Nick string
|
|
|
|
Username string
|
|
|
|
Realname string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
Hostname string
|
|
|
|
Logger Logger
|
|
|
|
Upstreams []Upstream // TODO: per-user
|
2020-02-04 17:56:07 +00:00
|
|
|
}
|
2020-02-04 09:46:22 +00:00
|
|
|
|
2020-02-04 17:56:07 +00:00
|
|
|
func (s *Server) prefix() *irc.Prefix {
|
|
|
|
return &irc.Prefix{Name: s.Hostname}
|
|
|
|
}
|
2020-02-04 10:25:53 +00:00
|
|
|
|
2020-02-06 15:03:07 +00:00
|
|
|
func (s *Server) Run() {
|
|
|
|
for i := range s.Upstreams {
|
2020-02-06 15:13:29 +00:00
|
|
|
upstream := &s.Upstreams[i]
|
2020-02-06 15:03:07 +00:00
|
|
|
// TODO: retry connecting
|
2020-02-06 15:13:29 +00:00
|
|
|
go func() {
|
2020-02-06 15:18:19 +00:00
|
|
|
if err := connect(s, upstream); err != nil {
|
2020-02-06 15:13:29 +00:00
|
|
|
s.Logger.Printf("Failed to connect to upstream server %q: %v", upstream.Addr, err)
|
|
|
|
}
|
|
|
|
}()
|
2020-02-06 15:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 10:25:53 +00:00
|
|
|
func (s *Server) Serve(ln net.Listener) error {
|
2020-02-04 09:46:22 +00:00
|
|
|
for {
|
2020-02-04 10:25:53 +00:00
|
|
|
c, err := ln.Accept()
|
2020-02-04 09:46:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to accept connection: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2020-02-06 15:18:19 +00:00
|
|
|
if err := handleConn(s, c); err != nil {
|
|
|
|
s.Logger.Printf("Error handling connection: %v", err)
|
2020-02-04 09:46:22 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|