]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/irc/ircconnection.js
1e5604f23175c029b581ba898a3c592b821470b6
[irc/quakenet/qwebirc.git] / js / irc / ircconnection.js
1 /* This could do with a rewrite from scratch. */
2
3 qwebirc.irc.IRCConnection = new Class({
4 Implements: [Events, Options],
5 options: {
6 initialNickname: "ircconnX",
7 timeout: 30000,
8 floodInterval: 250,
9 floodMax: 5,
10 errorAlert: true
11 },
12 initialize: function(options) {
13 this.setOptions(options);
14
15 this.initialNickname = this.options.initialNickname;
16
17 this.counter = 0;
18 this.disconnected = false;
19
20 this.lastActiveRequest = 0;
21 this.floodCounter = 0;
22
23 this.activerequest = null;
24 this.timeoutid = null;
25 },
26 __error: function(text) {
27 this.fireEvent("error", text);
28 if(this.options.errorAlert)
29 alert(text);
30 },
31 newRequest: function(url, onComplete, floodProtection) {
32 if(floodProtection) {
33 var t = new Date().getTime();
34
35 if(t - this.lastActiveRequest < this.options.floodInterval) {
36 if(this.floodCounter++ >= this.options.floodMax) {
37 if(!this.disconnected) {
38 this.disconnect();
39 this.__error("BUG: uncontrolled flood detected -- disconnected.");
40 return {"send": function() { }, "cancel": function() { }};
41 }
42 }
43 }
44 this.lastActiveRequest = t;
45 }
46
47 var r = new Request.JSON({
48 url: "/e/" + url + "?r=" + this.cacheAvoidance + "&t=" + this.counter++,
49 onComplete: onComplete
50 });
51
52 /* try to minimise the amount of headers */
53 r.headers = new Hash;
54 if(Browser.Engine.trident) {
55 r.addEvent("request", function() {
56 this.setRequestHeader("Cookie", "");
57 this.setRequestHeader("Cookie", "");
58 }.bind(r.xhr));
59 }
60
61 if(Browser.Engine.trident)
62 r.setHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
63
64 return r;
65 },
66 send: function(data) {
67 if(this.disconnected)
68 return false;
69 var r = this.newRequest("p", function(o) {
70 if(!o || (o[0] == false)) {
71 if(!this.disconnected) {
72 this.disconnected = true;
73 this.__error("An error occured: " + o[1]);
74 }
75 return false;
76 }
77 }.bind(this));
78 r.send("s=" + this.sessionid + "&c=" + encodeURIComponent(data));
79 return true;
80 },
81 __timeout: function() {
82 if(this.lastactiverequest) {
83 this.lastactiverequest.cancel();
84 this.lastactiverequest = null;
85 }
86 if(this.activerequest) {
87 this.lastactiverequest = this.activerequest;
88 /*this.activerequest.cancel();
89 this.activerequest = null;*/
90 }
91 if($defined(this.timeoutid)) {
92 $clear(this.timeoutid);
93 this.timeoutid = null;
94 }
95 this.recv();
96 },
97 recv: function() {
98 var r = this.newRequest("s", function(o) {
99 if(this.lastactiverequest != r)
100 this.activerequest = null;
101
102 if($defined(this.timeoutid)) {
103 $clear(this.timeoutid);
104 this.timeoutid = null;
105 }
106
107 if(o) {
108 if(this.lastactiverequest == r)
109 this.lastactiverequest = null;
110 this.lasttry = false;
111 if(o[0] == false) {
112 if(!this.disconnected) {
113 this.disconnected = true;
114
115 this.__error("An error occured: " + o[1]);
116 }
117 return;
118 }
119 o.each(function(x) {
120 this.fireEvent("recv", [x]);
121 }, this);
122 } else {
123 if(this.lastactiverequest == r) {
124 this.lastactiverequest = null;
125 return;
126 }
127 if(!this.disconnected) {
128 if(this.lasttry) {
129 this.disconnected = true;
130
131 this.__error("Error: the server closed the connection.");
132 return;
133 } else {
134 this.lasttry = true;
135 }
136 }
137 }
138
139 this.recv();
140 }.bind(this), true);
141
142 if(this.options.timeout)
143 this.timeoutid = this.__timeout.delay(this.options.timeout, this);
144
145 this.activerequest = r;
146 r.send("s=" + this.sessionid);
147 },
148 connect: function() {
149 this.cacheAvoidance = qwebirc.util.randHexString(16);
150
151 var r = this.newRequest("n", function(o) {
152 if(!o) {
153 this.disconnected = true;
154 this.__error("Couldn't connect to remote server.");
155 return;
156 }
157 if(o[0] == false) {
158 this.disconnected = true;
159 this.__error("An error occured: " + o[1]);
160 return;
161 }
162 this.sessionid = o[1];
163
164 this.recv();
165 }.bind(this));
166
167 r.send("nick=" + encodeURIComponent(this.initialNickname));
168 },
169 disconnect: function() {
170 this.disconnected = true;
171 }
172 });