-
Notifications
You must be signed in to change notification settings - Fork 3
/
baseBot.go
194 lines (159 loc) · 3.8 KB
/
baseBot.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/howeyc/gopass"
"golang.org/x/net/websocket"
)
type ResponseHandler func(string)
type IRCCResponse struct {
Success bool `json: "success,string"`
Message string `json: "message,string"`
Session string `json: "session,omitempty"`
Token string `json: "token,omitempty"`
}
type StreamJSON struct {
Type string `json: "type,string"`
URL string `json: "url,string,omitempty"`
}
type Config struct {
SessionKey string
Debug bool
}
var config Config
func getAuthToken() (data IRCCResponse, err error) {
resp, err := http.Post("https://www.irccloud.com/chat/auth-formtoken", "", nil)
if err != nil {
return
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
json.Unmarshal(respBody, &data)
return
}
func getSession(email, password string) (session string, err error) {
data, err := getAuthToken()
if err != nil {
return
}
body := url.Values{}
body.Add("email", email)
body.Add("password", password)
body.Add("token", data.Token)
headers := make(http.Header)
headers.Add("x-auth-formtoken", data.Token)
headers.Add("User-Agent", "fucking-ninja")
headers.Add("Content-Type", "x-www-form-urlencoded")
resp, err := post("https://www.irccloud.com/chat/login", headers, body)
if err != nil {
return
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
json.Unmarshal(respBody, &data)
err = nil
if data.Success != true {
err = errors.New("Login Failed, Message: " + data.Message)
}
return data.Session, err
}
func irccConfig(sessionKey string) (config *websocket.Config, err error) {
config, err = websocket.NewConfig("wss://api.irccloud.com/",
"https://www.irccloud.com")
if err != nil {
return
}
config.Header.Add("Origin", "https://api.irccloud.com")
config.Header.Add("Cookie", "session="+sessionKey)
config.Header.Add("User-Agent", "ninja")
return
}
func connectServerWS(key string, handler ResponseHandler) (err error) {
config, err := irccConfig(key)
if err != nil {
return
}
conn, err := websocket.DialConfig(config)
if err != nil {
return
}
var msg string
for {
err = websocket.Message.Receive(conn, &msg)
if err != nil {
return
} else {
handler(msg)
}
}
}
func responseHandler(line string) {
var msg StreamJSON
json.Unmarshal([]byte(line), &msg)
if msg.Type == "oob_include" {
go visitURL(msg.URL)
}
if config.Debug {
fmt.Println(line)
}
}
func visitURL(url string) {
client := http.Client{}
req, err := http.NewRequest("GET", "https://www.irccloud.com"+url, nil)
if err != nil {
panic(err)
}
req.Header.Add("Cookie", "session="+config.SessionKey)
req.Header.Add("Accept-Encoding", "gzip")
_, err = client.Do(req)
if err != nil {
panic(err)
}
}
func start(username, password string, debug bool, tries int) {
key, err := getSession(username, password)
if err != nil {
goto ManageError
}
config = Config{key, debug}
err = connectServerWS(key, responseHandler)
if err != nil {
goto ManageError
}
return
ManageError:
if tries > 0 {
fmt.Println("Error:", err.Error())
fmt.Println("Attempt number", tries)
start(username, password, debug, tries-1)
} else {
fmt.Println("Gave up reattempting")
fmt.Println("Last error", err.Error())
}
}
func main() {
email := flag.String("email", "", "IRCCloud email")
password := flag.String("password", "", "IRCCloud password")
debug := flag.Bool("debug", false, "Print the responses")
attempts := flag.Int("attempts", 5, "Number of attempts to make when disconnected from server")
flag.Parse()
if *email == "" {
flag.PrintDefaults()
return
}
if *password == "" {
fmt.Print("Password:")
*password = string(gopass.GetPasswd())
}
// try reconnecting 5 or specified times
start(*email, *password, *debug, *attempts)
}