mirror of
https://salsa.debian.org/mdosch/feed-to-muc.git
synced 2024-11-22 14:08:39 +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 {
|
if err != nil {
|
||||||
log.Fatal("Error: Can't create cache file:", err)
|
log.Fatal("Error: Can't create cache file:", err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
last = time.Now()
|
last = time.Now()
|
||||||
|
|
||||||
|
@ -96,6 +95,7 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string, filte
|
||||||
lastUpdateJSON, _ := json.MarshalIndent(lastUpdate, "", " ")
|
lastUpdateJSON, _ := json.MarshalIndent(lastUpdate, "", " ")
|
||||||
_, err = file.Write(lastUpdateJSON)
|
_, err = file.Write(lastUpdateJSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
file.Close()
|
||||||
log.Fatal("Error: Can't write last update time stamp to cache file:", err)
|
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)
|
file, err = os.OpenFile(cacheFile, os.O_RDWR, 0o600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
file.Close()
|
||||||
log.Fatal("Error: Can't open cache file:", err)
|
log.Fatal("Error: Can't open cache file:", err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
decoder := json.NewDecoder(file)
|
decoder := json.NewDecoder(file)
|
||||||
lastUpdate := feedCache{}
|
lastUpdate := feedCache{}
|
||||||
if err := decoder.Decode(&lastUpdate); err != nil {
|
if err := decoder.Decode(&lastUpdate); err != nil {
|
||||||
|
file.Close()
|
||||||
log.Fatal("Error: Can't decode laste updates time stamp:", err)
|
log.Fatal("Error: Can't decode laste updates time stamp:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
last, err = time.Parse(time.RFC3339, string(lastUpdate.LastChange))
|
last, err = time.Parse(time.RFC3339, string(lastUpdate.LastChange))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
file.Close()
|
||||||
log.Fatal("Error: Can't parse last updates time stamp:", err)
|
log.Fatal("Error: Can't parse last updates time stamp:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
file.Close()
|
||||||
|
|
||||||
fp := gofeed.NewParser()
|
fp := gofeed.NewParser()
|
||||||
feed, err := fp.ParseURL(feedURL)
|
feed, err := fp.ParseURL(feedURL)
|
||||||
|
@ -139,13 +142,11 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string, filte
|
||||||
if last.After(*feed.Items[0].UpdatedParsed) {
|
if last.After(*feed.Items[0].UpdatedParsed) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
} else {
|
} else if last.After(*feed.Items[0].PublishedParsed) {
|
||||||
// If cached timestamp is newer than the one of
|
// If cached timestamp is newer than the one of
|
||||||
// the last article return.
|
// 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.
|
// Check last n (defined in config) articles for new ones.
|
||||||
for i := max - 1; i >= 0; i-- {
|
for i := max - 1; i >= 0; i-- {
|
||||||
|
@ -188,13 +189,14 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string, filte
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error: Can't create cache file:", err)
|
log.Fatal("Error: Can't create cache file:", err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
lastUpdateJSON, _ := json.MarshalIndent(lastUpdate, "", " ")
|
lastUpdateJSON, _ := json.MarshalIndent(lastUpdate, "", " ")
|
||||||
_, err = file.Write(lastUpdateJSON)
|
_, err = file.Write(lastUpdateJSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
file.Close()
|
||||||
log.Fatal("Error: Can't write last update time stamp to cache file:", err)
|
log.Fatal("Error: Can't write last update time stamp to cache file:", err)
|
||||||
}
|
}
|
||||||
|
file.Close()
|
||||||
|
|
||||||
// Remove redirects and tracking parameters from URL.
|
// Remove redirects and tracking parameters from URL.
|
||||||
cleanURL, _ := removeTracking(article.Link)
|
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.
|
// 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.
|
// Split article description/content in single lines.
|
||||||
lines := strings.Split(description, "\n")
|
lines := strings.Split(description, "\n")
|
||||||
|
@ -268,10 +270,10 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string, filte
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !filterStrike {
|
if !filterStrike {
|
||||||
description = description + line
|
description += line
|
||||||
// Add new line, except for the last line.
|
// Add new line, except for the last line.
|
||||||
if i < descriptionLength-1 {
|
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).
|
// Only append article link if it is not yet contained in description (e.g. read more: URL).
|
||||||
if strings.Contains(description, article.Link) {
|
if strings.Contains(description, article.Link) {
|
||||||
// Replace article link with URL cleaned from redirects and trackers.
|
// 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
|
output = output + feed.Title + ": *" + article.Title + "*\n\n" + description
|
||||||
} else {
|
} else {
|
||||||
output = output + feed.Title + ": *" + article.Title + "*\n\n" + description + "\n" + cleanURL
|
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 {
|
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.
|
// Read configuration file into variable config.
|
||||||
file, _ := os.Open(configFile)
|
file, _ := os.Open(configFile)
|
||||||
defer file.Close()
|
|
||||||
decoder := json.NewDecoder(file)
|
decoder := json.NewDecoder(file)
|
||||||
config := configuration{}
|
config := configuration{}
|
||||||
if err := decoder.Decode(&config); err != nil {
|
if err := decoder.Decode(&config); err != nil {
|
||||||
|
file.Close()
|
||||||
log.Fatal("Error: Can't decode config: ", err)
|
log.Fatal("Error: Can't decode config: ", err)
|
||||||
}
|
}
|
||||||
|
file.Close()
|
||||||
|
|
||||||
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
||||||
err = os.MkdirAll(configPath, 0o700)
|
err = os.MkdirAll(configPath, 0o700)
|
||||||
|
|
|
@ -24,8 +24,9 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str
|
||||||
case xmpp.Chat:
|
case xmpp.Chat:
|
||||||
var command string
|
var command string
|
||||||
|
|
||||||
|
switch v.Type {
|
||||||
// Check for room mention of the bots nick if the the message type is groupchat.
|
// 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.
|
// Leave if option quiet is set.
|
||||||
if quiet {
|
if quiet {
|
||||||
break
|
break
|
||||||
|
@ -52,9 +53,9 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str
|
||||||
command = strings.ToLower(strings.Split(v.Text, " ")[1])
|
command = strings.ToLower(strings.Split(v.Text, " ")[1])
|
||||||
// If the message type is chat (e.g. private message), the command is the
|
// If the message type is chat (e.g. private message), the command is the
|
||||||
// first word.
|
// first word.
|
||||||
} else if v.Type == "chat" {
|
case "chat":
|
||||||
command = strings.ToLower(strings.Split(v.Text, " ")[0])
|
command = strings.ToLower(strings.Split(v.Text, " ")[0])
|
||||||
} else {
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -204,8 +205,9 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str
|
||||||
|
|
||||||
if v.Type == "get" {
|
if v.Type == "get" {
|
||||||
// Reply to disco#info requests according to https://xmpp.org/extensions/xep-0030.html.
|
// Reply to disco#info requests according to https://xmpp.org/extensions/xep-0030.html.
|
||||||
if strings.Contains(string(v.Query),
|
switch {
|
||||||
"<query xmlns='http://jabber.org/protocol/disco#info'/>") {
|
case strings.Contains(string(v.Query),
|
||||||
|
"<query xmlns='http://jabber.org/protocol/disco#info'/>"):
|
||||||
_, err := client.RawInformation(client.JID(), v.From, v.ID,
|
_, err := client.RawInformation(client.JID(), v.From, v.ID,
|
||||||
"result", "<query xmlns='http://jabber.org/protocol/disco#info'>"+
|
"result", "<query xmlns='http://jabber.org/protocol/disco#info'>"+
|
||||||
"<identity category='client' type='bot' name='feedbot'/>"+
|
"<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 {
|
if err != nil {
|
||||||
log.Fatal("Error: Failed to reply to disco#info:", err)
|
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.
|
// Reply to pings.
|
||||||
_, err := client.RawInformation(client.JID(), v.From, v.ID, "result", "")
|
_, err := client.RawInformation(client.JID(), v.From, v.ID, "result", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error: Failed to reply to ping:", err)
|
log.Fatal("Error: Failed to reply to ping:", err)
|
||||||
}
|
}
|
||||||
} else {
|
default:
|
||||||
// Send error replies for all other IQs.
|
// Send error replies for all other IQs.
|
||||||
_, err := client.RawInformation(client.JID(), v.From, v.ID, "error",
|
_, err := client.RawInformation(client.JID(), v.From, v.ID, "error",
|
||||||
"<error type='cancel'><service-unavailable "+
|
"<error type='cancel'><service-unavailable "+
|
||||||
|
|
Loading…
Reference in a new issue