From ad20283c232ffb626d0d33521e0eae844f46fc0e Mon Sep 17 00:00:00 2001 From: an Date: Wed, 28 Jun 2017 23:28:39 +0200 Subject: [PATCH] Make lib fully protocol agnostic --- modules/announcements.go | 6 ++---- modules/coffee.go | 28 +++++++++++++--------------- modules/fortune.go | 12 +++++------- modules/fuzzytime.go | 12 +++++------- modules/gogs.go | 6 ++---- modules/markov.go | 8 +++----- modules/modules.go | 12 ++++++++---- modules/quiz.go | 14 ++++++-------- modules/rss.go | 10 +++++----- modules/sc.go | 6 ++---- modules/stoll.go | 8 +++----- modules/twitch.go | 6 ++---- modules/weather.go | 22 ++++++++++------------ 13 files changed, 66 insertions(+), 84 deletions(-) diff --git a/modules/announcements.go b/modules/announcements.go index be2b1db..2a5c763 100644 --- a/modules/announcements.go +++ b/modules/announcements.go @@ -4,8 +4,6 @@ package modules import ( "strings" - - "github.com/sorcix/irc" ) var () @@ -14,8 +12,8 @@ func init() { MsgFuncs["announcements"] = anncouncementsHandleMessage } -func anncouncementsHandleMessage(m *irc.Message) { - tok := strings.Split(m.Trailing, " ") +func anncouncementsHandleMessage(m *Message) { + tok := strings.Split(m.Text, " ") if len(tok) < 1 { return } diff --git a/modules/coffee.go b/modules/coffee.go index 80e42f5..21558a0 100644 --- a/modules/coffee.go +++ b/modules/coffee.go @@ -11,8 +11,6 @@ import ( "strings" "sync" "time" - - "github.com/sorcix/irc" ) type Receivers struct { @@ -30,8 +28,8 @@ func init() { r.names = make(map[string]bool) } -func coffeeHandleMessage(m *irc.Message) { - tok := strings.Split(strings.Trim(m.Trailing, " "), " ") +func coffeeHandleMessage(m *Message) { + tok := strings.Split(strings.Trim(m.Text, " "), " ") if len(tok) < 1 { return } @@ -39,15 +37,15 @@ func coffeeHandleMessage(m *irc.Message) { case "!kaffee": switch len(tok) { case 1: - go r.addReceivers(nil) + go r.addReceivers(nil, m.Channel) default: - go r.addReceivers(tok[1:]) + go r.addReceivers(tok[1:], m.Channel) } default: } } -func (r *Receivers) addReceivers(newNames []string) { +func (r *Receivers) addReceivers(newNames []string, channel string) { r.mux.Lock() if r.running { if newNames != nil { @@ -57,18 +55,18 @@ func (r *Receivers) addReceivers(newNames []string) { if newNames != nil { r.addNames(newNames) } - go r.makeCoffee() + go r.makeCoffee(channel) } r.mux.Unlock() } -func (r *Receivers) makeCoffee() { +func (r *Receivers) makeCoffee(channel string) { r.mux.Lock() r.running = true r.mux.Unlock() - printAction("setzt Kaffee auf.") + printAction(channel, "setzt Kaffee auf.") time.Sleep(time.Second * 30) - printAction("stellt eine frische Kanne Kaffee in den Raum.") + printAction(channel, "stellt eine frische Kanne Kaffee in den Raum.") r.mux.Lock() if len(r.names) != 0 { var users string @@ -82,11 +80,11 @@ func (r *Receivers) makeCoffee() { users += " und " } } - printAction("gibt " + users + " einen frischen, richtig schwarzen, richtig leckeren Kaffee.") + printAction(channel, "gibt "+users+" einen frischen, richtig schwarzen, richtig leckeren Kaffee.") } r.mux.Unlock() time.Sleep(10 * time.Second) - SayCh <- "*\nProst! (c)" + SayCh <- channel + "\nProst! (c)" r.mux.Lock() r.names = make(map[string]bool) @@ -100,7 +98,7 @@ func (r *Receivers) addNames(newNames []string) { } } -func printAction(s string) { +func printAction(channel, s string) { //msg := ctcp.Encode(ctcp.ACTION, fmt.Sprintf(s)) - SayCh <- fmt.Sprintf("%s\n%s", "*", s) + SayCh <- fmt.Sprintf("%s\n%s", channel, s) } diff --git a/modules/fortune.go b/modules/fortune.go index b0cd8d4..097fb7d 100644 --- a/modules/fortune.go +++ b/modules/fortune.go @@ -7,27 +7,25 @@ import ( "fmt" "os/exec" "strings" - - "github.com/sorcix/irc" ) func init() { MsgFuncs["fortune"] = fortuneHandleMessage } -func fortuneHandleMessage(m *irc.Message) { - tok := strings.Split(m.Trailing, " ") +func fortuneHandleMessage(m *Message) { + tok := strings.Split(m.Text, " ") if len(tok) < 1 { return } switch tok[0] { case "!fortune": - fortune() + fortune(m.Channel) default: } } -func fortune() { +func fortune(channel string) { cmd := exec.Command("/usr/games/fortune", "/flokatirc/fortunes/") var out bytes.Buffer cmd.Stdout = &out @@ -41,7 +39,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("%s\n%s", channel, l) } } } diff --git a/modules/fuzzytime.go b/modules/fuzzytime.go index 802a66c..02802f7 100644 --- a/modules/fuzzytime.go +++ b/modules/fuzzytime.go @@ -6,29 +6,27 @@ import ( "fmt" "strings" "time" - - "github.com/sorcix/irc" ) func init() { MsgFuncs["fuzzytime"] = fuzzytimeHandleMessage } -func fuzzytimeHandleMessage(m *irc.Message) { - tok := strings.Split(m.Trailing, " ") +func fuzzytimeHandleMessage(m *Message) { + tok := strings.Split(m.Text, " ") if len(tok) < 1 { return } switch tok[0] { case "!time": - fuzzytimeShow() + fuzzytimeShow(m.Channel) //case "!q": // show() default: } } -func fuzzytimeShow() { +func fuzzytimeShow(channel string) { t := time.Now() h := t.Hour() tzcorrect := 1 @@ -63,7 +61,7 @@ func fuzzytimeShow() { default: s += fmt.Sprintf("%s Uhr\n", fuzzytimeSayHour(h+1)) } - SayCh <- fmt.Sprintf("*\n%s", s) + SayCh <- fmt.Sprintf("%s\n%s", channel, s) } func fuzzytimeSayHour(h int) string { diff --git a/modules/gogs.go b/modules/gogs.go index 4207862..50ebd49 100644 --- a/modules/gogs.go +++ b/modules/gogs.go @@ -7,8 +7,6 @@ package modules import ( "strings" "time" - - "github.com/sorcix/irc" ) var ( @@ -25,8 +23,8 @@ func gogsConfig() { //gogsAPIKey = ModParams["gogs-api-key"] } -func gogsHandleMessage(m *irc.Message) { - tok := strings.Split(m.Trailing, " ") +func gogsHandleMessage(m *Message) { + tok := strings.Split(m.Text, " ") if len(tok) < 1 { return } diff --git a/modules/markov.go b/modules/markov.go index 48707f8..ef3bc42 100644 --- a/modules/markov.go +++ b/modules/markov.go @@ -21,8 +21,6 @@ import ( "time" "code.dnix.de/an/xlog" - - "github.com/sorcix/irc" ) var ( @@ -40,8 +38,8 @@ func init() { RunFuncs["markov"] = markovRun } -func markovHandleMessage(m *irc.Message) { - text := m.Trailing +func markovHandleMessage(m *Message) { + text := m.Text if text == "" { return } @@ -52,7 +50,7 @@ func markovHandleMessage(m *irc.Message) { if responseText != "" { go func() { time.Sleep(time.Duration(rand.Intn(8)+2) * time.Second) - SayCh <- "*\n" + responseText + SayCh <- m.Channel + "\n" + responseText }() } } diff --git a/modules/modules.go b/modules/modules.go index 9eb37a7..7baebd5 100644 --- a/modules/modules.go +++ b/modules/modules.go @@ -9,17 +9,21 @@ package modules import ( "strings" "time" - - "github.com/sorcix/irc" ) var ( SayCh chan string - MsgFuncs = make(map[string]func(*irc.Message)) + MsgFuncs = make(map[string]func(*Message)) RunFuncs = make(map[string]func()) BotNick string ) +type Message struct { + From string + Channel string + Text string +} + func Init(ch chan string, mods string) { time.Sleep(5 * time.Second) SayCh = ch @@ -38,7 +42,7 @@ func Init(ch chan string, mods string) { } } -func HandleMessage(m *irc.Message) { +func HandleMessage(m *Message) { for _, fn := range MsgFuncs { fn(m) } diff --git a/modules/quiz.go b/modules/quiz.go index c5b7c76..011e30d 100644 --- a/modules/quiz.go +++ b/modules/quiz.go @@ -13,8 +13,6 @@ import ( "code.dnix.de/an/flokatilib/util" - "github.com/sorcix/irc" - "code.dnix.de/an/xlog" ) @@ -24,7 +22,7 @@ type quizQuestion struct { var ( quizCtrlCh = make(chan string, 1024) - quizAnswerCh = make(chan *irc.Message, 1024) + quizAnswerCh = make(chan *Message, 1024) quizQuestions []quizQuestion quizQuestionValues = map[string]int{"extreme": 5, "hard": 4, "normal": 3, "easy": 2, "baby": 1} ) @@ -35,8 +33,8 @@ func init() { rand.Seed(time.Now().Unix()) } -func quizHandleMessage(m *irc.Message) { - tok := strings.Split(m.Trailing, " ") +func quizHandleMessage(m *Message) { + tok := strings.Split(m.Text, " ") if len(tok) < 1 { return } @@ -162,14 +160,14 @@ func quizWaitForAnswer(q quizQuestion) (bool, bool, string, int) { xlog.Error(err.Error()) return false, false, "", 0 } - if re.MatchString(strings.ToLower(util.ReplaceUmlauts(m.Trailing))) { + if re.MatchString(strings.ToLower(util.ReplaceUmlauts(m.Text))) { bold := byte(0x02) - solver := strings.Split(m.Prefix.String(), "!")[0] + solver := m.From solver = string(bold) + solver + string(bold) value := quizQuestionValues[q.level] gain := int(points) * value SayCh <- fmt.Sprintf("%s\n%s hat die Frage korrekt beantwortet und erhält dafür %d (%d * %d) Punkte.", "*", solver, gain, points, value) - return true, true, solver, gain + return true, true, m.From, gain } break case s := <-quizCtrlCh: diff --git a/modules/rss.go b/modules/rss.go index ec7f3b5..c29e18e 100644 --- a/modules/rss.go +++ b/modules/rss.go @@ -19,11 +19,11 @@ import ( gorss "github.com/jteeuwen/go-pkg-rss" "github.com/jteeuwen/go-pkg-xmlx" - "github.com/sorcix/irc" ) var hideOutput = true -var rssFeeds = flag.String("rss_feeでs", "feeds.txt", "Feed list file") +var rssFeeds = flag.String("rss_feeds", "feeds.txt", "Feed list file") +var rssChannel = flag.String("rss_channel", "", "Target channel") func init() { MsgFuncs["rss"] = rssHandleMessage @@ -50,7 +50,7 @@ func rssRun() { }() } -func rssHandleMessage(m *irc.Message) { +func rssHandleMessage(m *Message) { } func rssPollFeed(uri string, timeout int, cr xmlx.CharsetFunc) { @@ -59,7 +59,7 @@ func rssPollFeed(uri string, timeout int, cr xmlx.CharsetFunc) { xlog.Info("Polling feed: %s", uri) if err := feed.Fetch(uri, cr); err != nil { xlog.Info("[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", *rssChannel, uri, err) return } <-time.After(time.Duration(feed.SecondsTillUpdate() * 1e9)) @@ -75,7 +75,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", *rssChannel, ni.Title, ni.Links[0].Href) } } diff --git a/modules/sc.go b/modules/sc.go index 2992a52..bfc69d5 100644 --- a/modules/sc.go +++ b/modules/sc.go @@ -16,8 +16,6 @@ import ( "code.dnix.de/an/xlog" "code.dnix.de/an/flokatilib/util" - - "github.com/sorcix/irc" ) const ( @@ -52,8 +50,8 @@ func init() { RunFuncs["sc"] = scScrapeLoop } -func scHandleMessage(m *irc.Message) { - tok := strings.Split(m.Trailing, " ") +func scHandleMessage(m *Message) { + tok := strings.Split(m.Text, " ") if len(tok) < 1 { return } diff --git a/modules/stoll.go b/modules/stoll.go index 32dd122..9d14857 100644 --- a/modules/stoll.go +++ b/modules/stoll.go @@ -7,8 +7,6 @@ import ( "math/rand" "strings" "time" - - "github.com/sorcix/irc" ) var quotes = [][]string{ @@ -525,8 +523,8 @@ func init() { rand.Seed(time.Now().Unix()) } -func stollHandleMessage(m *irc.Message) { - tok := strings.Split(m.Trailing, " ") +func stollHandleMessage(m *Message) { + tok := strings.Split(m.Text, " ") if len(tok) < 1 { return } @@ -537,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", m.Channel, line) } } diff --git a/modules/twitch.go b/modules/twitch.go index feade5f..62f6825 100644 --- a/modules/twitch.go +++ b/modules/twitch.go @@ -17,8 +17,6 @@ import ( "code.dnix.de/an/xlog" "code.dnix.de/an/flokatilib/util" - - "github.com/sorcix/irc" ) type TwitchChannelObject struct { @@ -141,8 +139,8 @@ func init() { RunFuncs["twitch"] = pollStreamData } -func twitchHandleMessage(m *irc.Message) { - tok := strings.Split(m.Trailing, " ") +func twitchHandleMessage(m *Message) { + tok := strings.Split(m.Text, " ") if len(tok) < 1 { return } diff --git a/modules/weather.go b/modules/weather.go index 219f084..e93671e 100644 --- a/modules/weather.go +++ b/modules/weather.go @@ -17,8 +17,6 @@ import ( "time" "code.dnix.de/an/xlog" - - "github.com/sorcix/irc" ) var ( @@ -85,8 +83,8 @@ func weatherConfig() { owmQueryAPIKey = *weatherApiKey } -func weatherHandleMessage(m *irc.Message) { - tok := strings.Split(m.Trailing, " ") +func weatherHandleMessage(m *Message) { + tok := strings.Split(m.Text, " ") if len(tok) < 1 { return } @@ -94,36 +92,36 @@ func weatherHandleMessage(m *irc.Message) { case "!weather", "!wetter": switch len(tok) { case 1: - SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+"Usage: !w ") + SayCh <- fmt.Sprintf("%s\n%s", m.Channel, weatherPrefix+"Usage: !w ") default: if strings.Compare(owmQueryAPIKey, "") == 0 { - SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+"No API Key set.") + SayCh <- fmt.Sprintf("%s\n%s", m.Channel, weatherPrefix+"No API Key set.") } else { - go getWeather(strings.Join(tok[1:], " ")) + go getWeather(strings.Join(tok[1:], " "), m.Channel) } } default: } } -func getWeather(query string) { +func getWeather(query, channel string) { q := owmQueryURLPrefix + query + owmQueryURLSuffix + owmQueryAPIKey r, err := http.Get(q) if err != nil { xlog.Error(err.Error()) - SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) + SayCh <- fmt.Sprintf("%s\n%s", channel, weatherPrefix+err.Error()) } else { re, err := ioutil.ReadAll(r.Body) if err != nil { xlog.Error(err.Error()) - SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) + SayCh <- fmt.Sprintf("%s\n%s", channel, weatherPrefix+err.Error()) } else { defer r.Body.Close() var wo WeatherObject err := json.Unmarshal(re, &wo) if err != nil { xlog.Error(err.Error()) - SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) + SayCh <- fmt.Sprintf("%s\n%s", channel, weatherPrefix+err.Error()) } else { var description string if len(wo.Weather) < 1 { @@ -131,7 +129,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("%s\n%s", channel, weatherPrefix+wo.Name+", "+wo.Sys.Country+ " - Temp: "+strconv.Itoa(int(wo.Main.Temp-273.15))+ "°C"+description+ ", Humidity: "+strconv.Itoa(int(wo.Main.Humidity))+