openidec

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

commit b6fad07801efd90823f842dffa9778d9ff3543cb
parent c47c8e7db2815aee98d4b038b451644751e7a19d
Author: Peter Kosyh <p.kosyh@gmail.com>
Date:   Thu, 11 Mar 2021 07:33:15 +0000

Merge branch 'master' of https://github.com/gl00my/ii-go

Diffstat:
Mii-node/main.go | 30++++++++++++++++++++++++------
Mii/db.go | 24+++++++++++++++++-------
2 files changed, 41 insertions(+), 13 deletions(-)

diff --git a/ii-node/main.go b/ii-node/main.go @@ -138,16 +138,34 @@ func main() { case "GET": udb.LoadUsers() args := strings.Split(r.URL.Path[9:], "/") - if len(args) >= 3 && args[1] == "u" && args[2] == "e" { + + if len(args) >= 3 && args[1] == "u" { pauth = args[0] if !udb.Access(pauth) { ii.Info.Printf("Access denied for pauth: %s", pauth) return } - echoes := args[3:] - if user := udb.UserInfo(pauth); user != nil { + user := udb.UserInfo(pauth) + if user == nil { + return + } + if args[2] == "e" { + echoes := args[3:] get_ue(echoes, db, *user, w, r) + return } + if args[2] == "m" { + ids := args[3:] + for _, i := range ids { + m, info := db.GetBundleInfo(i) + if m == "" || !db.Access(info, user) { + continue + } + fmt.Fprintf(w, "%s\n", m) + } + return + } + ii.Error.Printf("Wrong /u/point/ get request: %s", r.URL.Path[9:]) return } if len(args) != 2 { @@ -189,8 +207,8 @@ func main() { http.HandleFunc("/u/m/", func(w http.ResponseWriter, r *http.Request) { ids := strings.Split(r.URL.Path[5:], "/") for _, i := range ids { - m := db.GetBundle(i) - if m != "" { + m, info := db.GetBundleInfo(i) + if m != "" && !ii.IsPrivate(info.Echo) { fmt.Fprintf(w, "%s\n", m) } } @@ -206,7 +224,7 @@ func main() { } m := db.Get(id) ii.Info.Printf("/m/%s %s", id, m) - if m != nil { + if m != nil && !ii.IsPrivate(m.Echo) { fmt.Fprintf(w, "%s", m.String()) } }) diff --git a/ii/db.go b/ii/db.go @@ -387,22 +387,22 @@ func (db *DB) LookupIDS(Ids []string) []*MsgInfo { // If idx is true: load/create index. // Returns: msgid:base64 bundle. // Does not lock! -func (db *DB) _GetBundle(Id string, idx bool) string { +func (db *DB) _GetBundle(Id string, idx bool) (string, *MsgInfo) { info := db._Lookup(Id, false, idx) if info == nil { Info.Printf("Can not find bundle: %s\n", Id) - return "" + return "", nil } f, err := os.Open(db.BundlePath()) if err != nil { Error.Printf("Can not open DB: %s\n", err) - return "" + return "", nil } defer f.Close() _, err = f.Seek(info.Off, 0) if err != nil { Error.Printf("Can not seek DB: %s\n", err) - return "" + return "", nil } var bundle string err = f_lines(f, func(line string) bool { @@ -411,9 +411,9 @@ func (db *DB) _GetBundle(Id string, idx bool) string { }) if err != nil { Error.Printf("Can not get %s from DB: %s\n", Id, err) - return "" + return "", nil } - return bundle + return bundle, info } // Get bundle line by message id from db. @@ -425,6 +425,16 @@ func (db *DB) GetBundle(Id string) string { db.Lock() defer db.Unlock() + b, _ := db._GetBundle(Id, true) + return b +} + +func (db *DB) GetBundleInfo(Id string) (string, *MsgInfo) { + db.Sync.RLock() + defer db.Sync.RUnlock() + db.Lock() + defer db.Unlock() + return db._GetBundle(Id, true) } @@ -446,7 +456,7 @@ func (db *DB) Get(Id string) *Msg { // Get decoded message from db by message id. // Does NOT lock! Loads/create index if needed. func (db *DB) GetFast(Id string) *Msg { - bundle := db._GetBundle(Id, false) + bundle, _ := db._GetBundle(Id, false) if bundle == "" { return nil }