You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.1 KiB

  1. function User(socket, authenticatedDefault) {
  2. this.registered = false
  3. this.authenticated = authenticatedDefault
  4. this.nickname = undefined
  5. this.connection = socket
  6. this.realname = ""
  7. this.username = ""
  8. this.setNickname = function(nickname) {
  9. if (this.nickname !== undefined) {
  10. this.sendRaw(`:${this.nickname} NICK ${nickname}`)
  11. }
  12. this.nickname = nickname
  13. }
  14. this.getNickname = function(nickname) {
  15. return this.nickname
  16. }
  17. this.register = function(username, realname) {
  18. if (!this.authenticated) {
  19. this.connection.destroy()
  20. }
  21. this.username = username
  22. this.realname = realname
  23. this.registered = true
  24. }
  25. this.getAddress = function() {
  26. return this.connection.address().address
  27. }
  28. this.closeConnection = function () {
  29. this.connection.destroy()
  30. }
  31. this.sendMsg = function (from, message, to = this.nickname) {
  32. this.sendRaw(`:${from.nickname} PRIVMSG ${to} :${message}`, "ascii")
  33. }
  34. this.sendRaw = function(message) {
  35. this.connection.write(`${message}\r\n`, "ascii")
  36. }
  37. }
  38. module.exports = User