Advertisement
Guest User

Untitled

a guest
May 27th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. Channel = {"#chat": channel}
  3.  
  4. channel.eventBuffer[] = {event, text}
  5. channel.users
  6. channel.topic
  7.  
  8. */
  9.  
  10. var Channel = Class.create({
  11.    
  12.     users: [],
  13.     topic: "",
  14.     buffer: [],
  15.    
  16.     initialize: function() {
  17.         self = this;
  18.     },
  19. })
  20.  
  21.  
  22. var Client = Class.create({
  23.    
  24.     self: null,
  25.     socket: null,
  26.     buffer: "",
  27.     nickname: null,
  28.    
  29.     initialize: function() {
  30.         self = this;
  31.     },
  32.    
  33.     connect: function(host, nickname) {
  34.         this.nickname = nickname
  35.         this.socket = new WebSocket(host);
  36.         this.socket.type =
  37.         this.socket.onopen = this.onOpen;
  38.         this.socket.onclose = this.onClose;
  39.         this.socket.onmessage = this.onMessage;
  40.         this.socket.onerror = this.onError;
  41.     },
  42.    
  43.     disconnect: function() {
  44.         console.log(e)
  45.     },
  46.    
  47.     onError: function(e) {
  48.         console.log("error", e)
  49.     },
  50.    
  51.     onMessage: function(e) {
  52.         var lines = (self.buffer + e.data).split("\r\n");
  53.         self.buffer = lines.pop();
  54.  
  55.         lines.each(function(line) {
  56.             var prefix="", args=[];
  57.            
  58.             if (!line.startsWith(":")) {
  59.                 return;
  60.             }
  61.             if (line[0] == ':') {
  62.                 var temp = line.split(" ");
  63.                 prefix = temp[0].substr(1);
  64.                 temp.shift();
  65.                 line = temp.join(" ")
  66.             }
  67.             if (line.search(" :") != -1) {
  68.                 var temp = line.split(" :");
  69.                 var trailing = temp[1];
  70.                 args = temp[0].split(" ")
  71.                 args.push(trailing);
  72.             } else {
  73.                 args = line.split();
  74.             }
  75.             var command = args[0];
  76.             args.shift();
  77.             self.onCommandMessage(prefix, command, args);
  78.         });
  79.     },
  80.    
  81.     onOpen: function(e) {
  82.         self.sendRaw("NICK " + self.nickname);
  83.         self.sendRaw("USER " + self.nickname + " " + self.nickname + " " + self.nickname + " " + self.nickname);
  84.     },
  85.    
  86.     onClose: function(e) {
  87.         console.log("close", e)
  88.     },
  89.    
  90.     send: function(command, args, trail) {
  91.         this.sendRaw(line)
  92.     },
  93.    
  94.     sendRaw: function(line) {
  95.         this.socket.send(line + "\r\n")
  96.     },
  97.    
  98.     onCommandMessage: function(sender, command, arguments) {
  99.         switch(command) {
  100.             case "001":
  101.                 this.ircWelcome();
  102.                 break
  103.             case "JOIN":
  104.                 this.doJoin();
  105.                 break;
  106.             case "PART":
  107.                 this.doPart();
  108.                 break
  109.             case "PRIVMSG":
  110.                 this.doPrivmsg();
  111.                 break
  112.             case "NOTICE":
  113.                 this.doNotice();
  114.                 break
  115.             case "QUIT":
  116.                 this.doQuit();
  117.                 break
  118.             case "NICK":
  119.                 this.doNick();
  120.                 break
  121.             default:
  122.                 console.log("Discarding ", sender, command, arguments)
  123.         }
  124.     },
  125.    
  126.     ircWelcome: function() {
  127.         console.log("welcome");
  128.     },
  129.    
  130.     ircJoin: function(user, channel) {
  131.         console.log(user, channel);
  132.     },
  133.    
  134.     ircPart: function(user, channel, reason) {
  135.         console.log(user, channel, reason);
  136.     },
  137.    
  138.     ircQuit: function(user, reason) {
  139.         console.log(user, channel);
  140.     },
  141.    
  142.     ircNick: function(user, oldnick, newnick) {
  143.         console.log(user, oldnick, newnick);
  144.     },
  145.    
  146.     ircPrivmsg: function(user, target, message) {
  147.         console.log(user, target, message);
  148.     },
  149.    
  150.     ircNotice: function(user, target, message) {
  151.         console.log(user, channel, message);
  152.     },
  153. });
  154.  
  155. var client = new Client();
  156. client.connect("ws://chat.kevelbreh.co.za:8889", "websocket-user")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement