From 1da7017cdcec48d1d199511ebb315c5460d7a9f6 Mon Sep 17 00:00:00 2001 From: Joe Rayhawk Date: Wed, 13 Jun 2018 09:08:07 -0700 Subject: acronym.go: acronym generation bot command --- go/acronym.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 go/acronym.go diff --git a/go/acronym.go b/go/acronym.go new file mode 100644 index 0000000..955f256 --- /dev/null +++ b/go/acronym.go @@ -0,0 +1,67 @@ +package main + +import( + "bufio" + "fmt" + "github.com/hpcloud/tail" + "math/rand" + "net" + "os" + "regexp" + "strings" + "time" +) + +func check( e error ) { + if e != nil { + panic( e ) + } +} + +func main() { + rand.Seed( time.Now(). UnixNano() ) + dictfile, err := os.Open( "/usr/share/dict/american-english-large" ) + check( err ) + + defer dictfile.Close() + scanner := bufio.NewScanner( dictfile ) + scanner.Split(bufio.ScanLines) + var clines []string + var blines []string + var klines []string + for scanner.Scan() { + if strings.HasPrefix( scanner.Text(), "c" ) { + clines = append(clines, scanner.Text()) + } + if strings.HasPrefix( scanner.Text(), "b" ) { + blines = append(blines, scanner.Text()) + } + if strings.HasPrefix( scanner.Text(), "k" ) { + klines = append(klines, scanner.Text()) + } + } + + r := regexp.MustCompile( "^[0-9]{2}:[0-9]{2} <(.bungmonkey|.cornbugs|.wishapb|@[a-zA-Z0-9_]+)> +!cbk" ) + bungr := regexp.MustCompile( "^[0-9]{2}:[0-9]{2} <.bungmonkey> +!cbk" ) + logfile, err := tail.TailFile( os.ExpandEnv( "${HOME}/irclogs/twitch/#cornbugs.log" ), tail.Config{ Follow: true, Location: &tail.SeekInfo{ 0, os.SEEK_END } } ) + check( err ) + for logline := range logfile.Lines { + + if bungr.MatchString( logline.Text ) { + time.Sleep( time.Second ) + } + if r.MatchString( logline.Text ) { + var cbk string + cbk = fmt.Sprintf( "%s %s %s\n", clines[ rand.Intn( len( clines ) ) ], blines[ rand.Intn( len( blines ) ) ], klines[ rand.Intn( len( klines ) ) ] ) + fmt.Printf( "%v", cbk ) + ircsocket, err := net.Dial( "unix", os.ExpandEnv( "${HOME}/.irssi/twitch-cornbugs-socket" ) ) + check( err ) + byteswritten, err := ircsocket.Write( []byte( cbk ) ) + ircsocket.Close() + check( err ) + fmt.Printf( "%v bytes written\n", byteswritten ) + } + } + +} + -- cgit v1.2.3