2019-08-19 23:30:59 +00:00
|
|
|
package sircd
|
|
|
|
|
|
|
|
import (
|
2019-08-20 00:50:40 +00:00
|
|
|
"log"
|
2019-08-19 23:30:59 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
2019-08-20 00:50:40 +00:00
|
|
|
"container/list"
|
2019-08-19 23:30:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type sircd struct {
|
2019-08-20 00:50:40 +00:00
|
|
|
Running bool
|
|
|
|
log *log.Logger
|
|
|
|
netName string
|
|
|
|
ircPort uint16
|
|
|
|
httpPort uint16
|
2019-08-19 23:30:59 +00:00
|
|
|
ircListener net.Listener
|
2019-08-20 00:50:40 +00:00
|
|
|
ircClients *list.List
|
2019-08-19 23:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
CONN_HOST = "localhost"
|
|
|
|
CONN_PORT = "6667"
|
|
|
|
CONN_TYPE = "tcp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewSircd() *sircd {
|
|
|
|
s := new(sircd)
|
2019-08-20 00:50:40 +00:00
|
|
|
s.Running = true
|
|
|
|
s.ircClients = list.New()
|
2019-08-19 23:30:59 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-08-19 23:56:27 +00:00
|
|
|
func (s *sircd) SetLogger(l *log.Logger) {
|
2019-08-20 00:50:40 +00:00
|
|
|
s.log = l
|
2019-08-19 23:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sircd) startIRCServer() {
|
2019-08-20 00:50:40 +00:00
|
|
|
var err error
|
2019-08-19 23:30:59 +00:00
|
|
|
s.ircListener, err = net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
|
2019-08-20 00:50:40 +00:00
|
|
|
if err != nil {
|
2019-08-19 23:56:27 +00:00
|
|
|
log.Println("Error listening:", err.Error())
|
2019-08-19 23:30:59 +00:00
|
|
|
os.Exit(1)
|
2019-08-20 00:50:40 +00:00
|
|
|
}
|
|
|
|
defer s.ircListener.Close()
|
|
|
|
s.log.Println("Listening for irc on " + CONN_HOST + ":" + CONN_PORT)
|
2019-08-19 23:30:59 +00:00
|
|
|
|
2019-08-20 00:50:40 +00:00
|
|
|
for {
|
2019-08-19 23:30:59 +00:00
|
|
|
// Listen for an incoming connection.
|
|
|
|
conn, err := s.ircListener.Accept()
|
|
|
|
if err != nil {
|
2019-08-19 23:56:27 +00:00
|
|
|
s.log.Fatalf("Error accepting: ", err.Error())
|
2019-08-19 23:30:59 +00:00
|
|
|
}
|
|
|
|
// Handle connections in a new goroutine.
|
2019-08-19 23:56:27 +00:00
|
|
|
go s.handleIRCConnection(conn)
|
2019-08-19 23:30:59 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 00:50:40 +00:00
|
|
|
}
|
2019-08-19 23:30:59 +00:00
|
|
|
|
2019-08-20 00:50:40 +00:00
|
|
|
func (s *sircd) processIRC() {
|
|
|
|
for {
|
|
|
|
s.readFromClients()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sircd) readFromClients() {
|
|
|
|
for e := s.ircClients.Front(); e != nil; e = e.Next() {
|
|
|
|
client := e.Value.(clientIRCConnection)
|
|
|
|
client.ReadSocket()
|
|
|
|
client.ParseMessages()
|
|
|
|
}
|
2019-08-19 23:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sircd) Start() {
|
2019-08-20 00:50:40 +00:00
|
|
|
go s.startIRCServer()
|
2019-08-19 23:30:59 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 23:56:27 +00:00
|
|
|
func (s *sircd) handleIRCConnection(conn net.Conn) {
|
2019-08-20 00:50:40 +00:00
|
|
|
c := newClientIRCConnection(conn, s.log)
|
|
|
|
s.ircClients.PushBack(c)
|
2019-08-19 23:30:59 +00:00
|
|
|
}
|