demo.rb 4.32 KB
Newer Older
1 2 3
#!/usr/bin/env ruby

# Plugin to demonstrate the working of plugins
4
class Demo
5 6

	# This method is called when the plugin is first loaded
7
	def initialize( status, config, output, irc, timer )
8 9 10 11 12
		@status  = status
		@config  = config
		@output  = output
		@irc     = irc
		@timer   = timer
13 14
	end

15 16 17 18 19 20 21
	# Method that will get called just before a plugin is unloaded/reloaded. (optional)
	# If implemented must return true when ready to unload.
	def unload
		@output.std( "Received notification that this plugin will be unloaded.\n" )
		return true
	end

22
	# Default method, called when no argument is given (optional, but highly recomended)
Cool Fire's avatar
Cool Fire committed
23
	def main( _nick, _user, _host, from, _msg, _arguments, _con )
24
		@irc.message( from, "This is the demo module, function, function_admin and remote are availble." )
25 26 27
	end

	# Method that receives a notification when a user is kicked (optional)
Cool Fire's avatar
Cool Fire committed
28
	def kicked( nick, _user, _host, channel, kicked, reason )
29 30 31 32
		@irc.message( channel, kicked + " was kicked by " + nick + " for " + reason + "." )
	end

	# Method that receives a notification when a notice is received (optional)
Cool Fire's avatar
Cool Fire committed
33
	def noticed( nick,  _user,  _host,  _to,  message )
34 35 36 37
		@irc.message( nick, "Received notice from: " + nick + ": " + message )
	end

	# Method that receives a notification when a message is received, that is not a command (optional)
Cool Fire's avatar
Cool Fire committed
38
	def messaged( nick, _user, _host, from, message )
39 40 41
		if( @config.nick != nick )
			@irc.message( from, "Received message from: " + nick + ": " + message )
		end
42 43 44
	end

	# Method that receives a notification when a user joins (optional)
Cool Fire's avatar
Cool Fire committed
45
	def joined( nick, _user, _host, channel )
46 47 48 49
		@irc.message( channel, nick + " joined " + channel + "." )
	end

	# Method that receives a notification when a user parts (optional)
Cool Fire's avatar
Cool Fire committed
50
	def parted( nick, _user, _host, channel )
51 52 53 54
		@irc.message( channel, nick + " parted " + channel + "." )
	end

	# Method that receives a notification when a user quits (optional)
Cool Fire's avatar
Cool Fire committed
55
	def quited( nick, _user, _host, message )
56 57 58
		@output.std( nick + " quit: " + message )
	end

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
	# Method that receives numbered server messages like MOTD, rules, names etc. (optional)
	def servermsg( servername, messagenumber, message )
		@output.std( "Specific message #{messagenumber} received from #{servername}: #{message}\n" )
	end

	# Method that receives server messages (optional)
	def miscservermsg( unknown )
		@output.std( "Server message: #{unknown}\n" )
	end
	
	# Method that receives miscellaneous messages that don't fit any other profile (optional)
	def misc( unknown )
		@output.std( "Miscellaneous message: #{unknown}\n" )
	end
	
74
	# Function to send help about this plugin (Can also be called by the help plugin.)
Cool Fire's avatar
Cool Fire committed
75
	def help( nick, _user, _host, _from, _msg, _arguments, con )
76 77 78 79 80 81 82 83 84 85 86 87 88
		help = [
			"This plugin demonstrates the working of plugins.",
			"  demo function               - Public plugin function.",
			"  demo adminfunction          - Admin only plugin function"
		]

		# Print out help
		help.each do |line|
			if( con )
				@output.c( line + "\n" )
			else
				@irc.notice( nick, line )
			end
89 90 91
		end
	end
		
92
	# Generic function that can be called by any user
Cool Fire's avatar
Cool Fire committed
93
	def function( nick, _user, _host, from, _msg, _arguments, _con )
94 95 96 97
		@irc.message( from, nick + " called \"function\" from " + from + "." )
	end

	# Generic function that can only be called by an admin
Cool Fire's avatar
Cool Fire committed
98
	def functionadmin( nick, _user, host, from, _msg, _arguments, con )
99 100 101 102 103 104 105 106
		if( @config.auth( host, con ) )
			# Admin only code goes in here
			@irc.message( from, nick + " called \"function_admin\" from " + from + "." )
		else
			@irc.message( from, "Sorry " + nick + ", this is a function for admins only!" )
		end
	end

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	# This function uses a function from another plugin. The help plugin in this example.
	def remote( nick, user, host, from, msg, arguments, con )
		if( @status.checkplugin( "help" ) ) # Returns True if a plugin by that name is loaded.
			plugin = @status.getplugin( "help" ) # We can now use the 'plugin' object to make calls to an external plugin.

			# Send data along to the 'main' function of the 'help' plugin.
			plugin.main( nick, user, host, from, msg, arguments, con )
		else
			# Show error to user
			line = "The plugin this function depends on doesn't appear to be loaded."

			if( con )
				@output.c( line + "\n" )
			else
				@irc.notice( nick, line )
			end
		end
	end

Cool Fire's avatar
Cool Fire committed
126 127 128 129
	private
	def something
		# This is an internal function that cannot be called from outside this plugin.
	end
130
end