From 44ee1cf6287e38ade7aea75135b6ea4da945af65 Mon Sep 17 00:00:00 2001 From: Andreas Neue Date: Tue, 16 Feb 2016 07:57:14 +0100 Subject: [PATCH] Added module configuration params: -params=:(!:])* --- main.go | 2 +- modules/announcements.go | 2 +- modules/coffee.go | 10 ++++----- modules/fortune.go | 4 ++-- modules/fuzzytime.go | 4 ++-- modules/rss.go | 17 +++++++++----- modules/sc.go | 48 ++++++++++++++++++++-------------------- modules/stoll.go | 4 ++-- modules/twitch.go | 10 ++++----- modules/weather.go | 21 ++++++++++++------ 10 files changed, 67 insertions(+), 55 deletions(-) diff --git a/main.go b/main.go index a2e46d3..f9a96da 100644 --- a/main.go +++ b/main.go @@ -52,7 +52,7 @@ func main() { //mods := strings.Split(*modules, ",") //TODO: implement more robust list parsing - modules.Initialize(sayCh) + modules.Init(sayCh, *params) go func() { for { diff --git a/modules/announcements.go b/modules/announcements.go index 17abd33..3847e9b 100644 --- a/modules/announcements.go +++ b/modules/announcements.go @@ -12,7 +12,7 @@ import ( var () func init() { - msgHandlers["announcements"] = anncouncementsHandleMessage + MsgHandlers["announcements"] = anncouncementsHandleMessage log.Println("Initializing announcements module") } diff --git a/modules/coffee.go b/modules/coffee.go index eaa978e..03c51c9 100644 --- a/modules/coffee.go +++ b/modules/coffee.go @@ -15,7 +15,7 @@ import ( ) func init() { - msgHandlers["coffee"] = coffeeHandleMessage + MsgHandlers["coffee"] = coffeeHandleMessage log.Println("Initializing coffee module") } @@ -37,9 +37,9 @@ func coffeeHandleMessage(m *irc.Message) { } func coffeeMake(rec []string) { - sayCh <- "*\nsetzt Kaffee auf." + SayCh <- "*\nsetzt Kaffee auf." time.Sleep(30 * time.Second) - sayCh <- "*\nstellt eine frische Kanne Kaffee in den Raum." + SayCh <- "*\nstellt eine frische Kanne Kaffee in den Raum." if rec != nil { var users string for i, v := range rec { @@ -50,8 +50,8 @@ func coffeeMake(rec []string) { users += " und " } } - sayCh <- "*\ngibt " + users + " einen frischen, richtig schwarzen, richtig leckeren Kaffee." + SayCh <- "*\ngibt " + users + " einen frischen, richtig schwarzen, richtig leckeren Kaffee." } time.Sleep(10 * time.Second) - sayCh <- "*\nProst! (c)" + SayCh <- "*\nProst! (c)" } diff --git a/modules/fortune.go b/modules/fortune.go index e9dd649..b0db561 100644 --- a/modules/fortune.go +++ b/modules/fortune.go @@ -13,7 +13,7 @@ import ( ) func init() { - msgHandlers["fortune"] = fortuneHandleMessage + MsgHandlers["fortune"] = fortuneHandleMessage log.Println("Initializing fortune module") } @@ -43,7 +43,7 @@ func fortune() { s = strings.Replace(s, "\t", " ", -1) for _, l := range strings.Split(s, "\n") { if l != "" { - sayCh <- fmt.Sprintf("*\n%s", l) + SayCh <- fmt.Sprintf("*\n%s", l) } } } diff --git a/modules/fuzzytime.go b/modules/fuzzytime.go index dab1a00..38a09c3 100644 --- a/modules/fuzzytime.go +++ b/modules/fuzzytime.go @@ -12,7 +12,7 @@ import ( ) func init() { - msgHandlers["fuzzytime"] = fuzzytimeHandleMessage + MsgHandlers["fuzzytime"] = fuzzytimeHandleMessage log.Println("Initializing fuzzytime module") } @@ -67,7 +67,7 @@ func fuzzytimeShow() { s += fmt.Sprintf("%s Uhr\n", fuzzytimeSayHour(h+1)) } log.Println("saying now:", s) - sayCh <- fmt.Sprintf("*\n%s", s) + SayCh <- fmt.Sprintf("*\n%s", s) } diff --git a/modules/rss.go b/modules/rss.go index e9e8512..4000082 100644 --- a/modules/rss.go +++ b/modules/rss.go @@ -23,9 +23,14 @@ import ( var hideOutput = true func init() { - msgHandlers["rss"] = rssHandleMessage - log.Printf("Initializing news module") - path := "newsfeeds.conf" + MsgHandlers["rss"] = rssHandleMessage + log.Printf("Initializing rss module") + go rssRun() +} + +func rssRun() { + time.Sleep(5 * time.Second) + path := ModParams["rss-feeds"] file, err := os.Open(path) if err != nil { log.Fatal(err) @@ -53,7 +58,7 @@ func rssPollFeed(uri string, timeout int, cr xmlx.CharsetFunc) { log.Printf("Polling feed: %s", uri) if err := feed.Fetch(uri, cr); err != nil { log.Printf("[e] %s: %s", "*", uri, err) - sayCh <- fmt.Sprintf("%s\n[RSS] Error %s: %s", "*", uri, err) + SayCh <- fmt.Sprintf("%s\n[RSS] Error %s: %s", "*", uri, err) return } <-time.After(time.Duration(feed.SecondsTillUpdate() * 1e9)) @@ -61,7 +66,7 @@ func rssPollFeed(uri string, timeout int, cr xmlx.CharsetFunc) { } func rssChanHandler(feed *gorss.Feed, newchannels []*gorss.Channel) { - sayCh <- fmt.Sprintf("%s\n[RSS] %d new channel(s) in %s", "*", len(newchannels), feed.Url) + SayCh <- fmt.Sprintf("%s\n[RSS] %d new channel(s) in %s", "*", len(newchannels), feed.Url) } func rssItemHandler(feed *gorss.Feed, ch *gorss.Channel, newitems []*gorss.Item) { @@ -69,7 +74,7 @@ func rssItemHandler(feed *gorss.Feed, ch *gorss.Channel, newitems []*gorss.Item) return } for _, ni := range newitems { - sayCh <- fmt.Sprintf("%s\n[RSS] %v - %v", "*", ni.Title, ni.Links[0].Href) + SayCh <- fmt.Sprintf("%s\n[RSS] %v - %v", "*", ni.Title, ni.Links[0].Href) } } diff --git a/modules/sc.go b/modules/sc.go index 4cf9cd8..96ad87f 100644 --- a/modules/sc.go +++ b/modules/sc.go @@ -47,7 +47,7 @@ var ( ) func init() { - msgHandlers["sc"] = scHandleMessage + MsgHandlers["sc"] = scHandleMessage log.Println("Initializing sc module") go scScrapeLoop() } @@ -111,13 +111,13 @@ func scScraper() { nextFunds := ((funds / FUNDS_INT) * FUNDS_INT) + FUNDS_INT if curFans >= nextFans { - sayCh <- "*\n[SC] Star Citizens: " + util.NumberToString(curFans, '.') + SayCh <- "*\n[SC] Star Citizens: " + util.NumberToString(curFans, '.') } if curFleet >= nextFleet { - sayCh <- "*\n[SC] The UEE Fleet: " + util.NumberToString(curFleet, '.') + SayCh <- "*\n[SC] The UEE Fleet: " + util.NumberToString(curFleet, '.') } if curFunds >= nextFunds { - sayCh <- "*\n[SC] Funds raised: " + util.NumberToString(curFunds, '.') + SayCh <- "*\n[SC] Funds raised: " + util.NumberToString(curFunds, '.') } fans = curFans @@ -126,10 +126,10 @@ func scScraper() { } func showScStats() { - sayCh <- "*\n*** SC User and Funding Stats ***" - sayCh <- fmt.Sprintf("*\nFans: %s", util.NumberToString(fans, '.')) - sayCh <- fmt.Sprintf("*\nFleet: %s", util.NumberToString(fleet, '.')) - sayCh <- fmt.Sprintf("*\nFunds: $ %s", util.NumberToString(funds, '.')) + SayCh <- "*\n*** SC User and Funding Stats ***" + SayCh <- fmt.Sprintf("*\nFans: %s", util.NumberToString(fans, '.')) + SayCh <- fmt.Sprintf("*\nFleet: %s", util.NumberToString(fleet, '.')) + SayCh <- fmt.Sprintf("*\nFunds: $ %s", util.NumberToString(funds, '.')) } func showCitizen(handle string) { @@ -157,18 +157,18 @@ func showCitizen(handle string) { sid := reSid.FindStringSubmatch(string(body)) rank := reRank.FindStringSubmatch(string(body)) if len(name) > 1 { - sayCh <- "*\n*** Citizen Info ***" - sayCh <- "*\n" + "Name: " + html.UnescapeString(string(name[1])) + " [" + string(handle_[1]) + "]" - sayCh <- "*\n" + "URL: " + QUERY_CIT_URL + string(handle_[1]) - sayCh <- "*\n" + "UEE #: " + string(record[1]) + SayCh <- "*\n*** Citizen Info ***" + SayCh <- "*\n" + "Name: " + html.UnescapeString(string(name[1])) + " [" + string(handle_[1]) + "]" + SayCh <- "*\n" + "URL: " + QUERY_CIT_URL + string(handle_[1]) + SayCh <- "*\n" + "UEE #: " + string(record[1]) if len(org) > 1 { - sayCh <- "*\n" + "Organization: " + html.UnescapeString(string(org[1])) + " [" + string(sid[1]) + "]" - sayCh <- "*\n" + "Rank: " + html.UnescapeString(string(rank[1])) + SayCh <- "*\n" + "Organization: " + html.UnescapeString(string(org[1])) + " [" + string(sid[1]) + "]" + SayCh <- "*\n" + "Rank: " + html.UnescapeString(string(rank[1])) } else { - sayCh <- "*\n" + "Organization: " + SayCh <- "*\n" + "Organization: " } } else { - sayCh <- "*\n" + "*** No Such Citizen ***" + SayCh <- "*\n" + "*** No Such Citizen ***" } } @@ -195,14 +195,14 @@ func showOrganization(handle string) { focus := reFocus.FindStringSubmatch(string(body)) comm := reComm.FindStringSubmatch(string(body)) if len(name) > 1 { - sayCh <- "*\n*** Organization Info ***" - sayCh <- "*\nName: " + string(name[1]) + " [" + strings.ToUpper(handle) + "]" - sayCh <- "*\nURL: " + QUERY_ORG_URL + strings.ToUpper(handle) - sayCh <- "*\nMembers: " + string(count[1]) - sayCh <- "*\nModel: " + string(model[1]) - sayCh <- "*\nCommitment: " + string(comm[1]) - sayCh <- "*\nFocus: " + string(focus[1]) + ", " + string(focus[2]) + SayCh <- "*\n*** Organization Info ***" + SayCh <- "*\nName: " + string(name[1]) + " [" + strings.ToUpper(handle) + "]" + SayCh <- "*\nURL: " + QUERY_ORG_URL + strings.ToUpper(handle) + SayCh <- "*\nMembers: " + string(count[1]) + SayCh <- "*\nModel: " + string(model[1]) + SayCh <- "*\nCommitment: " + string(comm[1]) + SayCh <- "*\nFocus: " + string(focus[1]) + ", " + string(focus[2]) } else { - sayCh <- "*\n*** No Such Organization ***" + SayCh <- "*\n*** No Such Organization ***" } } diff --git a/modules/stoll.go b/modules/stoll.go index 0d23e70..dd3ca79 100644 --- a/modules/stoll.go +++ b/modules/stoll.go @@ -520,7 +520,7 @@ var quotes = [][]string{ } func init() { - msgHandlers["stoll"] = stollHandleMessage + MsgHandlers["stoll"] = stollHandleMessage } func stollHandleMessage(m *irc.Message) { @@ -535,6 +535,6 @@ func stollHandleMessage(m *irc.Message) { line += " " } line += "[Dr. Axel Stoll, promovierter Naturwissenschaftler]" - sayCh <- fmt.Sprintf("%s\n%s", "*", line) + SayCh <- fmt.Sprintf("%s\n%s", "*", line) } } diff --git a/modules/twitch.go b/modules/twitch.go index 3600e18..185d3d1 100644 --- a/modules/twitch.go +++ b/modules/twitch.go @@ -136,7 +136,7 @@ var ( ) func init() { - msgHandlers["twitch"] = twitchHandleMessage + MsgHandlers["twitch"] = twitchHandleMessage log.Println("Initializing twitch module") go pollStreamData() } @@ -165,7 +165,7 @@ func twitchHandleMessage(m *irc.Message) { } } if onlinestreams == 0 { - sayCh <- fmt.Sprintf("*\n%s", "[TWTCH] All streams offline") + SayCh <- fmt.Sprintf("*\n%s", "[TWTCH] All streams offline") } case 2: streamname := tok[1] @@ -176,7 +176,7 @@ func twitchHandleMessage(m *irc.Message) { co = getTwitchChannelObject(streamname) twitchSendMsg(co, so) } else { - sayCh <- fmt.Sprintf("*\n%s", "[TWTCH] "+streamname+" not found or offline") + SayCh <- fmt.Sprintf("*\n%s", "[TWTCH] "+streamname+" not found or offline") } default: } @@ -242,6 +242,6 @@ func getTwitchChannelObject(streamname string) TwitchChannelObject { } func twitchSendMsg(tcobj TwitchChannelObject, tso TwitchStreamObject) { - sayCh <- fmt.Sprintf("*\n%s", "[TWTCH] "+tso.Stream.Channel.DisplayName+", "+util.NumberToString(tso.Stream.Viewers, '.')+" viewers, playing: "+tso.Stream.Game+", "+tcobj.URL) - sayCh <- fmt.Sprintf("*\n%s", "[TWTCH] "+"Title: \""+tcobj.Status+"\"") + SayCh <- fmt.Sprintf("*\n%s", "[TWTCH] "+tso.Stream.Channel.DisplayName+", "+util.NumberToString(tso.Stream.Viewers, '.')+" viewers, playing: "+tso.Stream.Game+", "+tcobj.URL) + SayCh <- fmt.Sprintf("*\n%s", "[TWTCH] "+"Title: \""+tcobj.Status+"\"") } diff --git a/modules/weather.go b/modules/weather.go index 1687d41..6815b9b 100644 --- a/modules/weather.go +++ b/modules/weather.go @@ -14,6 +14,7 @@ import ( "net/http" "strconv" "strings" + "time" "github.com/sorcix/irc" ) @@ -69,8 +70,14 @@ type WeatherObject struct { } func init() { - msgHandlers["weather"] = weatherHandleMessage + MsgHandlers["weather"] = weatherHandleMessage log.Println("Initializing weather module") + go weatherConfig() +} + +func weatherConfig() { + time.Sleep(5 * time.Second) + owmQueryAPIKey = ModParams["weather-api-key"] } func weatherHandleMessage(m *irc.Message) { @@ -82,10 +89,10 @@ func weatherHandleMessage(m *irc.Message) { case "!weather", "!wetter": switch len(tok) { case 1: - sayCh <- fmt.Sprintf("*\n%s", weatherPrefix+"Usage: !w ") + SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+"Usage: !w ") default: if strings.Compare(owmQueryAPIKey, "") == 0 { - sayCh <- fmt.Sprintf("*\n%s", weatherPrefix+"No API Key set.") + SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+"No API Key set.") } else { go getWeather(strings.Join(tok[1:], " ")) } @@ -99,19 +106,19 @@ func getWeather(query string) { r, err := http.Get(q) if err != nil { log.Println(err) - sayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) + SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) } else { re, err := ioutil.ReadAll(r.Body) if err != nil { log.Println(err) - sayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) + SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) } else { defer r.Body.Close() var wo WeatherObject err := json.Unmarshal(re, &wo) if err != nil { log.Println(err) - sayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) + SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) } else { var description string if len(wo.Weather) < 1 { @@ -119,7 +126,7 @@ func getWeather(query string) { } else { description = ", " + wo.Weather[0].Description } - sayCh <- fmt.Sprintf("*\n%s", weatherPrefix+wo.Name+", "+wo.Sys.Country+ + SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+wo.Name+", "+wo.Sys.Country+ " - Temp: "+strconv.Itoa(int(wo.Main.Temp-273.15))+ "°C"+description+ ", Humidity: "+strconv.Itoa(int(wo.Main.Humidity))+