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

301 lines
5.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"
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
)
type ClientNameMap map[string]*Client
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
}
2012-04-18 07:11:35 +02:00
func NewServer(name string) *Server {
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),
}
go server.receiveCommands(commands)
NewNickServ(server)
return server
2012-04-07 20:44:59 +02:00
}
func (server *Server) receiveCommands(commands <-chan Command) {
for command := range commands {
2013-05-11 22:55:01 +02:00
log.Printf("%s ← %s %s", server, command.Client(), 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-09 20:05:10 +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) {
if !c.registered && c.HasNick() && c.HasUser() && (s.password == nil || c.serverPass) {
c.registered = true
replies := []Reply{RplWelcome(s, c), RplYourHost(s, c), RplCreated(s), RplMyInfo(s)}
for _, reply := range replies {
2013-05-09 20:05:10 +02:00
c.Replies() <- reply
}
}
}
2013-05-09 20:05:10 +02:00
func (s Server) Id() string {
2013-05-11 22:55:01 +02:00
return s.name
}
func (s Server) String() string {
return s.Id()
}
2013-05-09 20:05:10 +02:00
func (s Server) PublicId() string {
return s.Id()
}
2013-05-09 20:05:10 +02:00
func (s Server) Nick() string {
return s.name
}
func (s *Server) DeleteChannel(channel *Channel) {
delete(s.channels, channel.name)
}
//
// commands
//
2013-05-11 22:55:01 +02:00
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-09 20:05:10 +02:00
c.user.Replies() <- ErrNoPrivileges(s)
}
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 {
for name := range m.channels {
2013-05-09 20:05:10 +02:00
c.Replies() <- ErrNoSuchChannel(s, name)
}
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.Client().user
if user == nil {
for _, chname := range m.channels {
2013-05-09 20:05:10 +02:00
m.Client().Replies() <- ErrNoSuchChannel(s, chname)
}
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.Client().user
if user == nil {
2013-05-09 20:05:10 +02:00
m.Client().Replies() <- ErrNoSuchChannel(s, m.channel)
return
}
channel := s.channels[m.channel]
if channel == nil {
2013-05-09 20:05:10 +02:00
user.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.Client().user
if user == nil {
2013-05-09 20:05:10 +02:00
m.Client().Replies() <- ErrNoSuchNick(s, m.target)
return
}
if m.TargetIsChannel() {
channel := s.channels[m.target]
if channel == nil {
2013-05-09 20:05:10 +02:00
user.Replies() <- ErrNoSuchChannel(s, m.target)
return
}
2013-05-09 20:05:10 +02:00
channel.Commands() <- m
return
}
target := s.users[m.target]
if target == nil {
2013-05-09 20:05:10 +02:00
user.Replies() <- ErrNoSuchNick(s, m.target)
return
}
2013-05-09 20:05:10 +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
}