openidec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit fb122b79a8e0a208972d5e75c03a16f7ee4d3ba4
parent eadb2e2157e82f4a6d137c8aa43c98a988ca13f4
Author: Peter Kosyh <p.kosyh@gmail.com>
Date:   Sat,  5 Sep 2020 17:57:25 +0300

editing work

Diffstat:
Mii-node/lib/style.css | 4+++-
Aii-node/tpl/edit.tpl | 25+++++++++++++++++++++++++
Mii-node/tpl/new.tpl | 7+++++++
Mii-node/tpl/preview.tpl | 11+++++++++--
Mii-node/web.go | 54+++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mii/db.go | 10+++++++++-
6 files changed, 106 insertions(+), 5 deletions(-)

diff --git a/ii-node/lib/style.css b/ii-node/lib/style.css @@ -104,7 +104,9 @@ .odd { background: #eaffff; } - +#topic .echo { + font-weight: bold; +} #topic .msg { background-color: #ffffea; margin-bottom: 1em; diff --git a/ii-node/tpl/edit.tpl b/ii-node/tpl/edit.tpl @@ -0,0 +1,25 @@ +{{template "header.tpl" $}} +{{ $msg := index .Msg 0 }} +{{ with $msg }} +<table id="edit"> +<form method="post" enctype="application/x-www-form-urlencoded" action="/{{.MsgId}}/edit"> +<tr><td class="odd"> +<input type="hidden" name="id" value="{{.MsgId}}"> +<input type="hidden" name="repto" value="{{ . | repto}}"> +<input type="text" name="echo" class="echo" placeholder="{{.Echo}}" value="{{.Echo}}"><br> +<input type="text" name="to" class="to" placeholder="{{.To}}" value="{{.To}}"><br> +<input type="text" name="subj" class="subj" placeholder="{{.Subj}}" value="{{.Subj}}"><br> +<textarea type="text" name="msg" class="message" cols=60 row=16 placeholder="Text here."> +{{.Text}} +</textarea> +</td></tr> + +<tr><td class="odd center"> +<button class="form-button" type="submit" name="action" value="Submit">Submit</button> +<button class="form-button" type="submit" name="action" value="Preview">Preview</button> +</td></tr> +</form> + +</table> +{{ end }} +{{template "footer.tpl"}} diff --git a/ii-node/tpl/new.tpl b/ii-node/tpl/new.tpl @@ -1,7 +1,14 @@ {{template "header.tpl" $}} <table id="edit"> +{{ if eq .Echo "" }} +<form method="post" enctype="application/x-www-form-urlencoded" action="/new"> +{{ else }} <form method="post" enctype="application/x-www-form-urlencoded" action="/{{.Echo}}/new"> +{{ end }} <tr><td class="odd"> +{{ if eq .Echo "" }} +<input type="text" name="echo" class="echo" placeholder="echo" value=""><br> +{{ end }} <input type="text" name="to" class="to" placeholder="To" value="All"><br> <input type="text" name="subj" class="subj" placeholder="Subject"><br> <textarea type="text" name="msg" class="message" cols=60 row=16 placeholder="Hi, All!"> diff --git a/ii-node/tpl/preview.tpl b/ii-node/tpl/preview.tpl @@ -1,8 +1,9 @@ {{template "header.tpl" $}} -{{range .Msg}} +{{ with index .Msg 0 }} <div id="topic"> <div class="msg"> +<span class="echo">{{.Echo}}</span><br> <span class="subj">{{.Subj}}</span><br> <span class="info">{{.From}}({{.Addr}}) &mdash; {{.To}}<br>{{.Date | fdate}}</span><br> <div class="text"> @@ -16,9 +17,15 @@ </div> <table id="edit"> +{{ if eq $.Echo "" }} +<form method="post" enctype="application/x-www-form-urlencoded" action="/new"> +{{ else }} <form method="post" enctype="application/x-www-form-urlencoded" action="/{{.Echo}}/new"> - +{{ end }} <tr><td class="even"> +{{ if eq $.Echo "" }} +<input type="text" name="echo" class="echo" placeholder="{{.Echo}}" value="{{.Echo}}"><br> +{{ end }} <input type="text" name="to" class="to" placeholder="{{.To}}" value="{{.To}}"><br> <input type="text" name="subj" class="subj" placeholder="{{.Subj}}" value="{{.Subj}}"><br> <input type="hidden" name="repto" value="{{ . | repto}}"> diff --git a/ii-node/web.go b/ii-node/web.go @@ -299,6 +299,26 @@ func www_topic(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, i return err } +func www_edit(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, id string) error { + ctx := WebContext{ User: user, Echolist: www.edb, Ref: r.Header.Get("Referer") } + ctx.BasePath = id + switch r.Method { + case "GET": + m := www.db.Get(id) + if m == nil { + ii.Error.Printf("No such msg: %s", id) + return errors.New("No such msg") + } + msg := *m + ctx.Msg = append(ctx.Msg, &msg) + err := www.tpl.ExecuteTemplate(w, "edit.tpl", ctx) + return err + case "POST": + return www_new(user, www, w, r, "") + } + return nil +} + func www_new(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, echo string) error { ctx := WebContext{ User: user, Echolist: www.edb, Ref: r.Header.Get("Referer") } ctx.BasePath = echo @@ -309,6 +329,7 @@ func www_new(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, ech err := www.tpl.ExecuteTemplate(w, "new.tpl", ctx) return err case "POST": + edit := (echo == "") ii.Trace.Printf("www new topic in %s", echo) if err := r.ParseForm(); err != nil { ii.Error.Printf("Error in POST request: %s", err) @@ -322,6 +343,15 @@ func www_new(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, ech to := r.FormValue("to") msg := r.FormValue("msg") repto := r.FormValue("repto") + id := r.FormValue("id") + newecho := r.FormValue("echo") + if newecho != "" { + echo = newecho + } + if ! www.edb.Allowed(echo) { + ii.Error.Printf("This echo is disallowed") + return errors.New("This echo is disallowed") + } action := r.FormValue("action") text := fmt.Sprintf("%s\n%s\n%s\n\n%s", echo, to, subj, msg) m, err := ii.DecodeMsgline(text, false) @@ -334,14 +364,32 @@ func www_new(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, ech if repto != "" { m.Tags.Add("repto/" + repto) } + if id != "" { + om := www.db.Get(id) + if (om == nil || m.Addr != om.Addr) && user.Id != 1 { + ii.Error.Printf("Access denied") + return errors.New("Access denied") + } + m.MsgId = id + m.From = om.From + m.Addr = om.Addr + } if action == "Submit" { // submit - if err = www.db.Store(m); err != nil { + if edit { + err = www.db.Edit(m) + } else { + err = www.db.Store(m) + } + if err != nil { ii.Error.Printf("Error while storig new topic %s: %s", m.MsgId, err) return err } http.Redirect(w, r, "/"+m.MsgId+"#" + m.MsgId, http.StatusSeeOther) return nil } + if ! edit { + m.MsgId = "" + } ctx.Msg = append(ctx.Msg, m) err = www.tpl.ExecuteTemplate(w, "preview.tpl", ctx) return err @@ -492,10 +540,14 @@ func _handleWWW(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request) if len(args) > 1 { if args[1] == "reply" { return www_reply(user, www, w, r, args[0]) + } else if args[1] == "edit" { + return www_edit(user, www, w, r, args[0]) } fmt.Sscanf(args[1], "%d", &page) } return www_topic(user, www, w, r, args[0], page) + } else if path == "new" { + return www_new(user, www, w, r, "") } else if ii.IsEcho(args[0]) { page := 1 if len(args) > 1 { diff --git a/ii/db.go b/ii/db.go @@ -744,7 +744,15 @@ type EDB struct { Path string Sync sync.Mutex } - +func (db *EDB) Allowed(name string) bool { + if len(db.List) == 0 { + return true + } + if _, ok := db.Info[name]; ok { + return true + } + return false +} func LoadEcholist(path string) *EDB { var db EDB db.Path = path