exmo/bots/bybot/main.go

146 lines
4.6 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"math/rand"
"os"
"regexp"
"time"
"mcquay.me/exmo"
irc "github.com/fluffle/goirc/client"
)
var host = flag.String("host", "localhost:6668", "irc server hostname")
var channel = flag.String("channel", "#exmormon", "irc channel")
func main() {
maddr := os.Getenv("ADDR")
if maddr == "" {
fmt.Fprintf(os.Stderr, "failed to set ADDR\n")
os.Exit(1)
}
if err := exmo.Metrics(maddr); err != nil {
fmt.Fprintf(os.Stderr, "can't http: %v\n", err)
os.Exit(1)
}
rand.Seed(time.Now().UnixNano())
flag.Parse()
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
done := make(chan bool)
register(*host, *channel, done)
<-done
}
const name = "BYbot"
// BY is the Go embodiment of Brigham Young.
type BY struct {
quotes []string
poke chan bool
}
// Random returns a random BY quote. That asshat.
func (by *BY) Random() string {
if len(by.quotes) == 0 {
return ""
}
return by.quotes[rand.Intn(len(by.quotes))]
}
func (by *BY) spew(conn *irc.Conn, channel string) {
for {
delay := time.Duration(2+rand.Intn(24)) * time.Hour
log.Printf("next quote: %+v", delay)
timer := time.NewTimer(delay)
select {
case <-by.poke:
case <-timer.C:
}
conn.Privmsg(channel, by.Random())
}
}
func register(host, channel string, done chan<- bool) {
var p *regexp.Regexp
by := BY{
quotes,
make(chan bool),
}
c := irc.SimpleClient(name, name)
c.EnableStateTracking()
c.HandleFunc(
irc.CONNECTED,
func(conn *irc.Conn, line *irc.Line) {
conn.Join(channel)
var err error
p, err = regexp.Compile(`\b` + conn.Me().Nick + `\b`)
if err != nil {
log.Printf("%+v", err)
}
go by.spew(conn, channel)
},
)
c.HandleFunc(
irc.DISCONNECTED,
func(conn *irc.Conn, line *irc.Line) {
done <- true
},
)
c.HandleFunc(
irc.PRIVMSG,
func(conn *irc.Conn, line *irc.Line) {
if p.MatchString(line.Text()) {
log.Printf("addressed me: %v", line.Text())
by.poke <- true
}
},
)
if err := c.ConnectTo(host); err != nil {
fmt.Printf("Connection error: %s\n", err)
}
}
var quotes = []string{
// things he actually said
" Let me have the privilege of correcting a sermon, and it is as good Scripture as they deserve.",
"Cain slew his brother. Cain might have been killed, and that would have put a termination to that line of human beings. This was not to be, and the Lord put a mark upon him, which is the flat nose and black skin",
"I have never given any counsel that was wrong.",
"I have never yet preached a sermon and sent it out to the children of men, that they may not call Scripture",
"I know just as well what to teach this people and just what to say to them and what to do in order to bring them into the celestial kingdom.",
"I say now, when [my discourses] are copied and approved by me they are as good Scripture as is couched in this Bible!",
"If the white man who belongs to the chosen seed mixes his blood with the seed of Cain, the penalty, under the law of God, is death on the spot. This will always be so",
"It is enough. This is the right place, drive on.",
"My discourses are as good as Scripture!",
"No man or woman in this dispensation will ever enter into the celestial kingdom of God without the consent of Joseph Smith",
"Now hear it, O inhabitants of the earth, Jew and Gentile, Saint and sinner!",
"Now if any of you will deny the plurality of wives, and continue to do so, I promise that you will be damned.",
"Shall I tell you the law of God in regard to the African race?",
"The only men who become Gods, even the Sons of God, are those who enter into polygamy,",
"The success which has attended me in my presidency is owing to the blessings and mercy of the Almighty.",
"We knew that the children of Ham were to be the \"servant of servants,\"",
"You are damned if you deny polygamy!",
"You must confess Joseph Smith as a prophet of God in order to be saved.",
"You see some classes of the human family that are black, uncouth, uncomely, disagreeable and low in their habits, wild, and seemingly deprived of nearly all the blessings of the intelligence that is generally bestowed upon mankind",
"Your own blood must atone for some sins",
"[Because of me] The people have the oracles of God continually.",
"\"servant of servants\" ... looks weird when you type it out",
// artistic license
"I was a collosal asshole.",
"It is enough. This is the right place [... they can't prosecute polygamy here].",
"Wife! Bring me a wife!!",
"[mumble mumble mumble] (something racist) [mumble mumble mumble]",
"\"servant of servants\" ... looks weird when you type it out",
"♬♬ God changed his mind about black people (black people!!) ♬♬",
}