-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchat.go
146 lines (133 loc) · 2.92 KB
/
chat.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
138
139
140
141
142
143
144
145
package main
import (
"encoding/json"
"fmt"
"time"
"strings"
"os"
)
type InChat struct {
Convo string
Name string
Message string
File string
FileName string
}
/* To be stored in the DB. */
type Chat struct {
IpAddr string
Name string
Trip string
Country string
Message string
Count uint64
Date time.Time
FilePath string
FileName string
FilePreview string
FileSize string
FileDimensions string
Convo string
UserID string
}
/* To be visible to users. */
type OutChat struct {
Name string
Trip string
Country string
Message string
Count uint64
Date time.Time
FilePath string
FileName string
FilePreview string
FileSize string
FileDimensions string
Convo string
Capcode string //for stuff like (you) and (mod)
}
func createChat(data []byte, conn *Connection) *Chat{
c := new(Chat)
inchat := new(InChat)
err:=json.Unmarshal(data, inchat)
if err != nil {
fmt.Println("error: ", err)
}
if len(inchat.File) > 0 && len(inchat.FileName) > 0 {
// TODO FilePreview, FileDimensions
fmt.Println(len(inchat.File))
c.FilePath = handleUpload(inchat.File, inchat.FileName);
c.FileName = inchat.FileName
}
c.Name = strings.TrimSpace(inchat.Name)
if len(c.Name) == 0 {
c.Name = "Anonymous"
}
c.Convo = strings.TrimSpace(inchat.Convo)
if len(c.Convo) == 0 {
c.Convo = "General"
}
c.Message = strings.TrimSpace(inchat.Message)
c.Date = time.Now().UTC()
c.IpAddr = ExtractIpv4(conn.ipAddr);
return c
}
func (chat *Chat) DeleteFile() {
os.Remove(fmt.Sprintf("upload/%s",chat.FilePath));
}
func (chat *Chat) genCapcode(conn *Connection) string {
cap := ""
if ExtractIpv4(conn.ipAddr) == chat.IpAddr {
cap = "(You)"
}
return cap
}
func (chat *Chat) createJSON(conn *Connection) []byte{
outChat := OutChat{
Name: chat.Name,
Message: chat.Message,
Date: chat.Date,
Count: chat.Count,
Convo: chat.Convo,
FilePath: chat.FilePath,
Capcode: chat.genCapcode(conn),
}
j, err := json.Marshal(outChat)
if err != nil {
fmt.Println("error: ", err)
}
return j
}
func createJSONs(chats []Chat, conn * Connection) []byte{
var outChats []OutChat
for _, chat := range chats {
outChat := OutChat{
Name: chat.Name,
Message: chat.Message,
Date: chat.Date,
Count: chat.Count,
Convo: chat.Convo,
FilePath: chat.FilePath,
Capcode: chat.genCapcode(conn),
}
outChats = append(outChats, outChat)
}
j, err := json.Marshal(outChats)
if err != nil {
fmt.Println("error: ", err)
}
return j
}
func (chat *Chat) canBroadcast(conn *Connection) bool{
if len(chat.Message) == 0 {
return false
}
var t = h.channels[conn.channelName][conn]
// limit minimum broadcast time to 4 seconds
if time.Now().Sub(t).Seconds() < 4 {
return false
}
h.channels[conn.channelName][conn] = time.Now()
chat.Count = storage.getCount(conn.channelName) + 1
return true
}