public override string HandleCommand(IRCCommandBase command, Session session) { if (command is NICKCommand) { NICKCommand nickCommand = (NICKCommand)command; if (nickCommand.NickName == null) { return(Errors.GetErrorResponse(ErrorCode.ERR_NONICKNAMEGIVEN, null)); } else { foreach (Session S in ServerBackend.Instance.ClientSessions) { if (S.User != null) { if (S.User.Nickname == nickCommand.NickName) { // if (S.Socket != session.Socket) return(Errors.GetErrorResponse(ErrorCode.ERR_NICKNAMEINUSE, nickCommand.NickName)); } } } } foreach (User user in ServerBackend.Instance.Users) { if (user == session.User) { if (user.Realname != null) { user.Nickname = nickCommand.NickName; session.User.Nickname = nickCommand.NickName; session.ConnectionState = ConnectionState.Registered; return("OK"); } else { user.Nickname = nickCommand.NickName; session.User.Nickname = nickCommand.NickName; session.ConnectionState = ConnectionState.NotRegistered; return("OK"); } } } if (session.User == null) { session.User = new User(); session.User.Nickname = nickCommand.NickName; ServerBackend.Instance.Users.Add(session.User); } return("OK"); } else { throw new ArgumentException(); } }
public override string HandleCommand(IRCCommandBase command, Session session) { if (command is PRIVMSGCommand) { if (session.ConnectionState == ConnectionState.NotRegistered) { return("Unregistered user"); } PRIVMSGCommand privmsgCommand = (PRIVMSGCommand)command; if (privmsgCommand.Targets.Count == 0) { return(Errors.GetErrorResponse(ErrorCode.ERR_NORECIPIENT, "privmsg")); } if (privmsgCommand.Message == null) { return(Errors.GetErrorResponse(ErrorCode.ERR_NOTEXTTOSEND, null)); } bool found = false; for (int i = 0; i < privmsgCommand.Targets.Count; i++) { foreach (Session s in ServerBackend.Instance.ClientSessions) { if (s.User.Nickname == privmsgCommand.Targets[i]) { if (s.ConnectionState == ConnectionState.Registered) { found = true; } break; } } if (!found) { return(Errors.GetErrorResponse(ErrorCode.ERR_NOSUCHNICK, privmsgCommand.Targets[i])); } found = false; } for (int i = 0; i < privmsgCommand.Targets.Count; i++) { foreach (Session s in ServerBackend.Instance.ClientSessions) { if (s.User.Nickname == privmsgCommand.Targets[i]) { s.Socket.Send(Encoding.ASCII.GetBytes(":" + session.User.Nickname + " PRIVMSG :" + privmsgCommand.Message)); } } } return("OK"); } else { throw new ArgumentException(); } }
public override string HandleCommand(IRCCommandBase command, Session session) { if (command is QUITCommand) { QUITCommand quitCommand = (QUITCommand)command; ServerBackend.Instance.Users.Remove(session.User); session.ConnectionState = ConnectionState.Destroyed; ServerBackend.Instance.ClientSessions.Remove(session); return("OK"); } else { throw new ArgumentException(); } }
public abstract string HandleCommand(IRCCommandBase command, Session session);
public override string HandleCommand(IRCCommandBase command, Session session) { if (command is USERCommand) { USERCommand userCommand = (USERCommand)command; if (userCommand.UserName == null || userCommand.HostName == null || userCommand.ServerName == null || userCommand.RealName == null) { return(Errors.GetErrorResponse(ErrorCode.ERR_NEEDMOREPARAMS, "User")); } if (session.User != null) { if (session.User.Username != null) { return(Errors.GetErrorResponse(ErrorCode.ERR_ALREADYREGISTERED, null)); } } //foreach (User u in ServerBackend.Instance.Users) //{ // if (u.Username == userCommand.UserName) // { // return Errors.GetErrorResponse(ErrorCode.ERR_ALREADYREGISTERED, null); // } //} foreach (User user in ServerBackend.Instance.Users) { if (user == session.User) { if (user.Nickname != null) { session.User.Username = userCommand.UserName; session.User.Hostname = userCommand.HostName; session.User.Realname = userCommand.RealName; session.ConnectionState = ConnectionState.Registered; return("OK"); } else { session.User.Username = userCommand.UserName; session.User.Hostname = userCommand.HostName; session.User.Realname = userCommand.RealName; session.ConnectionState = ConnectionState.NotRegistered; return("OK"); } } } if (session.User == null) { session.User = new User(); session.User.Username = userCommand.UserName; session.User.Hostname = userCommand.HostName; session.User.Realname = userCommand.RealName; session.ConnectionState = ConnectionState.NotRegistered; ServerBackend.Instance.Users.Add(session.User); } return("OK"); } else { throw new ArgumentException(); } }