package main import ( "fmt" "libnmdc" "net" "strings" ) type Server struct { eventChan chan Event running bool name string client *Client channel Channel // Single blessed channel upstreamAddr libnmdc.HubAddress upstream libnmdc.HubConnection motd string } func NewServer(name string, upstream libnmdc.HubAddress) *Server { return &Server{eventChan: make(chan Event), name: name, client: nil, motd: "Connected to " + name, upstreamAddr: upstream, channel: Channel{ clientMap: make(map[string]*Client), modeMap: make(map[string]*ClientMode), }, } } func (s *Server) Run() { for event := range s.eventChan { s.handleEvent(event) } } func (s *Server) HandleConnection(conn net.Conn) { client := &Client{server: s, connection: conn, outputChan: make(chan string), signalChan: make(chan signalCode, 3), connected: true, } go client.clientThread() } func (s *Server) handleEvent(e Event) { 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) case disconnected: //Client disconnected case command: //Client send a command fields := strings.Fields(e.input) if len(fields) < 1 { return } if strings.HasPrefix(fields[0], ":") { fields = fields[1:] } command := strings.ToUpper(fields[0]) args := fields[1:] s.handleCommand(e.client, command, args) } } func (s *Server) handleCommand(client *Client, command string, args []string) { switch command { case "PING": client.reply(rplPong) case "INFO": client.reply(rplInfo, APP_DESCRIPTION) case "VERSION": client.reply(rplVersion, VERSION) case "NICK": client.reply(rplKill, "Can't change nicks on this server", "") client.disconnect() case "USER": if client.registered == true { client.reply(rplKill, "You're already registered.", "") client.disconnect() } if client.nick == "" { client.reply(rplKill, "Your nickname is already being used", "") client.disconnect() } else { client.reply(rplWelcome) client.registered = true // Spawn } case "JOIN": if client.registered == false { client.reply(errNotReg) return } if len(args) < 1 { client.reply(errMoreArgs) return } switch args[0] { case BLESSED_CHANNEL: // That's fine, but they're already there case "0": // Intend to quit all channels, but we don't allow that default: client.reply(rplKill, "There is only '"+BLESSED_CHANNEL+"'.", "") client.disconnect() } case "PART": if client.registered == false { client.reply(errNotReg) 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 case "PRIVMSG": if client.registered == false { client.reply(errNotReg) return } if len(args) < 2 { client.reply(errMoreArgs) return } message := strings.Join(args[1:], " ") // IRC is case-insensitive case-preserving. We can respect that for the // channel name, but not really for user nicks recipient := strings.ToLower(args[0]) if recipient == BLESSED_CHANNEL { for _, c := range s.channel.clientMap { if c != client { c.reply(rplMsg, client.nick, args[0], message) } } } else if nmdcUser, clientExists := s.upstream.Users[args[0]]; clientExists { s.upstream.SayPrivate(recipient, message) // client2.reply(rplMsg, client.nick, client2.nick, message) } else { client.reply(errNoSuchNick, args[0]) } case "QUIT": if client.registered == false { client.reply(errNotReg) return } client.disconnect() case "TOPIC": if client.registered == false { client.reply(errNotReg) return } if len(args) < 1 { client.reply(errMoreArgs) return } exists := strings.ToLower(args[0]) == BLESSED_CHANNEL if exists == false { client.reply(errNoSuchNick, args[0]) return } // Valid topic get if len(args) == 1 { client.reply(rplTopic, BLESSED_CHANNEL, s.upstream.HubName) return } // Disallow topic set client.reply(errNoPriv) return case "LIST": if client.registered == false { client.reply(errNotReg) return } listItem := fmt.Sprintf("%s %d :%s", BLESSED_CHANNEL, len(s.channel.clientMap), s.upstream.HubName) client.reply(rplList, listItem) client.reply(rplListEnd) case "OPER": if client.registered == false { client.reply(errNotReg) return } if len(args) < 2 { client.reply(errMoreArgs) return } //username := args[0] //password := args[1] if false { // op the user client.operator = true client.reply(rplOper) return } else { client.reply(errPassword) } case "KILL": if client.registered == false { client.reply(errNotReg) return } client.reply(errNoPriv) return case "KICK": if client.registered == false { client.reply(errNotReg) return } client.reply(errNoPriv) return case "MODE": if client.registered == false { client.reply(errNotReg) return } if len(args) < 1 { client.reply(errMoreArgs) return } if strings.ToLower(args[0]) != BLESSED_CHANNEL { client.reply(errNoSuchNick, args[0]) return } if len(args) == 1 { //No more args, they just want the mode client.reply(rplChannelModeIs, args[0], BLESSED_CHANNEL_MODE, "") } else { // Setting modes is disallowed client.reply(errNoPriv) } return default: client.reply(errUnknownCommand, command) } }