3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00
ergo/src/irc/server.go

358 lines
6.6 KiB
Go
Raw Normal View History

2012-04-07 20:44:59 +02:00
package irc
import (
"code.google.com/p/go.crypto/bcrypt"
"database/sql"
2012-04-07 20:44:59 +02:00
"log"
"net"
2012-04-18 07:21:41 +02:00
"time"
2012-04-07 20:44:59 +02:00
)
const (
DEBUG_SERVER = true
)
type ChannelNameMap map[string]*Channel
type UserNameMap map[string]*User
2013-05-11 22:55:01 +02:00
type ServiceNameMap map[string]Service
2012-04-07 20:44:59 +02:00
type Server struct {
2012-12-13 08:27:17 +01:00
hostname string
ctime time.Time
name string
password []byte
users UserNameMap
channels ChannelNameMap
services ServiceNameMap
commands chan<- Command
db *sql.DB
}
2012-04-18 07:11:35 +02:00
func NewServer(name string) *Server {
2013-06-03 01:53:06 +02:00
commands := make(chan Command)
2012-12-09 21:51:50 +01:00
server := &Server{
ctime: time.Now(),
name: name,
commands: commands,
users: make(UserNameMap),
channels: make(ChannelNameMap),
services: make(ServiceNameMap),
2013-05-26 22:28:22 +02:00
db: NewDatabase(),
}
go server.receiveCommands(commands)
NewNickServ(server)
2013-06-03 07:07:50 +02:00
Load(server.db, server)
return server
}
func (server *Server) Load(q Queryable) bool {
crs, err := FindAllChannels(q)
if err != nil {
log.Println(err)
return false
}
for _, cr := range crs {
channel := server.GetOrMakeChannel(cr.name)
channel.id = &(cr.id)
}
urs, err := FindAllUsers(q)
if err != nil {
log.Println(err)
return false
}
for _, ur := range urs {
user := NewUser(ur.nick, server)
user.SetHash(ur.hash)
if !user.Load(q) {
2013-05-26 22:28:22 +02:00
return false
}
2013-06-03 07:07:50 +02:00
}
return true
2012-04-07 20:44:59 +02:00
}
func (server *Server) receiveCommands(commands <-chan Command) {
for command := range commands {
if DEBUG_SERVER {
2013-05-26 22:28:22 +02:00
log.Printf("%s → %s : %s", command.Client(), server, command)
}
command.Client().atime = time.Now()
command.HandleServer(server)
}
}
2012-04-07 20:44:59 +02:00
func (s *Server) Listen(addr string) {
listener, err := net.Listen("tcp", addr)
if err != nil {
2012-04-08 08:32:08 +02:00
log.Fatal("Server.Listen: ", err)
2012-04-07 20:44:59 +02:00
}
s.hostname = LookupHostname(listener.Addr())
log.Print("Server.Listen: listening on ", addr)
2012-04-07 20:44:59 +02:00
for {
conn, err := listener.Accept()
if err != nil {
2012-04-08 08:32:08 +02:00
log.Print("Server.Listen: ", err)
2012-04-07 20:44:59 +02:00
continue
}
log.Print("Server.Listen: accepted ", conn.RemoteAddr())
NewClient(s, conn)
}
}
func (s *Server) GetOrMakeChannel(name string) *Channel {
channel := s.channels[name]
if channel == nil {
channel = NewChannel(s, name)
s.channels[name] = channel
}
return channel
}
// Send a message to clients of channels fromClient is a member.
2013-05-12 20:20:55 +02:00
func (s *Server) InterestedUsers(fromUser *User) UserSet {
users := make(UserSet)
users.Add(fromUser)
for channel := range fromUser.channels {
for user := range channel.members {
users.Add(user)
}
}
return users
2012-04-07 20:44:59 +02:00
}
// server functionality
func (s *Server) tryRegister(c *Client) {
2013-05-12 20:20:55 +02:00
if !c.registered && c.HasNick() && c.HasUsername() && s.CheckPassword(c) {
c.registered = true
2013-05-12 20:20:55 +02:00
replies := []Reply{
RplWelcome(s, c),
2013-06-03 07:07:50 +02:00
RplYourHost(s),
2013-05-12 20:20:55 +02:00
RplCreated(s),
RplMyInfo(s),
}
for _, reply := range replies {
2013-05-09 20:05:10 +02:00
c.Replies() <- reply
}
}
}
2013-05-12 20:20:55 +02:00
func (s *Server) CheckPassword(c *Client) bool {
return (s.password == nil) || c.serverPass
}
func (s *Server) Id() string {
2013-05-11 22:55:01 +02:00
return s.name
}
func (s *Server) String() string {
2013-05-11 22:55:01 +02:00
return s.Id()
}
func (s *Server) PublicId() string {
return s.Id()
}
func (s *Server) Nick() string {
return s.name
}
func (s *Server) DeleteChannel(channel *Channel) {
delete(s.channels, channel.name)
if err := DeleteChannel(s.db, channel); err != nil {
log.Println(err)
}
}
//
// commands
//
func (m *UnknownCommand) HandleServer(s *Server) {
2013-05-09 20:05:10 +02:00
m.Client().Replies() <- ErrUnknownCommand(s, m.command)
}
func (m *PingCommand) HandleServer(s *Server) {
2013-05-09 20:05:10 +02:00
m.Client().Replies() <- RplPong(s)
}
func (m *PongCommand) HandleServer(s *Server) {
// no-op
}
func (m *PassCommand) HandleServer(s *Server) {
err := bcrypt.CompareHashAndPassword(s.password, []byte(m.password))
if err != nil {
2013-05-09 20:05:10 +02:00
m.Client().Replies() <- ErrPasswdMismatch(s)
return
2012-04-09 16:57:55 +02:00
}
m.Client().serverPass = true
// no reply?
}
func (m *NickCommand) HandleServer(s *Server) {
c := m.Client()
if c.user == nil {
2013-05-09 20:05:10 +02:00
c.Replies() <- RplNick(c, m.nickname)
c.nick = m.nickname
s.tryRegister(c)
return
}
2012-12-09 21:51:50 +01:00
2013-05-12 20:20:55 +02:00
user := c.user
if s.users[m.nickname] != nil {
user.Replies() <- ErrNickNameInUse(s, m.nickname)
return
}
delete(s.users, user.nick)
s.users[m.nickname] = user
reply := RplNick(user, m.nickname)
for iuser := range s.InterestedUsers(user) {
iuser.Replies() <- reply
}
user.nick = m.nickname
}
func (m *UserMsgCommand) HandleServer(s *Server) {
c := m.Client()
if c.username != "" {
2013-05-09 20:05:10 +02:00
c.Replies() <- ErrAlreadyRegistered(s)
return
}
c.username, c.realname = m.user, m.realname
2012-12-09 21:51:50 +01:00
s.tryRegister(c)
}
func (m *QuitCommand) HandleServer(s *Server) {
c := m.Client()
user := c.user
if user != nil {
reply := RplQuit(c, m.message)
for user := range s.InterestedUsers(c.user) {
2013-05-09 20:05:10 +02:00
user.Replies() <- reply
}
}
c.conn.Close()
if user == nil {
return
}
user.LogoutClient(c)
if !user.HasClients() {
cmd := &PartCommand{
BaseCommand: BaseCommand{c},
}
for channel := range user.channels {
2013-05-09 20:05:10 +02:00
channel.Commands() <- cmd
}
}
}
func (m *JoinCommand) HandleServer(s *Server) {
c := m.Client()
if c.user == nil {
c.Replies() <- ErrNoPrivileges(s)
return
}
if m.zero {
cmd := &PartCommand{
BaseCommand: BaseCommand{c},
}
for channel := range c.user.channels {
2013-05-09 20:05:10 +02:00
channel.Commands() <- cmd
}
return
}
for name := range m.channels {
2013-05-09 20:05:10 +02:00
s.GetOrMakeChannel(name).Commands() <- m
}
}
func (m *PartCommand) HandleServer(s *Server) {
user := m.User()
if user == nil {
m.Client().Replies() <- ErrNoPrivileges(s)
return
}
for _, chname := range m.channels {
channel := s.channels[chname]
if channel == nil {
2013-05-09 20:05:10 +02:00
user.Replies() <- ErrNoSuchChannel(s, channel.name)
continue
}
2013-05-09 20:05:10 +02:00
channel.Commands() <- m
}
}
func (m *TopicCommand) HandleServer(s *Server) {
user := m.User()
// Hide all channels from logged-out clients.
if user == nil {
m.Client().Replies() <- ErrNoPrivileges(s)
return
}
channel := s.channels[m.channel]
if channel == nil {
2013-05-26 22:28:22 +02:00
m.Client().Replies() <- ErrNoSuchChannel(s, m.channel)
return
}
2013-05-09 20:05:10 +02:00
channel.Commands() <- m
}
func (m *PrivMsgCommand) HandleServer(s *Server) {
service := s.services[m.target]
if service != nil {
2013-05-09 20:05:10 +02:00
service.Commands() <- m
return
}
user := m.User()
// Hide all users from logged-out clients.
if user == nil {
m.Client().Replies() <- ErrNoPrivileges(s)
return
}
if m.TargetIsChannel() {
channel := s.channels[m.target]
if channel == nil {
2013-06-03 07:07:50 +02:00
m.Client().Replies() <- ErrNoSuchChannel(s, m.target)
return
}
2013-06-03 07:07:50 +02:00
channel.commands <- m
return
}
target := s.users[m.target]
if target == nil {
2013-06-03 07:07:50 +02:00
m.Client().Replies() <- ErrNoSuchNick(s, m.target)
return
}
2013-06-03 07:07:50 +02:00
target.commands <- m
2012-04-07 20:44:59 +02:00
}
2012-12-13 08:27:17 +01:00
func (m *ModeCommand) HandleServer(s *Server) {
2013-05-09 20:05:10 +02:00
m.Client().Replies() <- RplUModeIs(s, m.Client())
2012-12-13 08:27:17 +01:00
}