]>
jfr.im git - irc/quakenet/qwebirc.git/blob - js/irc/ircconnection.js
1e5604f23175c029b581ba898a3c592b821470b6
1 /* This could do with a rewrite from scratch. */
3 qwebirc
.irc
.IRCConnection
= new Class({
4 Implements: [Events
, Options
],
6 initialNickname: "ircconnX",
12 initialize: function(options
) {
13 this.setOptions(options
);
15 this.initialNickname
= this.options
.initialNickname
;
18 this.disconnected
= false;
20 this.lastActiveRequest
= 0;
21 this.floodCounter
= 0;
23 this.activerequest
= null;
24 this.timeoutid
= null;
26 __error: function(text
) {
27 this.fireEvent("error", text
);
28 if(this.options
.errorAlert
)
31 newRequest: function(url
, onComplete
, floodProtection
) {
33 var t
= new Date().getTime();
35 if(t
- this.lastActiveRequest
< this.options
.floodInterval
) {
36 if(this.floodCounter
++ >= this.options
.floodMax
) {
37 if(!this.disconnected
) {
39 this.__error("BUG: uncontrolled flood detected -- disconnected.");
40 return {"send": function() { }, "cancel": function() { }};
44 this.lastActiveRequest
= t
;
47 var r
= new Request
.JSON({
48 url: "/e/" + url
+ "?r=" + this.cacheAvoidance
+ "&t=" + this.counter
++,
49 onComplete: onComplete
52 /* try to minimise the amount of headers */
54 if(Browser
.Engine
.trident
) {
55 r
.addEvent("request", function() {
56 this.setRequestHeader("Cookie", "");
57 this.setRequestHeader("Cookie", "");
61 if(Browser
.Engine
.trident
)
62 r
.setHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
66 send: function(data
) {
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]);
78 r
.send("s=" + this.sessionid
+ "&c=" + encodeURIComponent(data
));
81 __timeout: function() {
82 if(this.lastactiverequest
) {
83 this.lastactiverequest
.cancel();
84 this.lastactiverequest
= null;
86 if(this.activerequest
) {
87 this.lastactiverequest
= this.activerequest
;
88 /*this.activerequest.cancel();
89 this.activerequest = null;*/
91 if($defined(this.timeoutid
)) {
92 $clear(this.timeoutid
);
93 this.timeoutid
= null;
98 var r
= this.newRequest("s", function(o
) {
99 if(this.lastactiverequest
!= r
)
100 this.activerequest
= null;
102 if($defined(this.timeoutid
)) {
103 $clear(this.timeoutid
);
104 this.timeoutid
= null;
108 if(this.lastactiverequest
== r
)
109 this.lastactiverequest
= null;
110 this.lasttry
= false;
112 if(!this.disconnected
) {
113 this.disconnected
= true;
115 this.__error("An error occured: " + o
[1]);
120 this.fireEvent("recv", [x
]);
123 if(this.lastactiverequest
== r
) {
124 this.lastactiverequest
= null;
127 if(!this.disconnected
) {
129 this.disconnected
= true;
131 this.__error("Error: the server closed the connection.");
142 if(this.options
.timeout
)
143 this.timeoutid
= this.__timeout
.delay(this.options
.timeout
, this);
145 this.activerequest
= r
;
146 r
.send("s=" + this.sessionid
);
148 connect: function() {
149 this.cacheAvoidance
= qwebirc
.util
.randHexString(16);
151 var r
= this.newRequest("n", function(o
) {
153 this.disconnected
= true;
154 this.__error("Couldn't connect to remote server.");
158 this.disconnected
= true;
159 this.__error("An error occured: " + o
[1]);
162 this.sessionid
= o
[1];
167 r
.send("nick=" + encodeURIComponent(this.initialNickname
));
169 disconnect: function() {
170 this.disconnected
= true;