alexyoung (owner)

Revisions

gist: 112520 Download_button fork
public
Public Clone URL: git://gist.github.com/112520.git
jschat-client-rhino.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// JsChat client code using Rhino
 
// rlwrap java -jar js.jar
// load('jschat-client-rhino.js');
 
var JsChat = {};
 
JsChat.Connection = function(server_address, port) {
  importPackage(java.io);
  importPackage(java.net);
 
  this.server_address = server_address;
  this.port = port;
}
 
JsChat.Connection.prototype = {
  sleep: function(milliseconds) {
    java.lang.Thread.sleep(milliseconds);
  },
 
  connect: function() {
    print("Connecting to: " + this.server_address);
    this.socket = new Socket(this.server_address, this.port);
    this.connection_in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
    this.connection_out = new PrintWriter(this.socket.getOutputStream(), true);
  },
 
  write: function(text) {
    this.connection_out.println(text);
    this.connection_out.flush();
    this.sleep(50);
  },
 
  read: function() {
    return this.connection_in.readLine();
  },
 
  close: function() {
    this.connection_out.close();
    this.connection_in.close();
    this.socket.close();
  }
};
 
JsChat.Protocol = function(connection) {
  this.connection = connection;
}
 
JsChat.Protocol.prototype = {
  identify: function(name) {
    this.connection.write('{"identify":"' + name + '"}');
  },
 
  // Assumes a single room for now
  join: function(room) {
    this.room = room;
    this.connection.write('{"join":"' + room + '"}');
  },
 
  message: function(text) {
    this.connection.write('{"send":"' + text + '","to":"' + this.room + '"}');
  },
 
  quit: function() {
    this.connection.close();
  }
};
 
JsChat.Responses = {};
JsChat.Responses.Display = {
  'identified': function(json) {
    print('Identified as: ' + json['name']);
  },
 
  'join': function(json) {
    print('Joined room: ' + json['room']);
  },
 
  'error': function(json) {
    print('[ERROR] ' + json['message']);
  },
 
  'message': function(json) {
    if (json['room']) {
      print('[' + json['room'] + '] <' + json['user'] + '> ' + json['message']);
    }
  }
}
 
JsChat.Session = function(options) {
  importClass(java.lang.Thread, java.lang.Runnable);
  this.connection = new JsChat.Connection(options.server, options.port);
  this.connection.connect();
  this.send = new JsChat.Protocol(this.connection);
  this.user_responses = options.responses;
  this.runReaderThread();
}
 
JsChat.Session.prototype = {
  quit: function() {
    this.send.quit();
  },
 
  read: function() {
    return this.connection.read();
  },
 
  handeUserResponses: function(json) {
    for (var response in this.user_responses) {
      var matcher = response;
      var method = this.user_responses[response];
      var regex = new RegExp(matcher);
      if (json['message'].match(matcher)) {
        this.send.message(method(json));
        return true;
      }
    }
    return false;
  },
 
  runReaderThread: function() {
    var session = this;
    var r = new Runnable() {
      run: function() {
        var connected = true;
        while (connected) {
          try {
            var json = session.readInput();
            if (json && json.display && JsChat.Responses.Display[json.display]) {
              var options = json[json.display];
              JsChat.Responses.Display[json.display](options);
 
              if (json.display == 'message') {
                session.handeUserResponses(options);
              }
            }
          } catch (exception) {
            print(exception);
            connected = false;
          }
        }
      }
    }
    new Thread(r).start();
  },
 
  readInput: function() {
    return eval('(' + this.read() + ')');
  }
};
 
print('Connecting...');
 
var Responses = {
  'B\\^\\]': function(message) {
    return message['user'] + ': hello';
  },
 
  'bye': function(message) {
    return 'Bye ' + message['user'];
  }
}
 
var jschat = new JsChat.Session({ server: 'irc.helicoid.net', port: 6789, responses: Responses });
jschat.send.identify('B^]');
jschat.send.join('#jschat');
jschat.send.message("hello this is a test");