webui/webui.go (view raw)
1package webui
2
3import (
4 "bytes"
5 "log"
6 "net/http"
7 "strconv"
8 "text/template"
9
10 "github.com/BiRabittoh/escarbot/telegram"
11)
12
13type WebUI struct {
14 Server *http.ServeMux
15 EscarBot *telegram.EscarBot
16
17 port string
18}
19
20var indexTemplate = template.Must(template.ParseFiles("index.html"))
21
22const toggleFormName = "toggle"
23
24func indexHandler(bot *telegram.EscarBot) http.HandlerFunc {
25 return func(w http.ResponseWriter, r *http.Request) {
26 buf := &bytes.Buffer{}
27 err := indexTemplate.Execute(buf, bot)
28 if err != nil {
29 http.Error(w, err.Error(), http.StatusInternalServerError)
30 return
31 }
32 buf.WriteTo(w)
33 }
34}
35
36func toggleBotProperty(w http.ResponseWriter, r *http.Request, bot *telegram.EscarBot) bool {
37 r.ParseForm()
38 res := r.Form.Get("toggle")
39 http.Redirect(w, r, "/", http.StatusFound)
40 return res == "on"
41}
42
43func getChatID(w http.ResponseWriter, r *http.Request, bot *telegram.EscarBot) (int64, error) {
44 r.ParseForm()
45 res := r.Form.Get("id")
46 http.Redirect(w, r, "/", http.StatusFound)
47 return strconv.ParseInt(res, 10, 64)
48}
49
50func linksHandler(bot *telegram.EscarBot) http.HandlerFunc {
51 return func(w http.ResponseWriter, r *http.Request) {
52 bot.LinkDetection = toggleBotProperty(w, r, bot)
53 }
54}
55
56func channelForwardHandler(bot *telegram.EscarBot) http.HandlerFunc {
57 return func(w http.ResponseWriter, r *http.Request) {
58 bot.ChannelForward = toggleBotProperty(w, r, bot)
59 }
60}
61
62func adminForwardHandler(bot *telegram.EscarBot) http.HandlerFunc {
63 return func(w http.ResponseWriter, r *http.Request) {
64 bot.AdminForward = toggleBotProperty(w, r, bot)
65 }
66}
67
68func channelHandler(bot *telegram.EscarBot) http.HandlerFunc {
69 return func(w http.ResponseWriter, r *http.Request) {
70 res, err := getChatID(w, r, bot)
71 if err != nil {
72 log.Println(err)
73 return
74 }
75 bot.ChannelID = res
76 }
77}
78
79func groupHandler(bot *telegram.EscarBot) http.HandlerFunc {
80 return func(w http.ResponseWriter, r *http.Request) {
81 res, err := getChatID(w, r, bot)
82 if err != nil {
83 log.Println(err)
84 return
85 }
86 bot.GroupID = res
87 }
88}
89
90func adminHandler(bot *telegram.EscarBot) http.HandlerFunc {
91 return func(w http.ResponseWriter, r *http.Request) {
92 res, err := getChatID(w, r, bot)
93 if err != nil {
94 log.Println(err)
95 return
96 }
97 bot.AdminID = res
98 }
99}
100
101func NewWebUI(port string, bot *telegram.EscarBot) WebUI {
102
103 go telegram.BotPoll(bot)
104
105 r := http.NewServeMux()
106 r.HandleFunc("/", indexHandler(bot))
107 r.HandleFunc("/setLinks", linksHandler(bot))
108 r.HandleFunc("/setChannelForward", channelForwardHandler(bot))
109 r.HandleFunc("/setAdminForward", adminForwardHandler(bot))
110 r.HandleFunc("/setChannel", channelHandler(bot))
111 r.HandleFunc("/setGroup", groupHandler(bot))
112 r.HandleFunc("/setAdmin", adminHandler(bot))
113
114 return WebUI{
115 Server: r,
116 EscarBot: bot,
117 port: port,
118 }
119}
120
121func (webui *WebUI) Poll() {
122 log.Println("Serving on port", webui.port)
123 err := http.ListenAndServe(":"+webui.port, webui.Server)
124 if err != nil {
125 log.Fatal(err)
126 }
127}