commit 15ca884a1774467588d8b1e06803d6ec423a63f4
parent b55764b12722a58f4af3524bb10bb842b1c9b0ce
Author: Peter Kosyh <p.kosyh@gmail.com>
Date: Fri, 4 Sep 2020 22:24:41 +0300
echolist support
Diffstat:
5 files changed, 49 insertions(+), 15 deletions(-)
diff --git a/ii-node/main.go b/ii-node/main.go
@@ -51,10 +51,12 @@ var db_opt *string = flag.String("db", "./db", "II database path (directory)")
var listen_opt *string = flag.String("L", ":8080", "Listen address")
var sysname_opt *string = flag.String("sys", "ii-go", "Node name")
var verbose_opt *bool = flag.Bool("v", false, "Verbose")
+var echo_opt *string = flag.String("e", "list.txt", "Echoes list")
type WWW struct {
tpl *template.Template
db *ii.DB
+ edb *ii.EDB
}
func main() {
@@ -62,6 +64,7 @@ func main() {
ii.OpenLog(ioutil.Discard, os.Stdout, os.Stderr)
db := open_db(*db_opt)
+ edb := ii.LoadEcholist(*echo_opt)
flag.Parse()
if *verbose_opt {
@@ -69,8 +72,9 @@ func main() {
}
db.Name = *sysname_opt
-
- WebInit(&www, db)
+ www.db = db
+ www.edb = edb
+ WebInit(&www)
fs := http.FileServer(http.Dir("lib"))
http.Handle("/lib/", http.StripPrefix("/lib/", fs))
diff --git a/ii-node/tpl/header.tpl b/ii-node/tpl/header.tpl
@@ -14,7 +14,7 @@
<tr>
<td class="title">
<span class="logo"><a href="/">ii-go</a></span>
- <span class="info">II/IDEC networks {{ with .Echo }} :: <a href="/{{.}}">{{.}}</a>{{end}}
+ <span class="info">II/IDEC networks {{ with .Echo }} :: <a href="/{{.}}">{{.}}</a> <span class="info">{{index $.Echolist.Info .}}</span>{{end}}
</span>
</td>
<td class="links">
diff --git a/ii-node/tpl/index.tpl b/ii-node/tpl/index.tpl
@@ -13,7 +13,9 @@
{{ else }}
<tr class="even">
{{ end }}
-<td class="echo"><a href="{{.Name}}">{{.Name}}</a></td>
+<td class="echo"><a href="{{.Name}}">{{.Name}}</a><br>
+<span class="info">{{ index $.Echolist.Info .Name }}</span>
+</td>
<td class="topics extra">{{.Topics}}</td>
<td class="count extra">{{.Count}}</td>
<td class="info">{{with .Msg}}<span class="subj">{{.Subj}}</span><br><a href="/{{.MsgId}}#{{.MsgId}}">{{.Date | fdate}}</a> by {{.From}}{{end}}</td>
diff --git a/ii-node/web.go b/ii-node/web.go
@@ -25,11 +25,12 @@ type WebContext struct {
Pager []int
BasePath string
User *ii.User
+ Echolist *ii.EDB
Selected string
}
func www_register(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request) error {
- ctx := WebContext{ User: user }
+ ctx := WebContext{ User: user, Echolist: www.edb }
ii.Trace.Printf("www register")
switch r.Method {
case "GET":
@@ -59,7 +60,7 @@ func www_register(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request
}
func www_login(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request) error {
- ctx := WebContext{ User: user, BasePath: "login" }
+ ctx := WebContext{ User: user, BasePath: "login", Echolist: www.edb }
ii.Trace.Printf("www login")
switch r.Method {
case "GET":
@@ -88,7 +89,7 @@ func www_login(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request) e
}
func www_profile(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request) error {
- ctx := WebContext{ User: user, BasePath: "profile" }
+ ctx := WebContext{ User: user, BasePath: "profile", Echolist: www.edb }
ii.Trace.Printf("www profile")
if user.Name == "" {
ii.Error.Printf("Access denied")
@@ -111,7 +112,7 @@ func www_logout(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request)
}
func www_index(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request) error {
- ctx := WebContext{ User: user }
+ ctx := WebContext{ User: user, Echolist: www.edb }
ii.Trace.Printf("www index")
@@ -201,7 +202,7 @@ func makePager(ctx *WebContext, count int, page int) int {
func www_topics(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, echo string, page int) error {
db := www.db
- ctx := WebContext{ User: user, Echo: echo }
+ ctx := WebContext{ User: user, Echo: echo, Echolist: www.edb }
mis := db.LookupIDS(db.SelectIDS(ii.Query{Echo: echo}))
ii.Trace.Printf("www topics: %s", echo)
topicsIds := getTopics(db, mis)
@@ -248,7 +249,7 @@ func www_topics(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request,
func www_topic(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, id string, page int) error {
db := www.db
- ctx := WebContext{ User: user }
+ ctx := WebContext{ User: user, Echolist: www.edb }
mi := db.Lookup(id)
if mi == nil {
@@ -298,7 +299,7 @@ func www_topic(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, i
}
func www_new(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, echo string) error {
- ctx := WebContext{ User: user }
+ ctx := WebContext{ User: user, Echolist: www.edb }
ctx.BasePath = echo
ctx.Echo = echo
@@ -338,7 +339,7 @@ func www_new(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, ech
}
func www_reply(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, id string) error {
- ctx := WebContext{ User: user }
+ ctx := WebContext{ User: user, Echolist: www.edb }
ctx.BasePath = id
switch r.Method {
@@ -439,7 +440,7 @@ func msg_format(txt string) template.HTML {
return template.HTML(f)
}
-func WebInit(www *WWW, db *ii.DB) {
+func WebInit(www *WWW) {
funcMap := template.FuncMap{
"fdate": func (date int64) string {
return time.Unix(date, 0).Format("2006-01-02 15:04:05")
@@ -450,12 +451,11 @@ func WebInit(www *WWW, db *ii.DB) {
return r
},
}
- www.db = db
www.tpl = template.Must(template.New("main").Funcs(funcMap).ParseGlob("tpl/*.tpl"))
}
func handleErr(user *ii.User, www WWW, w http.ResponseWriter, err error) {
- ctx := WebContext{ Error: err.Error(), User: user }
+ ctx := WebContext{ Error: err.Error(), User: user, Echolist: www.edb }
www.tpl.ExecuteTemplate(w, "error.tpl", ctx)
}
diff --git a/ii/db.go b/ii/db.go
@@ -730,3 +730,31 @@ func LoadUsers(path string) *UDB {
}
return &db
}
+
+type EDB struct {
+ Info map[string]string
+ List []string
+ Path string
+ Sync sync.Mutex
+}
+
+func LoadEcholist(path string) *EDB {
+ var db EDB
+ db.Path = path
+ db.Info = make(map[string]string)
+ err := file_lines(path, func(line string) bool {
+ a := strings.SplitN(line, ":", 3)
+ if len(a) < 2 {
+ Error.Printf("Wrong entry in echo DB: %s", line)
+ return true
+ }
+ db.Info[a[0]] = a[2]
+ db.List = append(db.List, a[0])
+ return true
+ })
+ if err != nil {
+ Error.Printf("Can not read echo DB: %s", err)
+ return nil
+ }
+ return &db
+}