nmdc-ircfrontend/server.go

351 lines
7.0 KiB
Go
Raw Normal View History

2013-08-24 06:48:28 +00:00
package main
import (
"fmt"
2013-08-29 19:55:17 +00:00
"log"
2013-08-24 06:48:28 +00:00
"net"
"regexp"
"strings"
"golang.org/x/crypto/bcrypt"
2013-08-24 06:48:28 +00:00
)
var (
nickRegexp = regexp.MustCompile(`^[a-zA-Z\[\]_^{|}][a-zA-Z0-9\[\]_^{|}]*$`)
2013-08-27 12:40:51 +00:00
channelRegexp = regexp.MustCompile(`^#[a-zA-Z0-9_\-]+$`)
2013-08-24 06:48:28 +00:00
)
func NewServer() *Server {
return &Server{eventChan: make(chan Event),
name: "",
2013-08-24 06:48:28 +00:00
clientMap: make(map[string]*Client),
channelMap: make(map[string]*Channel),
operatorMap: make(map[string][]byte),
motd: "",
}
2013-08-24 06:48:28 +00:00
}
func (s *Server) Run() {
2013-08-24 07:36:57 +00:00
for event := range s.eventChan {
2013-08-24 19:32:23 +00:00
s.handleEvent(event)
2013-08-24 07:36:57 +00:00
}
2013-08-24 06:48:28 +00:00
}
func (s *Server) HandleConnection(conn net.Conn) {
client := &Client{server: s,
connection: conn,
outputChan: make(chan string),
2013-08-24 19:58:38 +00:00
signalChan: make(chan signalCode, 3),
2013-08-24 06:48:28 +00:00
channelMap: make(map[string]*Channel),
connected: true,
}
2013-08-24 06:48:28 +00:00
go client.clientThread()
}
func (s *Server) handleEvent(e Event) {
2013-08-29 19:55:17 +00:00
defer func(event Event) {
err := recover()
if err != nil {
2013-09-08 21:50:27 +00:00
log.Printf("Recovered from error when handling event: %+v", event)
2013-08-29 19:55:17 +00:00
log.Println(err)
}
}(e)
2013-08-24 06:48:28 +00:00
2013-09-08 15:24:17 +00:00
switch e.event {
case connected:
//Client connected
e.client.reply(rplMOTDStart)
motd := s.motd
for len(motd) > 80 {
e.client.reply(rplMOTD, motd[:80])
motd = motd[80:]
}
if len(motd) > 0 {
e.client.reply(rplMOTD, motd)
}
e.client.reply(rplEndOfMOTD)
2013-09-08 15:24:17 +00:00
case disconnected:
//Client disconnected
case command:
//Client send a command
fields := strings.Fields(e.input)
if len(fields) < 1 {
return
}
2013-08-24 06:48:28 +00:00
2013-09-08 15:24:17 +00:00
if strings.HasPrefix(fields[0], ":") {
fields = fields[1:]
}
command := strings.ToUpper(fields[0])
args := fields[1:]
s.handleCommand(e.client, command, args)
2013-08-24 06:48:28 +00:00
}
2013-09-08 15:24:17 +00:00
}
2013-08-24 06:48:28 +00:00
2013-09-08 15:24:17 +00:00
func (s *Server) handleCommand(client *Client, command string, args []string) {
2013-08-24 06:48:28 +00:00
2013-09-08 15:24:17 +00:00
switch command {
2013-10-21 12:34:12 +00:00
case "PING":
client.reply(rplPong)
2013-09-08 15:24:17 +00:00
case "INFO":
client.reply(rplInfo, APP_DESCRIPTION)
2013-09-08 15:24:17 +00:00
case "VERSION":
client.reply(rplVersion, VERSION)
2013-09-08 15:24:17 +00:00
case "NICK":
client.reply(rplKill, "Can't change nicks on this server", "")
client.disconnect()
2013-08-24 06:48:28 +00:00
2013-09-08 15:24:17 +00:00
case "USER":
if client.nick == "" {
client.reply(rplKill, "Your nickname is already being used", "")
2013-09-08 15:24:17 +00:00
client.disconnect()
2013-08-24 06:48:28 +00:00
} else {
2013-09-08 15:24:17 +00:00
client.reply(rplWelcome)
client.registered = true
2013-08-24 06:48:28 +00:00
}
2013-09-08 15:24:17 +00:00
case "JOIN":
if client.registered == false {
client.reply(errNotReg)
2013-08-24 06:48:28 +00:00
return
}
if len(args) < 1 {
2013-09-08 15:24:17 +00:00
client.reply(errMoreArgs)
2013-08-24 06:48:28 +00:00
return
}
if args[0] == "0" {
// Intend to quit all channels
// But we don't allow that
2013-08-24 06:48:28 +00:00
return
}
channels := strings.Split(args[0], ",")
for _, channel := range channels {
//Join the channel if it's valid
if channelRegexp.MatchString(channel) {
2013-09-08 15:24:17 +00:00
client.joinChannel(channel)
2013-08-24 06:48:28 +00:00
}
}
2013-09-08 15:24:17 +00:00
case "PART":
if client.registered == false {
client.reply(errNotReg)
2013-08-24 06:48:28 +00:00
return
}
// you can check out any time you like, but you can never leave
// we'll need to transmit client.reply(rplPart, c.nick, channel.name, reason) messages on behalf of other nmdc users, though
2013-08-24 06:48:28 +00:00
2013-09-08 15:24:17 +00:00
case "PRIVMSG":
if client.registered == false {
client.reply(errNotReg)
2013-08-24 06:48:28 +00:00
return
}
if len(args) < 2 {
2013-09-08 15:24:17 +00:00
client.reply(errMoreArgs)
2013-08-24 06:48:28 +00:00
return
}
message := strings.Join(args[1:], " ")
2013-08-27 12:40:51 +00:00
channel, chanExists := s.channelMap[strings.ToLower(args[0])]
2013-09-08 15:59:56 +00:00
client2, clientExists := s.clientMap[strings.ToLower(args[0])]
2013-08-24 06:48:28 +00:00
if chanExists {
2013-08-29 15:58:50 +00:00
if channel.mode.noExternal {
if _, inChannel := channel.clientMap[client.key]; !inChannel {
2013-08-29 15:58:50 +00:00
//Not in channel, not allowed to send
2013-09-08 15:24:17 +00:00
client.reply(errCannotSend, args[0])
2013-08-29 15:58:50 +00:00
return
}
}
2013-08-27 18:40:59 +00:00
if channel.mode.moderated {
clientMode := channel.modeMap[client.key]
2013-08-27 18:40:59 +00:00
if !clientMode.operator && !clientMode.voice {
//It's moderated and we're not +v or +o, do nothing
2013-09-08 15:24:17 +00:00
client.reply(errCannotSend, args[0])
2013-08-27 18:40:59 +00:00
return
}
}
2013-08-24 06:48:28 +00:00
for _, c := range channel.clientMap {
2013-09-08 15:24:17 +00:00
if c != client {
c.reply(rplMsg, client.nick, args[0], message)
2013-08-24 06:48:28 +00:00
}
}
} else if clientExists {
2016-03-08 01:38:49 +00:00
client2.reply(rplMsg, client.nick, client2.nick, message)
2013-08-24 06:48:28 +00:00
} else {
2013-09-08 15:24:17 +00:00
client.reply(errNoSuchNick, args[0])
2013-08-24 06:48:28 +00:00
}
2013-09-08 15:24:17 +00:00
case "QUIT":
if client.registered == false {
client.reply(errNotReg)
2013-08-24 06:48:28 +00:00
return
}
2013-09-08 15:24:17 +00:00
client.disconnect()
2013-08-24 06:48:28 +00:00
2013-09-08 15:24:17 +00:00
case "TOPIC":
if client.registered == false {
client.reply(errNotReg)
2013-08-24 06:48:28 +00:00
return
}
if len(args) < 1 {
2013-09-08 15:24:17 +00:00
client.reply(errMoreArgs)
2013-08-24 06:48:28 +00:00
return
}
2013-08-27 12:40:51 +00:00
channel, exists := s.channelMap[strings.ToLower(args[0])]
2013-08-24 06:48:28 +00:00
if exists == false {
2013-09-08 15:24:17 +00:00
client.reply(errNoSuchNick, args[0])
2013-08-24 06:48:28 +00:00
return
}
// Valid topic get
2013-08-24 06:48:28 +00:00
if len(args) == 1 {
client.reply(rplTopic, channel.name, s.name)
2013-08-24 06:48:28 +00:00
return
}
// Disallow topic set
client.reply(errNoPriv)
return
2013-08-24 06:48:28 +00:00
2013-09-08 15:24:17 +00:00
case "LIST":
if client.registered == false {
client.reply(errNotReg)
2013-08-24 06:48:28 +00:00
return
}
if len(args) == 0 {
for channelName, channel := range s.channelMap {
2013-08-29 17:08:39 +00:00
if channel.mode.secret {
if _, inChannel := channel.clientMap[client.key]; !inChannel {
2013-08-29 17:08:39 +00:00
//Not in the channel, skip
continue
}
}
2013-08-24 06:48:28 +00:00
listItem := fmt.Sprintf("%s %d :%s", channelName, len(channel.clientMap), channel.topic)
client.reply(rplList, listItem)
2013-08-24 06:48:28 +00:00
}
client.reply(rplListEnd)
2013-08-24 06:48:28 +00:00
} else {
channels := strings.Split(args[0], ",")
for _, channelName := range channels {
2013-08-27 12:40:51 +00:00
if channel, exists := s.channelMap[strings.ToLower(channelName)]; exists {
2013-08-24 06:48:28 +00:00
listItem := fmt.Sprintf("%s %d :%s", channelName, len(channel.clientMap), channel.topic)
client.reply(rplList, listItem)
2013-08-24 06:48:28 +00:00
}
}
client.reply(rplListEnd)
2013-08-24 06:48:28 +00:00
}
2013-09-08 15:24:17 +00:00
case "OPER":
if client.registered == false {
client.reply(errNotReg)
2013-08-24 06:48:28 +00:00
return
}
if len(args) < 2 {
2013-09-08 15:24:17 +00:00
client.reply(errMoreArgs)
2013-08-24 06:48:28 +00:00
return
}
username := args[0]
password := args[1]
if hashedPassword, exists := s.operatorMap[username]; exists {
//nil means the passwords matched
if err := bcrypt.CompareHashAndPassword(hashedPassword, []byte(password)); err == nil {
2013-09-08 15:24:17 +00:00
client.operator = true
client.reply(rplOper)
2013-08-24 06:48:28 +00:00
return
}
}
2013-09-08 15:24:17 +00:00
client.reply(errPassword)
2013-08-24 06:48:28 +00:00
2013-09-08 15:24:17 +00:00
case "KILL":
if client.registered == false {
client.reply(errNotReg)
2013-08-24 06:48:28 +00:00
return
}
2013-09-08 15:24:17 +00:00
if client.operator == false {
client.reply(errNoPriv)
2013-08-24 06:48:28 +00:00
return
}
if len(args) < 1 {
2013-09-08 15:24:17 +00:00
client.reply(errMoreArgs)
2013-08-24 06:48:28 +00:00
return
}
nick := args[0]
2013-08-29 20:20:34 +00:00
reason := strings.Join(args[1:], " ")
2013-08-31 17:44:25 +00:00
client, exists := s.clientMap[strings.ToLower(nick)]
if !exists {
2013-09-08 15:24:17 +00:00
client.reply(errNoSuchNick, nick)
2013-08-31 17:44:25 +00:00
return
2013-08-24 06:48:28 +00:00
}
2013-09-08 15:24:17 +00:00
client.reply(rplKill, client.nick, reason)
2013-08-31 17:44:25 +00:00
client.disconnect()
2013-09-08 15:24:17 +00:00
case "KICK":
if client.registered == false {
client.reply(errNotReg)
2013-08-29 20:10:28 +00:00
return
}
client.reply(errNoPriv)
return
2013-08-29 20:10:28 +00:00
2013-09-08 15:24:17 +00:00
case "MODE":
if client.registered == false {
client.reply(errNotReg)
return
}
if len(args) < 1 {
2013-09-08 15:24:17 +00:00
client.reply(errMoreArgs)
return
}
channelKey := strings.ToLower(args[0])
channel, channelExists := s.channelMap[channelKey]
if !channelExists {
2013-09-08 15:24:17 +00:00
client.reply(errNoSuchNick, args[0])
return
}
mode := channel.mode
if len(args) == 1 {
//No more args, they just want the mode
2013-09-08 15:24:17 +00:00
client.reply(rplChannelModeIs, args[0], mode.String(), "")
return
}
// Setting modes is disallowed
client.reply(errNoPriv)
return
2013-08-24 06:48:28 +00:00
default:
2013-09-08 15:24:17 +00:00
client.reply(errUnknownCommand, command)
2013-08-24 06:48:28 +00:00
}
}