summaryrefslogtreecommitdiff
path: root/irc/channel.go
diff options
context:
space:
mode:
Diffstat (limited to 'irc/channel.go')
-rw-r--r--irc/channel.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/irc/channel.go b/irc/channel.go
new file mode 100644
index 0000000..c4a9e6f
--- /dev/null
+++ b/irc/channel.go
@@ -0,0 +1,40 @@
+package irc
+
+import (
+ "bufio"
+ "fmt"
+ "net"
+)
+
+const chanCapacity = 64
+
+func ChanInOut(conn net.Conn) (in <-chan Message, out chan<- Message) {
+ in_ := make(chan Message, chanCapacity)
+ out_ := make(chan Message, chanCapacity)
+
+ go func() {
+ r := bufio.NewScanner(conn)
+ for r.Scan() {
+ line := r.Text()
+ msg, err := ParseMessage(line)
+ if err != nil {
+ continue
+ }
+ in_ <- msg
+ }
+ close(in_)
+ }()
+
+ go func() {
+ for msg := range out_ {
+ // TODO send messages by batches
+ _, err := fmt.Fprintf(conn, "%s\r\n", msg.String())
+ if err != nil {
+ break
+ }
+ }
+ _ = conn.Close()
+ }()
+
+ return in_, out_
+}