-
Notifications
You must be signed in to change notification settings - Fork 0
/
webzogger.go
156 lines (132 loc) · 4.42 KB
/
webzogger.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
package main
import (
"fmt"
"regexp"
"net/http"
_ "github.com/mattn/go-sqlite3"
"database/sql"
"flag"
"hash/fnv"
"strconv"
"strings"
"github.com/goji/httpauth"
"io/ioutil"
)
var dbPath string
var dbName string
func handler(w http.ResponseWriter, r *http.Request) {
var tabline string
// Beginning
fmt.Fprintf(w, "<html>\n<head>\n<title>ZOG is logging you!</title>\n</head>\n<body link='#0000FF' vlink='#CC00FF' bgcolor ='#CC00FF'><big><p><b>Welcome to ZOG</b></p></big>\n%s", r.URL.Path[1:])
// Open DB
db, err := sql.Open("sqlite3", dbPath)
checkErr(err)
// Query DB
//rows, err := db.Query("SELECT * FROM zogger")
rows, err := db.Query("SELECT * FROM zogger ORDER BY currentTime DESC")
checkErr(err)
// Output to web
for rows.Next() {
var serverName string
var roomName string
var userName string
var currentTime string
var ircMessage string
err = rows.Scan(&serverName, &roomName, &userName, ¤tTime, &ircMessage)
checkErr(err)
// Normalize percent signs
serverName = strings.Replace(serverName, "%", "%%", -1)
roomName = strings.Replace(roomName, "%", "%%", -1)
userName = strings.Replace(userName, "%", "%%", -1)
currentTime = strings.Replace(currentTime, "%", "%%", -1)
ircMessage = strings.Replace(ircMessage, "%", "%%", -1)
// Normalize < sign
serverName = strings.Replace(serverName, "<", "<", -1)
roomName = strings.Replace(roomName, "<", "<", -1)
userName = strings.Replace(userName, "<", "<", -1)
currentTime = strings.Replace(currentTime, "<", "<", -1)
ircMessage = strings.Replace(ircMessage, "<", "<", -1)
// Normalize > sign
serverName = strings.Replace(serverName, ">", ">", -1)
roomName = strings.Replace(roomName, ">", ">", -1)
userName = strings.Replace(userName, ">", ">", -1)
currentTime = strings.Replace(currentTime, ">", ">", -1)
ircMessage = strings.Replace(ircMessage, ">", ">", -1)
// Highlight links
ircMessage = highlight_links(ircMessage)
// Getting userName's hash
h := fnv.New32a()
h.Write([]byte(userName))
bgHash := h.Sum32()
strBgHash := strconv.FormatUint(uint64(bgHash), 16)
bgColor := strBgHash[len(strBgHash)-6:]
//fmt.Printf("|" + bgColor + "|")
tabline = "<br><span style='background-color: #" + bgColor + "'><b>" + userName + ":</b></span>" + "<span style='background-color: #99FFFF'>" + ircMessage + "</span>" + "<small><i>: at " + currentTime + "</i></small>%s"
fmt.Fprintf(w, tabline, r.URL.Path[1:])
}
// Close DB
db.Close()
checkErr(err)
// Ending
fmt.Fprintf(w, "</body></html>%s", r.URL.Path[1:])
}
func highlight_links(ircMessage string) string {
var link []string
var extracted_link string
var formatted_link string
re := regexp.MustCompile("( |^)http(s?)://[^ ]+")
for {
link = re.FindStringSubmatch(ircMessage)
if link != nil {
extracted_link = strings.TrimSpace(string(link[0]))
//fmt.Println(extracted_link)
formatted_link = "<a href=" + extracted_link + ">" + extracted_link + "</a> "
ircMessage = strings.Replace(ircMessage, link[0], formatted_link, -1)
} else {
break
}
}
return ircMessage
}
func main() {
var listenSocket string
var webUser string
var webAss string
var webAssFile string
var webCrtFile string
var webKeyFile string
flag.StringVar(&listenSocket, "listenSocket", ":9991", "a string var")
flag.StringVar(&dbPath, "dbPath", "db.sqlite3", "a string var")
flag.StringVar(&dbName, "dbName", "zogger", "a string var")
flag.StringVar(&webUser, "webUser", "", "a string var")
flag.StringVar(&webAss, "webAss", "", "a string var")
flag.StringVar(&webAssFile, "webAssFile", "", "a string var")
flag.StringVar(&webCrtFile, "webCrtFile", "", "a string var")
flag.StringVar(&webKeyFile, "webKeyFile", "", "a string var")
// Parse the flags
flag.Parse()
// Using HTTP-auth if needed
if webUser != "" {
// Read assword from file
if webAssFile != "" {
fmt.Printf("Reading room assword from: " + webAssFile + "\n")
dat, err := ioutil.ReadFile(webAssFile)
checkErr(err)
webAss = strings.TrimSpace(string(dat))
}
http.Handle("/", httpauth.SimpleBasicAuth(webUser, webAss)(http.HandlerFunc(handler)))
} else {
http.HandleFunc("/", handler)
}
// Verify if HTTPS should be used
if webCrtFile == "" {
http.ListenAndServe(listenSocket, nil)
} else {
http.ListenAndServeTLS(listenSocket, webCrtFile, webKeyFile, nil)
}
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}