...

Source file src/codeberg.org/tslocum/sriracha/internal/server/server_plugin.go

Documentation: codeberg.org/tslocum/sriracha/internal/server

     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 changes != "" {
    70  			s.log(db, data.Account, nil, fmt.Sprintf("Reset plugin %s", info.Name), changes)
    71  		}
    72  		http.Redirect(w, r, fmt.Sprintf("/sriracha/plugin/%s", strings.ToLower(info.Name)), http.StatusFound)
    73  		return
    74  	}
    75  
    76  	split := strings.Split(PathString(r, "/sriracha/plugin/view/"), "/")
    77  	if len(split) > 0 {
    78  		plugin, info = pluginByName(split[0])
    79  		if plugin != nil {
    80  			pServe, ok := plugin.(sriracha.PluginWithServe)
    81  			if !ok {
    82  				http.Redirect(w, r, "/sriracha/plugin/", http.StatusFound)
    83  				return
    84  			}
    85  			msg, err := pServe.Serve(db, data.Account, w, r)
    86  			if err != nil {
    87  				data.ManageError(err.Error())
    88  				return
    89  			} else if msg != "" {
    90  				data.Template = "manage_info"
    91  				data.Message = template.HTML(`<h2 class="managetitle">`+strings.Title(info.Name)+`</h2>`) + msg
    92  			} else {
    93  				data.Template = ""
    94  			}
    95  			return
    96  		}
    97  	}
    98  
    99  	plugin, info = pluginByName(PathString(r, "/sriracha/plugin/"))
   100  	if plugin != nil {
   101  		data.Manage.Plugin = info
   102  
   103  		if r.Method == http.MethodPost {
   104  			r.ParseForm()
   105  			formKeys := make([]string, len(r.Form))
   106  			{
   107  				var i int
   108  				for key := range r.Form {
   109  					formKeys[i] = key
   110  					i++
   111  				}
   112  				sort.Slice(formKeys, func(i, j int) bool {
   113  					return formKeys[i] < formKeys[j]
   114  				})
   115  			}
   116  
   117  			pUpdate, _ := plugin.(sriracha.PluginWithUpdate)
   118  			var changed bool
   119  			var changes string
   120  			for i, c := range info.Config {
   121  				var newValue string
   122  				for _, key := range formKeys {
   123  					values := r.Form[key]
   124  					if strings.HasPrefix(key, "config_"+c.Name) && len(values) > 0 {
   125  						for _, v := range values {
   126  							if strings.TrimSpace(v) == "" {
   127  								continue
   128  							} else if newValue != "" {
   129  								newValue += "|||"
   130  							}
   131  							newValue += v
   132  						}
   133  					}
   134  				}
   135  
   136  				oldValue := c.Value
   137  				if oldValue == newValue {
   138  					continue
   139  				}
   140  				db.SaveString(strings.ToLower(info.Name+"."+c.Name), newValue)
   141  				info.Config[i].Value = newValue
   142  				changed = true
   143  
   144  				if pUpdate != nil {
   145  					db.Plugin = info.Name
   146  					pUpdate.Update(db, c.Name)
   147  					db.Plugin = ""
   148  				}
   149  
   150  				if c.Sensitive {
   151  					continue
   152  				}
   153  				oldLabel := oldValue
   154  				newLabel := newValue
   155  				if c.Type == sriracha.TypeBoolean {
   156  					if oldValue != "1" {
   157  						oldLabel = "false"
   158  					} else {
   159  						oldLabel = "true"
   160  					}
   161  					if newValue != "1" {
   162  						newLabel = "false"
   163  					} else {
   164  						newLabel = "true"
   165  					}
   166  				}
   167  				if changes != "" {
   168  					changes += " "
   169  				}
   170  				changes += fmt.Sprintf(`[%s: "%s" > "%s"]`, strings.Title(strings.ReplaceAll(c.Name, "_", " ")), oldLabel, newLabel)
   171  			}
   172  
   173  			if changed {
   174  				s.log(db, data.Account, nil, fmt.Sprintf("Updated plugin %s", info.Name), changes)
   175  			}
   176  			http.Redirect(w, r, "/sriracha/plugin", http.StatusFound)
   177  		}
   178  		return
   179  	}
   180  
   181  	data.Manage.Plugins = allPluginInfo
   182  }
   183  

View as plain text