2014-02-14 04:37:16 +01:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2014-02-20 22:03:33 +01:00
|
|
|
"io"
|
2014-02-14 04:37:16 +01:00
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2014-02-18 08:58:02 +01:00
|
|
|
const (
|
2014-02-20 22:03:33 +01:00
|
|
|
R = '→'
|
|
|
|
W = '←'
|
|
|
|
EOF = ""
|
2014-02-18 08:58:02 +01:00
|
|
|
)
|
|
|
|
|
2014-02-14 04:37:16 +01:00
|
|
|
type Socket struct {
|
2014-02-20 03:13:35 +01:00
|
|
|
conn net.Conn
|
|
|
|
writer *bufio.Writer
|
2014-02-14 04:37:16 +01:00
|
|
|
}
|
|
|
|
|
2014-03-08 23:24:17 +01:00
|
|
|
func NewSocket(conn net.Conn, commands chan<- Command) *Socket {
|
2014-02-14 04:37:16 +01:00
|
|
|
socket := &Socket{
|
2014-02-20 03:13:35 +01:00
|
|
|
conn: conn,
|
|
|
|
writer: bufio.NewWriter(conn),
|
2014-02-14 04:37:16 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 02:04:24 +01:00
|
|
|
go socket.readLines(commands)
|
2014-02-20 22:03:33 +01:00
|
|
|
|
2014-02-14 04:37:16 +01:00
|
|
|
return socket
|
|
|
|
}
|
|
|
|
|
|
|
|
func (socket *Socket) String() string {
|
|
|
|
return socket.conn.RemoteAddr().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (socket *Socket) Close() {
|
2014-02-20 03:13:35 +01:00
|
|
|
socket.conn.Close()
|
2014-03-09 00:01:15 +01:00
|
|
|
Log.debug.Printf("%s closed", socket)
|
2014-02-14 04:37:16 +01:00
|
|
|
}
|
|
|
|
|
2014-03-08 23:24:17 +01:00
|
|
|
func (socket *Socket) readLines(commands chan<- Command) {
|
2014-03-28 21:47:49 +01:00
|
|
|
commands <- NewProxyCommand(AddrLookupHostname(socket.conn.RemoteAddr()))
|
2014-02-24 02:04:24 +01:00
|
|
|
|
2014-03-28 01:51:29 +01:00
|
|
|
scanner := bufio.NewScanner(socket.conn)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2014-02-17 07:24:49 +01:00
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2014-03-09 00:01:15 +01:00
|
|
|
Log.debug.Printf("%s → %s", socket, line)
|
2014-02-24 02:04:24 +01:00
|
|
|
|
|
|
|
msg, err := ParseCommand(line)
|
|
|
|
if err != nil {
|
|
|
|
// TODO error messaging to client
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
commands <- msg
|
2014-02-14 04:37:16 +01:00
|
|
|
}
|
2014-02-20 22:03:33 +01:00
|
|
|
|
2014-03-28 01:51:29 +01:00
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
Log.debug.Printf("%s error: %s", socket, err)
|
2014-02-24 02:04:24 +01:00
|
|
|
}
|
2014-03-28 01:51:29 +01:00
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
commands <- NewQuitCommand("connection closed")
|
2014-03-28 02:58:12 +01:00
|
|
|
|
2014-03-28 00:49:22 +01:00
|
|
|
close(commands)
|
2014-02-14 04:37:16 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 02:04:24 +01:00
|
|
|
func (socket *Socket) Write(line string) (err error) {
|
2014-02-20 03:13:35 +01:00
|
|
|
if _, err = socket.writer.WriteString(line); socket.isError(err, W) {
|
|
|
|
return
|
2014-02-18 18:45:10 +01:00
|
|
|
}
|
2014-02-18 08:58:02 +01:00
|
|
|
|
2014-02-20 20:15:42 +01:00
|
|
|
if _, err = socket.writer.WriteString(CRLF); socket.isError(err, W) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-27 22:43:52 +01:00
|
|
|
go socket.flush()
|
2014-02-20 20:15:42 +01:00
|
|
|
|
2014-03-09 00:01:15 +01:00
|
|
|
Log.debug.Printf("%s ← %s", socket, line)
|
2014-02-20 03:13:35 +01:00
|
|
|
return
|
2014-02-14 04:37:16 +01:00
|
|
|
}
|
|
|
|
|
2014-03-27 22:43:52 +01:00
|
|
|
func (socket *Socket) flush() {
|
|
|
|
socket.isError(socket.writer.Flush(), W)
|
|
|
|
}
|
|
|
|
|
2014-02-18 08:58:02 +01:00
|
|
|
func (socket *Socket) isError(err error, dir rune) bool {
|
2014-02-14 04:37:16 +01:00
|
|
|
if err != nil {
|
2014-03-09 00:01:15 +01:00
|
|
|
if err != io.EOF {
|
|
|
|
Log.debug.Printf("%s %c error: %s", socket, dir, err)
|
2014-02-14 04:37:16 +01:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|