]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/irc/ircconnection.js
375fbf8d0102d880ddb80926cefba116a37b35c4
[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 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.options.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 }
47 if(this.activerequest) {
48 this.lastactiverequest = this.activerequest;
49 /*this.activerequest.cancel();
50 this.activerequest = null;*/
51 }
52 if($defined(this.timeoutid)) {
53 $clear(this.timeoutid);
54 this.timeoutid = null;
55 }
56 this.recv();
57 },
58 recv: function() {
59 var r = new Request.JSON({url: "/e/s/" + this.sessionid + "?t=" + this.counter++, onComplete: function(o) {
60 if(this.lastactiverequest != r)
61 this.activerequest = null;
62
63 if($defined(this.timeoutid)) {
64 $clear(this.timeoutid);
65 this.timeoutid = null;
66 }
67
68 if(o) {
69 if(this.lastactiverequest == r)
70 this.lastactiverequest = null;
71 this.lasttry = false;
72 if(o[0] == false) {
73 if(!this.disconnected) {
74 this.disconnected = true;
75
76 this.__error("An error occured: " + o[1]);
77 }
78 return;
79 }
80 o.each(function(x) {
81 this.fireEvent("recv", [x]);
82 }, this);
83 } else {
84 if(this.lastactiverequest == r) {
85 this.lastactiverequest = null;
86 return;
87 }
88 if(!this.disconnected) {
89 if(this.lasttry) {
90 this.disconnected = true;
91
92 this.__error("Error: the server closed the connection.");
93 return;
94 } else {
95 this.lasttry = true;
96 }
97 }
98 }
99
100 this.recv();
101 }.bind(this)});
102
103 if(this.options.timeout)
104 this.timeoutid = this.__timeout.delay(this.options.timeout, this);
105
106 this.activerequest = r;
107 r.get();
108 },
109 connect: function() {
110 var r = new Request.JSON({url: "/e/n?nick=" + encodeURIComponent(this.initialNickname) + "&r=" + Math.random() * 1024 * 1024, onComplete: function(o) {
111 if(!o) {
112 this.disconnected = true;
113 this.__error("Couldn't connect to remote server.");
114 return;
115 }
116 if(o[0] == false) {
117 this.disconnected = true;
118 this.__error("An error occured: " + o[1]);
119 return;
120 }
121 this.sessionid = o[1];
122
123 this.recv();
124 }.bind(this)});
125 r.post();
126 },
127 disconnect: function() {
128 this.disconnected = true;
129 }
130 });