Repository navigation
Expand file tree
/
Copy pathuser.go
More file actions
157 lines (132 loc) · 3.28 KB
/
Copy pathuser.go
File metadata and controls
157 lines (132 loc) · 3.28 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"C"
"io"
"strings"
"syscall"
"golang.org/x/crypto/ssh/terminal"
"github.com/scirocco6/icb"
"github.com/bobappleyard/readline"
)
// ReadFromUser is the main user input thread
func ReadFromUser() {
readline.Completer = NickCompleter
readline.CompletionAppendChar = ' '
var rfdset syscall.FdSet
for i := 0; i < 16; i++ {
rfdset.Bits[i] = 0
}
rfdset.Bits[0] = 1
for {
terminal.MakeRaw(0)
selerr := syscall.Select(1, &rfdset, nil, nil, nil)
if selerr != nil {
break
// log.Warning(selerr) // unsure what if anything could cause select to error here
}
screenMutex.Lock()
terminal.Restore(0, sane)
message, err := readline.String("")
screenMutex.Unlock()
if err == io.EOF {
break
}
if message == "" {
continue
}
readline.AddHistory(message)
parse(message)
}
}
func parse(message string) {
if strings.HasPrefix(message, "/") { // handle commands
message = message[1:]
command := strings.SplitN(message, " ", 3)
switch command[0] {
case "beep":
{
beep(command)
}
case "m": // send a private message to a user
{
privateMessage(command)
}
case "w": // obtain a listing of who is on
{
who(command)
}
case "g": // join a group
{
join(command)
}
case "q": // quit
{
cleanExit()
}
default:
{
PrintToScreen("Unrecognized command \n'/" + message + "'\n")
}
}
} else { // by defualt send a public message to the channel
publicMessage(message)
}
}
func beep(parameters []string) {
if len(parameters) != 2 {
PrintToScreen("Usage: /beep nick")
} else {
icb.CreatePacket("beep", parameters[1]).Send()
}
}
func publicMessage(message string) {
sendable, remainder := messageSplitter(message)
icb.CreatePacket("public", sendable).Send() // send the first part
if remainder != "" {
publicMessage(remainder) // send the rest if any
}
}
func privateMessage(parameters []string) {
if len(parameters) != 3 {
PrintToScreen("Usage: /m nick message")
}
PushNick(parameters[1])
sendPrivateMessage(parameters[1], parameters[2])
}
func sendPrivateMessage(nick string, message string) {
sendable, remainder := messageSplitter(message)
icb.CreatePacket("private", nick, sendable).Send() // send the first part
if remainder != "" {
sendPrivateMessage(nick, remainder) // send the rest if any
}
}
// messageSplitter takes a message and breaks it into sendable chunks. Preference is
// given to splitting on spaces
func messageSplitter(message string) (string, string) {
maxLength := 240
if len(message) <= maxLength {
return message, ""
}
sendable := message[:maxLength]
index := strings.LastIndex(sendable, " ") // if there are any spaces trim to that instead
if index == -1 {
index = maxLength
} else {
sendable = message[:index] // reduce the short message to the word before the space
}
return sendable, message[index+1:]
}
func who(parameters []string) {
if len(parameters) == 1 { // obtain listing of who is in every group
icb.CreatePacket("global_who").Send()
} else { // listing for who is in a particular group or group a user is in
icb.CreatePacket("local_who", parameters[1]).Send()
}
}
func join(parameters []string) {
if len(parameters) != 2 {
PrintToScreen("Usage: /g group")
} else {
icb.CreatePacket("join", parameters[1]).Send()
}
}