-
Notifications
You must be signed in to change notification settings - Fork 10
/
toxCallbacks.go
192 lines (157 loc) · 5.55 KB
/
toxCallbacks.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"encoding/hex"
"encoding/json"
"github.com/codedust/go-tox"
"log"
"os"
"time"
)
func onFriendRequest(t *gotox.Tox, publicKey []byte, message string) {
log.Printf("New friend request from %s\n", hex.EncodeToString(publicKey))
storage.StoreFriendRequest(hex.EncodeToString(publicKey), message)
broadcastToClients(createSimpleJSONEvent("friend_requests_update"))
}
func onFriendMessage(t *gotox.Tox, friendnumber uint32, messagetype gotox.ToxMessageType, message string) {
type jsonEvent struct {
Type string `json:"type"`
Friend uint32 `json:"friend"`
Time int64 `json:"time"`
Message string `json:"message"`
IsAction bool `json:"isAction"`
}
e, _ := json.Marshal(jsonEvent{
Type: "friend_message",
Friend: friendnumber,
Time: time.Now().Unix() * 1000,
Message: message,
IsAction: messagetype == gotox.TOX_MESSAGE_TYPE_ACTION,
})
publicKey, _ := tox.FriendGetPublickey(friendnumber)
storage.StoreMessage(hex.EncodeToString(publicKey), true, messagetype == gotox.TOX_MESSAGE_TYPE_ACTION, message)
broadcastToClients(string(e))
}
func onFriendConnectionStatusChanges(t *gotox.Tox, friendnumber uint32, connectionStatus gotox.ToxConnection) {
type jsonEvent struct {
Type string `json:"type"`
Friend uint32 `json:"friend"`
Online bool `json:"online"`
}
e, _ := json.Marshal(jsonEvent{
Type: "connection_status",
Friend: friendnumber,
Online: connectionStatus != gotox.TOX_CONNECTION_NONE,
})
broadcastToClients(string(e))
}
func onFriendNameChanges(t *gotox.Tox, friendnumber uint32, newname string) {
type jsonEvent struct {
Type string `json:"type"`
Friend uint32 `json:"friend"`
Name string `json:"name"`
}
e, _ := json.Marshal(jsonEvent{
Type: "name_changed",
Friend: friendnumber,
Name: newname,
})
broadcastToClients(string(e))
}
func onFriendStatusMessageChanges(t *gotox.Tox, friendnumber uint32, status string) {
type jsonEvent struct {
Type string `json:"type"`
Friend uint32 `json:"friend"`
StatusMsg string `json:"status_msg"`
}
e, _ := json.Marshal(jsonEvent{
Type: "status_message_changed",
Friend: friendnumber,
StatusMsg: status,
})
broadcastToClients(string(e))
}
func onFriendStatusChanges(t *gotox.Tox, friendnumber uint32, userstatus gotox.ToxUserStatus) {
type jsonEvent struct {
Type string `json:"type"`
Friend uint32 `json:"friend"`
Status string `json:"status"`
}
e, _ := json.Marshal(jsonEvent{
Type: "status_changed",
Friend: friendnumber,
Status: getUserStatusAsString(userstatus),
})
broadcastToClients(string(e))
}
func onFileRecv(t *gotox.Tox, friendnumber uint32, filenumber uint32, kind gotox.ToxFileKind, filesize uint64, filename string) {
if kind == gotox.TOX_FILE_KIND_AVATAR {
publicKey, _ := tox.FriendGetPublickey(friendnumber)
file, err := os.Create("../html/avatars/" + hex.EncodeToString(publicKey) + ".png")
if err != nil {
log.Println("[ERROR] Error creating file", "../html/avatars/"+hex.EncodeToString(publicKey)+".png")
}
// only accept avatars with a file size <= CFG_MAX_AVATAR_SIZE
if filesize <= CFG_MAX_AVATAR_SIZE {
// append the file to the map of active file transfers
transfers[filenumber] = FileTransfer{fileHandle: file, fileSize: filesize, fileKind: kind}
t.FileControl(friendnumber, filenumber, gotox.TOX_FILE_CONTROL_RESUME)
} else {
t.FileControl(friendnumber, filenumber, gotox.TOX_FILE_CONTROL_CANCEL)
}
} else if kind == gotox.TOX_FILE_KIND_DATA {
file, err := os.Create("../html/download/" + filename)
if err != nil {
log.Println("[ERROR] Error creating file", "../html/download/"+filename)
}
// append the file to the map of active file transfers
transfers[filenumber] = FileTransfer{fileHandle: file, fileSize: filesize, fileKind: kind}
// TODO do not accept any file send request without asking the user
t.FileControl(friendnumber, filenumber, gotox.TOX_FILE_CONTROL_RESUME)
} else {
log.Print("onFileRecv: unknown TOX_FILE_KIND: ", kind)
}
}
func onFileRecvControl(t *gotox.Tox, friendnumber uint32, filenumber uint32, fileControl gotox.ToxFileControl) {
transfer, ok := transfers[filenumber]
if !ok {
log.Println("Error: File handle does not exist")
return
}
// TODO handle TOX_FILE_CONTROL_RESUME and TOX_FILE_CONTROL_PAUSE
if fileControl == gotox.TOX_FILE_CONTROL_CANCEL {
// delete file handle
transfer.fileHandle.Close()
delete(transfers, filenumber)
}
}
func onFileRecvChunk(t *gotox.Tox, friendnumber uint32, filenumber uint32, position uint64, data []byte) {
transfer, ok := transfers[filenumber]
if !ok {
if len(data) == 0 {
// ignore the zero-length chunk that indicates that the transfer is
// complete (see below)
return
}
log.Println("Error: File handle does not exist")
return
}
// write data to the file handle
transfer.fileHandle.WriteAt(data, (int64)(position))
// file transfer completed
if position+uint64(len(data)) >= transfer.fileSize {
// Some clients will send us another zero-length chunk without data (only
// required for stream, not necessary for files with a known size) and some
// will not.
// We will delete the file handle now (we aleady reveived the whole file)
// and ignore the file handle error when the empty chunk arrives.
fileKind := transfer.fileKind
transfer.fileHandle.Sync()
transfer.fileHandle.Close()
delete(transfers, filenumber)
log.Println("File transfer completed (receiving)", filenumber)
if fileKind == gotox.TOX_FILE_KIND_AVATAR {
// update friendlist
broadcastToClients(createSimpleJSONEvent("avatar_update"))
}
}
}