File: telnetclient.tcl

package info (click to toggle)
sbnc 1.2-26
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,216 kB
  • sloc: cpp: 17,556; ansic: 15,514; sh: 13,419; tcl: 5,567; php: 448; makefile: 284
file content (356 lines) | stat: -rw-r--r-- 9,239 bytes parent folder | download | duplicates (3)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# shroudBNC - an object-oriented framework for IRC
# Copyright (C) 2005 Gunnar Beutner
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

internalbind command tclient:command
internalbind client tclient:client
internalbind attach tclient:attach
#internalbind server tclient:dccchat "\001DCC CHAT *\001"

proc tclient:command {client params} {
	set command [lindex $params 0]

	if {[string equal -nocase $command "addtelnet"]} {
		set session [lindex $params 1]
		set host [lindex $params 2]
		set port [lindex $params 3]

		if {$port == ""} {
			bncreply "Syntax: addtelnet <Name> <Host> <Port>"

			haltoutput
			return
		}

		if {[tclient:issession $session]} {
			bncreply "A session with that name already exists."

			haltoutput
			return
		}

		tclient:addsession $session $host $port

		bncreply "Done."
		haltoutput
	} elseif {[string equal -nocase $command "deltelnet"]} {
		set session [lindex $params 1]

		if {![tclient:issession $session]} {
			bncreply "There is no such session."

			haltoutput
			return
		}

		tclient:delsession $session

		bncreply "Done."
		haltoutput
	} elseif {[string equal -nocase $command "telnetsessions"]} {
		namespace eval [getns] {
			if {![info exists activesessions]} { set activesessions [list] }
		}

		upvar [getns]::activesessions activesessions

		foreach sessionobj [getbncuser [getctx] tag tsessions] {
			if {[lsearch $activesessions "[lindex $sessionobj 0] *"] == -1} {
				set status "Not active"
			} else {
				set status "Active"
			}

			bncreply "[lindex $sessionobj 0] - [lindex $sessionobj 1]:[lindex $sessionobj 2] - $status"
		}

		bncreply "End of SESSIONS."
		haltoutput
	} elseif {[string equal -nocase $command "help"]} {
		bncaddcommand "addtelnet" "User" "creates a new telnet session" "Syntax: addtelnet <Name> <Host> <Port>\nCreates a new telnet session."
		bncaddcommand "deltelnet" "User" "removes a telnet session" "Syntax: deltelnet <Name>\nRemoves a telnet session."
		bncaddcommand "telnetsessions" "User" "shows a list of telnet sessions" "Syntax: telnetsessions\nLists all telnet sessions."
	}
}

proc tclient:issession {session} {
	set sessions [getbncuser [getctx] tag tsessions]

	if {[lsearch $sessions "$session *"] != -1} {
		return 1
	} else {
		return 0
	}
}

proc tclient:addsession {session host port} {
	set sessionobj [list $session $host $port]
	set sessions [getbncuser [getctx] tag tsessions]

	lappend sessions $sessionobj
	setbncuser [getctx] tag tsessions $sessions

	tclient:checksessions
}

proc tclient:delsession {session} {
	namespace eval [getns] {
		if {![info exists activesessions]} { set activesessions [list] }
	}

	upvar [getns]::activesessions activesessions

	set sessions [getbncuser [getctx] tag tsessions]
	set sessionidx [lsearch $sessions "$session *"]

	if {$sessionidx == -1} { return }

	set sessions [lreplace $sessions $sessionidx $sessionidx]
	setbncuser [getctx] tag tsessions $sessions

	set sessionobj [lsearch -inline $activesessions "$session *"]

	if {$sessionobj != ""} {
		catch [list killdcc [lindex $sessionobj 2]]
	}

	tclient:checksessions
}

proc tclient:checksessions {} {
	namespace eval [getns] {
		if {![info exists activesessions]} { set activesessions [list] }
	}

	upvar [getns]::activesessions activesessions

	foreach sessionobj [getbncuser [getctx] tag tsessions] {
		if {[lsearch $activesessions "[lindex $sessionobj 0] *"] == -1} {
			utimer 5 [list tclient:connect [lindex $sessionobj 0]]
		}
	}

	foreach sessionobj $activesessions {
		if {[lsearch [getbncuser [getctx] tag tsessions] "[lindex $sessionobj 0] *"] == -1} {
			tclient:replyas [lindex $sessionobj 0] "\00303Disconnecting session."
		}
	}
}

proc tclient:replyas {session text} {
	global botnick

	set text [string map [list "\033\[1m" "" "\033\[0m" ""] $text]

	putclient ":-$session!telnet@session PRIVMSG $botnick :$text"
}

proc tclient:connect {session} {
	if {![tclient:issession $session]} { return }

	namespace eval [getns] {
		if {![info exists activesessions]} { set activesessions [list] }
	}

	upvar [getns]::activesessions activesessions

	if {[lsearch $activesessions "$session *"] != -1} { return }

	set sessionobj [lsearch -inline [getbncuser [getctx] tag tsessions] "$session *"]

	if {[lindex $sessionobj 1] == 0} {
		return
	}

	tclient:replyas [lindex $sessionobj 0] "\00303Reconnecting to [lindex $sessionobj 1]:[lindex $sessionobj 2]"

	set connection [connect [lindex $sessionobj 1] [lindex $sessionobj 2]]
	control $connection tclient:line

	lappend activesessions [list $session 0 $connection]

	return
}

proc tclient:disconnect {session} {
	namespace eval [getns] {
		if {![info exists activesessions]} { set activesessions [list] }
	}

	upvar [getns]::activesessions activesessions

	set index [lsearch $activesessions "$session *"]

	if {$index != -1} {
		set sessionobj [lindex $activesessions $index]
		catch [list killdcc [lindex $sessionobj 2]]

		set activesessions [lreplace $activesessions $index $index]

		if {[lindex $sessionobj 1]} {
			tclient:delsession [lindex $sessionobj 0]
		}

		tclient:replyas [lindex $sessionobj 0] "\00303Disconnected from telnet server."
	}

	tclient:checksessions
}

proc tclient:line {idx line} {
	set sessionobj ""

	foreach user [bncuserlist] {
		setctx $user

		namespace eval [getns] {
			if {![info exists activesessions]} { set activesessions [list] }
		}

		upvar [getns]::activesessions activesessions

		set sessionobj [lsearch -inline $activesessions "* $idx"]

		if {$sessionobj != ""} {
			break
		}
	}

	if {$sessionobj == ""} {
		return
	}

	setctx "$user<*"

	if {$line == ""} {
		tclient:disconnect [lindex $sessionobj 0]

		return
	}

	tclient:replyas [lindex $sessionobj 0] [sbnc:telnet2text $line]
}

proc tclient:client {client params} {
	global botnick

	if {[string equal -nocase [lindex $params 0] "PRIVMSG"]} {
		if {[string index [lindex $params 1] 0] != "-"} { return }
		set session [string range [lindex $params 1] 1 end]
		if {![tclient:issession $session]} { return }

		namespace eval [getns] {
			if {![info exists activesessions]} { set activesessions [list] }
		}

		upvar [getns]::activesessions activesessions
		set sessionobj [lsearch -inline $activesessions "$session *"]

		if {$sessionobj == ""} {
			tclient:checksessions
			return
		}

		if {[catch [list putdcc [lindex $sessionobj 2] [lindex $params 2]]]} {
			tclient:disconnect [lindex $sessionobj 0]
		}

		haltoutput
	} elseif {[string equal -nocase [lindex $params 0] "WHOIS"]} {
		if {[string index [lindex $params 1] 0] != "-"} { return }
		set session [string range [lindex $params 1] 1 end]
		if {![tclient:issession $session]} { return }

		set sessionobj [lsearch -inline [getbncuser [getctx] tag tsessions] "$session *"]

		set server [getbncuser [getctx] realserver]

		putclient ":$server 311 $botnick -[lindex $sessionobj 0] [lindex $sessionobj 2] [lindex $sessionobj 1] * :Telnet session"
		putclient ":$server 318 $botnick -[lindex $sessionobj 0] :End of /WHOIS list."

		haltoutput
	}
}

proc tclient:attach {client} {
	namespace eval [getns] {
		if {![info exists activesessions]} { set activesessions [list] }
	}

	upvar [getns]::activesessions activesessions

	foreach sessionobj $activesessions {
		tclient:replyas [lindex $sessionobj 0] "\00303Telnet session is active."
	}

	tclient:checksessions
}

proc sbnc:telnet2text {input} {
	set out [list]

	for {set i 0} {$i < [string length $input]} {incr i} {
		set chr [string index $input $i]

		if {[scan $chr "%c"] == 255} {
			incr i 2
		} else {
			lappend out $chr
		}
	}

	return [join $out ""]
}

proc tclient:createtempsession {session idx} {
	if {[tclient:issession $session]} { return }

	tclient:addsession $session 0 0

	namespace eval [getns] {
		if {![info exists activesessions]} { set activesessions [list] }
	}

	upvar [getns]::activesessions activesessions

	set index [lsearch $activesessions "$session *"]

	if {$index != -1} { return }

	lappend activesessions [list $session 1 $idx]

	control $idx tclient:line

	tclient:replyas $session "\00303DCC session is active."
}

proc tclient:dccchat {client params} {
	namespace eval [getns] {
		if {![info exists activesessions]} { set activesessions [list] }
	}

	upvar [getns]::activesessions activesessions

	if {![string equal -nocase [lindex $params 1] "PRIVMSG"]} { return }

	set ctcp [lindex $params 3]
	if {[catch [list int2ip [lindex [split $ctcp] 3]] ip]} { return }
	set port [string range [lindex [split $ctcp] 4] 0 end-1]

	tclient:createtempsession [lindex [split [lindex $params 0] "!"] 0]-[llength $activesessions] [connect $ip $port]

	haltoutput
}