]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/script.rb
Fix the "list" function. Now counts the pages correctly.
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / script.rb
1 # Plugin for the Ruby IRC bot (http://linuxbrit.co.uk/rbot/)
2 #
3 # Create mini plugins on IRC.
4 #
5 # Scripts are little Ruby programs that run in the context of the script plugin. You 
6 # can create them directly in an IRC channel, and invoke them just like normal rbot plugins. 
7 #
8 # (c) 2006 Mark Kretschmann <markey@web.de>
9 # Licensed under GPL V2.
10
11
12 Command = Struct.new( "Command", :code, :nick, :created, :channel )
13
14
15 class ScriptPlugin < Plugin
16
17   def initialize
18     super
19     if @registry.has_key?(:commands)
20       @commands = @registry[:commands]
21     else
22       @commands = Hash.new
23     end
24
25     # Migrate old Hash to new:
26     @commands.each_pair do |name, cmd|
27       unless cmd.instance_of?( Command )
28         @commands[name] = Command.new( cmd, 'unknown hacker', 'somedate', '#somechan' )
29       end
30     end
31   end
32
33
34   def save
35     @registry[:commands] = @commands
36   end
37
38
39   def help( plugin, topic="" )
40     if topic == "add"
41       "Scripts are little Ruby programs that run in the context of the script plugin. You can access @bot (class IrcBot), m (class PrivMessage), user (class String, either the first argument, or if missing the sourcenick), and args (class Array, an array of arguments). Example: 'script add greet m.reply( 'Hello ' + user )'. Invoke the script just like a plugin: '<botnick>: greet'."
42     else  
43       "Create mini plugins on IRC. 'script add <name> <code>' => Create script named <name> with the code <source>. 'script list' => Show a list of all known scripts. 'script show <name>' => Show the source code for <name>. 'script del <name>' => Delete the script <name>."
44     end
45   end
46
47
48   def listen( m )
49     name = m.message.split.first
50
51     if m.address? and @commands.has_key?( name )
52       code = @commands[name].code.dup.untaint
53
54       # Convenience variables, can be accessed by scripts:
55       args = m.message.split
56       args.delete_at( 0 ) 
57       user = args.empty? ? m.sourcenick : args.first  
58
59       Thread.start {
60         $SAFE = 3
61
62         begin
63           eval( code )
64         rescue => e
65           m.reply( "Script '#{name}' crapped out :(" )
66           m.reply( e.inspect )
67         end
68       }
69     end
70   end
71
72
73   def handle_add( m, params, force = false )
74     name    = params[:name]
75     if !force and @commands.has_key?( name )
76       m.reply( "#{m.sourcenick}: #{name} already exists. Use 'add -f' if you really want to overwrite it." )
77       return
78     end
79
80     code    = params[:code].join( " " )
81     nick    = m.sourcenick
82     created = Time.new.strftime '%Y/%m/%d %H:%m'
83     channel = m.target
84
85     command = Command.new( code, nick, created, channel )
86     @commands[name] = command
87
88     m.reply( "done" )
89   end
90
91
92   def handle_add_force( m, params )
93     handle_add( m, params, true )
94   end
95     
96
97   def handle_del( m, params )
98     name = params[:name]
99     unless @commands.has_key?( name )
100       m.reply( "Script does not exist." ); return
101     end
102
103     @commands.delete( name )
104     m.reply( "done" )
105   end
106
107
108   def handle_list( m, params )
109     if @commands.length == 0
110       m.reply( "No scripts available." ); return
111     end
112
113     cmds_per_page = 30
114     cmds = @commands.keys.sort
115     num_pages = cmds.length / cmds_per_page + 1
116     page = params[:page].to_i
117     page = [page, 1].max
118     page = [page, num_pages].min
119     str = cmds[(page-1)*cmds_per_page, cmds_per_page].join(', ') 
120
121     m.reply "Available scripts (page #{page}/#{num_pages}): #{str}" 
122   end
123
124
125   def handle_show( m, params )
126     name = params[:name]
127     unless @commands.has_key?( name )
128       m.reply( "Script does not exist." ); return
129     end
130
131     cmd = @commands[name]
132     m.reply( "#{cmd.code} [#{cmd.nick}, #{cmd.created} in #{cmd.channel}]" )
133  end
134
135 end
136
137
138 plugin = ScriptPlugin.new
139 plugin.register( "script" )
140
141 plugin.map 'script add -f :name *code', :action => 'handle_add_force', :auth => 'scriptedit'
142 plugin.map 'script add :name *code',    :action => 'handle_add',       :auth => 'scriptedit'
143 plugin.map 'script del :name',          :action => 'handle_del',       :auth => 'scriptedit'
144 plugin.map 'script list :page',         :action => 'handle_list',      :defaults => { :page => '1' }
145 plugin.map 'script show :name',         :action => 'handle_show'
146
147