2012-04-07 20:44:59 +02:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2014-02-09 07:06:10 +01:00
|
|
|
"io"
|
2013-05-11 23:43:06 +02:00
|
|
|
"log"
|
2012-04-07 20:44:59 +02:00
|
|
|
"net"
|
2012-12-09 07:54:58 +01:00
|
|
|
"strings"
|
2012-04-07 20:44:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Adapt `net.Conn` to a `chan string`.
|
2012-04-18 03:16:57 +02:00
|
|
|
func StringReadChan(conn net.Conn) <-chan string {
|
2012-04-07 20:44:59 +02:00
|
|
|
ch := make(chan string)
|
2012-04-08 08:32:08 +02:00
|
|
|
reader := bufio.NewReader(conn)
|
2012-04-07 20:44:59 +02:00
|
|
|
go func() {
|
|
|
|
for {
|
2014-02-09 07:06:10 +01:00
|
|
|
line, err := reader.ReadString('\n')
|
2012-04-07 20:44:59 +02:00
|
|
|
if err != nil {
|
2014-02-09 07:06:10 +01:00
|
|
|
if err != io.EOF {
|
|
|
|
log.Printf("%s → %s error: %s", conn.RemoteAddr(), conn.LocalAddr(), err)
|
|
|
|
}
|
2012-04-07 20:44:59 +02:00
|
|
|
break
|
|
|
|
}
|
2013-05-11 23:43:06 +02:00
|
|
|
if DEBUG_NET {
|
2014-02-09 02:43:59 +01:00
|
|
|
log.Printf("%s → %s %s", conn.RemoteAddr(), conn.LocalAddr(), line)
|
2013-05-11 23:43:06 +02:00
|
|
|
}
|
2014-02-09 07:06:10 +01:00
|
|
|
|
|
|
|
ch <- strings.TrimSpace(line)
|
2012-04-07 20:44:59 +02:00
|
|
|
}
|
|
|
|
}()
|
2012-04-08 08:32:08 +02:00
|
|
|
return ch
|
|
|
|
}
|
2012-04-07 20:44:59 +02:00
|
|
|
|
2014-02-09 07:06:10 +01:00
|
|
|
func maybeLogWriteError(conn net.Conn, err error) bool {
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Printf("%s ← %s error: %s", conn.RemoteAddr(), conn.LocalAddr(), err)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2012-04-18 03:16:57 +02:00
|
|
|
func StringWriteChan(conn net.Conn) chan<- string {
|
2012-04-08 08:32:08 +02:00
|
|
|
ch := make(chan string)
|
|
|
|
writer := bufio.NewWriter(conn)
|
2012-04-07 20:44:59 +02:00
|
|
|
go func() {
|
|
|
|
for str := range ch {
|
2013-05-11 23:43:06 +02:00
|
|
|
if DEBUG_NET {
|
2014-02-09 02:43:59 +01:00
|
|
|
log.Printf("%s ← %s %s", conn.RemoteAddr(), conn.LocalAddr(), str)
|
2013-05-11 23:43:06 +02:00
|
|
|
}
|
2014-02-09 07:06:10 +01:00
|
|
|
if _, err := writer.WriteString(str); maybeLogWriteError(conn, err) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if _, err := writer.WriteString(CRLF); maybeLogWriteError(conn, err) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err := writer.Flush(); maybeLogWriteError(conn, err) {
|
2012-04-07 20:44:59 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return ch
|
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
|
|
|
|
func LookupHostname(addr net.Addr) string {
|
|
|
|
addrStr := addr.String()
|
|
|
|
ipaddr, _, err := net.SplitHostPort(addrStr)
|
|
|
|
if err != nil {
|
|
|
|
return addrStr
|
|
|
|
}
|
2014-02-10 00:14:53 +01:00
|
|
|
switch ipaddr {
|
|
|
|
case "127.0.0.1", "::1":
|
|
|
|
return "localhost"
|
|
|
|
}
|
2014-02-10 06:04:37 +01:00
|
|
|
names, err := net.LookupAddr(ipaddr)
|
2012-12-09 07:54:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return ipaddr
|
|
|
|
}
|
2014-02-10 06:10:36 +01:00
|
|
|
return names[0][0 : len(names[0])-1]
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|