func (self RedisDB) CountPostsInGroup(newsgroup string, time_frame int64) (result int64, err error) { now := util.TimeNow() if time_frame > 0 { time_frame = now - time_frame } else if time_frame < 0 { time_frame = 0 } result, err = self.client.ZCount(GROUP_ARTICLE_POSTTIME_WKR_PREFIX+newsgroup, strconv.Itoa(int(time_frame)), strconv.Itoa(int(now))).Result() return }
func (self RedisDB) BanEncAddr(encaddr string) (err error) { _, err = self.client.HMSet(ENCRYPTED_IP_BAN_PREFIX+encaddr, "encaddr", encaddr, "made", strconv.Itoa(int(util.TimeNow()))).Result() return }
func (self RedisDB) BanAddr(addr string) (err error) { isnet, ipnet := util.IsSubnet(addr) if !isnet { _, err = self.client.HMSet(IP_BAN_PREFIX+addr, "addr", addr, "made", strconv.Itoa(int(util.TimeNow()))).Result() return } isBanned, err := self.CheckIPBanned(addr) if !isBanned && err == nil { //make sure this range isn't banned already min, max := util.IPNet2MinMax(ipnet) start := util.ZeroIPString(min) end := util.ZeroIPString(max) self.clearIPRange(start, end) //delete all banned ranges that are contained within this range _, err = self.client.ZAdd(IP_RANGE_BAN_KR, redis.Z{Score: 0.0, Member: end}).Result() if err != nil { return } _, err = self.client.HMSet(IP_RANGE_BAN_PREFIX+end, "start", start, "end", end, "made", strconv.Itoa(int(util.TimeNow()))).Result() } return }
// register a message with the database func (self RedisDB) RegisterArticle(message *model.Article) (err error) { pipe := self.client.Pipeline() defer pipe.Close() msgid := message.MessageID group := message.Newsgroup has, _ := self.HasNewsgroup(group) if !has { self.RegisterNewsgroup(group) } has, _ = self.HasArticle(msgid) if has { return } //insert IP addr := message.Addr ip := net.ParseIP(addr) if ip != nil { addr = util.ZeroIPString(ip) pipe.ZAddNX(IP_RANGE_BAN_KR, redis.Z{Score: 0.0, Member: addr}) } pipe.SAdd(IP_ARTICLE_KR_PREFIX+addr, msgid) now := util.TimeNow() // insert article metadata pipe.HMSet(ARTICLE_PREFIX+msgid, "msgid", msgid, "message_id_hash", util.HashMessageID(msgid), "message_newsgroup", group, "time_obtained", strconv.Itoa(int(now)), "message_ref_id", message.Reference) pipe.Set(HASH_MESSAGEID_PREFIX+util.HashMessageID(msgid), msgid, 0) // update newsgroup pipe.ZAddXX(GROUP_POSTTIME_WKR, redis.Z{Score: float64(now), Member: group}) pipe.ZAddNX(GROUP_ARTICLE_POSTTIME_WKR_PREFIX+group, redis.Z{Score: float64(now), Member: msgid}) // insert article post pipe.HMSet(ARTICLE_POST_PREFIX+msgid, "newsgroup", group, "message_id", msgid, "ref_id", message.Reference, "name", message.Name, "subject", message.Subject, "path", message.Path, "time_posted", strconv.Itoa(int(message.Posted)), "message", message.Text, "addr", addr) if group != "ctl" { // control messages aren't added to the global keyring pipe.ZAddNX(ARTICLE_WKR, redis.Z{Score: float64(now), Member: msgid}) } // set / update thread state if message.Reference == "" { // insert new thread for op pipe.ZAddNX(GROUP_THREAD_POSTTIME_WKR_PREFIX+group, redis.Z{Score: float64(message.Posted), Member: msgid}) pipe.ZAddNX(GROUP_THREAD_BUMPTIME_WKR_PREFIX+group, redis.Z{Score: float64(message.Posted), Member: msgid}) if group != "ctl" { pipe.ZAddNX(THREAD_BUMPTIME_WKR, redis.Z{Score: float64(message.Posted), Member: msgid}) } } else { ref := message.Reference if !util.IsSage(message.Subject) { // bump it nigguh pipe.ZAddXX(GROUP_THREAD_BUMPTIME_WKR_PREFIX+group, redis.Z{Score: float64(message.Posted), Member: ref}) pipe.ZAddXX(THREAD_BUMPTIME_WKR, redis.Z{Score: float64(message.Posted), Member: ref}) } // update last posted pipe.ZAddXX(GROUP_THREAD_POSTTIME_WKR_PREFIX+group, redis.Z{Score: float64(message.Posted), Member: ref}) pipe.ZAddNX(THREAD_POST_WKR+ref, redis.Z{Score: float64(message.Posted), Member: msgid}) } // register article header for k, val := range message.Header { k = strings.ToLower(k) for _, v := range val { k = strings.ToLower(k) header := "Name::" + k + "::Value::" + v pipe.SAdd(HEADER_KR_PREFIX+header, msgid) pipe.SAdd(MESSAGEID_HEADER_KR_PREFIX+msgid, header) } } // add nntp message numbers number, _ := self.client.ZIncrBy(ARTICLE_NUMBERS_PREFIX+"last", float64(1), group).Result() pipe.ZAddNX(ARTICLE_NUMBERS_PREFIX+"group::"+group, redis.Z{Score: number, Member: msgid}) // register all attachments atts := message.Attachments if atts != nil { for _, att := range atts { hash := att.Hash pipe.SAdd(ATTACHMENT_ARTICLE_KR_PREFIX+hash, msgid) pipe.SAdd(ARTICLE_ATTACHMENT_KR_PREFIX+msgid, hash) pipe.HSetNX(ATTACHMENT_PREFIX+hash, "message_id", msgid) pipe.HSetNX(ATTACHMENT_PREFIX+hash, "sha_hash", hash) pipe.HSetNX(ATTACHMENT_PREFIX+hash, "filename", att.Name) pipe.HSetNX(ATTACHMENT_PREFIX+hash, "filepath", att.Path) pipe.HSetNX(ATTACHMENT_PREFIX+hash, "mime", att.Mime) } } _, err = pipe.Exec() if err != nil { log.Println("failed to register nntp article", err) } return }
// register a new newsgroup func (self RedisDB) RegisterNewsgroup(group string) error { _, err := self.client.ZAddNX(GROUP_POSTTIME_WKR, redis.Z{Score: float64(util.TimeNow()), Member: group}).Result() return err }
func (self RedisDB) BanArticle(messageID, reason string) error { has, err := self.ArticleBanned(messageID) if has { log.Println(messageID, "already banned") return err } _, err = self.client.HMSet(BANNED_ARTICLE_PREFIX+messageID, "message_id", messageID, "time_banned", strconv.Itoa(int(util.TimeNow())), "ban_reason", reason).Result() return err }
func (self RedisDB) BanNewsgroup(group string) (err error) { _, err = self.client.HMSet(BANNED_GROUP_PREFIX+group, "newsgroup", group, "time_banned", strconv.Itoa(int(util.TimeNow()))).Result() return }