summaryrefslogtreecommitdiff
path: root/go/acronym.go
blob: b6ead6b8d89e14e6c7fedb4113b64344eb15f809 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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[ strings.ToLower( scanner.Text()[:1] ) ] = append( dict[ strings.ToLower( scanner.Text()[:1] ) ], scanner.Text() )
	}

	gdictfile, err := os.Open( "/usr/share/dict/ngerman" )
	check( err )

	defer gdictfile.Close()
	gscanner := bufio.NewScanner( gdictfile )
	gscanner.Split( bufio.ScanLines )
	gdict := make( map[string][]string )
	for gscanner.Scan() {
		gdict[ strings.ToLower( gscanner.Text()[:1] ) ] = append( gdict[ strings.ToLower( gscanner.Text()[:1] ) ], gscanner.Text() )
	}

	backronymr := regexp.MustCompile( "^[0-9]{2}:[0-9]{2} <(.bungmonkey|.cornbugs|.wishapb|.ameera|.bungmonkey|.nottobiii|.sash_isynergy|@[a-zA-Z0-9_]+)> +!backronym ([a-zA-Z]{1,12})( |$)" )
	akronymr := regexp.MustCompile( "^[0-9]{2}:[0-9]{2} <(.bungmonkey|.cornbugs|.wishapb|.ameera|.bungmonkey|.nottobiii|.sash_isynergy|@[a-zA-Z0-9_]+)> +!akronym ([a-zA-Z]{1,12})( |$)" )
	bungr := regexp.MustCompile( "^[0-9]{2}:[0-9]{2} <.bungmonkey> +!cbk" )
	logfile, err := tail.TailFile( os.ExpandEnv( "${HOME}/irclogs/twitch/#bungmonkey.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 backronymr.MatchString( logline.Text ) {
			var commandarg []string
			var backronymarr []string
			var backronymstring string

			commandarg = backronymr.FindStringSubmatch( logline.Text )
			for _, char := range( commandarg[2] ) {
				backronymarr = append( backronymarr, dict[ strings.ToLower( string( char ) )][ rand.Intn( len( dict[ strings.ToLower( string( char ) ) ] ) ) ] )
			}
			backronymstring = strings.Join( backronymarr, " " )
			ircsocket, err := net.Dial( "unix", os.ExpandEnv( "${HOME}/.irssi/twitch-socket" ) )
			check( err )
			backronymstring = fmt.Sprintf( "#bungmonkey %s\n", backronymstring )
			byteswritten, err := ircsocket.Write( []byte( backronymstring ) )
			ircsocket.Close()
			fmt.Printf( "%v\n", backronymstring )
			fmt.Printf( "%v bytes written\n", byteswritten )
		}
		if akronymr.MatchString( logline.Text ) {
			var gcommandargg []string
			var gbackronymarr []string
			var gbackronymstring string

			gcommandargg = akronymr.FindStringSubmatch( logline.Text )
			for _, char := range( gcommandargg[2] ) {
				gbackronymarr = append( gbackronymarr, gdict[ strings.ToLower( string( char ) )][ rand.Intn( len( gdict[ strings.ToLower( string( char ) ) ] ) ) ] )
			}
			gbackronymstring = strings.Join( gbackronymarr, " " )
			ircsocket, err := net.Dial( "unix", os.ExpandEnv( "${HOME}/.irssi/twitch-socket" ) )
			check( err )
			gbackronymstring = fmt.Sprintf( "#bungmonkey %s\n", gbackronymstring )
			byteswritten, err := ircsocket.Write( []byte( gbackronymstring ) )
			ircsocket.Close()
			fmt.Printf( "%v\n", gbackronymstring )
			fmt.Printf( "%v bytes written\n", byteswritten )
		}
	}

}