debug.rb 2.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#!/usr/bin/env ruby

# Plugin to help with debugging
class Debug

	# This method is called when the plugin is first loaded
	def initialize( status, config, output, irc, timer )
		@status  = status
		@config  = config
		@output  = output
		@irc     = irc
		@timer   = timer
	end

	# Default method, called when no argument is given (optional, but highly recomended)
	def main( nick, user, host, from, msg, arguments, con )
		
	end
	
	# Function to send help about this plugin (Can also be called by the help plugin.)
Cool Fire's avatar
Cool Fire committed
21
	def help( nick, _user, _host, _from, _msg, _arguments, con )
22 23 24
		help = [
			"Plugin to help with debugging.",
			"  debug threadlist             - Print list of threads to console.",
Cool Fire's avatar
Cool Fire committed
25 26
			"  debug threadtrace [thread]   - Show stacktrace for thread.",
			"  debug threadkill [thread]    - Kill thread.",
27 28 29 30 31 32 33 34 35 36 37 38 39 40
			"  debug level                  - Set debug level. (No input sanitsing!)"
		]

		# Print out help
		help.each do |line|
			if( con )
				@output.c( line + "\n" )
			else
				@irc.notice( nick, line )
			end
		end
	end
		
	# Show backtrace of specific thread
Cool Fire's avatar
Cool Fire committed
41
	def threadtrace( _nick, _user, _host, _from, _msg, arguments, _con )
42 43 44
		Thread.list.each do |thr| 
			if( thr.to_s =~ /#{arguments}/ )
				thr.backtrace.each do |level|
Cool Fire's avatar
Cool Fire committed
45
					@output.info( "#{level}\n" )
46
				end
Cool Fire's avatar
Cool Fire committed
47
				@output.info( "\n" )
48 49 50 51 52 53
			end
		end
	end


	# List all threads + status
Cool Fire's avatar
Cool Fire committed
54
	def threadlist( _nick, _user, _host, _from, _msg, _arguments, _con )
55 56 57
		Thread.list.each do |thr| 
			@output.info( "#{thr.inspect}\n" )
		end
Cool Fire's avatar
Cool Fire committed
58 59 60 61
		@output.info( "\n" )
	end

	# Kill thread
Cool Fire's avatar
Cool Fire committed
62
	def threadkill( nick, _user, host, from, _msg, arguments, con )
Cool Fire's avatar
Cool Fire committed
63 64 65
		if( @config.auth( host, con ) )
			Thread.list.each do |thr| 
				if( thr.to_s =~ /#{arguments}/ )
Cool Fire's avatar
Cool Fire committed
66
					@output.info( "#{thr.kill}\n\n" )
Cool Fire's avatar
Cool Fire committed
67 68 69 70 71
				end
			end
		else
			@irc.message( from, "Sorry " + nick + ", this is a function for admins only!" )
		end
72 73 74
	end

	# Set debug level
Cool Fire's avatar
Cool Fire committed
75
	def level( nick, _user, host, from, _msg, arguments, con )
76 77 78 79 80 81 82 83
		if( @config.auth( host, con ) )
			@status.debug( arguments.to_i )
			@irc.notice( nick, "Set debug level to #{arguments}" )
		else
			@irc.message( from, "Sorry " + nick + ", this is a function for admins only!" )
		end
	end
end