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