2013-08-24 06:48:28 +00:00
|
|
|
package main
|
|
|
|
|
2016-05-05 07:20:36 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2016 The `nmdc-ircfrontend' author(s)
|
|
|
|
Copyright (C) 2013 Harry Jeffery
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-08-24 06:48:28 +00:00
|
|
|
import (
|
2016-05-03 06:47:24 +00:00
|
|
|
"bytes"
|
2013-08-24 06:48:28 +00:00
|
|
|
"fmt"
|
2016-05-03 06:47:24 +00:00
|
|
|
"io"
|
2016-05-03 06:25:27 +00:00
|
|
|
"libnmdc"
|
2016-05-07 00:59:28 +00:00
|
|
|
"log"
|
2013-08-24 06:48:28 +00:00
|
|
|
"net"
|
|
|
|
"strings"
|
2016-05-03 07:57:02 +00:00
|
|
|
"time"
|
2013-08-24 06:48:28 +00:00
|
|
|
)
|
|
|
|
|
2016-05-07 05:07:16 +00:00
|
|
|
type ClientState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
CSUnregistered ClientState = iota
|
|
|
|
CSRegistered
|
|
|
|
CSJoined
|
|
|
|
)
|
|
|
|
|
2016-05-03 06:25:27 +00:00
|
|
|
type Server struct {
|
2016-05-05 07:16:45 +00:00
|
|
|
name string
|
|
|
|
motd string
|
2016-05-05 07:11:40 +00:00
|
|
|
|
2016-05-07 05:07:16 +00:00
|
|
|
clientConn net.Conn
|
|
|
|
clientState ClientState
|
2016-05-05 07:11:40 +00:00
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
upstreamLauncher libnmdc.HubConnectionOptions
|
2016-05-05 06:56:55 +00:00
|
|
|
upstreamCloser chan struct{}
|
2016-05-03 07:57:02 +00:00
|
|
|
upstream *libnmdc.HubConnection
|
2016-05-05 07:57:31 +00:00
|
|
|
|
2016-05-07 05:07:16 +00:00
|
|
|
verbose bool
|
|
|
|
autojoin bool
|
2016-05-03 06:25:27 +00:00
|
|
|
}
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2016-05-05 07:11:40 +00:00
|
|
|
func NewServer(name string, upstream libnmdc.HubAddress, conn net.Conn) *Server {
|
2016-05-03 07:57:02 +00:00
|
|
|
self := libnmdc.NewUserInfo("")
|
|
|
|
self.ClientTag = APP_DESCRIPTION
|
|
|
|
|
2016-05-05 07:11:40 +00:00
|
|
|
return &Server{
|
2016-05-07 05:07:16 +00:00
|
|
|
name: name,
|
|
|
|
clientConn: conn,
|
|
|
|
clientState: CSUnregistered,
|
|
|
|
motd: "Connected to " + name + ". You /must/ join " + BLESSED_CHANNEL + " to continue.",
|
2016-05-03 07:25:37 +00:00
|
|
|
upstreamLauncher: libnmdc.HubConnectionOptions{
|
|
|
|
Address: upstream,
|
2016-05-03 07:57:02 +00:00
|
|
|
Self: *self,
|
2016-05-03 07:25:37 +00:00
|
|
|
},
|
2016-05-05 06:56:55 +00:00
|
|
|
upstreamCloser: make(chan struct{}, 1),
|
2016-05-02 06:45:13 +00:00
|
|
|
}
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 01:55:29 +00:00
|
|
|
func (s *Server) verboseln(line string) {
|
|
|
|
if s.verbose {
|
|
|
|
log.Println(line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) verbosef(fmt string, args ...interface{}) {
|
|
|
|
if s.verbose {
|
|
|
|
log.Printf(fmt, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-05 07:11:40 +00:00
|
|
|
func (s *Server) RunWorker() {
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2016-05-05 07:45:59 +00:00
|
|
|
// Send the connection handshake.
|
2016-05-03 07:25:37 +00:00
|
|
|
// Can't connect to the upstream server yet, until we've recieved a nick.
|
2016-05-07 02:46:23 +00:00
|
|
|
s.sendMOTD(s.motd)
|
2016-05-03 07:25:37 +00:00
|
|
|
|
2016-05-03 06:47:24 +00:00
|
|
|
for {
|
2016-05-05 07:45:59 +00:00
|
|
|
if s.clientConn == nil {
|
|
|
|
break // abandon thread
|
|
|
|
}
|
|
|
|
|
2016-05-03 06:47:24 +00:00
|
|
|
buf := make([]byte, CLIENT_READ_BUFFSIZE)
|
2016-05-07 00:59:28 +00:00
|
|
|
|
2016-05-05 07:11:40 +00:00
|
|
|
ln, err := s.clientConn.Read(buf)
|
2016-05-03 07:57:02 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
2016-05-05 07:45:59 +00:00
|
|
|
break // abandon thread
|
2016-05-03 06:47:24 +00:00
|
|
|
}
|
2016-05-07 05:50:39 +00:00
|
|
|
|
|
|
|
// If this was a /timeout/, send a KA and continue.
|
|
|
|
|
|
|
|
// But otherwise, it was a real error (e.g. unexpected disconnect)
|
2016-05-07 01:55:29 +00:00
|
|
|
s.verboseln(err.Error())
|
2016-05-07 05:50:39 +00:00
|
|
|
break // abandon
|
2016-05-03 07:57:02 +00:00
|
|
|
}
|
2016-05-03 06:47:24 +00:00
|
|
|
|
2016-05-03 07:57:02 +00:00
|
|
|
rawLines := buf[:ln]
|
|
|
|
rawLines = bytes.Replace(rawLines, []byte("\r\n"), []byte("\n"), -1)
|
|
|
|
rawLines = bytes.Replace(rawLines, []byte("\r"), []byte("\n"), -1)
|
|
|
|
lines := bytes.Split(rawLines, []byte("\n"))
|
|
|
|
for _, line := range lines {
|
|
|
|
if len(line) > 0 {
|
|
|
|
|
|
|
|
// Client sent a command
|
|
|
|
fields := strings.Fields(string(line))
|
|
|
|
if len(fields) < 1 {
|
|
|
|
return
|
|
|
|
}
|
2016-05-03 07:25:37 +00:00
|
|
|
|
2016-05-03 07:57:02 +00:00
|
|
|
if strings.HasPrefix(fields[0], ":") {
|
|
|
|
fields = fields[1:]
|
2016-05-03 07:25:37 +00:00
|
|
|
}
|
2016-05-03 07:57:02 +00:00
|
|
|
|
|
|
|
s.handleCommand(strings.ToUpper(fields[0]), fields[1:])
|
2016-05-03 06:47:24 +00:00
|
|
|
}
|
2016-03-13 06:51:10 +00:00
|
|
|
}
|
2016-05-03 06:47:24 +00:00
|
|
|
}
|
2016-05-02 07:23:15 +00:00
|
|
|
|
2016-05-07 01:55:29 +00:00
|
|
|
s.verboseln("Broken loop.")
|
2016-05-05 07:45:59 +00:00
|
|
|
|
|
|
|
// Cleanup upstream
|
2016-05-07 05:07:16 +00:00
|
|
|
if s.upstream != nil {
|
2016-05-07 00:59:28 +00:00
|
|
|
s.upstreamCloser <- struct{}{} // always safe to do this /once/
|
2016-05-05 07:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up ourselves
|
2016-05-07 00:59:28 +00:00
|
|
|
s.DisconnectClient() // if not already done
|
2016-05-03 06:47:24 +00:00
|
|
|
}
|
2016-05-02 07:23:15 +00:00
|
|
|
|
2016-05-05 07:11:40 +00:00
|
|
|
func (s *Server) upstreamWorker() {
|
2016-05-03 07:57:02 +00:00
|
|
|
|
2016-05-07 05:51:07 +00:00
|
|
|
var BLANK_NICK = "PtokaX" // "\xC2\xA0"
|
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
// Read loop
|
|
|
|
for {
|
|
|
|
select {
|
2016-05-05 07:11:40 +00:00
|
|
|
case <-s.upstreamCloser:
|
2016-05-05 06:56:55 +00:00
|
|
|
// Abandon the upstream connection
|
2016-05-07 01:55:29 +00:00
|
|
|
s.verboseln("Abandoning upstream connection...")
|
2016-05-05 06:56:55 +00:00
|
|
|
s.upstream.Disconnect()
|
2016-05-03 07:25:37 +00:00
|
|
|
return
|
2013-09-08 15:24:17 +00:00
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
case hubEvent := <-s.upstream.OnEvent:
|
|
|
|
switch hubEvent.EventType {
|
|
|
|
case libnmdc.EVENT_USER_JOINED:
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(rplJoin, hubEvent.Nick, BLESSED_CHANNEL)
|
2016-05-07 06:38:25 +00:00
|
|
|
// If we want to JOIN with the full power of the supplied nick!user@host, then we'll need to actually remember the active client's USER parameters
|
2016-05-03 07:25:37 +00:00
|
|
|
|
|
|
|
case libnmdc.EVENT_USER_PART:
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(rplPart, hubEvent.Nick, BLESSED_CHANNEL, "Disconnected")
|
2016-05-03 07:25:37 +00:00
|
|
|
|
|
|
|
case libnmdc.EVENT_USER_UPDATED_INFO:
|
|
|
|
// description change - no relevance for IRC users
|
|
|
|
|
|
|
|
case libnmdc.EVENT_CONNECTION_STATE_CHANGED:
|
2016-05-07 05:51:07 +00:00
|
|
|
s.reply(rplMsg, BLANK_NICK, BLESSED_CHANNEL, "* Upstream: "+hubEvent.StateChange.Format())
|
2016-05-03 07:25:37 +00:00
|
|
|
|
|
|
|
case libnmdc.EVENT_HUBNAME_CHANGED:
|
2016-05-05 07:26:31 +00:00
|
|
|
s.sendChannelTopic(hubEvent.Nick)
|
2016-05-03 06:47:24 +00:00
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
case libnmdc.EVENT_PRIVATE:
|
2016-05-07 06:38:25 +00:00
|
|
|
//s.reply(rplMsg, hubEvent.Nick, s.upstreamLauncher.Self.Nick, hubEvent.Message)
|
|
|
|
s.reply(rplMsg, hubEvent.Nick+"!"+hubEvent.Nick+"@"+hubEvent.Nick, s.upstreamLauncher.Self.Nick, hubEvent.Message)
|
2016-05-03 06:47:24 +00:00
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
case libnmdc.EVENT_PUBLIC:
|
2016-05-07 06:40:12 +00:00
|
|
|
if hubEvent.Nick == s.upstreamLauncher.Self.Nick {
|
|
|
|
// irc doesn't echo our own pubchat
|
2016-05-05 07:57:31 +00:00
|
|
|
} else {
|
2016-05-07 06:38:25 +00:00
|
|
|
// s.reply(rplMsg, hubEvent.Nick, BLESSED_CHANNEL, hubEvent.Message)
|
|
|
|
s.reply(rplMsg, hubEvent.Nick+"!"+hubEvent.Nick+"@"+hubEvent.Nick, BLESSED_CHANNEL, hubEvent.Message)
|
2016-05-05 07:57:31 +00:00
|
|
|
}
|
2016-05-03 07:25:37 +00:00
|
|
|
|
|
|
|
case libnmdc.EVENT_SYSTEM_MESSAGE_FROM_CONN, libnmdc.EVENT_SYSTEM_MESSAGE_FROM_HUB:
|
2016-05-07 05:07:16 +00:00
|
|
|
// FIXME blank names work well in hexchat, but not in yaaic
|
2016-05-07 05:51:07 +00:00
|
|
|
s.reply(rplMsg, BLANK_NICK, BLESSED_CHANNEL, hubEvent.Message)
|
2016-05-03 07:25:37 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-08 15:24:17 +00:00
|
|
|
}
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
func (s *Server) handleCommand(command string, args []string) {
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2016-05-07 01:55:29 +00:00
|
|
|
s.verbosef(" >>> '%s' %v", command, args)
|
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
switch command {
|
2013-10-21 12:34:12 +00:00
|
|
|
case "PING":
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(rplPong)
|
2016-05-02 07:00:24 +00:00
|
|
|
|
2016-05-07 05:07:16 +00:00
|
|
|
case "PONG":
|
|
|
|
// do nothing
|
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "INFO":
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(rplInfo, APP_DESCRIPTION)
|
2016-05-02 07:00:24 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "VERSION":
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(rplVersion, VERSION)
|
2016-05-03 07:25:37 +00:00
|
|
|
|
2016-05-07 05:07:16 +00:00
|
|
|
case "MOTD":
|
|
|
|
s.sendMOTD(s.motd)
|
|
|
|
|
|
|
|
case "CAP":
|
|
|
|
return
|
|
|
|
/*
|
|
|
|
if len(args) < 1 {
|
|
|
|
s.reply(errMoreArgs)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if args[0] == "LS" {
|
|
|
|
s.writeClient("CAP * LS :nmdc-ircfrontend") // no special IRCv3 capabilities available
|
|
|
|
} else {
|
|
|
|
s.writeClient(fmt.Sprintf(":%s 410 * %s :Invalid CAP command", s.name, args[0]))
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2016-05-07 05:51:21 +00:00
|
|
|
case "PROTOCTL":
|
|
|
|
// we advertised support for NAMESX, if this happens the client accepted it
|
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
case "PASS":
|
2016-05-05 07:16:45 +00:00
|
|
|
// RFC2812 registration. Stash the password for later
|
2016-05-03 07:25:37 +00:00
|
|
|
if len(args) < 1 {
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(errMoreArgs)
|
2016-05-03 07:25:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
s.upstreamLauncher.NickPassword = args[0]
|
2016-05-02 07:00:24 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "NICK":
|
2016-05-03 07:25:37 +00:00
|
|
|
if len(args) < 1 {
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(errMoreArgs)
|
2016-05-03 07:25:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.upstreamLauncher.Self.Nick == "" {
|
|
|
|
// allow set, as part of the login phase
|
|
|
|
s.upstreamLauncher.Self.Nick = args[0]
|
|
|
|
} else {
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(rplKill, "Can't change nicks on this server", "")
|
|
|
|
s.DisconnectClient()
|
2016-05-03 07:25:37 +00:00
|
|
|
}
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "USER":
|
2016-05-03 07:57:02 +00:00
|
|
|
// This command sets altname, realname, ... none of which we use
|
|
|
|
// It's the final step in a PASS/NICK/USER login handshake.
|
|
|
|
|
2016-05-07 05:07:16 +00:00
|
|
|
if s.clientState != CSUnregistered {
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(rplKill, "You're already registered.", "")
|
|
|
|
s.DisconnectClient()
|
2016-05-03 07:57:02 +00:00
|
|
|
return
|
2016-05-03 06:25:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 07:45:59 +00:00
|
|
|
if s.upstreamLauncher.Self.Nick == "" {
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(rplKill, "Your nickname is already being used", "")
|
|
|
|
s.DisconnectClient()
|
2016-05-03 07:57:02 +00:00
|
|
|
return
|
|
|
|
}
|
2016-05-03 06:25:27 +00:00
|
|
|
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(rplWelcome)
|
2016-05-07 05:07:16 +00:00
|
|
|
s.clientState = CSRegistered
|
2016-05-05 07:57:38 +00:00
|
|
|
|
2016-05-07 05:07:16 +00:00
|
|
|
// TODO tell the client that they /must/ join #chat
|
|
|
|
if s.autojoin {
|
|
|
|
s.handleCommand("JOIN", []string{BLESSED_CHANNEL})
|
|
|
|
}
|
2016-05-03 07:57:02 +00:00
|
|
|
|
2016-05-07 00:59:34 +00:00
|
|
|
// Send a CTCP VERSION request to the client. If the IRC client can
|
|
|
|
// supply a client version string, we can replace our tag with it,
|
|
|
|
// but that's optional
|
|
|
|
|
2016-05-05 07:16:45 +00:00
|
|
|
default:
|
|
|
|
s.handleRegisteredCommand(command, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleRegisteredCommand(command string, args []string) {
|
|
|
|
|
2016-05-07 05:07:16 +00:00
|
|
|
if s.clientState == CSUnregistered {
|
2016-05-05 07:16:45 +00:00
|
|
|
s.reply(errNotReg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch command {
|
2013-09-08 15:24:17 +00:00
|
|
|
case "JOIN":
|
2013-08-24 06:48:28 +00:00
|
|
|
if len(args) < 1 {
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(errMoreArgs)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-02 07:13:09 +00:00
|
|
|
switch args[0] {
|
|
|
|
case BLESSED_CHANNEL:
|
2016-05-07 05:07:16 +00:00
|
|
|
if s.clientState != CSJoined {
|
|
|
|
// Join for the first time
|
|
|
|
s.clientState = CSJoined
|
|
|
|
|
|
|
|
// Acknowledge
|
|
|
|
s.reply(rplJoin, s.upstreamLauncher.Self.Nick, BLESSED_CHANNEL)
|
|
|
|
|
2016-05-07 05:52:07 +00:00
|
|
|
// Send channel mode immediately (hexchat optimisation)
|
|
|
|
s.sendChannelMode()
|
|
|
|
|
2016-05-07 05:07:16 +00:00
|
|
|
// Send (initially just us) nicklist for the chat channel
|
|
|
|
s.sendNames()
|
|
|
|
|
|
|
|
// Spawn upstream connection
|
|
|
|
s.upstream = s.upstreamLauncher.Connect()
|
|
|
|
go s.upstreamWorker()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// They're already here, ignore
|
|
|
|
// Can happen if autojoin is enabled but the client already requested a login
|
|
|
|
}
|
|
|
|
|
2016-05-02 07:13:09 +00:00
|
|
|
case "0":
|
2016-05-07 05:07:16 +00:00
|
|
|
// Quitting all channels? Drop client
|
|
|
|
s.reply(rplKill, "Bye.", "")
|
|
|
|
s.DisconnectClient()
|
|
|
|
|
2016-05-02 07:13:09 +00:00
|
|
|
default:
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(rplKill, "There is only '"+BLESSED_CHANNEL+"'.", "")
|
|
|
|
s.DisconnectClient()
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "PART":
|
2016-05-07 01:55:29 +00:00
|
|
|
if len(args) < 1 {
|
|
|
|
s.reply(errMoreArgs)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if args[0] == BLESSED_CHANNEL {
|
|
|
|
// You can check out any time you like, but you can never leave
|
|
|
|
s.reply(rplJoin, s.upstreamLauncher.Self.Nick, BLESSED_CHANNEL)
|
2016-05-07 05:07:16 +00:00
|
|
|
s.sendNames()
|
2016-05-07 01:55:29 +00:00
|
|
|
}
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2016-05-07 05:07:16 +00:00
|
|
|
case "PRIVMSG", "NOTICE":
|
2013-08-24 06:48:28 +00:00
|
|
|
if len(args) < 2 {
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(errMoreArgs)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-07 05:50:53 +00:00
|
|
|
if s.upstream == nil || s.upstream.State != libnmdc.CONNECTIONSTATE_CONNECTED {
|
2016-05-05 08:02:29 +00:00
|
|
|
s.reply(errCannotSend, args[0])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-05 07:57:49 +00:00
|
|
|
message := strings.Join(args[1:], " ")[1:] // strip leading colon
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2016-05-07 06:50:04 +00:00
|
|
|
if strings.HasPrefix(message, "\x01ACTION ") {
|
|
|
|
message = "/me " + message[8:]
|
|
|
|
message = message[:len(message)-1]
|
|
|
|
}
|
|
|
|
|
2016-05-03 06:25:27 +00:00
|
|
|
// IRC is case-insensitive case-preserving. We can respect that for the
|
|
|
|
// channel name, but not really for user nicks
|
|
|
|
|
2016-05-03 07:57:02 +00:00
|
|
|
if strings.ToLower(args[0]) == BLESSED_CHANNEL {
|
2016-05-07 06:40:12 +00:00
|
|
|
s.upstream.SayPublic(message)
|
2016-05-03 06:00:40 +00:00
|
|
|
|
2016-05-03 07:57:02 +00:00
|
|
|
} else if _, clientExists := s.upstream.Users[args[0]]; clientExists {
|
|
|
|
s.upstream.SayPrivate(args[0], message)
|
2016-05-03 06:00:40 +00:00
|
|
|
|
2013-08-24 06:48:28 +00:00
|
|
|
} else {
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(errNoSuchNick, args[0])
|
2016-05-03 06:00:40 +00:00
|
|
|
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "QUIT":
|
2016-05-05 07:11:40 +00:00
|
|
|
s.DisconnectClient()
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "TOPIC":
|
2013-08-24 06:48:28 +00:00
|
|
|
if len(args) < 1 {
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(errMoreArgs)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-05 07:26:31 +00:00
|
|
|
if strings.ToLower(args[0]) != BLESSED_CHANNEL {
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(errNoSuchNick, args[0])
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 1 {
|
2016-05-05 07:26:31 +00:00
|
|
|
s.sendChannelTopic(s.upstream.HubName) // Valid topic get
|
|
|
|
} else {
|
|
|
|
s.reply(errNoPriv) // Disallow topic set
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "LIST":
|
2016-05-05 07:11:40 +00:00
|
|
|
listItem := fmt.Sprintf("%s %d :%s", BLESSED_CHANNEL, len(s.upstream.Users), s.upstream.HubName)
|
|
|
|
s.reply(rplList, listItem)
|
|
|
|
s.reply(rplListEnd)
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "OPER":
|
2013-08-24 06:48:28 +00:00
|
|
|
if len(args) < 2 {
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(errMoreArgs)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-03 07:57:02 +00:00
|
|
|
// Can't use this command.
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(errPassword)
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2016-05-07 01:55:29 +00:00
|
|
|
case "KILL", "KICK":
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(errNoPriv)
|
2016-05-03 06:25:27 +00:00
|
|
|
return
|
2013-08-31 17:44:25 +00:00
|
|
|
|
2016-05-07 01:55:29 +00:00
|
|
|
case "WHO":
|
|
|
|
if len(args) < 1 {
|
|
|
|
s.reply(errMoreArgs)
|
2013-08-29 20:10:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-07 05:52:27 +00:00
|
|
|
// s.sendNames() // Ignore what we're "supposed" to do
|
|
|
|
s.sendWho(args[0])
|
2013-08-29 20:10:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "MODE":
|
2013-08-29 16:57:33 +00:00
|
|
|
if len(args) < 1 {
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(errMoreArgs)
|
2013-08-29 16:57:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-03 06:00:40 +00:00
|
|
|
if strings.ToLower(args[0]) != BLESSED_CHANNEL {
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(errNoSuchNick, args[0])
|
2013-08-29 16:57:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 1 {
|
2016-05-03 07:57:02 +00:00
|
|
|
// No more args, they just want the mode
|
2016-05-07 05:52:27 +00:00
|
|
|
s.sendChannelMode()
|
2016-05-03 06:00:40 +00:00
|
|
|
} else {
|
|
|
|
// Setting modes is disallowed
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(errNoPriv)
|
2013-08-29 16:57:33 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 07:00:24 +00:00
|
|
|
return
|
2013-08-29 16:57:33 +00:00
|
|
|
|
2013-08-24 06:48:28 +00:00
|
|
|
default:
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(errUnknownCommand, command)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) DisconnectClient() {
|
2016-05-05 07:45:59 +00:00
|
|
|
if s.clientConn != nil {
|
|
|
|
s.clientConn.Close()
|
|
|
|
}
|
2016-05-05 07:11:40 +00:00
|
|
|
s.clientConn = nil
|
2016-05-07 05:07:16 +00:00
|
|
|
s.clientState = CSUnregistered
|
2016-05-05 07:16:45 +00:00
|
|
|
|
|
|
|
// Readloop will stop, which kills the upstream connection too
|
2016-05-05 07:11:40 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 05:07:16 +00:00
|
|
|
func (s *Server) sendNames() {
|
|
|
|
names := s.upstreamLauncher.Self.Nick
|
|
|
|
if s.upstream != nil {
|
|
|
|
for nick, _ := range s.upstream.Users {
|
|
|
|
names += " " + nick
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.reply(rplNames, BLESSED_CHANNEL, names)
|
|
|
|
s.reply(rplEndOfNames, BLESSED_CHANNEL)
|
|
|
|
}
|
|
|
|
|
2016-05-07 05:52:27 +00:00
|
|
|
func (s *Server) sendChannelMode() {
|
|
|
|
s.reply(rplChannelModeIs, BLESSED_CHANNEL, BLESSED_CHANNEL_MODE, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) sendWho(arg string) {
|
|
|
|
if arg == BLESSED_CHANNEL {
|
|
|
|
// always include ourselves
|
|
|
|
s.reply(rplWho, s.upstreamLauncher.Self.Nick, arg)
|
|
|
|
|
|
|
|
for nick, _ := range s.upstream.Users {
|
|
|
|
if nick != s.upstreamLauncher.Self.Nick { // but don't repeat ourselves
|
|
|
|
s.reply(rplWho, nick, arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// argument is a filter
|
|
|
|
for nick, _ := range s.upstream.Users {
|
|
|
|
if strings.Contains(nick, arg) {
|
|
|
|
s.reply(rplWho, nick, arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.reply(rplEndOfWho, arg)
|
|
|
|
}
|
|
|
|
|
2016-05-05 07:26:31 +00:00
|
|
|
func (s *Server) sendChannelTopic(topic string) {
|
|
|
|
if len(topic) > 0 {
|
|
|
|
s.reply(rplTopic, BLESSED_CHANNEL, s.upstream.HubName)
|
|
|
|
} else {
|
|
|
|
s.reply(rplNoTopic, BLESSED_CHANNEL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-07 02:46:23 +00:00
|
|
|
func (s *Server) sendMOTD(motd string) {
|
2016-05-05 07:11:40 +00:00
|
|
|
s.reply(rplMOTDStart)
|
|
|
|
|
|
|
|
for len(motd) > 80 {
|
|
|
|
s.reply(rplMOTD, motd[:80])
|
|
|
|
motd = motd[80:]
|
|
|
|
}
|
|
|
|
if len(motd) > 0 {
|
|
|
|
s.reply(rplMOTD, motd)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.reply(rplEndOfMOTD)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a reply to a user with the code specified
|
|
|
|
func (s *Server) reply(code replyCode, args ...string) {
|
|
|
|
|
|
|
|
switch code {
|
|
|
|
case rplWelcome:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 001 %s :Welcome to %s", s.name, s.upstreamLauncher.Self.Nick, s.name))
|
2016-05-07 05:51:21 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 005 %s NAMESX :are supported by this server", s.name, s.upstreamLauncher.Self.Nick))
|
|
|
|
|
2016-05-05 07:11:40 +00:00
|
|
|
case rplJoin:
|
|
|
|
s.writeClient(fmt.Sprintf(":%s JOIN %s", args[0], args[1]))
|
|
|
|
case rplPart:
|
|
|
|
s.writeClient(fmt.Sprintf(":%s PART %s %s", args[0], args[1], args[2]))
|
|
|
|
case rplTopic:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 332 %s %s :%s", s.name, s.upstreamLauncher.Self.Nick, args[0], args[1]))
|
2016-05-05 07:11:40 +00:00
|
|
|
case rplNoTopic:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 331 %s %s :No topic is set", s.name, s.upstreamLauncher.Self.Nick, args[0]))
|
2016-05-07 01:55:29 +00:00
|
|
|
|
2016-05-05 07:11:40 +00:00
|
|
|
case rplNames:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 353 %s = %s :%s", s.name, s.upstreamLauncher.Self.Nick, args[0], args[1]))
|
2016-05-05 07:11:40 +00:00
|
|
|
case rplEndOfNames:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 366 %s %s :End of NAMES list", s.name, s.upstreamLauncher.Self.Nick, args[0]))
|
2016-05-07 01:55:29 +00:00
|
|
|
|
|
|
|
case rplWho:
|
2016-05-07 02:46:23 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 352 %s %s %s %s %s %s H :0 %s", s.name, s.upstreamLauncher.Self.Nick, args[1], args[0], args[0], s.name, args[0], args[0]))
|
2016-05-07 01:55:29 +00:00
|
|
|
case rplEndOfWho:
|
2016-05-07 02:46:23 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 315 %s %s :End of WHO list", s.name, s.upstreamLauncher.Self.Nick, args[0]))
|
2016-05-07 01:55:29 +00:00
|
|
|
|
2016-05-05 07:11:40 +00:00
|
|
|
case rplNickChange:
|
|
|
|
s.writeClient(fmt.Sprintf(":%s NICK %s", args[0], args[1]))
|
|
|
|
case rplKill:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s KILL %s A %s", args[0], s.upstreamLauncher.Self.Nick, args[1]))
|
2016-05-07 05:52:27 +00:00
|
|
|
|
2016-05-05 07:11:40 +00:00
|
|
|
case rplMsg:
|
|
|
|
for _, itm := range strings.Split(args[2], "\n") {
|
2016-05-07 05:51:45 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s PRIVMSG %s :%s", args[0], args[1], itm))
|
2016-05-05 07:11:40 +00:00
|
|
|
}
|
2016-05-07 05:52:27 +00:00
|
|
|
|
2016-05-05 07:11:40 +00:00
|
|
|
case rplList:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 322 %s %s", s.name, s.upstreamLauncher.Self.Nick, args[0]))
|
2016-05-05 07:11:40 +00:00
|
|
|
case rplListEnd:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 323 %s", s.name, s.upstreamLauncher.Self.Nick))
|
2016-05-07 05:52:27 +00:00
|
|
|
|
2016-05-05 07:11:40 +00:00
|
|
|
case rplOper:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 381 %s :You are now an operator", s.name, s.upstreamLauncher.Self.Nick))
|
2016-05-05 07:11:40 +00:00
|
|
|
case rplChannelModeIs:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 324 %s %s %s %s", s.name, s.upstreamLauncher.Self.Nick, args[0], args[1], args[2]))
|
2016-05-05 07:11:40 +00:00
|
|
|
case rplKick:
|
|
|
|
s.writeClient(fmt.Sprintf(":%s KICK %s %s %s", args[0], args[1], args[2], args[3]))
|
|
|
|
case rplInfo:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 371 %s :%s", s.name, s.upstreamLauncher.Self.Nick, args[0]))
|
2016-05-05 07:11:40 +00:00
|
|
|
case rplVersion:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 351 %s %s", s.name, s.upstreamLauncher.Self.Nick, args[0]))
|
2016-05-05 07:11:40 +00:00
|
|
|
case rplMOTDStart:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 375 %s :- Message of the day - ", s.name, s.upstreamLauncher.Self.Nick))
|
2016-05-05 07:11:40 +00:00
|
|
|
case rplMOTD:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 372 %s :- %s", s.name, s.upstreamLauncher.Self.Nick, args[0]))
|
2016-05-05 07:11:40 +00:00
|
|
|
case rplEndOfMOTD:
|
2016-05-07 05:07:16 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 376 %s :- End of MOTD", s.name, s.upstreamLauncher.Self.Nick))
|
2016-05-05 07:11:40 +00:00
|
|
|
case rplPong:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s PONG %s %s", s.name, s.upstreamLauncher.Self.Nick, s.name))
|
2016-05-05 07:11:40 +00:00
|
|
|
case errMoreArgs:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 461 %s :Not enough params", s.name, s.upstreamLauncher.Self.Nick))
|
2016-05-05 07:11:40 +00:00
|
|
|
case errNoNick:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 431 %s :No nickname given", s.name, s.upstreamLauncher.Self.Nick))
|
2016-05-05 07:11:40 +00:00
|
|
|
case errInvalidNick:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 432 %s %s :Erronenous nickname", s.name, s.upstreamLauncher.Self.Nick, args[0]))
|
2016-05-05 07:11:40 +00:00
|
|
|
case errNickInUse:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 433 %s %s :Nick already in use", s.name, s.upstreamLauncher.Self.Nick, args[0]))
|
2016-05-05 07:11:40 +00:00
|
|
|
case errAlreadyReg:
|
|
|
|
s.writeClient(fmt.Sprintf(":%s 462 :You need a valid nick first", s.name))
|
|
|
|
case errNoSuchNick:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 401 %s %s :No such nick/channel", s.name, s.upstreamLauncher.Self.Nick, args[0]))
|
2016-05-05 07:11:40 +00:00
|
|
|
case errUnknownCommand:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 421 %s %s :Unknown command", s.name, s.upstreamLauncher.Self.Nick, args[0]))
|
2016-05-05 07:11:40 +00:00
|
|
|
case errNotReg:
|
2016-05-07 05:07:16 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 451 :- You have not registered", s.name))
|
2016-05-05 07:11:40 +00:00
|
|
|
case errPassword:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 464 %s :Error, password incorrect", s.name, s.upstreamLauncher.Self.Nick))
|
2016-05-05 07:11:40 +00:00
|
|
|
case errNoPriv:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 481 %s :Permission denied", s.name, s.upstreamLauncher.Self.Nick))
|
2016-05-05 07:11:40 +00:00
|
|
|
case errCannotSend:
|
2016-05-05 07:45:59 +00:00
|
|
|
s.writeClient(fmt.Sprintf(":%s 404 %s %s :Cannot send to channel", s.name, s.upstreamLauncher.Self.Nick, args[0]))
|
2016-05-05 07:11:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) writeClient(output string) {
|
|
|
|
if s.clientConn == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-07 01:55:29 +00:00
|
|
|
s.verbosef(" <<< %s", output)
|
|
|
|
|
2016-05-05 07:11:40 +00:00
|
|
|
s.clientConn.SetWriteDeadline(time.Now().Add(time.Second * 30))
|
|
|
|
if _, err := fmt.Fprintf(s.clientConn, "%s\r\n", output); err != nil {
|
|
|
|
s.DisconnectClient()
|
|
|
|
return
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
|
|
|
}
|