client.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Model and handle the clients of the server
  2. class Client
  3. include Telnet
  4. attr_reader :id
  5. attr_reader :buffer
  6. attr_reader :in_lines
  7. attr_accessor :account
  8. attr_accessor :character
  9. attr_accessor :mode
  10. def initialize(id)
  11. @id = id
  12. @buffer = ""
  13. @in_lines = []
  14. @account = nil
  15. @character= nil
  16. @mode = Mode::Setup.new(self)
  17. end
  18. def self.add(client_id)
  19. @clients ||= {}
  20. @clients[client_id] = Client.new(client_id)
  21. return @clients[client_id]
  22. end
  23. def self.get(client_id)
  24. @clients ||= {}
  25. return @clients[client_id]
  26. end
  27. def self.remove(client_id)
  28. @clients[client_id] = nil
  29. end
  30. def send_to_client(text)
  31. log "Client #{@id}, send_to_client #{text}"
  32. Woe::Server.send_to_client(@id, text)
  33. end
  34. def puts(text)
  35. log "Client #{@id}, puts #{text}"
  36. Woe::Server.puts(@id, text)
  37. end
  38. def printf(fmt, *args)
  39. if args && !args.empty?
  40. text = fmt.format(*args)
  41. else
  42. text = fmt
  43. end
  44. puts(text)
  45. end
  46. def raw_puts(text)
  47. log "Client #{@id}, puts #{text}"
  48. Woe::Server.raw_puts(@id, text)
  49. end
  50. def raw_printf(fmt, *args)
  51. if args
  52. text = fmt.format(*args)
  53. else
  54. text = fmt
  55. end
  56. puts(text)
  57. end
  58. def negotiate(how, what)
  59. log "Client #{@id} negotiate #{how} #{what}"
  60. Woe::Server.negotiate(@id, how, what)
  61. end
  62. def password_mode
  63. self.negotiate(TELNET_WILL, TELNET_TELOPT_ECHO)
  64. end
  65. def normal_mode
  66. self.negotiate(TELNET_WONT, TELNET_TELOPT_ECHO)
  67. end
  68. def on_negotiate(how, opt)
  69. if (opt == TELNET_TELOPT_COMPRESS2)
  70. if (how == TELNET_DO)
  71. log "Beginning compress2 mode"
  72. Woe::Server.begin_compress2(@id)
  73. end
  74. elsif (opt == TELNET_TELOPT_TELOPT_TTYPE)
  75. if (how == TELNET_WILL) || (opt == TELNET_DO)
  76. end
  77. end
  78. end
  79. def ask_type
  80. self.negotiate(TELNET_DO, TELNET_TELOPT_TTYPE
  81. end
  82. def on_start
  83. ask_ttype
  84. @mode.on_start
  85. end
  86. def on_input(str)
  87. @buffer ||= ""
  88. @buffer << str
  89. if @buffer["\r\n"]
  90. command, rest = @buffer.split("\r\n", 1)
  91. command.chomp!
  92. log "Client #{@id}, command #{command}"
  93. if (command.strip == "!quit")
  94. self.send_to_client("Bye bye!")
  95. Woe::Server.disconnect(@id)
  96. Client.remove(@id)
  97. return nil
  98. elsif (command.strip == "!load")
  99. log "Reloading main script."
  100. self.puts("Reloading main script.")
  101. script "main.rb"
  102. else
  103. @mode.do_command(command);
  104. end
  105. command = nil
  106. @buffer = rest
  107. end
  108. end
  109. end
  110. log "Mruby client script loaded OK."