diff --git a/getArticles.go b/getArticles.go index e919ef7..0ea17f1 100644 --- a/getArticles.go +++ b/getArticles.go @@ -87,7 +87,6 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string, filte if err != nil { log.Fatal("Error: Can't create cache file:", err) } - defer file.Close() last = time.Now() @@ -96,6 +95,7 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string, filte lastUpdateJSON, _ := json.MarshalIndent(lastUpdate, "", " ") _, err = file.Write(lastUpdateJSON) if err != nil { + file.Close() log.Fatal("Error: Can't write last update time stamp to cache file:", err) } @@ -103,21 +103,24 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string, filte file, err = os.OpenFile(cacheFile, os.O_RDWR, 0o600) if err != nil { + file.Close() log.Fatal("Error: Can't open cache file:", err) } - defer file.Close() decoder := json.NewDecoder(file) lastUpdate := feedCache{} if err := decoder.Decode(&lastUpdate); err != nil { + file.Close() log.Fatal("Error: Can't decode laste updates time stamp:", err) } last, err = time.Parse(time.RFC3339, string(lastUpdate.LastChange)) if err != nil { + file.Close() log.Fatal("Error: Can't parse last updates time stamp:", err) } } + file.Close() fp := gofeed.NewParser() feed, err := fp.ParseURL(feedURL) @@ -139,12 +142,10 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string, filte if last.After(*feed.Items[0].UpdatedParsed) { return "", err } - } else { + } else if last.After(*feed.Items[0].PublishedParsed) { // If cached timestamp is newer than the one of // the last article return. - if last.After(*feed.Items[0].PublishedParsed) { - return "", err - } + return "", err } // Check last n (defined in config) articles for new ones. @@ -188,13 +189,14 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string, filte if err != nil { log.Fatal("Error: Can't create cache file:", err) } - defer file.Close() lastUpdateJSON, _ := json.MarshalIndent(lastUpdate, "", " ") _, err = file.Write(lastUpdateJSON) if err != nil { + file.Close() log.Fatal("Error: Can't write last update time stamp to cache file:", err) } + file.Close() // Remove redirects and tracking parameters from URL. cleanURL, _ := removeTracking(article.Link) @@ -249,7 +251,7 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string, filte } // Remove lines only consisting of "> "; thank you reddit. - description = strings.Replace(description, "> \n", "", -1) + description = strings.ReplaceAll(description, "> \n", "") // Split article description/content in single lines. lines := strings.Split(description, "\n") @@ -268,10 +270,10 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string, filte } } if !filterStrike { - description = description + line + description += line // Add new line, except for the last line. if i < descriptionLength-1 { - description = description + "\n" + description += "\n" } } } @@ -293,7 +295,7 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string, filte // Only append article link if it is not yet contained in description (e.g. read more: URL). if strings.Contains(description, article.Link) { // Replace article link with URL cleaned from redirects and trackers. - description = strings.Replace(description, article.Link, cleanURL, -1) + description = strings.ReplaceAll(description, article.Link, cleanURL) output = output + feed.Title + ": *" + article.Title + "*\n\n" + description } else { output = output + feed.Title + ": *" + article.Title + "*\n\n" + description + "\n" + cleanURL @@ -301,7 +303,7 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string, filte } if i > 0 { - output = output + "\n\n---\n\n" + output += "\n\n---\n\n" } } diff --git a/openConfig.go b/openConfig.go index 9d513c6..f52352e 100644 --- a/openConfig.go +++ b/openConfig.go @@ -62,12 +62,13 @@ func openConfig(configFilePtr *string) configuration { // Read configuration file into variable config. file, _ := os.Open(configFile) - defer file.Close() decoder := json.NewDecoder(file) config := configuration{} if err := decoder.Decode(&config); err != nil { + file.Close() log.Fatal("Error: Can't decode config: ", err) } + file.Close() if _, err := os.Stat(configFile); os.IsNotExist(err) { err = os.MkdirAll(configPath, 0o700) diff --git a/processStanzas.go b/processStanzas.go index 77a295c..eb002ea 100644 --- a/processStanzas.go +++ b/processStanzas.go @@ -24,8 +24,9 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str case xmpp.Chat: var command string + switch v.Type { // Check for room mention of the bots nick if the the message type is groupchat. - if v.Type == "groupchat" { + case "groupchat": // Leave if option quiet is set. if quiet { break @@ -52,9 +53,9 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str command = strings.ToLower(strings.Split(v.Text, " ")[1]) // If the message type is chat (e.g. private message), the command is the // first word. - } else if v.Type == "chat" { + case "chat": command = strings.ToLower(strings.Split(v.Text, " ")[0]) - } else { + default: break } @@ -204,8 +205,9 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str if v.Type == "get" { // Reply to disco#info requests according to https://xmpp.org/extensions/xep-0030.html. - if strings.Contains(string(v.Query), - "") { + switch { + case strings.Contains(string(v.Query), + ""): _, err := client.RawInformation(client.JID(), v.From, v.ID, "result", ""+ ""+ @@ -213,13 +215,13 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str if err != nil { log.Fatal("Error: Failed to reply to disco#info:", err) } - } else if strings.Contains(string(v.Query), "") { + case strings.Contains(string(v.Query), ""): // Reply to pings. _, err := client.RawInformation(client.JID(), v.From, v.ID, "result", "") if err != nil { log.Fatal("Error: Failed to reply to ping:", err) } - } else { + default: // Send error replies for all other IQs. _, err := client.RawInformation(client.JID(), v.From, v.ID, "error", "