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(`
CREATE TABLE channel (
name TEXT NOT NULL UNIQUE,
flags TEXT,
key TEXT,
topic TEXT,
user_limit INTEGER)`)
flags TEXT NOT NULL,
key TEXT NOT NULL,
topic TEXT NOT NULL,
user_limit INTEGER DEFAULT 0)`)
if err != nil {
log.Fatal(err)
}

View File

@ -35,7 +35,6 @@ func NewChannel(s *Server, name string) *Channel {
}
s.channels[name] = channel
s.db.Exec(`INSERT INTO channel (name) VALUES (?)`, channel.name)
return channel
}
@ -203,15 +202,13 @@ func (channel *Channel) SetTopic(client *Client, topic string) {
}
channel.topic = topic
channel.server.db.Exec(`
UPDATE channel
SET topic = ?
WHERE name = ?`, channel.topic, channel.name)
reply := RplTopicMsg(client, channel)
for member := range channel.members {
member.Reply(reply)
}
channel.Persist()
}
func (channel *Channel) CanSpeak(client *Client) bool {
@ -371,10 +368,19 @@ func (channel *Channel) Mode(client *Client, changes ChannelModeChanges) {
member.Reply(reply)
}
channel.Persist()
}
}
func (channel *Channel) Persist() {
if channel.flags[Persistent] {
channel.server.db.Exec(`
UPDATE channel
SET flags = ?
WHERE name = ?`, channel.flags.String(), channel.name)
INSERT OR REPLACE INTO channel
(name, flags, key, topic)
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)
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
FROM channel`)
if err != nil {
@ -75,19 +85,13 @@ func NewServer(config *Config) *Server {
}
channel := NewChannel(server, name)
for flag := range flags {
for _, flag := range flags {
channel.flags[ChannelMode(flag)] = true
}
channel.key = key
channel.topic = topic
channel.userLimit = userLimit
}
for _, listenerConf := range config.Listeners {
go server.listen(listenerConf)
}
return server
}
func (server *Server) processCommand(cmd Command) {

View File

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