openidec

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

commit 748ab40552cc79f9bd1f7bdd4da79900a1664f54
parent bae6252cdf8da39d1252cbf5d96198ad0c917e41
Author: Peter Kosyh <p.kosyh@gmail.com>
Date:   Wed,  2 Sep 2020 19:32:05 +0300

blacklist.txt extension

Diffstat:
Mii-node/main.go | 8+++++++-
Mii-tool/main.go | 16++++++++++++++++
Mii/db.go | 61++++++++++++++++++++++++++++++++++++++++++++++++++++---------
Mii/net.go | 6+++---
4 files changed, 78 insertions(+), 13 deletions(-)

diff --git a/ii-node/main.go b/ii-node/main.go @@ -78,6 +78,12 @@ func main() { fmt.Fprintf(w, "%s:%d:\n", v.Name, v.Count) } }) + http.HandleFunc("/blacklist.txt", func(w http.ResponseWriter, r *http.Request) { + ids := db.SelectIDS(ii.Query { Blacklisted: true } ) + for _, v := range ids { + fmt.Fprintf(w, "%s\n", v) + } + }) http.HandleFunc("/u/point/", func(w http.ResponseWriter, r *http.Request) { var pauth, tmsg string switch r.Method { @@ -172,7 +178,7 @@ func main() { } }) http.HandleFunc("/x/features", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "list.txt\nu/e\nx/c\n") + fmt.Fprintf(w, "list.txt\nblacklist.txt\nu/e\nx/c\n") }) ii.Info.Printf("Listening on %s", *listen_opt) if err := http.ListenAndServe(*listen_opt, nil); err != nil { diff --git a/ii-tool/main.go b/ii-tool/main.go @@ -75,6 +75,7 @@ Commands: get <msgid> - show message from database select <echo> [[start]:lim] - get slice from echo index - recreate index + blacklist <msgid> - blacklist msg useradd <name> <e-mail> <password> - adduser Options: -db=<path> - database path @@ -84,6 +85,21 @@ Options: os.Exit(1) } switch cmd := args[0]; cmd { + case "blacklist": + if len(args) < 2 { + fmt.Printf("No msgid supplied\n") + os.Exit(1) + } + db := open_db(*db_opt) + m := db.Get(args[1]) + if m != nil { + if err := db.Blacklist(m); err != nil { + fmt.Printf("Can not blacklist: %s\n", err) + os.Exit(1) + } + } else { + fmt.Printf("No such msg") + } case "send": if len(args) < 4 { fmt.Printf("No argumnet(s) supplied\nShould be: <server> <pauth> and <file|->.\n") diff --git a/ii/db.go b/ii/db.go @@ -173,9 +173,11 @@ func (db *DB) LoadIndex() error { if os.IsNotExist(err) { file, err = db._ReopenIndex() if err != nil { + Error.Printf("Can not seek to end of index") return err } } else { + Error.Printf("Can not open index: %s", err) return err } } @@ -183,14 +185,16 @@ func (db *DB) LoadIndex() error { info, err := file.Stat() if err != nil { + Error.Printf("Can not stat index: %s", err) return err } fsize := info.Size() if db.Idx.Hash != nil { // already loaded if fsize > db.Idx.FileSize { - Trace.Printf("Refreshing index file...") + Trace.Printf("Refreshing index file...%d>%d", fsize, db.Idx.FileSize) if _, err := file.Seek(0, 2); err != nil { + Error.Printf("Can not seek index: %s", err) return err } Idx = db.Idx @@ -198,24 +202,28 @@ func (db *DB) LoadIndex() error { Info.Printf("Index file truncated, rebuild inndex...") file, err = db._ReopenIndex() if err != nil { + Error.Printf("Can not reopen index: %s", err) return err } defer file.Close() + } else { + return nil } - return nil } else { Idx.Hash = make(map[string]MsgInfo) } var err2 error + linenr := 0 err = f_lines(file, func(line string) bool { + linenr ++ info := strings.Split(line, ":") if len(info) < 3 { - err2 = errors.New("Wrong format") + err2 = errors.New("Wrong format on line:" + fmt.Sprintf("%d", linenr)) return false } mi := MsgInfo{Id: info[0], Echo: info[1]} if _, err := fmt.Sscanf(info[2], "%d", &mi.Off); err != nil { - err2 = errors.New("Wrong offset") + err2 = errors.New("Wrong offset on line: " + fmt.Sprintf("%d", linenr)) return false } if len(info) > 3 { @@ -228,9 +236,11 @@ func (db *DB) LoadIndex() error { return true }) if err != nil { + Error.Printf("Can not parse index: %s", err) return err } if err2 != nil { + Error.Printf("Can not parse index: %s", err2) return err2 } Idx.FileSize = fsize @@ -238,12 +248,12 @@ func (db *DB) LoadIndex() error { return nil } -func (db *DB) _Lookup(Id string) *MsgInfo { +func (db *DB) _Lookup(Id string, bl bool) *MsgInfo { if err := db.LoadIndex(); err != nil { return nil } info, ok := db.Idx.Hash[Id] - if !ok { + if !ok || (!bl && info.Off < 0) { return nil } return &info @@ -255,7 +265,16 @@ func (db *DB) Lookup(Id string) *MsgInfo { db.Lock() defer db.Unlock() - return db._Lookup(Id) + return db._Lookup(Id, false) +} + +func (db *DB) Exists(Id string) *MsgInfo { + db.Sync.RLock() + defer db.Sync.RUnlock() + db.Lock() + defer db.Unlock() + + return db._Lookup(Id, true) } func (db *DB) LookupIDS(Ids []string) []*MsgInfo { @@ -265,7 +284,7 @@ func (db *DB) LookupIDS(Ids []string) []*MsgInfo { db.Lock() defer db.Unlock() for _, id := range Ids { - i := db._Lookup(id) + i := db._Lookup(id, false) if i != nil { info = append(info, i) } @@ -279,7 +298,7 @@ func (db *DB) GetBundle(Id string) string { db.Lock() defer db.Unlock() - info := db._Lookup(Id) + info := db._Lookup(Id, false) if info == nil { Info.Printf("Can not find bundle: %s\n", Id) return "" @@ -324,6 +343,7 @@ type Query struct { Repto string Start int Lim int + Blacklisted bool } func prependStr(x []string, y string) []string { @@ -334,6 +354,9 @@ func prependStr(x []string, y string) []string { } func (db *DB) Match(info MsgInfo, r Query) bool { + if r.Blacklisted { + return info.Off < 0 + } if r.Echo != "" && r.Echo != info.Echo { return false } @@ -374,6 +397,9 @@ func (db *DB) Echoes(names []string) []Echo { for i := 0; i < size; i++ { id := db.Idx.List[i] info := db.Idx.Hash[id] + if info.Off < 0 { + continue + } e := info.Echo if names != nil { // filter? if _, ok := filter[e]; !ok { @@ -463,6 +489,23 @@ func (db *DB) Edit(m *Msg) error { return db._Store(m, true) } +func (db *DB) Blacklist(m *Msg) error { + db.Sync.Lock() + defer db.Sync.Unlock() + db.Lock() + defer db.Unlock() + + repto, _ := m.Tag("repto") + if repto != "" { + repto = ":" + repto + } + rec := fmt.Sprintf("%s:%s:%d%s", m.MsgId, m.Echo, -1, repto) + if err := append_file(db.IndexPath(), rec); err != nil { + return err + } + return nil +} + func (db *DB) _Store(m *Msg, edit bool) error { db.Sync.Lock() defer db.Sync.Unlock() diff --git a/ii/net.go b/ii/net.go @@ -64,7 +64,7 @@ func (n *Node) Fetcher(db *DB, Echo string, limit int, wait *sync.WaitGroup, con defer wait.Done() if n.IsFeature("u/e") { /* fast path */ id, err := http_get_id(n.Host + "/u/e/" + Echo + "/-1:1") - if err == nil && db.Lookup(id) != nil { /* no sync needed */ + if err == nil && db.Exists(id) != nil { /* no sync needed */ Info.Printf("%s: no sync needed", Echo) return } @@ -82,7 +82,7 @@ func (n *Node) Fetcher(db *DB, Echo string, limit int, wait *sync.WaitGroup, con limit = 0 break } - if db.Lookup(id) != nil { + if db.Exists(id) != nil { break } try++ @@ -102,7 +102,7 @@ func (n *Node) Fetcher(db *DB, Echo string, limit int, wait *sync.WaitGroup, con if strings.Contains(line, ".") { return true } - if db.Lookup(line) == nil { + if db.Exists(line) == nil { res = append(res, line) } return true