]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/irc/ircconnection.js
619251b99ccb53f6c2b332166a6f4a69b1968574
[irc/quakenet/qwebirc.git] / js / irc / ircconnection.js
1 /* This could do with a rewrite from scratch. */
2
3 var IRCConnection = new Class({
4 Implements: [Events, Options],
5 options: {
6 initialNickname: "ircconnX",
7 timeout: 30000,
8 errorAlert: true
9 },
10 initialize: function(options) {
11 this.setOptions(options);
12
13 this.initialNickname = this.options.initialNickname;
14
15 this.counter = 0;
16 this.disconnected = false;
17
18 this.activerequest = null;
19 this.timeoutid = null;
20 },
21 __error: function(text) {
22 this.fireEvent("error", text);
23 if(this.errorAlert)
24 alert(text);
25 },
26 send: function(data) {
27 if(this.disconnected)
28 return false;
29 var r = new Request.JSON({url: "/e/p/" + this.sessionid + "?c=" + encodeURIComponent(data) + "&t=" + this.counter++, onComplete: function(o) {
30 if(!o || (o[0] == false)) {
31 if(!this.disconnected) {
32 this.disconnected = true;
33 this.__error("An error occured: " + o[1]);
34 }
35 return false;
36 }
37 }.bind(this)});
38
39 r.get();
40 return true;
41 },
42 __timeout: function() {
43 if(this.lastactiverequest) {
44 this.lastactiverequest.cancel();
45 this.lastactiverequest = null;
46 alert("warning: last active request");
47 }
48 if(this.activerequest) {
49 this.lastactiverequest = this.activerequest;
50 /*this.activerequest.cancel();
51 this.activerequest = null;*/
52 }
53 if($defined(this.timeoutid)) {
54 $clear(this.timeoutid);
55 this.timeoutid = null;
56 }
57 this.recv();
58 },
59 recv: function() {
60 var r = new Request.JSON({url: "/e/s/" + this.sessionid + "?t=" + this.counter++, onComplete: function(o) {
61 if(this.lastactiverequest != r)
62 this.activerequest = null;
63
64 if($defined(this.timeoutid)) {
65 $clear(this.timeoutid);
66 this.timeoutid = null;
67 }
68
69 if(o) {
70 if(this.lastactiverequest == r)
71 this.lastactiverequest = null;
72 this.lasttry = false;
73 if(o[0] == false) {
74 if(!this.disconnected) {
75 this.disconnected = true;
76
77 this.__error("An error occured: " + o[1]);
78 }
79 return;
80 }
81 o.each(function(x) {
82 this.fireEvent("recv", [x]);
83 }, this);
84 } else {
85 if(this.lastactiverequest == r) {
86 this.lastactiverequest = null;
87 return;
88 }
89 if(!this.disconnected) {
90 if(this.lasttry) {
91 this.disconnected = true;
92
93 this.__error("Error: the server closed the connection.");
94 return;
95 } else {
96 this.lasttry = true;
97 }
98 }
99 }
100
101 this.recv();
102 }.bind(this)});
103
104 if(this.options.timeout)
105 this.timeoutid = this.__timeout.delay(this.options.timeout, this);
106
107 this.activerequest = r;
108 r.get();
109 },
110 connect: function() {
111 var r = new Request.JSON({url: "/e/n?nick=" + encodeURIComponent(this.initialNickname) + "&r=" + Math.random() * 1024 * 1024, onComplete: function(o) {
112 if(!o) {
113 this.disconnected = true;
114 alert("Couldn't connect to remote server.");
115 return;
116 }
117 if(o[0] == false) {
118 this.disconnected = true;
119 this.__error("An error occured: " + o[1]);
120 return;
121 }
122 this.sessionid = o[1];
123
124 this.recv();
125 }.bind(this)});
126 r.post();
127 },
128 disconnect: function() {
129 this.disconnected = true;
130 }
131 });