Merge pull request 'lint' (#2) from ptr_rzmv/openidec:lint into master

Reviewed-on: #2
master
vasyahacker 2023-04-04 18:30:41 +03:00
commit bfdeead859
9 changed files with 82 additions and 34 deletions

View File

@ -107,7 +107,10 @@ Options:
db := open_db(*db_opt)
db.Lock()
defer db.Unlock()
db.LoadIndex()
if err := db.LoadIndex(); err != nil {
fmt.Printf("Can not load index: %s\n", err)
os.Exit(1)
}
for _, v := range db.Idx.List {
if echo != "" {
mi := db.Idx.Hash[v]
@ -196,6 +199,10 @@ Options:
}
return true
})
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
fmt.Printf("%d lines... %d dups...\n", nr, dup)
if dup == 0 {
os.Exit(0)
@ -241,19 +248,19 @@ Options:
}
hash[id] += 1
if hash[id] > 0 {
fmt.Printf("Error. DB has changed. Aborted.\n")
fmt.Println("Error. DB has changed. Aborted.")
os.Exit(1)
}
return true
})
f.Close()
if err != nil {
fmt.Printf("Error: %s\n")
fmt.Println("Error:", err)
os.Exit(1)
}
for _, v := range hash {
if v != 0 {
fmt.Printf("Error. DB shrinked. Aborted.\n")
fmt.Println("Error. DB shrinked. Aborted.")
os.Exit(1)
}
}
@ -261,7 +268,7 @@ Options:
case "fetch":
var echolist []string
if len(args) < 2 {
fmt.Printf("No url supplied\n")
fmt.Println("No url supplied")
os.Exit(1)
}
db := open_db(*db_opt)
@ -392,7 +399,10 @@ Options:
}
case "sort":
db := open_db(*db_opt)
db.LoadIndex()
if err := db.LoadIndex(); err != nil {
fmt.Printf("Can not load index: %s\n", err)
os.Exit(1)
}
scanner := bufio.NewScanner(os.Stdin)
var mm []*ii.Msg
for scanner.Scan() {

View File

@ -24,7 +24,11 @@ func open_db(path string) *ii.DB {
}
func PointMsg(edb *ii.EDB, db *ii.DB, udb *ii.UDB, pauth string, tmsg string) string {
udb.LoadUsers()
err := udb.LoadUsers()
if err != nil {
ii.Error.Printf("Error loading users: %s", err)
return err.Error()
}
if !udb.Access(pauth) {
ii.Info.Printf("Access denied for pauth: %s", pauth)
@ -33,24 +37,24 @@ func PointMsg(edb *ii.EDB, db *ii.DB, udb *ii.UDB, pauth string, tmsg string) st
m, err := ii.DecodeMsgline(tmsg, true)
if err != nil {
ii.Error.Printf("Receive point msg: %s", err)
return fmt.Sprintf("%s", err)
return err.Error()
}
if r, _ := m.Tag("repto"); r != "" {
if db.Lookup(r) == nil {
ii.Error.Printf("Receive point msg with wrong repto.")
return fmt.Sprintf("Receive point msg with wrong repto.")
return "Receive point msg with wrong repto."
}
}
if !edb.Allowed(m.Echo) {
ii.Error.Printf("This echo is disallowed")
return fmt.Sprintf("This echo is disallowed")
return "This echo is disallowed"
}
m.From = udb.Name(pauth)
m.Addr = fmt.Sprintf("%s,%d", db.Name, udb.Id(pauth))
if err := db.Store(m); err != nil {
ii.Error.Printf("Store point msg: %s", err)
return fmt.Sprintf("%s", err)
return err.Error()
}
return "msg ok"
}
@ -165,7 +169,11 @@ func main() {
var pauth, tmsg string
switch r.Method {
case "GET":
udb.LoadUsers()
err := udb.LoadUsers()
if err != nil {
ii.Error.Printf("Error loaging users: %s", err)
return
}
args := strings.Split(r.URL.Path[9:], "/")
if len(args) >= 3 && args[1] == "u" {
@ -206,7 +214,7 @@ func main() {
return
}
ii.Info.Printf("/u/point/%s/%s GET request", pauth, tmsg)
fmt.Fprintf(w, PointMsg(edb, db, udb, pauth, tmsg))
fmt.Fprint(w, PointMsg(edb, db, udb, pauth, tmsg))
})
http.HandleFunc("/u/point", func(w http.ResponseWriter, r *http.Request) {
var pauth, tmsg string
@ -222,7 +230,7 @@ func main() {
return
}
ii.Info.Printf("/u/point/%s/%s POST request", pauth, tmsg)
fmt.Fprintf(w, PointMsg(edb, db, udb, pauth, tmsg))
fmt.Fprint(w, PointMsg(edb, db, udb, pauth, tmsg))
})
http.HandleFunc("/x/c/", func(w http.ResponseWriter, r *http.Request) {
enames := strings.Split(r.URL.Path[5:], "/")

View File

@ -268,7 +268,9 @@ func www_avatar(ctx *WebContext, w http.ResponseWriter, r *http.Request, user st
}
b64 := base64.URLEncoding.EncodeToString([]byte(ava))
ii.Trace.Printf("New avatar for %s: %s", ctx.User.Name, b64)
ctx.User.Tags.Add("avatar/" + b64)
if err := ctx.User.Tags.Add("avatar/" + b64); err != nil {
return err
}
}
if err := ctx.www.udb.Edit(ctx.User); err != nil {
ii.Error.Printf("Error saving avatar: " + user)
@ -444,7 +446,9 @@ func www_topics(ctx *WebContext, w http.ResponseWriter, r *http.Request, page in
db.Sync.RLock()
defer db.Sync.RUnlock()
db.LoadIndex()
if err := db.LoadIndex(); err != nil {
return err
}
for _, t := range topicsIds {
topic := Topic{}
topic.Ids = t
@ -637,7 +641,9 @@ func www_new(ctx *WebContext, w http.ResponseWriter, r *http.Request) error {
m.From = ctx.User.Name
m.Addr = fmt.Sprintf("%s,%d", ctx.www.db.Name, ctx.User.Id)
if repto != "" {
m.Tags.Add("repto/" + repto)
if err := m.Tags.Add("repto/" + repto); err != nil {
return err
}
}
if id != "" {
om := ctx.www.db.Get(id)
@ -684,7 +690,9 @@ func www_reply(ctx *WebContext, w http.ResponseWriter, r *http.Request, quote bo
msg := *m
msg.To = msg.From
msg.Subj = "Re: " + strings.TrimPrefix(msg.Subj, "Re: ")
msg.Tags.Add("repto/" + id)
if err := msg.Tags.Add("repto/" + id); err != nil {
return err
}
if quote {
msg.Text = msg_quote(msg.Text, msg.From)
} else {
@ -861,7 +869,6 @@ func msg_trunc(m *ii.Msg, maxlen int, more string) string {
}
}
if pre {
pre = false
f += "</pre>\n"
}
return f
@ -920,7 +927,10 @@ func WebInit(www *WWW) {
func handleErr(ctx *WebContext, w http.ResponseWriter, err error) {
ctx.Error = err.Error()
ctx.Template = "error.tpl"
ctx.www.tpl.ExecuteTemplate(w, "error.tpl", ctx)
if err := ctx.www.tpl.ExecuteTemplate(w, "error.tpl", ctx); err != nil {
// FIXME: correctly handle this error
panic(err)
}
}
func handleWWW(www *WWW, w http.ResponseWriter, r *http.Request) {
@ -930,8 +940,11 @@ func handleWWW(www *WWW, w http.ResponseWriter, r *http.Request) {
ctx.www = www
ctx.Sysname = www.db.Name
ctx.Host = www.Host
www.udb.LoadUsers()
err := _handleWWW(&ctx, w, r)
err := www.udb.LoadUsers()
if err != nil {
handleErr(&ctx, w, err)
}
err = _handleWWW(&ctx, w, r)
if err != nil {
handleErr(&ctx, w, err)
}

View File

@ -135,7 +135,9 @@ Options:
db := open_db(*db_opt)
db.Lock()
defer db.Unlock()
db.LoadIndex()
if err := db.LoadIndex(); err != nil {
return
}
scanner := bufio.NewScanner(os.Stdin)
var mis []*ii.Msg

View File

@ -64,5 +64,7 @@ func main() {
log.Printf("listen-addr=%s upstream-url=%s", srv.Addr, u.String())
srv.ListenAndServeTLS("", "")
if err := srv.ListenAndServeTLS("", ""); err != nil {
log.Fatal(err)
}
}

View File

@ -112,7 +112,7 @@ func (db *DB) IndexPath() string {
// Return path to database itself
func (db *DB) BundlePath() string {
return fmt.Sprintf("%s", db.Path)
return db.Path
}
// Returns path to lock.
@ -196,8 +196,13 @@ func (db *DB) _CreateIndex() error {
if v, _ := msg.Tag("access"); v == "blacklist" {
ioff = -1
}
fidx.WriteString(fmt.Sprintf("%s:%s:%d:%s:%s:%s\n",
_, err := fidx.WriteString(fmt.Sprintf("%s:%s:%d:%s:%s:%s\n",
msg.MsgId, msg.Echo, ioff, msg.To, msg.From, repto))
if err != nil {
// FIXME: handle this error
panic(err)
}
off += int64(len(line) + 1)
return true
})
@ -699,7 +704,10 @@ func (db *DB) GetTopics(mi []*MsgInfo) map[string][]string {
intopic := make(map[string]string)
topics := make(map[string][]string)
db.LoadIndex()
if err := db.LoadIndex(); err != nil {
// FIXME: handle this error
panic(err)
}
for _, m := range mi {
if _, ok := intopic[m.Id]; ok {
continue
@ -778,7 +786,9 @@ func (db *DB) Edit(m *Msg) error {
// to store it in DB. While loading index, blacklisted messages
// are marked by negative Off field (-1).
func (db *DB) Blacklist(m *Msg) error {
m.Tags.Add("access/blacklist")
if err := m.Tags.Add("access/blacklist"); err != nil {
return err
}
return db.Edit(m)
//db.Sync.Lock()

View File

@ -129,7 +129,9 @@ func DecodeMsgline(msg string, enc bool) (*Msg, error) {
if strings.HasPrefix(repto, "@repto:") {
start += 1
repto = strings.Trim(strings.Split(repto, ":")[1], " ")
m.Tags.Add("repto/" + repto)
if err := m.Tags.Add("repto/" + repto); err != nil {
return nil, err
}
Trace.Printf("Add repto tag: %s", repto)
}
for i := start; i < len(text); i++ {

View File

@ -18,10 +18,9 @@ var Test_msg string = "a5OX4lC8uB8OIzzzGQ5B:" +
"LouINCd0L4sINC60L7QvdC10YfQvdC+LCDQsdCw0LPQuCDQvNC+0LPRg9GCINCx0YvRgtGMLg=="
func TestParse(t *testing.T) {
var m *Msg
m, _ = DecodeBundle(Test_msg)
if m == nil {
t.Error("Can not decode msg")
m, err := DecodeBundle(Test_msg)
if err != nil {
t.Errorf("Can not decode msg: %s", err)
}
text := m.MsgId + ":" + m.Encode()
decoded, _ := base64.StdEncoding.DecodeString(text)

View File

@ -143,7 +143,9 @@ func (n *Node) Fetcher(db *DB, Echo string, limit int, wait *sync.WaitGroup, con
}); err != nil {
return
}
n.Store(db, res)
// FIXME: handle this error
_ = n.Store(db, res)
}
// Do not run more then MaxConnections goroutines in the same time