Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
introduce redis unban channel
Browse files Browse the repository at this point in the history
  • Loading branch information
sztanpet committed Dec 29, 2013
1 parent 031f673 commit ba33d3b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion TODO
Expand Up @@ -3,7 +3,7 @@ more general todo, shortterm:
✔ persist options that are worth persisting between restarts @done (13-09-11 00:26)
☐ direct messages
☐ privileged web interface for introspecting the chat and influencing behaviour (like throttle times)

https://code.google.com/p/gogoprotobuf/
short-medium term:
☐ IRC gateway
☐ polling through the chat
Expand Down
39 changes: 38 additions & 1 deletion bans.go
Expand Up @@ -4,7 +4,8 @@ import (
"database/sql"
"fmt"
"github.com/go-sql-driver/mysql"
"github.com/vmihailenco/redis"
redis "github.com/vmihailenco/redis/v2"
"strconv"
"sync"
"time"
)
Expand Down Expand Up @@ -34,6 +35,7 @@ func initBans(redisdb int64) {
func (b *Bans) run(redisdb int64) {
b.loadActive()
refreshban := b.setupRefresh(redisdb)
unban := b.setupUnban(redisdb)
t := time.NewTicker(time.Minute)
cp := watchdog.register("ban thread", time.Minute)
defer watchdog.unregister("ban thread")
Expand All @@ -51,6 +53,23 @@ func (b *Bans) run(redisdb int64) {
D("Refreshing bans")
b.loadActive()
}
case m := <-unban:
if m.Err != nil {
D("Error receiving from redis pub/sub channel unbanuserid")
unban = b.setupUnban(redisdb)
} else {
if len(m.Message) == 0 {
continue
}
userid, err := strconv.ParseUint(m.Message, 10, 32)
if err != nil {
D("Error parsing message as uint32:", m.Message, err)
continue
}
uid := Userid(userid)
b.unbanUserid(uid)
mutes.unmuteUserid(uid)
}
}
}
}
Expand All @@ -73,6 +92,24 @@ refreshagain:
return refreshban
}

func (b *Bans) setupUnban(redisdb int64) chan *redis.Message {
unbanagain:
c, err := rds.PubSubClient()
if err != nil {
B("Unable to create redis pubsub client: ", err)
time.Sleep(500 * time.Millisecond)
goto unbanagain
}
unban, err := c.Subscribe(fmt.Sprintf("unbanuserid-%d", redisdb))
if err != nil {
B("Unable to subscribe to the redis unbanuserid channel: ", err)
time.Sleep(500 * time.Millisecond)
goto unbanagain
}

return unban
}

func (b *Bans) clean() {
b.userlock.Lock()
defer b.userlock.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion cache.go
Expand Up @@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/vmihailenco/redis"
redis "github.com/vmihailenco/redis/v2"
"time"
)

Expand Down
13 changes: 7 additions & 6 deletions connection.go
Expand Up @@ -395,20 +395,20 @@ func (c *Connection) OnMsg(data []byte) {

// strip off /me for anti-spam purposes
var bmsg []byte
if len(msg) > 3 && msg[:3] == "/me" {
bmsg = []byte(strings.TrimSpace(msg[3:]))
if len(msg) > 4 && msg[:4] == "/me " {
bmsg = []byte(strings.TrimSpace(msg[4:]))
} else {
bmsg = []byte(msg)
}

hash := md5.New()
h := hash.Sum(bmsg)
if bytes.Equal(h, c.user.lastmessage) {
tsum := md5.Sum(bmsg)
sum := tsum[:]
if bytes.Equal(sum, c.user.lastmessage) {
c.user.delayscale++
c.SendError("duplicate")
return
}
c.user.lastmessage = h
c.user.lastmessage = sum

}

Expand Down Expand Up @@ -558,6 +558,7 @@ func (c *Connection) OnUnban(data []byte) {
}

bans.unbanUserid(uid)
mutes.unmuteUserid(uid)
out := c.getEventDataOut()
out.Data = user.Data
out.Targetuserid = uid
Expand Down
2 changes: 1 addition & 1 deletion user.go
Expand Up @@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/vmihailenco/redis"
redis "github.com/vmihailenco/redis/v2"
"io/ioutil"
"net/http"
"net/url"
Expand Down

0 comments on commit ba33d3b

Please sign in to comment.