This repository has been archived by the owner on Mar 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
104 lines (96 loc) · 2.35 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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
)
type MessageDump struct {
Bid int `json:"bid"`
Type string `json:"type"`
URL string `json:"url"`
}
type Msg struct {
Bid float64 `json:"bid"`
Chan string `json:"chan"`
Cid float64 `json:"cid"`
Eid float64 `json:"eid"`
From string `json:"from"`
FromHost string `json:"from_host"`
FromMode string `json:"from_mode"`
FromName string `json:"from_name"`
Hostmask string `json:"hostmask"`
IdentPrefix string `json:"ident_prefix"`
Msg string `json:"msg"`
Type string `json:"type"`
}
var FileLogging *bool
var RetryCount int
func main() {
FileLogging = flag.Bool("filelogging", false, "true to enable logging to ./log")
flag.Parse()
fmt.Fprintf(os.Stderr, "IRCCloud Streamer")
streamtoken := ""
if len(os.Args) == 2 {
streamtoken = os.Args[1]
} else {
sb, e := ioutil.ReadFile("./.session")
if e != nil {
log.Fatal("cannot read session cookie. Please make a .session file and put your cookie in there.")
}
lines := strings.Split(string(sb), "\n")
if len(lines) != 1 {
log.Fatal("There is more than one line in the session file. wat.")
}
streamtoken = lines[0]
}
h, e := http.NewRequest("GET", "https://www.irccloud.com/chat/stream", nil)
if e != nil {
panic(e)
}
client := &http.Client{}
h.Header.Add("Cookie", fmt.Sprintf("session=%s", streamtoken))
resp, e := client.Do(h)
if e != nil {
panic(e)
}
var f *os.File
if *FileLogging {
var err error
f, err = os.OpenFile("./log", os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic("cannot open the log file.")
}
}
reader := bufio.NewReader(resp.Body)
for {
s, err := reader.ReadString('\n')
wat := MessageDump{}
json.Unmarshal([]byte(s), &wat)
if wat.URL != "" && wat.Type == "oob_include" {
h, e = http.NewRequest("GET", "https://www.irccloud.com"+wat.URL, nil)
h.Header.Add("Cookie", fmt.Sprintf("session=%s", streamtoken))
tresp, _ := client.Do(h)
ioutil.ReadAll(tresp.Body)
fmt.Fprintf(os.Stderr, "Did the OOB include, the stream should work ~forever~ now.")
}
if err != nil {
time.Sleep(time.Second * 5)
RetryCount++
if RetryCount > 5 {
panic(err)
}
main()
}
fmt.Printf("%s", s)
if *FileLogging {
f.WriteString(s)
}
}
}