-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
175 lines (164 loc) · 3.95 KB
/
main.go
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//rektcorder is a recorder and player for destiny.gg chat logs
//Copyright (C) 2013 Eric Culp
//
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU Affero General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU Affero General Public License for more details.
//
//You should have received a copy of the GNU Affero General Public License
//along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"flag"
"io"
"log"
"time"
"code.google.com/p/go.net/websocket"
"github.com/wsxiaoys/terminal/color"
)
var Config struct {
Debug bool
Joins bool
Sid string
RecordDir string
Record bool
Play bool
Start string
Silent bool
Timeout time.Duration
}
func init() {
flag.BoolVar(&Config.Debug, "debug", false, "show debug output")
flag.BoolVar(&Config.Joins, "joins", false, "show joins and leaves")
flag.StringVar(&Config.Sid, "sid", "", "use sid to login")
flag.BoolVar(&Config.Record, "record", false, "log messages for playback")
flag.StringVar(&Config.RecordDir, "record-dir", ".", "dir in which to log messages for playback")
flag.BoolVar(&Config.Play, "play", false, "playback message logs instead of listening")
flag.StringVar(&Config.Start, "start", "", "time to start playback")
flag.BoolVar(&Config.Silent, "silent", false, "don't print messages")
flag.DurationVar(&Config.Timeout, "timeout", time.Duration(30)*time.Second,
"panic after no messages have been received for this long")
flag.Parse()
}
func debug(v ...interface{}) {
if Config.Debug {
log.Panicln(v...)
}
}
func debugf(f string, v ...interface{}) {
if Config.Debug {
log.Printf(f, v...)
}
}
func connect() *websocket.Conn {
var wsconfig websocket.Config
if Config.Sid != "" {
wsconfig.Header.Set("Cookie", "sid="+Config.Sid)
}
for {
log.Printf("Connecting...\n")
ws, err := websocket.Dial("ws://www.destiny.gg:9998/ws", "", "http://www.destiny.gg")
if err == nil {
return ws
}
}
}
func timeout() {
panic("Chat timed out")
}
func listen() {
ws := connect()
defer func() {
err := ws.Close()
if err != nil {
log.Fatal(err)
}
}()
var ml *MessageLog
if Config.Record {
ml = NewMessageLog(Config.RecordDir)
defer ml.Close()
}
timer := time.AfterFunc(Config.Timeout, timeout)
for {
var v interface{}
err := Codec.Receive(ws, &v)
if err == io.EOF {
ws.Close()
debugf("EOF, Reconnect\n")
ws = connect()
continue
} else if err == ErrMessageUnknownHeader {
continue
} else if err == ErrMessageNoHeader {
continue
} else if err != nil {
log.Fatal(err)
}
timer.Reset(Config.Timeout)
switch v := v.(type) {
case Msg:
if !Config.Silent {
v.Print()
}
if Config.Record {
err = ml.Write(&v)
if err != nil {
log.Fatal(err)
}
}
case Ping:
var p Pong
p.Data = v.Data
err := Codec.Send(ws, p)
if err != nil {
log.Fatal(err)
}
case Names:
case Join:
if Config.Joins {
color.Printf("@{/}* %s joined@{|}\n", v.Nick)
}
case Quit:
if Config.Joins {
color.Printf("@{/}* %s quit@{|}\n", v.Nick)
}
case Broadcast:
if Config.Joins {
color.Printf("@{/}* %s@{|}\n", v.Data)
}
default:
log.Printf("unknown value %T %v\n", v, v)
}
}
}
func playback() {
p := NewPlayer(Config.RecordDir)
start, err := time.Parse("2006-01-02T15:04:05", Config.Start)
if err != nil {
debugf("%s isn't a date, trying twitch\n", Config.Start)
var t Twitch
v, err := t.Video(Config.Start)
if err != nil {
log.Fatal(err)
}
start = v.RecordedAt.UTC()
}
err = p.Playback(start)
if err != nil {
log.Fatal(err)
}
}
func main() {
if Config.Play {
playback()
} else {
listen()
}
}