core.rb 10.8 KB
Newer Older
1 2 3
#!/usr/bin/env ruby

# Plugin that handles all the core functionality for the bot
4 5
class Core
	def initialize( status, config, output, irc, timer )
6 7 8 9 10
		@status  = status
		@config  = config
		@output  = output
		@irc     = irc
		@timer   = timer
11 12
	end

Cool Fire's avatar
Cool Fire committed
13
	# Messaging commands
Cool Fire's avatar
Cool Fire committed
14
	def message( nick, _user, host, _from, _msg, arguments, con )
15
		if( @config.auth( host, con ) )
Cool Fire's avatar
Cool Fire committed
16
			if( !arguments.nil? && !arguments.empty? )
17 18 19
				to, message = arguments.split( ' ', 2 )
			end
	
Cool Fire's avatar
Cool Fire committed
20
			if( !to.nil? && !to.empty? && !message.nil? && !message.empty? )
21 22 23 24 25 26 27 28 29 30 31
				@irc.message( to, message )
			else
				if( con )
					@output.cinfo( "Usage: message send_to message to send" )
				else
					@irc.notice( nick, "Usage: " + @config.command + "message send_to message to send" )
				end
			end
		end
	end

Cool Fire's avatar
Cool Fire committed
32
	def raw( nick, _user, host, _from, _msg, arguments, con )
Cool Fire's avatar
Cool Fire committed
33 34 35 36 37 38 39 40 41 42 43 44 45
		if( @config.auth( host, con ) )
			if( !arguments.nil? && !arguments.empty? )
				@irc.raw( arguments )
			else
				if( con )
					@output.cinfo( "Usage: raw line to send to IRC server" )
				else
					@irc.notice( nick, "Usage: " + @config.command + "raw line to send to IRC server" )
				end
			end
		end
	end

Cool Fire's avatar
Cool Fire committed
46
	def action( nick, _user, host, _from, _msg, arguments, con )
47
		if( @config.auth( host, con ) )
Cool Fire's avatar
Cool Fire committed
48
			if( !arguments.nil? && !arguments.empty? )
49 50 51
				to, action = arguments.split( ' ', 2 )
			end

Cool Fire's avatar
Cool Fire committed
52
			if( !to.nil? && !to.empty? && !action.nil? && !action.empty? )
53 54 55 56 57 58 59 60 61 62 63
				@irc.action( to, action )
			else
				if( con )
					@output.cinfo( "Usage: action send_to message to send" )
				else
					@irc.notice( nick, "Usage: " + @config.command + "action send_to message to send" )
				end
			end
		end
	end

Cool Fire's avatar
Cool Fire committed
64
	def notice( nick, _user, host, _from, _msg, arguments, con )
65
		if( @config.auth( host, con ) )
Cool Fire's avatar
Cool Fire committed
66
			if( !arguments.nil? && !arguments.empty? )
67 68 69
				to, message = arguments.split( ' ', 2 )
			end

Cool Fire's avatar
Cool Fire committed
70
			if( !to.nil? && !message.nil? && !to.empty? && !message.empty? )
71 72 73 74 75 76 77 78 79 80 81
				@irc.notice( to, message )
			else
				if( con )
					@output.cinfo( "Usage: notice send_to message to send" )
				else
					@irc.notice( nick, "Usage: " + @config.command + "notice send_to message to send" )
				end
			end
		end
	end

Cool Fire's avatar
Cool Fire committed
82
	# Join/part commands
Cool Fire's avatar
Cool Fire committed
83
	def join( nick, _user, host, _from, _msg, chan, con )
84
		if( @config.auth( host, con ) )
Cool Fire's avatar
Cool Fire committed
85
			if( !chan.nil? && !chan.empty? )
86 87 88
				@irc.join( chan )
			else
				if( con )
Cool Fire's avatar
Cool Fire committed
89
					@output.cinfo( "Usage: join #channel", true )
90 91 92 93 94 95 96
				else
					@irc.notice( nick, "Usage: " + @config.command + "join #channel" )
				end
			end
		end
	end

Cool Fire's avatar
Cool Fire committed
97
	def part( nick, _user, host, _from, _msg, chan, con )
98
		if( @config.auth( host, con ) )
Cool Fire's avatar
Cool Fire committed
99
			if( !chan.nil? && !chan.empty? )
100 101 102
				@irc.part( chan )
			else
				if( con )
Cool Fire's avatar
Cool Fire committed
103
					@output.cinfo( "Usage: part #channel", true )
104 105 106 107 108 109 110
				else
					@irc.notice( nick, "Usage: " + @config.command + "part #channel" )
				end
			end
		end
	end

Cool Fire's avatar
Cool Fire committed
111
	# Mode command
Cool Fire's avatar
Cool Fire committed
112
	def mode( nick, _user, host, _from, _msg, arguments, con )
Cool Fire's avatar
Cool Fire committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
		if( @config.auth( host, con ) )
			if( arguments == nil )
				arguments = ""
			end

			if( arguments.split.size >= 2 )
				if( arguments.split.size >= 3 )
					chan, mode, name = arguments.split( ' ', 3 )
				elsif( arguments.split.size == 2 )
					chan, mode = arguments.split( ' ', 2 )
					name = ""
				end
				@irc.mode( chan, mode, name )
			else
				if( con )
Cool Fire's avatar
Cool Fire committed
128
					@output.cinfo( "Usage: mode #channel +/-mode nick", true )
Cool Fire's avatar
Cool Fire committed
129 130 131 132 133 134 135
				else
					@irc.notice( nick, "Usage: " + @config.command + "Usage: mode #channel +/-mode nick" )
				end
			end
		end
	end

136
	# Oper commands
Cool Fire's avatar
Cool Fire committed
137
	def op( nick, _user, host, from, _msg, arguments, con )
138
		if( @config.auth( host, con ) )
Cool Fire's avatar
Cool Fire committed
139 140 141 142 143 144 145 146
			if( arguments == nil )
				arguments = ""
			end

			if( ( con && arguments.split.size == 2 ) || ( !con ) )
				if( con || arguments.split.size == 2 )
					chan, name = arguments.split( ' ', 2 )
				elsif( arguments.split.size == 1 )
147
					chan = from
Cool Fire's avatar
Cool Fire committed
148 149 150 151
					name = arguments
				else
					chan = from
					name = nick
152
				end
Cool Fire's avatar
Cool Fire committed
153
				@irc.mode( chan, "+o", name, true )
154 155 156 157 158 159 160 161 162 163
			else
				if( con )
					@output.cinfo( "Usage: op #channel user" )
				else
					@irc.notice( nick, "Usage: " + @config.command + "op #channel user" )
				end
			end
		end
	end

Cool Fire's avatar
Cool Fire committed
164
	def deop( nick, _user, host, from, _msg, arguments, con )
165
		if( @config.auth( host, con ) )
Cool Fire's avatar
Cool Fire committed
166 167 168 169 170 171 172 173
			if( arguments == nil )
				arguments = ""
			end

			if( ( con && arguments.split.size == 2 ) || ( !con ) )
				if( con || arguments.split.size == 2 )
					chan, name = arguments.split( ' ', 2 )
				elsif( arguments.split.size == 1 )
174
					chan = from
Cool Fire's avatar
Cool Fire committed
175 176 177 178
					name = arguments
				else
					chan = from
					name = nick
179
				end
Cool Fire's avatar
Cool Fire committed
180
				@irc.mode( chan, "-o", name, true )
181 182 183 184 185 186 187 188 189 190 191
			else
				if( con )
					@output.cinfo( "Usage: deop #channel user" )
				else
					@irc.notice( nick, "Usage: " + @config.command + "deop #channel user" )
				end
			end
		end
	end

	# Half-oper commands
Cool Fire's avatar
Cool Fire committed
192
	def hop( nick, _user, host, from, _msg, arguments, con )
193
		if( @config.auth( host, con ) )
Cool Fire's avatar
Cool Fire committed
194 195 196 197 198 199 200 201
			if( arguments == nil )
				arguments = ""
			end

			if( ( con && arguments.split.size == 2 ) || ( !con ) )
				if( con || arguments.split.size == 2 )
					chan, name = arguments.split( ' ', 2 )
				elsif( arguments.split.size == 1 )
202
					chan = from
Cool Fire's avatar
Cool Fire committed
203 204 205 206
					name = arguments
				else
					chan = from
					name = nick
207
				end
Cool Fire's avatar
Cool Fire committed
208
				@irc.mode( chan, "+h", name, true )
209 210 211 212 213 214 215 216 217 218
			else
				if( con )
					@output.cinfo( "Usage: hop #channel user" )
				else
					@irc.notice( nick, "Usage: " + @config.command + "hop #channel user" )
				end
			end
		end
	end

Cool Fire's avatar
Cool Fire committed
219
	def dehop( nick, _user, host, from, _msg, arguments, con )
220
		if( @config.auth( host, con ) )
Cool Fire's avatar
Cool Fire committed
221 222 223 224 225 226 227 228
			if( arguments == nil )
				arguments = ""
			end

			if( ( con && arguments.split.size == 2 ) || ( !con ) )
				if( con || arguments.split.size == 2 )
					chan, name = arguments.split( ' ', 2 )
				elsif( arguments.split.size == 1 )
229
					chan = from
Cool Fire's avatar
Cool Fire committed
230 231 232 233
					name = arguments
				else
					chan = from
					name = nick
234
				end
Cool Fire's avatar
Cool Fire committed
235
				@irc.mode( chan, "-h", name, true )
236 237 238 239 240 241 242 243 244 245 246
			else
				if( con )
					@output.cinfo( "Usage: dehop #channel user" )
				else
					@irc.notice( nick, "Usage: " + @config.command + "dehop #channel user" )
				end
			end
		end
	end

	# Voice commands
Cool Fire's avatar
Cool Fire committed
247
	def voice( nick, _user, host, from, _msg, arguments, con )
248
		if( @config.auth( host, con ) )
Cool Fire's avatar
Cool Fire committed
249 250 251 252 253 254 255 256
			if( arguments == nil )
				arguments = ""
			end

			if( ( con && arguments.split.size == 2 ) || ( !con ) )
				if( con || arguments.split.size == 2 )
					chan, name = arguments.split( ' ', 2 )
				elsif( arguments.split.size == 1 )
257
					chan = from
Cool Fire's avatar
Cool Fire committed
258 259 260 261
					name = arguments
				else
					chan = from
					name = nick
262
				end
Cool Fire's avatar
Cool Fire committed
263
				@irc.mode( chan, "+v", name, true )
264 265 266 267 268 269 270 271 272 273
			else
				if( con )
					@output.cinfo( "Usage: voice #channel user" )
				else
					@irc.notice( nick, "Usage: " + @config.command + "voice #channel user" )
				end
			end
		end
	end

Cool Fire's avatar
Cool Fire committed
274
	def devoice( nick, _user, host, from, _msg, arguments, con )
275
		if( @config.auth( host, con ) )
Cool Fire's avatar
Cool Fire committed
276 277 278 279 280 281 282 283
			if( arguments == nil )
				arguments = ""
			end

			if( ( con && arguments.split.size == 2 ) || ( !con ) )
				if( con || arguments.split.size == 2 )
					chan, name = arguments.split( ' ', 2 )
				elsif( arguments.split.size == 1 )
284
					chan = from
Cool Fire's avatar
Cool Fire committed
285 286 287 288
					name = arguments
				else
					chan = from
					name = nick
289
				end
Cool Fire's avatar
Cool Fire committed
290
				@irc.mode( chan, "-v", name, true )
291 292 293 294 295 296 297 298 299 300
			else
				if( con )
					@output.cinfo( "Usage: devoice #channel user" )
				else
					@irc.notice( nick, "Usage: " + @config.command + "devoice #channel user" )
				end
			end
		end
	end

Cool Fire's avatar
Cool Fire committed
301
	# Kick commands
Cool Fire's avatar
Cool Fire committed
302
	def kick( nick, _user, host, from, _msg, arguments, con )
Cool Fire's avatar
Cool Fire committed
303 304 305 306 307 308 309 310 311 312 313 314 315
		if( @config.auth( host, con ) )
			if( !arguments.nil? && !arguments.empty? )
				if( arguments =~ /^#/ )
					chan, name, reason = arguments.split( ' ', 3 )
				else
					name, reason = arguments.split( ' ', 2 )
					chan = from
				end

				if( reason.nil? || reason.empty?)
					reason = "Requested by " + nick + "."
				end

Cool Fire's avatar
Cool Fire committed
316
				@irc.kick( chan, name, reason, true )
Cool Fire's avatar
Cool Fire committed
317 318 319 320 321 322 323 324 325 326
			else
				if( con )
					@output.cinfo( "Usage: kick #channel nick" )
				else
					@irc.notice( nick, "Usage: " + @config.command + "kick #channel nick" )
				end
			end
		end
	end

327
	# Banning commands
Cool Fire's avatar
Cool Fire committed
328
	def ban( nick, _user, host, from, _msg, arguments, con )
Cool Fire's avatar
Cool Fire committed
329
		if( @config.auth( host, con ) )
Cool Fire's avatar
Cool Fire committed
330
			if( !arguments.nil? && !arguments.empty? )
Cool Fire's avatar
Cool Fire committed
331 332 333
				chan, host = arguments.split( ' ', 2 )
			end

Cool Fire's avatar
Cool Fire committed
334
			if( host.nil? || host.empty? ) # Overload parameters when no channel is given
Cool Fire's avatar
Cool Fire committed
335 336 337 338
				host	= chan
				chan	= from
			end

Cool Fire's avatar
Cool Fire committed
339
			@irc.mode( chan, "+b", host, true )
Cool Fire's avatar
Cool Fire committed
340 341 342 343 344 345 346 347 348
		else
			if( con )
				@output.cinfo( "Usage: ban #channel host" )
			else
				@irc.notice( nick, "Usage: " + @config.command + "ban #channel host" )
			end
		end
	end

Cool Fire's avatar
Cool Fire committed
349
	def unban( nick, _user, host, from, _msg, arguments, con )
Cool Fire's avatar
Cool Fire committed
350 351 352 353 354 355 356 357 358 359
		if( @config.auth( host, con ) )
			if( !arguments.nil? && !arguments.empty? )
				chan, host = arguments.split( ' ', 2 )
			end

			if( host.nil? || host.empty? ) # Overload parameters when no channel is given
				host	= chan
				chan	= from
			end

Cool Fire's avatar
Cool Fire committed
360
			@irc.mode( chan, "-b", host, true )
Cool Fire's avatar
Cool Fire committed
361 362 363 364 365 366 367 368 369
		else
			if( con )
				@output.cinfo( "Usage: unban #channel host" )
			else
				@irc.notice( nick, "Usage: " + @config.command + "unban #channel host" )
			end
		end
	end

Cool Fire's avatar
Cool Fire committed
370
	def timeban( nick, _user, host, from, _msg, arguments, con )
371
		if( @config.auth( host, con ) )
372
			if( @config.threads && @status.threads )
Cool Fire's avatar
Cool Fire committed
373
				if( !arguments.nil? && !arguments.empty? )
374 375 376
					chan, host, timeout = arguments.split( ' ', 3 )
				end

Cool Fire's avatar
Cool Fire committed
377 378
				if( !chan.nil? && !chan.empty? && !host.nil? && !host.empty? && ( !con || ( !timeout.nil? && !timeout.empty? ) ) )
					if( timeout.nil? || timeout.empty? ) # Overload parameters when no channel is given
379 380 381 382 383
						timeout	= host
						host	= chan
						chan	= from
					end

Cool Fire's avatar
Cool Fire committed
384 385
					@irc.mode( chan, "+b", host, true )
					@timer.action( timeout.to_i, "@irc.mode( \"#{chan}\", \"-b\", \"#{host}\", true )" )
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405

					if( con )
						@output.cinfo( "Unban set for " + timeout.to_s + " seconds from now." )
						@irc.message( chan, "Unban set for " + timeout.to_s + " seconds from now." )
					else
						@irc.message( from, "Unban set for " + timeout.to_s + " seconds from now." )
					end
				else
					if( con )
						@output.cinfo( "Usage: timeban #channel host seconds" )
					else
						@irc.notice( nick, "Usage: " + @config.command + "timeban #channel host seconds" )
					end
				end
			else
				@irc.notice( nick, "Timeban not availble when threading is disabled." )
			end
		end
	end

406 407
	# Echo version to the user
	def version( nick, user, host, from, msg, arguments, con )
Cool Fire's avatar
Cool Fire committed
408
		output = "Running: " + @config.version + " on Ruby " + RUBY_VERSION
409
		if( con )
Cool Fire's avatar
Cool Fire committed
410
			@output.cinfo( output )
411 412 413
		else
			@irc.notice( nick, output )
		end
Cool Fire's avatar
Cool Fire committed
414 415 416 417
		uptime( nick, user, host, from, msg, arguments, con )
	end

	# Echo uptime to the user
Cool Fire's avatar
Cool Fire committed
418
	def uptime( nick, _user, _host, _from, _msg, _arguments, con )
Cool Fire's avatar
Cool Fire committed
419
		uptime = "Uptime: " + @status.uptime + "."
Cool Fire's avatar
Cool Fire committed
420
		if( con )
Cool Fire's avatar
Cool Fire committed
421
			@output.cinfo( uptime )
Cool Fire's avatar
Cool Fire committed
422 423 424
		else
			@irc.notice( nick, uptime )
		end
425 426
	end

Cool Fire's avatar
Cool Fire committed
427
	def nick( _nick, _user, host, _from, _msg, arguments, con )
428
		if( @config.auth( host, con ) )
429 430 431 432
			if( !arguments.nil? && !arguments.empty? )
				@config.nick( arguments )
				@irc.nick( arguments )
			end
433 434 435
		end
	end
end