File: reconnect

package info (click to toggle)
epic5 3.0.3-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 5,328 kB
  • sloc: ansic: 75,810; makefile: 648; ruby: 227; python: 215; sh: 78; perl: 13
file content (283 lines) | stat: -rw-r--r-- 9,736 bytes parent folder | download
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
if (word(2 $loadinfo()) != [pf]) {
	load -pf $word(1 $loadinfo());
	return;
};

## We depend on 'reconnect_required' hook and '$serverctl(GET $server OPEN)',
## and '$serverctl(GET x NEXT_SERVER_IN_GROUP)' which are not available in
## previous versions.

if (info(i) < 1999 || J !~ [EPIC5*]) {
	xecho -b EPIC5 (rev. 1999) or newer is required;
	return;
};

load addset;

package reconnect;

## Reconnect script for EPIC5.
## Written by zlonix@efnet with help of hop@efnet, public domain.
## Help with testing by skered@efnet.
##
## Version: 1.0 (September, 2021)
##  - Initial roll-out
##
## Version 2.0 (Febrary, 2022)
##  - Major rewrite of script logic - now we don't use infinite timers,
##    but use re-scheduling - the timer schedules itself if appropriate.
##    Idea by hop@efnet.
##
## Version 3.0 (September, 2022)
##  - Uppon reconnect send one join line with all channels and keys instead
##    of bunch separate joins.

## This script do basic reconnection procedure for you, it hooks on newly
## implemented hook 'reconnect_required', it is thrown when network connection
## experience problems. For more details read UPDATES document.
##
## The script doesn't have complicated logic, for example if you fiddle with
## your windows while being in process of reconnection (killing them for
## example, or changing server association) unexpected things may happen,
## channels be joined not in correct windows, or not joined at all, be aware of
## that. Also don't change server refnums while in reconnect process - we rely
## on its consistency.
##
## Do not connect to more than one server per group. It's recommended to have
## sane ircII.servers file in your IRCLIB environment variable, with groups and
## all of that (on disconnect the script tries to reconnect to the server
## $auto_reconnect_retries times before trying next server in the group, looping
## over when tried the last one), also, note that Libera.chat supports supplying
## your password for the account upon connect, through password token in server
## description so you don't need to use sasl_auth script anymore. For details
## about server description and its tokens visit
## http://help.epicsol.org/server_description.
##
## If you use group <default> - reconnects will be made only to original server,
## no switching to other servers in the default group will happen.  Reconnection
## will unconditionally abort after $auto_reconnect_retries. To reconnect
## manually use /reconnect command or change the server with /server.
##
## Another limitation is that the script won't catch 'Connection refused', or
## any DNS errors, since it's not considered as action for 'reconnect_required'
## hook. It means, that if you start the client, and it can't connect to the
## specified server, reconnect won't happen.
##
## All of this has been sacrificed for simplicity of the implementation.
##
## If you encounter any bugs - please save the log with '/lastlog -file <path>'
## and report it to #epic@efnet.

## TODO
##
## Currently there is not much testing done with servers, which have round
## robin DNS, like irc.libera.chat.

addset auto_reconnect_delay int;
set auto_reconnect_delay 75;

addset auto_reconnect_join_delay int;
set auto_reconnect_join_delay 5;

addset auto_reconnect_delay_method bool;
set auto_reconnect_delay_method off;

addset auto_reconnect_retries int;
set auto_reconnect_retries 5;

addset auto_reconnect_try_other_servers bool;
set auto_reconnect_try_other_servers on;

addset auto_reconnect bool {
	if (*0 == [on]) {
		on #-server_state 100 '% % ACTIVE' {
			@ :group = serverctl(GET $0 GROUP);

			## If we use default group for more than one server
			## we need to encode server name as a tag for our
			## counters and variables, since user may be connected
			## to more than one server in default group, and when
			## we will experience disconnect for more than one server,
			## timers and variables will share encoded "<default>"
			## tag.
			##
			## We encode(), because default "<default>" group
			## isn't accepted as lval for auto_reconnect array
			if (group == [<default>]) {
				@ :group_enc = encode($serverctl(GET $0 NAME));
			} else {
				@ :group_enc = encode($serverctl(GET $0 GROUP));
			};

			## Stop reconnection timers on successful connect
			timer -delete auto_reconnect.$group_enc;
			^assign -auto_reconnect.failures[$group_enc];

			## Send one line with all channels to join
			## https://www.rfc-editor.org/rfc/rfc1459#section-4.2.1
			@ :chans = auto_reconnect[$group_enc][chans];
			@ :keys = auto_reconnect[$group_enc][keys];

			if (@chans) {
				if (auto_reconnect_delay_method == [on]) {
					@i = auto_reconnect_join_delay;
					fe chans chan {
						timer $i quote join $chan;
						@i++;
					};
				} else {
					quote join $sar(g/ /,/$chans) $sar(g/ /,/$keys);
				};
			};

		};
		on #-channel_claim 100 * {
			@ :group = serverctl(GET $0 GROUP);

			if (group == [<default>]) {
				@ :group_enc = encode($serverctl(GET $0 NAME));
			} else {
				@ :group_enc = encode($serverctl(GET $0 GROUP));
			};

			## If we can't find the channel in auto_reconnect array for the
			## group - we're not in reconnect situation, user just has joined
			## a channel on his own - do not proceed.
			@ :idx = findw($1 $auto_reconnect[$group_enc][chans]);
			if (idx == -1) {
				return;
			};

			@ :key = word($idx $auto_reconnect[$group_enc][keys]);
			@ :uuid = word($idx $auto_reconnect[$group_enc][wins]);
			@ :win = windowctl(REFNUM $uuid);

			if (windowctl(GET $win SERVER) == *0 && windowctl(GET $2 uuid) != uuid) {
				window $uuid claim $1;
			};

			@ ::auto_reconnect[$group_enc][chans] = notw($idx $auto_reconnect[$group_enc][chans]);
			@ ::auto_reconnect[$group_enc][keys] = notw($idx $auto_reconnect[$group_enc][keys]);
			@ ::auto_reconnect[$group_enc][wins] = notw($idx $auto_reconnect[$group_enc][wins]);

		};
		alias auto_reconnect.handler (server, void) {
			@ :server_name = serverctl(GET $server NAME);
			@ :group = serverctl(GET $server GROUP);

			if (group == [<default>]) {
				@ :group_enc = encode($serverctl(GET $server NAME));
			} else {
				@ :group_enc = encode($serverctl(GET $server GROUP));
			};

			## Check if we're reconnecting already
			if (timerctl(REFNUM auto_reconnect.$group_enc)) {
				return;
			};

			if (!serverctl(GET $server OPEN)) {
				@ :attempt = (auto_reconnect.failures[$group_enc] % auto_reconnect_retries) + 1;
				@ auto_reconnect.failures[$group_enc]++;

				if (attempt <= auto_reconnect_retries && auto_reconnect.overflow != 1) {
					## This is ugly hack for detecting when we must
					## try next server in a group
					@ auto_reconnect.overflow = (attempt == auto_reconnect_retries);

					xecho -b Autoreconnecting to $server_name [group $group] - attempt $attempt;
					server $server;
				} else {
					@ auto_reconnect.overflow = 0;

					if (auto_reconnect_try_other_servers == [on] && group != [<default>]) {
						@ :old_server_name = serverctl(GET $server NAME);
						@ :server = serverctl(GET $server NEXT_SERVER_IN_GROUP);
						@ :new_server_name = serverctl(GET $server NAME);

						xecho -b Maximum auto reconnect retries to server $old_server_name has been reached;
						xecho -b Switching to the next server in group $group - $new_server_name - attempt $attempt;
						server $server;
					} else {
						xecho -b Maximum auto reconnect retries failed - Use /RECONNECT to reconnect;
						^assign -auto_reconnect.failures[$group_enc];
						return;
					};
				};
			} else {
				xecho -b Timeout for autoreconnect retry has been reached, but you're still in the process of connection;
				xecho -b Scheduling another try in $auto_reconnect_delay seconds;
			};

			timer -server $server -refnum auto_reconnect.$group_enc $auto_reconnect_delay auto_reconnect.handler $server;
		};
		on #-reconnect_required 100 * {
			@ :server = serverctl(GET $0 NAME);
			@ :group = serverctl(GET $0 GROUP);

			if (group == [<default>]) {
				@ :group_enc = encode($serverctl(GET $0 NAME));
			} else {
				@ :group_enc = encode($serverctl(GET $0 GROUP));
			};

			xecho -b Connection to server $server [group $group] has been lost, autoreconnecting in $auto_reconnect_delay seconds;

			timer -server $0 -refnum auto_reconnect.$group_enc $auto_reconnect_delay auto_reconnect.handler $0;
		};
		on #-channel_lost 100 * {
			if (serverctl(GET $0 STATE) == [CLOSING]) {
				@ :group = serverctl(GET $0 GROUP);
				@ :uuid = windowctl(GET $2 UUID);

				if (group == [<default>]) {
					@ :group_enc = encode($serverctl(GET $0 NAME));
				} else {
					@ :group_enc = encode($group);
				};

				push auto_reconnect[$group_enc][chans] $1;
				push auto_reconnect[$group_enc][keys] $key($1);
				push auto_reconnect[$group_enc][wins] $uuid;
			};
		};
		alias lsreconnects (void) {
			xecho -v -b Currently reconnecting to following groups:;

			fe ($timerctl(REFNUMS)) timer {
				if (timer =~ [auto_reconnect.*]) {
					@ :group = decode($after(1 . $timer));

					xecho -v -b $group;
				};
			};
		};
		alias rmreconnect (void) {
			@ :server = windowctl(GET $winnum() SERVER);
			@ :group = serverctl(GET $server GROUP);

			if (group == [<default>]) {
				@ :group_enc = encode($serverctl(GET $server NAME));
			} else {
				@ :group_enc = encode($group);
			};

			xecho -v -b Canceling reconnect to $decode($group_enc);
			timer -delete auto_reconnect.$group_enc;
		};
	} else {
		fe ($timerctl(REFNUMS)) timer {
			if (timer =~ [auto_reconnect.*]) {
				timer -delete $timer;
			};
		};
		on #-server_state 100 -'% % ACTIVE';
		on #-reconnect_required 100 -*;
		on #-channel_lost 100 -*;
		alais -lsreconnects;
		alias -rmreconnect;
		alias -auto_reconnect.handler;
	};
};

set auto_reconnect on;