-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
348 lines (296 loc) · 10.3 KB
/
main.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 (
"embed"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"time"
"github.com/gen2brain/beeep"
"github.com/getlantern/systray"
)
//go:embed icon.ico
var content embed.FS
var DEBUG = 0
// Config represents the configuration structure
type Config struct {
XFToken string `json:"_xfToken"`
Cookie string `json:"cookie"`
}
// Define a struct to represent the JSON data
type Data struct {
Visitor struct {
TotalUnread json.Number `json:"total_unread"`
} `json:"visitor"`
}
// Define a constant for the version
const version = "1.1.0" // Replace with your actual version
// main is the entry point function for the program.
//
// It opens a log file for writing, sets the log output to both the file and standard output,
// checks if a config file exists and creates it if not,
// reads the configuration from the config file,
// and initializes the system tray.
func main() {
// Open the log file for writing
logFile, err := os.Create("runtime.log")
if err != nil {
// If there's an error opening the log file, log it and continue
log.Println("Error opening log file:", err)
}
// Set the log output to both the file and standard output
log.SetOutput(io.MultiWriter(os.Stdout, logFile))
// Close the log file when you're done
logFile.Close()
// Check if config file exists, create it if not
configPath := "config.json"
if _, err := os.Stat(configPath); os.IsNotExist(err) {
if err := createDefaultConfig(configPath); err != nil {
log.Fatalf("Error creating default config: %v\n", err)
}
}
// Read the configuration
config, err := readConfig(configPath)
if err != nil {
log.Fatalf("Error reading config: %v\n", err)
}
// If values are empty, close the program
if config.XFToken == "" || config.Cookie == "" {
log.Fatal("XFToken and Cookie values are required. Closing the program.")
}
// Initialize the system tray
systray.Run(onReady(config), onExit)
}
// onReady sets up the system tray icon and menu.
//
// It takes a `config` parameter of type `Config` and returns a function that
// sets up the system tray icon and menu.
func onReady(config Config) func() {
return func() {
// Read the icon file
iconData, err := content.ReadFile("icon.ico")
if err != nil {
log.Printf("Error reading embedded icon file: %v\n", err)
return
}
// Set up the system tray icon and tooltip
systray.SetIcon(iconData)
systray.SetTooltip("F95-Notify v" + version)
// Add menu items
mViewNotifications := systray.AddMenuItem("View Notifications", "View unread notifications")
mVisitGithub := systray.AddMenuItem("Visit Github", "Visit the GitHub repository")
mQuit := systray.AddMenuItem("Quit", "Quit the application")
// Run the background process in a goroutine
go runBackgroundProcess(config)
// Handle menu item clicks
go func() {
for {
select {
case <-mViewNotifications.ClickedCh:
openURL("https://f95zone.to/account/alerts") // Replace with your actual URL
case <-mVisitGithub.ClickedCh:
openURL("https://github.com/Official-Husko/f95-notify") // Replace with your actual GitHub URL
case <-mQuit.ClickedCh:
log.Println("Quit menu item clicked")
systray.Quit()
}
}
}()
}
}
// openURL opens the specified URL in the default web browser.
//
// It takes a string parameter `url` which represents the URL to be opened.
// The function does not return anything.
func openURL(url string) {
// Command represents an external command being prepared or run.
cmd := exec.Command("rundll32", "url.dll,FileProtocolHandler", url)
// Start starts the specified command but does not wait for it to complete.
err := cmd.Start()
if err != nil {
// Printf formats its arguments according to the format, analogous to C's printf.
log.Printf("Error opening URL: %v\n", err)
}
}
// onExit is a function that performs cleanup tasks before exiting the program.
//
// It does not take any parameters.
// It does not return any values.
func onExit() {
// Print a log message indicating that cleanup is being performed
log.Println("Cleaning up...")
// Print a message indicating that the program is exiting
fmt.Println("Exiting...")
// Terminate the program with exit code 0
os.Exit(0)
}
// runBackgroundProcess runs a background process that periodically makes a GET request to a specified URL
// and processes the response.
//
// It takes a Config struct as a parameter which contains the necessary configuration values such as the
// URL, headers, and authentication tokens.
//
// The function does not return anything.
func runBackgroundProcess(config Config) {
// set up a ticker to trigger the function periodically
var ticker *time.Ticker
if DEBUG == 1 {
ticker = time.NewTicker(5 * time.Second)
log.Println("Debugging detected: using shorter ticker duration")
} else {
ticker = time.NewTicker(5 * time.Minute)
}
defer ticker.Stop() // stop the ticker when the function ends
for range ticker.C { // iterate over the ticker channel
// specify the URL for the GET request
url := "https://f95zone.to/account/unread-alert?_xfResponseType=json"
// set up the headers for the GET request
headers := map[string]string{
"User-Agent": fmt.Sprintf("F95 Notify/%s (by Official Husko on GitHub)", version),
"Accept": "application/json",
"Cookie": config.Cookie,
"_xfToken": config.XFToken,
}
// make the GET request
response, err := makeGetRequest(url, headers)
if err != nil {
log.Printf("Error making GET request: %v\n", err) // log the error and continue to the next iteration
continue
}
log.Printf("Response: %s\n", string(response)) // log the response for further analysis
var data Data // create a variable to hold the unmarshalled response
err = json.Unmarshal(response, &data) // unmarshal the response into the data variable
if err != nil {
log.Printf("Error unmarshalling JSON: %v\n", err) // log the error and continue to the next iteration
continue
}
// Check if "visitor" field and "total_unread" field are present
if data.Visitor.TotalUnread == "" {
log.Println("Error: 'total_unread' field is missing in the response JSON")
// Send a notification with "invalid" as title and body
title := "Outdated Session?"
body := "The server returned no data. Please update your Token and Cookie."
sendNotification(title, body)
}
totalUnread, err := data.Visitor.TotalUnread.Int64()
if err != nil {
log.Println("Error converting TotalUnread to int64:", err)
} else if totalUnread > 0 { // check if totalUnread is greater than 0
log.Printf("Total Unread: %d\n", totalUnread)
title := "New Notifications!"
body := fmt.Sprintf("You have %d unread notifications.", totalUnread)
sendNotification(title, body)
}
}
}
// sendNotification sends a notification with the given title and message.
//
// Parameters:
// - title: The title of the notification.
// - message: The message content of the notification.
// Return type: None.
func sendNotification(title, message string) {
// Use the beeep.Notify function to send a notification with the given title, message, and icon.
err := beeep.Notify(title, message, "icon.ico")
// If there is an error while sending the notification, log the error.
if err != nil {
log.Printf("Error sending notification: %v\n", err)
}
}
// makeGetRequest sends a GET request to the specified URL with the given headers and returns the response body as a byte array.
//
// Parameters:
// - url: The URL to send the GET request to.
// - headers: A map of headers to include in the request.
//
// Returns:
// - []byte: The response body as a byte array.
// - error: An error if there was a problem sending the request or reading the response body.
func makeGetRequest(url string, headers map[string]string) ([]byte, error) {
// Create a new HTTP client
client := &http.Client{}
// Create a new HTTP GET request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Printf("Error creating request: %s", err)
return nil, err
}
// Set headers
for key, value := range headers {
req.Header.Set(key, value)
}
// Send the HTTP request
resp, err := client.Do(req)
if err != nil {
log.Printf("Error sending request: %s", err)
return nil, err
}
defer resp.Body.Close()
// Read the response body using io.ReadAll
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("Error reading response body: %s", err)
return nil, err
}
// Return the response body
return body, nil
}
// createDefaultConfig creates a default configuration and writes it to a file.
//
// Parameters:
// - filePath: the path to the file where the default configuration will be written.
//
// Returns:
// - error: if there was an error creating or writing the default configuration.
func createDefaultConfig(filePath string) error {
// Define the default configuration
defaultConfig := Config{
XFToken: "Your XF Token",
Cookie: "Your Cookie",
}
// Convert the configuration to JSON
configJSON, err := json.MarshalIndent(defaultConfig, "", " ")
if err != nil {
log.Printf("Error marshaling default configuration to JSON: %s", err)
return err
}
// Write the default configuration to the file
err = os.WriteFile(filePath, configJSON, 0644)
if err != nil {
log.Printf("Error writing default configuration to file: %s", err)
return err
}
// Print success message
fmt.Println("Default config created successfully.")
// Explicitly exit the program
os.Exit(0)
return nil
}
// readConfig reads the configuration file and returns a Config struct and an error.
//
// It takes a filePath string as a parameter, which represents the path of the configuration file.
// It returns a Config struct, which contains the configuration data, and an error if any error occurs while reading or unmarshaling the configuration file.
func readConfig(filePath string) (Config, error) {
// Read the configuration file
configJSON, err := os.ReadFile(filePath)
if err != nil {
log.Printf("Failed to read configuration file: %v", err)
return Config{}, err
}
// Unmarshal the configuration
var config Config
err = json.Unmarshal(configJSON, &config)
if err != nil {
log.Printf("Failed to unmarshal configuration: %v", err)
return Config{}, err
}
// Check if XFToken or Cookie is equal to default values
if config.XFToken == "Your XF Token" || config.Cookie == "Your Cookie" {
log.Println("Config values are set to default. Exiting.")
os.Exit(0)
}
return config, nil
}