mirror of
https://salsa.debian.org/mdosch/feed-to-muc.git
synced 2024-12-22 04:45:25 +01:00
[gocritic]: Improve code quality.
This commit is contained in:
parent
0d28cd318f
commit
91e91b68ae
3 changed files with 25 additions and 20 deletions
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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),
|
||||
"<query xmlns='http://jabber.org/protocol/disco#info'/>") {
|
||||
switch {
|
||||
case strings.Contains(string(v.Query),
|
||||
"<query xmlns='http://jabber.org/protocol/disco#info'/>"):
|
||||
_, err := client.RawInformation(client.JID(), v.From, v.ID,
|
||||
"result", "<query xmlns='http://jabber.org/protocol/disco#info'>"+
|
||||
"<identity category='client' type='bot' name='feedbot'/>"+
|
||||
|
@ -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), "<ping xmlns='urn:xmpp:ping'/>") {
|
||||
case strings.Contains(string(v.Query), "<ping xmlns='urn:xmpp:ping'/>"):
|
||||
// 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",
|
||||
"<error type='cancel'><service-unavailable "+
|
||||
|
|
Loading…
Reference in a new issue