summaryrefslogtreecommitdiff
path: root/go/acronym.go
blob: ef92ef4fd187d0fb0ec110f6898f176eeb6da066 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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)
	dict := make( map[string][]string )
	for scanner.Scan() {
		dict[scanner.Text()[:1]] = append(dict[scanner.Text()[:1]], 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", dict["c"][ rand.Intn( len( dict["c"] ) ) ], dict["b"][ rand.Intn( len( dict["b"] ) ) ], dict["k"][ rand.Intn( len( dict["k"] ) ) ] )
			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 )
		}
	}

}