2012-04-07 20:44:59 +02:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2012-12-15 23:34:20 +01:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
type ClientNameMap map[string]*Client
|
|
|
|
type ChannelNameMap map[string]*Channel
|
|
|
|
type UserNameMap map[string]*User
|
|
|
|
|
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
|
2012-12-15 23:34:20 +01:00
|
|
|
commands chan<- Command
|
|
|
|
password []byte
|
|
|
|
users UserNameMap
|
|
|
|
channels ChannelNameMap
|
2012-04-18 03:16:57 +02:00
|
|
|
}
|
|
|
|
|
2012-04-18 07:11:35 +02:00
|
|
|
func NewServer(name string) *Server {
|
2012-12-15 23:34:20 +01:00
|
|
|
commands := make(chan Command)
|
2012-12-09 21:51:50 +01:00
|
|
|
server := &Server{
|
|
|
|
ctime: time.Now(),
|
|
|
|
name: name,
|
2012-12-15 23:34:20 +01:00
|
|
|
commands: commands,
|
|
|
|
users: make(UserNameMap),
|
|
|
|
channels: make(ChannelNameMap),
|
|
|
|
}
|
|
|
|
go server.receiveCommands(commands)
|
2012-04-18 03:16:57 +02:00
|
|
|
return server
|
2012-04-07 20:44:59 +02:00
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func (server *Server) receiveCommands(commands <-chan Command) {
|
|
|
|
for command := range commands {
|
|
|
|
command.Client().atime = time.Now()
|
|
|
|
command.Handle(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
|
|
|
}
|
|
|
|
|
2012-12-09 07:54:58 +01: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
|
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
log.Print("Server.Listen: accepted ", conn.RemoteAddr())
|
2012-12-15 23:34:20 +01:00
|
|
|
NewClient(s, conn)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2012-12-15 23:34:20 +01:00
|
|
|
func (s *Server) InterestedUsers(fromUser *User) UserSet {
|
|
|
|
users := make(UserSet)
|
|
|
|
users[fromUser] = true
|
|
|
|
for channel := range fromUser.channels {
|
|
|
|
for user := range channel.members {
|
|
|
|
users[user] = true
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
return users
|
2012-04-07 20:44:59 +02:00
|
|
|
}
|
|
|
|
|
2012-12-09 07:54:58 +01:00
|
|
|
// server functionality
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
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 {
|
|
|
|
c.replies <- reply
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ChangeUserMode(c *Client, modes []string) {
|
|
|
|
// Don't allow any mode changes.
|
|
|
|
c.replies <- RplUModeIs(s, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Id() string {
|
|
|
|
return s.hostname
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) PublicId() string {
|
|
|
|
return s.Id()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) DeleteChannel(channel *Channel) {
|
|
|
|
delete(s.channels, channel.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// commands
|
|
|
|
//
|
|
|
|
|
|
|
|
func (m *PingCommand) Handle(s *Server) {
|
|
|
|
m.Client().replies <- RplPong(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *PongCommand) Handle(s *Server) {
|
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *PassCommand) Handle(s *Server) {
|
|
|
|
err := bcrypt.CompareHashAndPassword(s.password, []byte(m.password))
|
|
|
|
if err != nil {
|
|
|
|
m.Client().replies <- ErrPasswdMismatch(s)
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
2012-04-09 16:57:55 +02:00
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
m.Client().serverPass = true
|
|
|
|
// no reply?
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *NickCommand) Handle(s *Server) {
|
|
|
|
c := m.Client()
|
|
|
|
if c.user == nil {
|
|
|
|
c.replies <- RplNick(c, m.nickname)
|
|
|
|
c.nick = m.nickname
|
|
|
|
s.tryRegister(c)
|
|
|
|
return
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
2012-12-09 21:51:50 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
c.user.replies <- ErrNoPrivileges(s)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func (m *UserCommand) Handle(s *Server) {
|
|
|
|
c := m.Client()
|
2012-12-09 07:54:58 +01:00
|
|
|
if c.username != "" {
|
2012-12-15 23:34:20 +01:00
|
|
|
c.replies <- ErrAlreadyRegistered(s)
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
c.username, c.realname = m.user, m.realname
|
2012-12-09 21:51:50 +01:00
|
|
|
s.tryRegister(c)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func (m *QuitCommand) Handle(s *Server) {
|
|
|
|
c := m.Client()
|
|
|
|
reply := RplQuit(c, m.message)
|
|
|
|
for user := range s.InterestedUsers(c.user) {
|
|
|
|
user.replies <- reply
|
|
|
|
}
|
|
|
|
c.conn.Close()
|
|
|
|
user := c.user
|
|
|
|
user.LogoutClient(c)
|
|
|
|
|
|
|
|
if !user.HasClients() {
|
|
|
|
cmd := &PartChannelCommand{
|
|
|
|
Command: m,
|
|
|
|
}
|
|
|
|
for channel := range c.user.channels {
|
|
|
|
channel.commands <- cmd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *JoinCommand) Handle(s *Server) {
|
|
|
|
c := m.Client()
|
|
|
|
if m.zero {
|
|
|
|
cmd := &PartChannelCommand{
|
|
|
|
Command: m,
|
|
|
|
}
|
|
|
|
for channel := range c.user.channels {
|
|
|
|
channel.commands <- cmd
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i, name := range m.channels {
|
|
|
|
key := ""
|
|
|
|
if len(m.keys) > i {
|
|
|
|
key = m.keys[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
s.GetOrMakeChannel(name).commands <- &JoinChannelCommand{m, key}
|
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func (m *PartCommand) Handle(s *Server) {
|
|
|
|
user := m.Client().user
|
|
|
|
for _, chname := range m.channels {
|
|
|
|
channel := s.channels[chname]
|
|
|
|
|
|
|
|
if channel == nil {
|
|
|
|
user.replies <- ErrNoSuchChannel(s, channel.name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.commands <- &PartChannelCommand{m, m.message}
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func (m *TopicCommand) Handle(s *Server) {
|
|
|
|
user := m.Client().user
|
|
|
|
channel := s.channels[m.channel]
|
|
|
|
if channel == nil {
|
|
|
|
user.replies <- ErrNoSuchChannel(s, m.channel)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.topic == "" {
|
|
|
|
channel.commands <- &GetTopicChannelCommand{m}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.commands <- &SetTopicChannelCommand{m}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *PrivMsgCommand) Handle(s *Server) {
|
|
|
|
user := m.Client().user
|
|
|
|
|
|
|
|
if m.TargetIsChannel() {
|
|
|
|
channel := s.channels[m.target]
|
|
|
|
if channel == nil {
|
|
|
|
user.replies <- ErrNoSuchNick(s, m.target)
|
|
|
|
return
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
|
|
|
|
channel.commands <- &PrivMsgChannelCommand{m}
|
|
|
|
return
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
|
|
|
|
target := s.users[m.target]
|
|
|
|
if target != nil {
|
|
|
|
target.replies <- ErrNoSuchNick(s, m.target)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
target.replies <- RplPrivMsg(user, target, m.message)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func (m *LoginCommand) Handle(s *Server) {
|
|
|
|
client := m.Client()
|
|
|
|
if client.user != nil {
|
|
|
|
client.replies <- ErrAlreadyRegistered(s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user := s.users[m.nick]
|
|
|
|
if user == nil {
|
|
|
|
client.replies <- ErrNoSuchNick(s, m.nick)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !user.Login(client, m.nick, m.password) {
|
|
|
|
client.replies <- ErrRestricted(s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client.replies <- RplNick(client, m.nick)
|
|
|
|
// TODO join channels
|
2012-04-07 20:44:59 +02:00
|
|
|
}
|
2012-12-13 08:27:17 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func (m *ReserveCommand) Handle(s *Server) {
|
|
|
|
client := m.Client()
|
|
|
|
if client.user != nil {
|
|
|
|
client.replies <- ErrAlreadyRegistered(s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.users[m.nick] != nil {
|
|
|
|
client.replies <- ErrNickNameInUse(s, m.nick)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.users[m.nick] = NewUser(m.nick, m.password, s)
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|