3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-25 21:39:25 +01:00

properly persist channels

This commit is contained in:
Jeremy Latt 2014-02-25 15:57:35 -08:00
parent a30bb540e9
commit b30e247e77
4 changed files with 33 additions and 20 deletions

View File

@ -33,10 +33,10 @@ func initDB(config *irc.Config) {
_, err = db.Exec(` _, err = db.Exec(`
CREATE TABLE channel ( CREATE TABLE channel (
name TEXT NOT NULL UNIQUE, name TEXT NOT NULL UNIQUE,
flags TEXT, flags TEXT NOT NULL,
key TEXT, key TEXT NOT NULL,
topic TEXT, topic TEXT NOT NULL,
user_limit INTEGER)`) user_limit INTEGER DEFAULT 0)`)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -35,7 +35,6 @@ func NewChannel(s *Server, name string) *Channel {
} }
s.channels[name] = channel s.channels[name] = channel
s.db.Exec(`INSERT INTO channel (name) VALUES (?)`, channel.name)
return channel return channel
} }
@ -203,15 +202,13 @@ func (channel *Channel) SetTopic(client *Client, topic string) {
} }
channel.topic = topic channel.topic = topic
channel.server.db.Exec(`
UPDATE channel
SET topic = ?
WHERE name = ?`, channel.topic, channel.name)
reply := RplTopicMsg(client, channel) reply := RplTopicMsg(client, channel)
for member := range channel.members { for member := range channel.members {
member.Reply(reply) member.Reply(reply)
} }
channel.Persist()
} }
func (channel *Channel) CanSpeak(client *Client) bool { func (channel *Channel) CanSpeak(client *Client) bool {
@ -371,10 +368,19 @@ func (channel *Channel) Mode(client *Client, changes ChannelModeChanges) {
member.Reply(reply) member.Reply(reply)
} }
channel.Persist()
}
}
func (channel *Channel) Persist() {
if channel.flags[Persistent] {
channel.server.db.Exec(` channel.server.db.Exec(`
UPDATE channel INSERT OR REPLACE INTO channel
SET flags = ? (name, flags, key, topic)
WHERE name = ?`, channel.flags.String(), channel.name) VALUES (?, ?, ?, ?)`,
channel.name, channel.flags.String(), channel.key, channel.topic)
} else {
channel.server.db.Exec(`DELETE FROM channel WHERE name = ?`, channel.name)
} }
} }

View File

@ -59,7 +59,17 @@ func NewServer(config *Config) *Server {
signal.Notify(server.signals, os.Interrupt, os.Kill) signal.Notify(server.signals, os.Interrupt, os.Kill)
rows, err := db.Query(` server.loadChannels()
for _, listenerConf := range config.Listeners {
go server.listen(listenerConf)
}
return server
}
func (server *Server) loadChannels() {
rows, err := server.db.Query(`
SELECT name, flags, key, topic, user_limit SELECT name, flags, key, topic, user_limit
FROM channel`) FROM channel`)
if err != nil { if err != nil {
@ -75,19 +85,13 @@ func NewServer(config *Config) *Server {
} }
channel := NewChannel(server, name) channel := NewChannel(server, name)
for flag := range flags { for _, flag := range flags {
channel.flags[ChannelMode(flag)] = true channel.flags[ChannelMode(flag)] = true
} }
channel.key = key channel.key = key
channel.topic = topic channel.topic = topic
channel.userLimit = userLimit channel.userLimit = userLimit
} }
for _, listenerConf := range config.Listeners {
go server.listen(listenerConf)
}
return server
} }
func (server *Server) processCommand(cmd Command) { func (server *Server) processCommand(cmd Command) {

View File

@ -107,6 +107,9 @@ func (clients ClientNameMap) Remove(client *Client) error {
type ChannelModeSet map[ChannelMode]bool type ChannelModeSet map[ChannelMode]bool
func (set ChannelModeSet) String() string { func (set ChannelModeSet) String() string {
if len(set) == 0 {
return ""
}
strs := make([]string, len(set)) strs := make([]string, len(set))
index := 0 index := 0
for mode := range set { for mode := range set {