File: misc.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 (256 lines) | stat: -rw-r--r-- 5,755 bytes parent folder | download | duplicates (2)
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
# 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.

package require bnc 0.2

proc sbncnick {} {
	return [getcurrentnick]
}

proc isbotnick {nick} {
	return [string equal -nocase $nick [sbncnick]]
}

proc botisop {{channel ""}} {
	return [isop [sbncnick] $channel]
}

proc botishalfop {{channel ""}} {
	return [ishalfop [sbncnick] $channel]
}

proc botisvoice {{channel ""}} {
	return [isvoice [sbncnick] $channel]
}

proc botonchan {{channel ""}} {
	return [onchan [sbncnick] $channel]
}

proc unixtime {} {
	return [clock seconds]
}

proc ctime {timeval} {
	return [clock format $timeval]
}

proc strftime {formatstring {time ""}} {
	if {$time == ""} {
		return [clock format [clock seconds] -format $formatstring]
	} else {
		return [clock format $time -format $formatstring]
	}
}

proc putkick {channel nicks {reason ""}} {
	foreach nick [split $nicks ","] {
		putquick "KICK $channel $nick :$reason"
	}
}

proc bncnotc {text} {
	putclient ":-sBNC!core@shroudbnc.info PRIVMSG $::botnick :$text"
}

proc bncrnotc {text} {
	putclient ":-sBNC!core@shroudbnc.info NOTICE $::botnick :$text"
}

proc getchanidle {nick chan} {
	return [expr [internalgetchanidle $nick $chan] / 60]
}

proc duration {seconds} {
	set result ""

	set dur [expr 365*24*60*60]
	if {[format %.0f [expr $seconds/$dur]] != 0} {
		set years [format %.0f [expr $seconds/$dur]]
		if {$years == "1"} {
			set result "$result$years year "
		} {set result "$result$years years "}
		set seconds [expr $seconds - $years*$dur]
	}

	set dur [expr 7*24*60*60]
	if {[format %.0f [expr $seconds/$dur]] != 0} {
		set weeks [format %.0f [expr $seconds/$dur]]
		if {$weeks == "1"} {
			set result "$result$weeks week "
		} {set result "$result$weeks weeks "}
		set seconds [expr $seconds - $weeks*$dur]
	}

	set dur [expr 24*60*60]
	if {[format %.0f [expr $seconds/$dur]] != 0} {
		set days [format %.0f [expr $seconds/$dur]]
		if {$days == "1"} {
			set result "$result$days day "
		} {set result "$result$days days "}
		set seconds [expr $seconds - $days*$dur]
	}

	set dur [expr 60*60]
	if {[format %.0f [expr $seconds/$dur]] != 0} {
		set hours [format %.0f [expr $seconds/$dur]]
		if {$hours == "1"} {
			set result "$result$hours hour "
		} {set result "$result$hours hours "}
		set seconds [expr $seconds - $hours*$dur]
	}

	set dur 60
	if {[format %.0f [expr $seconds/$dur]] != 0} {
		set minutes [format %.0f [expr $seconds/$dur]]
		if {$minutes == "1"} {
			set result "$result$minutes minute "
		} {set result "$result$minutes minutes "}
		set seconds [expr $seconds - $minutes*$dur]
	}

	if {$seconds != 0} {
		if {$seconds == "1"} {
			set result "$result$seconds second "
		} {set result "$result$seconds seconds "}
	}

	if {$result == ""} {
		set result "0 seconds"
	}

	return $result
}

proc chanlist {channel {flags ""}} {
	set res [list]

	foreach nick [internalchanlist $channel] {
		if {$flags == "" || [matchattr [nick2hand $nick] $flags $channel]} {
			lappend res $nick
		}
	}

	return $res
}

proc nick2hand {nick {channel ""}} {
	if {($channel != "" && [onchan $nick $channel]) || [onchan $nick]} {
		return [finduser $nick![getchanhost $nick]]
	} else {
		return ""
	}
}

proc hand2nick {handle {channel ""}} {
	if {$channel != ""} {
		set channels [list $channel]
	} else {
		set channels [channels]
	}

	foreach chan $channels {
		foreach user [internalchanlist $chan] {
			set u [finduser $user![getchanhost $user]]

			if {[string equal $u $handle]} {
				return $user
			}
		}
	}

	return ""
}

proc handonchan {handle {channel ""}} {
	if {[hand2nick $handle $channel] != ""} {
		return 1
	} else {
		return 0
	}
}

proc putcmdlog {text} {
	putlog "(command) $text"
}

proc putxferlog {text} {
	putlog "(xfer) $text"
}

proc putloglev {level channel text} {
	putlog "(level: $level, channel: $channel) $text"
}

proc int2ip {num} {
	binary scan [binary format I $num] cccc a b c d

	return [format %d.%d.%d.%d [expr $a&255] [expr $b&255] [expr $c&255] [expr $d&255]]
}

proc ip2int {ip} {
	foreach {a b c d} [split $ip .] {
		break
	}

	return [expr ($a<<8+$b<<8+$c<<8)+$d]
}

proc stripcodes {flags text} {
	variable result

	regsub -all "\[\002\017\]" $text "" result
	regsub -all "\003\[0-9\]\[0-9\]?\(,\[0-9\]\[0-9\]?\)?" $result "" result	
	regsub -all "\[\003\017\026\037\]" $result "" result

	return $result
}

proc dnslookup {args} {
	if {[llength $args] < 2} {
		return -code error "wrong # args: shrould be dnslookup ip-address/hostname proc ?arg1? ... ?argN?"
	}

	set host [lindex $args 0]
	set proc [lindex $args 1]
	set arg [lrange $args 2 end]

	if {[regexp {^\d*\.\d*\.\d*\.\d*$} $host]} {
		set reverse 1
		set ipv6 0
	} elseif {[string first ":" $host] != -1} {
		set reverse 1
		set ipv6 1
	} else {
		set reverse 0
		set ipv6 0
	}

	internaldnslookup $host dnslookup:callback $reverse $ipv6 [list $proc $arg]
}

proc dnslookup:callback {addr host status cookie} {
	set proc [lindex $cookie 0]

	set cmd [list $proc $addr $host $status]

	foreach arg [lindex $cookie 1] {
		lappend cmd $arg
	}

	eval $cmd
}