1 package server
2
3 import (
4 "fmt"
5 "html/template"
6 "net/http"
7 "sort"
8 "strings"
9
10 "codeberg.org/tslocum/sriracha"
11 "codeberg.org/tslocum/sriracha/internal/database"
12 . "codeberg.org/tslocum/sriracha/model"
13 . "codeberg.org/tslocum/sriracha/util"
14 )
15
16 func (s *Server) servePlugin(data *templateData, db *database.DB, w http.ResponseWriter, r *http.Request) {
17 if data.forbidden(w, RoleAdmin) {
18 return
19 }
20 data.Template = "manage_plugin"
21 data.Boards = db.AllBoards()
22
23 plugin, info := pluginByName(PathString(r, "/sriracha/plugin/reset/"))
24 if plugin != nil {
25 var changes string
26 pUpdate, _ := plugin.(sriracha.PluginWithUpdate)
27 for i, c := range info.Config {
28 defaultValue := c.Default
29 if c.Type == sriracha.TypeEnum {
30 defaultValue = ""
31 }
32
33 oldValue := info.Config[i].Value
34 if oldValue == defaultValue {
35 continue
36 }
37 db.SaveString(strings.ToLower(info.Name+"."+c.Name), defaultValue)
38 info.Config[i].Value = defaultValue
39
40 if pUpdate != nil {
41 db.Plugin = info.Name
42 pUpdate.Update(db, c.Name)
43 db.Plugin = ""
44 }
45
46 if c.Sensitive {
47 continue
48 }
49 oldLabel := oldValue
50 newLabel := info.Config[i].Value
51 if info.Config[i].Type == sriracha.TypeBoolean {
52 if oldValue != "1" {
53 oldLabel = "false"
54 } else {
55 oldLabel = "true"
56 }
57 if info.Config[i].Value != "1" {
58 newLabel = "false"
59 } else {
60 newLabel = "true"
61 }
62 }
63 if changes != "" {
64 changes += " "
65 }
66 changes += fmt.Sprintf(`[%s: "%s" > "%s"]`, strings.Title(strings.ReplaceAll(c.Name, "_", " ")), oldLabel, newLabel)
67 }
68
69 if _, ok := plugin.(sriracha.PluginWithRules); ok {
70 s.refreshRulesCache(db)
71 }
72
73 if changes != "" {
74 s.log(db, data.Account, nil, fmt.Sprintf("Reset plugin %s", info.Name), changes)
75 }
76 data.Redirect(w, r, fmt.Sprintf("/sriracha/plugin/%s", strings.ToLower(info.Name)))
77 return
78 }
79
80 split := strings.Split(PathString(r, "/sriracha/plugin/view/"), "/")
81 if len(split) > 0 {
82 plugin, info = pluginByName(split[0])
83 if plugin != nil {
84 pServe, ok := plugin.(sriracha.PluginWithServe)
85 if !ok {
86 data.Redirect(w, r, "/sriracha/plugin/")
87 return
88 }
89 msg, err := pServe.Serve(db, data.Account, w, r)
90 if err != nil {
91 data.ManageError(err.Error())
92 return
93 } else if msg != "" {
94 data.Template = "manage_info"
95 data.Message = template.HTML(`<h2 class="managetitle">`+strings.Title(info.Name)+`</h2>`) + msg
96 } else {
97 data.Template = ""
98 }
99 return
100 }
101 }
102
103 plugin, info = pluginByName(PathString(r, "/sriracha/plugin/"))
104 if plugin != nil {
105 data.Manage.Plugin = info
106
107 if r.Method == http.MethodPost {
108 r.ParseForm()
109 formKeys := make([]string, len(r.Form))
110 {
111 var i int
112 for key := range r.Form {
113 formKeys[i] = key
114 i++
115 }
116 sort.Slice(formKeys, func(i, j int) bool {
117 return formKeys[i] < formKeys[j]
118 })
119 }
120
121 pUpdate, _ := plugin.(sriracha.PluginWithUpdate)
122 var changed bool
123 var changes string
124 for i, c := range info.Config {
125 var newValue string
126 for _, key := range formKeys {
127 values := r.Form[key]
128 if strings.HasPrefix(key, "config_"+c.Name) && len(values) > 0 {
129 for _, v := range values {
130 if strings.TrimSpace(v) == "" {
131 continue
132 } else if newValue != "" {
133 newValue += "|||"
134 }
135 newValue += v
136 }
137 }
138 }
139
140 oldValue := c.Value
141 if oldValue == newValue {
142 continue
143 }
144 db.SaveString(strings.ToLower(info.Name+"."+c.Name), newValue)
145 info.Config[i].Value = newValue
146 changed = true
147
148 if pUpdate != nil {
149 db.Plugin = info.Name
150 pUpdate.Update(db, c.Name)
151 db.Plugin = ""
152 }
153
154 if c.Sensitive {
155 continue
156 }
157 oldLabel := oldValue
158 newLabel := newValue
159 if c.Type == sriracha.TypeBoolean {
160 if oldValue != "1" {
161 oldLabel = "false"
162 } else {
163 oldLabel = "true"
164 }
165 if newValue != "1" {
166 newLabel = "false"
167 } else {
168 newLabel = "true"
169 }
170 }
171 if changes != "" {
172 changes += " "
173 }
174 changes += fmt.Sprintf(`[%s: "%s" > "%s"]`, strings.Title(strings.ReplaceAll(c.Name, "_", " ")), oldLabel, newLabel)
175 }
176
177 if _, ok := plugin.(sriracha.PluginWithRules); ok {
178 s.refreshRulesCache(db)
179 }
180
181 if changed {
182 s.log(db, data.Account, nil, fmt.Sprintf("Updated plugin %s", info.Name), changes)
183 }
184 data.Redirect(w, r, "/sriracha/plugin")
185 }
186 return
187 }
188
189 data.Manage.Plugins = allPluginInfo
190 }
191
View as plain text