Added module configuration params: -params=<key>:<val>(!<key>:<val>])*

This commit is contained in:
Andreas Neue 2016-02-16 07:57:14 +01:00
parent 5f2dd41d55
commit 44ee1cf628
10 changed files with 67 additions and 55 deletions

View File

@ -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 {

View File

@ -12,7 +12,7 @@ import (
var ()
func init() {
msgHandlers["announcements"] = anncouncementsHandleMessage
MsgHandlers["announcements"] = anncouncementsHandleMessage
log.Println("Initializing announcements module")
}

View File

@ -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)"
}

View File

@ -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)
}
}
}

View File

@ -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)
}

View File

@ -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)
}
}

View File

@ -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: <none>"
SayCh <- "*\n" + "Organization: <none>"
}
} 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 ***"
}
}

View File

@ -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)
}
}

View File

@ -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+"\"")
}

View File

@ -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 <zip/city/state/country>")
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+"Usage: !w <zip/city/state/country>")
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))+