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 (
"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
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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 <zip/city/state/country>")
SayCh <- fmt.Sprintf("%s\n%s", m.Channel, 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("%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))+