|
@@ -1,126 +0,0 @@
|
1
|
|
-// Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
|
2
|
|
-// released under the MIT license
|
3
|
|
-
|
4
|
|
-package irc
|
5
|
|
-
|
6
|
|
-import (
|
7
|
|
- "fmt"
|
8
|
|
- "net"
|
9
|
|
- "strings"
|
10
|
|
-
|
11
|
|
- "github.com/DanielOaks/girc-go/ircmsg"
|
12
|
|
-)
|
13
|
|
-
|
14
|
|
-// ClientSocket listens to a socket using the IRC protocol, processes events,
|
15
|
|
-// and also sends IRC lines out of that socket.
|
16
|
|
-type ClientSocket struct {
|
17
|
|
- receiveLines chan string
|
18
|
|
- ReceiveEvents chan Message
|
19
|
|
- SendLines chan string
|
20
|
|
- socket Socket
|
21
|
|
- client Client
|
22
|
|
-}
|
23
|
|
-
|
24
|
|
-// NewClientSocket returns a new ClientSocket.
|
25
|
|
-func NewClientSocket(conn net.Conn, client Client) ClientSocket {
|
26
|
|
- return ClientSocket{
|
27
|
|
- receiveLines: make(chan string),
|
28
|
|
- ReceiveEvents: make(chan Message),
|
29
|
|
- SendLines: make(chan string),
|
30
|
|
- socket: NewSocket(conn),
|
31
|
|
- client: client,
|
32
|
|
- }
|
33
|
|
-}
|
34
|
|
-
|
35
|
|
-// Start creates and starts running the necessary event loops.
|
36
|
|
-func (cs *ClientSocket) Start() {
|
37
|
|
- go cs.RunEvents()
|
38
|
|
- go cs.RunSocketSender()
|
39
|
|
- go cs.RunSocketListener()
|
40
|
|
-}
|
41
|
|
-
|
42
|
|
-// RunEvents handles received IRC lines and processes incoming commands.
|
43
|
|
-func (cs *ClientSocket) RunEvents() {
|
44
|
|
- var exiting bool
|
45
|
|
- var line string
|
46
|
|
- for !exiting {
|
47
|
|
- select {
|
48
|
|
- case line = <-cs.receiveLines:
|
49
|
|
- if line != "" {
|
50
|
|
- fmt.Println("<- ", strings.TrimRight(line, "\r\n"))
|
51
|
|
- exiting = cs.processIncomingLine(line)
|
52
|
|
- }
|
53
|
|
- }
|
54
|
|
- }
|
55
|
|
- // empty the receiveLines queue
|
56
|
|
- cs.socket.Close()
|
57
|
|
- select {
|
58
|
|
- case <-cs.receiveLines:
|
59
|
|
- // empty
|
60
|
|
- default:
|
61
|
|
- // empty
|
62
|
|
- }
|
63
|
|
-}
|
64
|
|
-
|
65
|
|
-// RunSocketSender sends lines to the IRC socket.
|
66
|
|
-func (cs *ClientSocket) RunSocketSender() {
|
67
|
|
- var err error
|
68
|
|
- var line string
|
69
|
|
- for {
|
70
|
|
- line = <-cs.SendLines
|
71
|
|
- err = cs.socket.Write(line)
|
72
|
|
- fmt.Println(" ->", strings.TrimRight(line, "\r\n"))
|
73
|
|
- if err != nil {
|
74
|
|
- break
|
75
|
|
- }
|
76
|
|
- }
|
77
|
|
-}
|
78
|
|
-
|
79
|
|
-// RunSocketListener receives lines from the IRC socket.
|
80
|
|
-func (cs *ClientSocket) RunSocketListener() {
|
81
|
|
- var errConn error
|
82
|
|
- var line string
|
83
|
|
-
|
84
|
|
- for {
|
85
|
|
- line, errConn = cs.socket.Read()
|
86
|
|
- cs.receiveLines <- line
|
87
|
|
- if errConn != nil {
|
88
|
|
- break
|
89
|
|
- }
|
90
|
|
- }
|
91
|
|
- if !cs.socket.Closed {
|
92
|
|
- cs.Send(nil, "", "ERROR", "Closing connection")
|
93
|
|
- cs.socket.Close()
|
94
|
|
- }
|
95
|
|
-}
|
96
|
|
-
|
97
|
|
-// Send sends an IRC line to the listener.
|
98
|
|
-func (cs *ClientSocket) Send(tags *map[string]ircmsg.TagValue, prefix string, command string, params ...string) error {
|
99
|
|
- ircmsg := ircmsg.MakeMessage(tags, prefix, command, params...)
|
100
|
|
- line, err := ircmsg.Line()
|
101
|
|
- if err != nil {
|
102
|
|
- return err
|
103
|
|
- }
|
104
|
|
- cs.SendLines <- line
|
105
|
|
- return nil
|
106
|
|
-}
|
107
|
|
-
|
108
|
|
-// processIncomingLine splits and handles the given command line.
|
109
|
|
-// Returns true if client is exiting (sent a QUIT command, etc).
|
110
|
|
-func (cs *ClientSocket) processIncomingLine(line string) bool {
|
111
|
|
- msg, err := ircmsg.ParseLine(line)
|
112
|
|
- if err != nil {
|
113
|
|
- cs.Send(nil, "", "ERROR", "Your client sent a malformed line")
|
114
|
|
- return true
|
115
|
|
- }
|
116
|
|
-
|
117
|
|
- command, canBeParsed := Commands[msg.Command]
|
118
|
|
-
|
119
|
|
- if canBeParsed {
|
120
|
|
- return command.Run(cs.client.server, &cs.client, msg)
|
121
|
|
- }
|
122
|
|
- //TODO(dan): This is an error+disconnect purely for reasons of testing.
|
123
|
|
- // Later it may be downgraded to not-that-bad.
|
124
|
|
- cs.Send(nil, "", "ERROR", fmt.Sprintf("Your client sent a command that could not be parsed [%s]", msg.Command))
|
125
|
|
- return true
|
126
|
|
-}
|