~taiite/senpai

ref: a532aae30446d7d76c74780bebaf0a40c775694e senpai/irc/channel.go -rw-r--r-- 630 bytes
a532aae3Hubert Hirtz Fix history not being loaded on channel re-JOIN 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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_
}