Make lib fully protocol agnostic

This commit is contained in:
an 2017-06-28 23:28:39 +02:00
parent 080ab0b19f
commit ad20283c23
13 changed files with 66 additions and 84 deletions

View File

@ -4,8 +4,6 @@ package modules
import ( import (
"strings" "strings"
"github.com/sorcix/irc"
) )
var () var ()
@ -14,8 +12,8 @@ func init() {
MsgFuncs["announcements"] = anncouncementsHandleMessage MsgFuncs["announcements"] = anncouncementsHandleMessage
} }
func anncouncementsHandleMessage(m *irc.Message) { func anncouncementsHandleMessage(m *Message) {
tok := strings.Split(m.Trailing, " ") tok := strings.Split(m.Text, " ")
if len(tok) < 1 { if len(tok) < 1 {
return return
} }

View File

@ -11,8 +11,6 @@ import (
"strings" "strings"
"sync" "sync"
"time" "time"
"github.com/sorcix/irc"
) )
type Receivers struct { type Receivers struct {
@ -30,8 +28,8 @@ func init() {
r.names = make(map[string]bool) r.names = make(map[string]bool)
} }
func coffeeHandleMessage(m *irc.Message) { func coffeeHandleMessage(m *Message) {
tok := strings.Split(strings.Trim(m.Trailing, " "), " ") tok := strings.Split(strings.Trim(m.Text, " "), " ")
if len(tok) < 1 { if len(tok) < 1 {
return return
} }
@ -39,15 +37,15 @@ func coffeeHandleMessage(m *irc.Message) {
case "!kaffee": case "!kaffee":
switch len(tok) { switch len(tok) {
case 1: case 1:
go r.addReceivers(nil) go r.addReceivers(nil, m.Channel)
default: default:
go r.addReceivers(tok[1:]) go r.addReceivers(tok[1:], m.Channel)
} }
default: default:
} }
} }
func (r *Receivers) addReceivers(newNames []string) { func (r *Receivers) addReceivers(newNames []string, channel string) {
r.mux.Lock() r.mux.Lock()
if r.running { if r.running {
if newNames != nil { if newNames != nil {
@ -57,18 +55,18 @@ func (r *Receivers) addReceivers(newNames []string) {
if newNames != nil { if newNames != nil {
r.addNames(newNames) r.addNames(newNames)
} }
go r.makeCoffee() go r.makeCoffee(channel)
} }
r.mux.Unlock() r.mux.Unlock()
} }
func (r *Receivers) makeCoffee() { func (r *Receivers) makeCoffee(channel string) {
r.mux.Lock() r.mux.Lock()
r.running = true r.running = true
r.mux.Unlock() r.mux.Unlock()
printAction("setzt Kaffee auf.") printAction(channel, "setzt Kaffee auf.")
time.Sleep(time.Second * 30) 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() r.mux.Lock()
if len(r.names) != 0 { if len(r.names) != 0 {
var users string var users string
@ -82,11 +80,11 @@ func (r *Receivers) makeCoffee() {
users += " und " 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() r.mux.Unlock()
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
SayCh <- "*\nProst! (c)" SayCh <- channel + "\nProst! (c)"
r.mux.Lock() r.mux.Lock()
r.names = make(map[string]bool) 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)) //msg := ctcp.Encode(ctcp.ACTION, fmt.Sprintf(s))
SayCh <- fmt.Sprintf("%s\n%s", "*", s) SayCh <- fmt.Sprintf("%s\n%s", channel, s)
} }

View File

@ -7,27 +7,25 @@ import (
"fmt" "fmt"
"os/exec" "os/exec"
"strings" "strings"
"github.com/sorcix/irc"
) )
func init() { func init() {
MsgFuncs["fortune"] = fortuneHandleMessage MsgFuncs["fortune"] = fortuneHandleMessage
} }
func fortuneHandleMessage(m *irc.Message) { func fortuneHandleMessage(m *Message) {
tok := strings.Split(m.Trailing, " ") tok := strings.Split(m.Text, " ")
if len(tok) < 1 { if len(tok) < 1 {
return return
} }
switch tok[0] { switch tok[0] {
case "!fortune": case "!fortune":
fortune() fortune(m.Channel)
default: default:
} }
} }
func fortune() { func fortune(channel string) {
cmd := exec.Command("/usr/games/fortune", "/flokatirc/fortunes/") cmd := exec.Command("/usr/games/fortune", "/flokatirc/fortunes/")
var out bytes.Buffer var out bytes.Buffer
cmd.Stdout = &out cmd.Stdout = &out
@ -41,7 +39,7 @@ func fortune() {
s = strings.Replace(s, "\t", " ", -1) s = strings.Replace(s, "\t", " ", -1)
for _, l := range strings.Split(s, "\n") { for _, l := range strings.Split(s, "\n") {
if l != "" { if l != "" {
SayCh <- fmt.Sprintf("*\n%s", l) SayCh <- fmt.Sprintf("%s\n%s", channel, l)
} }
} }
} }

View File

@ -6,29 +6,27 @@ import (
"fmt" "fmt"
"strings" "strings"
"time" "time"
"github.com/sorcix/irc"
) )
func init() { func init() {
MsgFuncs["fuzzytime"] = fuzzytimeHandleMessage MsgFuncs["fuzzytime"] = fuzzytimeHandleMessage
} }
func fuzzytimeHandleMessage(m *irc.Message) { func fuzzytimeHandleMessage(m *Message) {
tok := strings.Split(m.Trailing, " ") tok := strings.Split(m.Text, " ")
if len(tok) < 1 { if len(tok) < 1 {
return return
} }
switch tok[0] { switch tok[0] {
case "!time": case "!time":
fuzzytimeShow() fuzzytimeShow(m.Channel)
//case "!q": //case "!q":
// show() // show()
default: default:
} }
} }
func fuzzytimeShow() { func fuzzytimeShow(channel string) {
t := time.Now() t := time.Now()
h := t.Hour() h := t.Hour()
tzcorrect := 1 tzcorrect := 1
@ -63,7 +61,7 @@ func fuzzytimeShow() {
default: default:
s += fmt.Sprintf("%s Uhr\n", fuzzytimeSayHour(h+1)) 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 { func fuzzytimeSayHour(h int) string {

View File

@ -7,8 +7,6 @@ package modules
import ( import (
"strings" "strings"
"time" "time"
"github.com/sorcix/irc"
) )
var ( var (
@ -25,8 +23,8 @@ func gogsConfig() {
//gogsAPIKey = ModParams["gogs-api-key"] //gogsAPIKey = ModParams["gogs-api-key"]
} }
func gogsHandleMessage(m *irc.Message) { func gogsHandleMessage(m *Message) {
tok := strings.Split(m.Trailing, " ") tok := strings.Split(m.Text, " ")
if len(tok) < 1 { if len(tok) < 1 {
return return
} }

View File

@ -21,8 +21,6 @@ import (
"time" "time"
"code.dnix.de/an/xlog" "code.dnix.de/an/xlog"
"github.com/sorcix/irc"
) )
var ( var (
@ -40,8 +38,8 @@ func init() {
RunFuncs["markov"] = markovRun RunFuncs["markov"] = markovRun
} }
func markovHandleMessage(m *irc.Message) { func markovHandleMessage(m *Message) {
text := m.Trailing text := m.Text
if text == "" { if text == "" {
return return
} }
@ -52,7 +50,7 @@ func markovHandleMessage(m *irc.Message) {
if responseText != "" { if responseText != "" {
go func() { go func() {
time.Sleep(time.Duration(rand.Intn(8)+2) * time.Second) time.Sleep(time.Duration(rand.Intn(8)+2) * time.Second)
SayCh <- "*\n" + responseText SayCh <- m.Channel + "\n" + responseText
}() }()
} }
} }

View File

@ -9,17 +9,21 @@ package modules
import ( import (
"strings" "strings"
"time" "time"
"github.com/sorcix/irc"
) )
var ( var (
SayCh chan string SayCh chan string
MsgFuncs = make(map[string]func(*irc.Message)) MsgFuncs = make(map[string]func(*Message))
RunFuncs = make(map[string]func()) RunFuncs = make(map[string]func())
BotNick string BotNick string
) )
type Message struct {
From string
Channel string
Text string
}
func Init(ch chan string, mods string) { func Init(ch chan string, mods string) {
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
SayCh = ch 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 { for _, fn := range MsgFuncs {
fn(m) fn(m)
} }

View File

@ -13,8 +13,6 @@ import (
"code.dnix.de/an/flokatilib/util" "code.dnix.de/an/flokatilib/util"
"github.com/sorcix/irc"
"code.dnix.de/an/xlog" "code.dnix.de/an/xlog"
) )
@ -24,7 +22,7 @@ type quizQuestion struct {
var ( var (
quizCtrlCh = make(chan string, 1024) quizCtrlCh = make(chan string, 1024)
quizAnswerCh = make(chan *irc.Message, 1024) quizAnswerCh = make(chan *Message, 1024)
quizQuestions []quizQuestion quizQuestions []quizQuestion
quizQuestionValues = map[string]int{"extreme": 5, "hard": 4, "normal": 3, "easy": 2, "baby": 1} 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()) rand.Seed(time.Now().Unix())
} }
func quizHandleMessage(m *irc.Message) { func quizHandleMessage(m *Message) {
tok := strings.Split(m.Trailing, " ") tok := strings.Split(m.Text, " ")
if len(tok) < 1 { if len(tok) < 1 {
return return
} }
@ -162,14 +160,14 @@ func quizWaitForAnswer(q quizQuestion) (bool, bool, string, int) {
xlog.Error(err.Error()) xlog.Error(err.Error())
return false, false, "", 0 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) bold := byte(0x02)
solver := strings.Split(m.Prefix.String(), "!")[0] solver := m.From
solver = string(bold) + solver + string(bold) solver = string(bold) + solver + string(bold)
value := quizQuestionValues[q.level] value := quizQuestionValues[q.level]
gain := int(points) * value 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) 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 break
case s := <-quizCtrlCh: case s := <-quizCtrlCh:

View File

@ -19,11 +19,11 @@ import (
gorss "github.com/jteeuwen/go-pkg-rss" gorss "github.com/jteeuwen/go-pkg-rss"
"github.com/jteeuwen/go-pkg-xmlx" "github.com/jteeuwen/go-pkg-xmlx"
"github.com/sorcix/irc"
) )
var hideOutput = true 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() { func init() {
MsgFuncs["rss"] = rssHandleMessage 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) { 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) xlog.Info("Polling feed: %s", uri)
if err := feed.Fetch(uri, cr); err != nil { if err := feed.Fetch(uri, cr); err != nil {
xlog.Info("[e] %s: %s", "*", uri, err) 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 return
} }
<-time.After(time.Duration(feed.SecondsTillUpdate() * 1e9)) <-time.After(time.Duration(feed.SecondsTillUpdate() * 1e9))
@ -75,7 +75,7 @@ func rssItemHandler(feed *gorss.Feed, ch *gorss.Channel, newitems []*gorss.Item)
return return
} }
for _, ni := range newitems { 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)
} }
} }

View File

@ -16,8 +16,6 @@ import (
"code.dnix.de/an/xlog" "code.dnix.de/an/xlog"
"code.dnix.de/an/flokatilib/util" "code.dnix.de/an/flokatilib/util"
"github.com/sorcix/irc"
) )
const ( const (
@ -52,8 +50,8 @@ func init() {
RunFuncs["sc"] = scScrapeLoop RunFuncs["sc"] = scScrapeLoop
} }
func scHandleMessage(m *irc.Message) { func scHandleMessage(m *Message) {
tok := strings.Split(m.Trailing, " ") tok := strings.Split(m.Text, " ")
if len(tok) < 1 { if len(tok) < 1 {
return return
} }

View File

@ -7,8 +7,6 @@ import (
"math/rand" "math/rand"
"strings" "strings"
"time" "time"
"github.com/sorcix/irc"
) )
var quotes = [][]string{ var quotes = [][]string{
@ -525,8 +523,8 @@ func init() {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
} }
func stollHandleMessage(m *irc.Message) { func stollHandleMessage(m *Message) {
tok := strings.Split(m.Trailing, " ") tok := strings.Split(m.Text, " ")
if len(tok) < 1 { if len(tok) < 1 {
return return
} }
@ -537,6 +535,6 @@ func stollHandleMessage(m *irc.Message) {
line += " " line += " "
} }
line += "[Dr. Axel Stoll, promovierter Naturwissenschaftler]" line += "[Dr. Axel Stoll, promovierter Naturwissenschaftler]"
SayCh <- fmt.Sprintf("%s\n%s", "*", line) SayCh <- fmt.Sprintf("%s\n%s", m.Channel, line)
} }
} }

View File

@ -17,8 +17,6 @@ import (
"code.dnix.de/an/xlog" "code.dnix.de/an/xlog"
"code.dnix.de/an/flokatilib/util" "code.dnix.de/an/flokatilib/util"
"github.com/sorcix/irc"
) )
type TwitchChannelObject struct { type TwitchChannelObject struct {
@ -141,8 +139,8 @@ func init() {
RunFuncs["twitch"] = pollStreamData RunFuncs["twitch"] = pollStreamData
} }
func twitchHandleMessage(m *irc.Message) { func twitchHandleMessage(m *Message) {
tok := strings.Split(m.Trailing, " ") tok := strings.Split(m.Text, " ")
if len(tok) < 1 { if len(tok) < 1 {
return return
} }

View File

@ -17,8 +17,6 @@ import (
"time" "time"
"code.dnix.de/an/xlog" "code.dnix.de/an/xlog"
"github.com/sorcix/irc"
) )
var ( var (
@ -85,8 +83,8 @@ func weatherConfig() {
owmQueryAPIKey = *weatherApiKey owmQueryAPIKey = *weatherApiKey
} }
func weatherHandleMessage(m *irc.Message) { func weatherHandleMessage(m *Message) {
tok := strings.Split(m.Trailing, " ") tok := strings.Split(m.Text, " ")
if len(tok) < 1 { if len(tok) < 1 {
return return
} }
@ -94,36 +92,36 @@ func weatherHandleMessage(m *irc.Message) {
case "!weather", "!wetter": case "!weather", "!wetter":
switch len(tok) { switch len(tok) {
case 1: case 1:
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+"Usage: !w <zip/city/state/country>") SayCh <- fmt.Sprintf("%s\n%s", m.Channel, weatherPrefix+"Usage: !w <zip/city/state/country>")
default: default:
if strings.Compare(owmQueryAPIKey, "") == 0 { 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 { } else {
go getWeather(strings.Join(tok[1:], " ")) go getWeather(strings.Join(tok[1:], " "), m.Channel)
} }
} }
default: default:
} }
} }
func getWeather(query string) { func getWeather(query, channel string) {
q := owmQueryURLPrefix + query + owmQueryURLSuffix + owmQueryAPIKey q := owmQueryURLPrefix + query + owmQueryURLSuffix + owmQueryAPIKey
r, err := http.Get(q) r, err := http.Get(q)
if err != nil { if err != nil {
xlog.Error(err.Error()) xlog.Error(err.Error())
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) SayCh <- fmt.Sprintf("%s\n%s", channel, weatherPrefix+err.Error())
} else { } else {
re, err := ioutil.ReadAll(r.Body) re, err := ioutil.ReadAll(r.Body)
if err != nil { if err != nil {
xlog.Error(err.Error()) xlog.Error(err.Error())
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) SayCh <- fmt.Sprintf("%s\n%s", channel, weatherPrefix+err.Error())
} else { } else {
defer r.Body.Close() defer r.Body.Close()
var wo WeatherObject var wo WeatherObject
err := json.Unmarshal(re, &wo) err := json.Unmarshal(re, &wo)
if err != nil { if err != nil {
xlog.Error(err.Error()) xlog.Error(err.Error())
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) SayCh <- fmt.Sprintf("%s\n%s", channel, weatherPrefix+err.Error())
} else { } else {
var description string var description string
if len(wo.Weather) < 1 { if len(wo.Weather) < 1 {
@ -131,7 +129,7 @@ func getWeather(query string) {
} else { } else {
description = ", " + wo.Weather[0].Description 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))+ " - Temp: "+strconv.Itoa(int(wo.Main.Temp-273.15))+
"°C"+description+ "°C"+description+
", Humidity: "+strconv.Itoa(int(wo.Main.Humidity))+ ", Humidity: "+strconv.Itoa(int(wo.Main.Humidity))+