-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
137 lines (115 loc) · 2.73 KB
/
client.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bufio"
"crypto/rsa"
"fmt"
"log"
"net"
"strings"
)
// structure of a client i.e a user ( a new connection, will have this structure )
type client struct {
// client connection details
conn net.Conn
// identifier for the client :
// client will be known on the server by this name
name string
// identifier for the (other) client :
// the other client (person), this client is talking to currently
contact string
// commands to facilitate chat system
commands chan<- command
// private
private *rsa.PrivateKey
// public
public rsa.PublicKey
}
// function to read input
func (c *client) readInput() {
// continuously...
for {
// read user input
msg, err := bufio.NewReader(c.conn).ReadString('\n')
if err != nil {
// abort if an error occurs
return
}
// process input, to parse commands
msg = strings.Trim(msg, "\r\n")
args := strings.Split(msg, " ")
cmd := strings.TrimSpace(args[0])
// update client command for desired command
switch cmd {
case "/name":
// specify your name
c.commands <- command{
id: cmdName,
client: c,
args: args,
}
case "/join":
// connect to another user :
// to be able to chat with him/her
c.commands <- command{
id: cmdJoin,
client: c,
args: args,
}
case "/list":
// display all the available users on the server :
// these are ones you ( a client ) can join and chat to
c.commands <- command{
id: cmdList,
client: c,
}
case "/msg":
// send a message to the user ( another client ) you have joined
c.commands <- command{
id: cmdMsg,
client: c,
args: args,
}
case "/quit":
// exit the chat system
c.commands <- command{
id: cmdQuit,
client: c,
}
case "/help":
// return command list
c.commands <- command{
id: cmdHelp,
client: c,
}
// for any other command
default:
c.err(fmt.Errorf("unknown command: %s", cmd))
c.msg(c, "* use '/help' to command to send message to selected user")
}
}
}
// writes an error message current client
func (c *client) err(err error) {
_, e := c.conn.Write([]byte("err: " + err.Error() + "\n"))
if e != nil {
log.Fatalln("unable to write to connection", e)
}
}
// writes a message to specified client
func (c *client) msg(x *client, msg string) {
// if contacting other client
if c.private != x.private {
dMsg := decrypt(msg, *x.private)
// write message to client
_, e := x.conn.Write([]byte("> " + dMsg + "\n"))
if e != nil {
log.Fatalln("unable to write over client connection")
}
} else {
// write message to client
_, e := x.conn.Write([]byte("> " + msg + "\n"))
if e != nil {
log.Fatalln("unable to write over client connection")
}
}
}