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-12-17 04:13:53 +01:00
|
|
|
type ServiceNameMap map[string]*Service
|
|
|
|
|
|
|
|
type Command interface {
|
|
|
|
ClientMessage
|
|
|
|
Handle(*Server)
|
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
|
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
|
|
|
password []byte
|
|
|
|
users UserNameMap
|
|
|
|
channels ChannelNameMap
|
2012-12-17 04:13:53 +01:00
|
|
|
services ServiceNameMap
|
|
|
|
commands chan<- Command
|
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),
|
2012-12-17 04:13:53 +01:00
|
|
|
services: make(ServiceNameMap),
|
2012-12-15 23:34:20 +01:00
|
|
|
}
|
|
|
|
go server.receiveCommands(commands)
|
2012-12-17 04:13:53 +01:00
|
|
|
NewNickServ(server)
|
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 {
|
2012-12-17 04:13:53 +01:00
|
|
|
log.Printf("%s %T %+v", server.Id(), command, command)
|
2012-12-15 23:34:20 +01:00
|
|
|
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)
|
2012-12-17 04:13:53 +01:00
|
|
|
users.Add(fromUser)
|
2012-12-15 23:34:20 +01:00
|
|
|
for channel := range fromUser.channels {
|
|
|
|
for user := range channel.members {
|
2012-12-17 04:13:53 +01:00
|
|
|
users.Add(user)
|
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) Id() string {
|
|
|
|
return s.hostname
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) PublicId() string {
|
|
|
|
return s.Id()
|
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
func (s *Server) Nick() string {
|
|
|
|
return s.name
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func (s *Server) DeleteChannel(channel *Channel) {
|
|
|
|
delete(s.channels, channel.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// commands
|
|
|
|
//
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
func (m *UnknownCommand) Handle(s *Server) {
|
|
|
|
m.Client().replies <- ErrUnknownCommand(s, m.command)
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
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-17 04:13:53 +01:00
|
|
|
func (m *UserMsgCommand) Handle(s *Server) {
|
2012-12-15 23:34:20 +01:00
|
|
|
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()
|
2012-12-17 04:13:53 +01:00
|
|
|
|
|
|
|
user := c.user
|
|
|
|
if user != nil {
|
|
|
|
reply := RplQuit(c, m.message)
|
|
|
|
for user := range s.InterestedUsers(c.user) {
|
|
|
|
user.replies <- reply
|
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
}
|
|
|
|
c.conn.Close()
|
2012-12-17 04:13:53 +01:00
|
|
|
if user == nil {
|
|
|
|
return
|
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
user.LogoutClient(c)
|
2012-12-15 23:34:20 +01:00
|
|
|
if !user.HasClients() {
|
2012-12-17 04:13:53 +01:00
|
|
|
cmd := &PartCommand{
|
|
|
|
BaseCommand: &BaseCommand{c},
|
2012-12-15 23:34:20 +01:00
|
|
|
}
|
2012-12-17 04:13:53 +01:00
|
|
|
for channel := range user.channels {
|
2012-12-15 23:34:20 +01:00
|
|
|
channel.commands <- cmd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *JoinCommand) Handle(s *Server) {
|
|
|
|
c := m.Client()
|
2012-12-17 04:13:53 +01:00
|
|
|
|
|
|
|
if c.user == nil {
|
|
|
|
for name := range m.channels {
|
|
|
|
c.replies <- ErrNoSuchChannel(s, name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
if m.zero {
|
2012-12-17 04:13:53 +01:00
|
|
|
cmd := &PartCommand{
|
|
|
|
BaseCommand: &BaseCommand{c},
|
2012-12-15 23:34:20 +01:00
|
|
|
}
|
|
|
|
for channel := range c.user.channels {
|
|
|
|
channel.commands <- cmd
|
|
|
|
}
|
2012-12-17 04:13:53 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for name := range m.channels {
|
|
|
|
s.GetOrMakeChannel(name).commands <- m
|
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
|
2012-12-17 04:13:53 +01:00
|
|
|
|
|
|
|
if user == nil {
|
|
|
|
for _, chname := range m.channels {
|
|
|
|
m.Client().replies <- ErrNoSuchChannel(s, chname)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
for _, chname := range m.channels {
|
|
|
|
channel := s.channels[chname]
|
|
|
|
|
|
|
|
if channel == nil {
|
|
|
|
user.replies <- ErrNoSuchChannel(s, channel.name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
channel.commands <- m
|
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
|
2012-12-17 04:13:53 +01:00
|
|
|
|
|
|
|
if user == nil {
|
|
|
|
m.Client().replies <- ErrNoSuchChannel(s, m.channel)
|
2012-12-15 23:34:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
channel := s.channels[m.channel]
|
|
|
|
if channel == nil {
|
|
|
|
user.replies <- ErrNoSuchChannel(s, m.channel)
|
2012-12-15 23:34:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
channel.commands <- m
|
2012-12-15 23:34:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *PrivMsgCommand) Handle(s *Server) {
|
2012-12-17 04:13:53 +01:00
|
|
|
service := s.services[m.target]
|
|
|
|
if service != nil {
|
|
|
|
service.commands <- m
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
user := m.Client().user
|
2012-12-17 04:13:53 +01:00
|
|
|
if user == nil {
|
|
|
|
m.Client().replies <- ErrNoSuchNick(s, m.target)
|
|
|
|
return
|
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
|
|
|
|
if m.TargetIsChannel() {
|
|
|
|
channel := s.channels[m.target]
|
|
|
|
if channel == nil {
|
2012-12-17 04:13:53 +01:00
|
|
|
user.replies <- ErrNoSuchChannel(s, m.target)
|
2012-12-15 23:34:20 +01:00
|
|
|
return
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
channel.commands <- m
|
2012-12-15 23:34:20 +01:00
|
|
|
return
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
|
|
|
|
target := s.users[m.target]
|
2012-12-17 04:13:53 +01:00
|
|
|
if target == nil {
|
|
|
|
user.replies <- ErrNoSuchNick(s, m.target)
|
2012-12-15 23:34:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
target.commands <- m
|
2012-04-07 20:44:59 +02:00
|
|
|
}
|
2012-12-13 08:27:17 +01:00
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
func (m *ModeCommand) Handle(s *Server) {
|
|
|
|
m.Client().replies <- RplUModeIs(s, m.Client())
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|