source: tags/start/modules/plugins.tcl @ 1143

Revision 2, 13.8 KB checked in by jamesoff, 9 years ago (diff)

Initial revision

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1## plugins engine for bMotion
2#
3# $Id$
4#
5
6###############################################################################
7# bMotion - an 'AI' TCL script for eggdrops
8# Copyright (C) James Michael Seward 2000-2002
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful, but
16# WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18# General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23###############################################################################
24
25## Simple plugins
26if [info exists bMotion_plugins_simple] { unset bMotion_plugins_simple }
27set bMotion_plugins_simple(dummy) "_{100,100}Š0Š/has a tremendous plugin-related error (wahey)$"
28
29## Admin plugins (.bmotion)
30if [info exists bMotion_plugins_admin] { unset bMotion_plugins_admin }
31set bMotion_plugins_admin(dummy) "none"
32
33## complex plugins
34if [info exists bMotion_plugins_complex] { unset bMotion_plugins_complex }
35set bMotion_plugins_complex(dummy) "none"
36
37## output plugins
38if [info exists bMotion_plugins_output] { unset bMotion_plugins_output }
39set bMotion_plugins_output(dummy) "none"
40
41## action simple plugins
42if [info exists bMotion_plugins_action_simple] { unset bMotion_plugins_action_simple }
43set bMotion_plugins_action_simple(dummy) "none"
44
45## action complex plugins
46if [info exists bMotion_plugins_action_complex] { unset bMotion_plugins_action_complex }
47set bMotion_plugins_action_complex(dummy) "none"
48
49
50##############################################################################################################################
51## Load a simple plugin
52proc bMotion_plugin_add_simple { id match chance response language} {
53  global bMotion_plugins_simple plugins bMotion_testing
54
55  if {$bMotion_testing == 0} {
56    catch {
57      set test $bMotion_plugins_simple($id)
58      putlog "bMotion: ALERT! Simple plugin $id is defined more than once"
59      return 0
60    }
61  }
62  if [bMotion_plugin_check_allowed "simple:$id"] {
63    set bMotion_plugins_simple($id) "${match}Š${chance}Š$responseŠ$language"
64    bMotion_putloglev 2 * "bMotion: added simple plugin: $id"
65    append plugins "$id,"
66    return 1
67  }
68  bMotion_putloglev d * "bMotion: ignoring disallowed plugin simple:$id"
69}
70
71
72## Find a simple plugin
73proc bMotion_plugin_find_simple { text lang } {
74  global bMotion_plugins_simple botnicks
75  set s [array startsearch bMotion_plugins_simple]
76
77  while {[set key [array nextelement bMotion_plugins_simple $s]] != ""} {
78    if {$key == "dummy"} { continue }
79    set val $bMotion_plugins_simple($key)
80    set blah [split $val "Š"]
81    set rexp [lindex $blah 0]
82    set chance [lindex $blah 1]
83    set response [lindex $blah 2]
84    set language [lindex $blah 3]
85    if {[string match $lang $language] || ($language == "any")} {
86      set rexp [bMotionInsertString $rexp "%botnicks" "${botnicks}"]
87      if [regexp -nocase $rexp $text] {
88        set c [rand 100]
89        if {$chance > $c} {
90          array donesearch bMotion_plugins_simple $s
91          return $response
92        }
93      }
94    }
95  }
96  array donesearch bMotion_plugins_simple $s 
97  return ""
98}
99
100
101
102## Load an admin plugin
103proc bMotion_plugin_add_admin { id match flags callback language } {
104  global bMotion_plugins_admin plugins bMotion_testing
105
106  if {$bMotion_testing == 0} {
107    catch {
108      set test $bMotion_plugins_admin($id)
109      putlog "bMotion: ALERT! admin plugin $id is defined more than once"
110      return 0
111    }
112  }
113
114  if [bMotion_plugin_check_allowed "admin:$id"] {
115    set bMotion_plugins_admin($id) "${match}Š${flags}Š$callbackŠ$language"
116    bMotion_putloglev 2 * "bMotion: added admin plugin: $id"
117    append plugins "$id,"
118    return 1
119  }
120  bMotion_putloglev d * "bMotion: ignoring disallowed plugin admin:$id"
121}
122
123## Find an admin plugin
124proc bMotion_plugin_find_admin { text lang } {
125  global bMotion_plugins_admin
126  set s [array startsearch bMotion_plugins_admin]
127
128  while {[set key [array nextelement bMotion_plugins_admin $s]] != ""} {
129    if {$key == "dummy"} { continue }
130    set val $bMotion_plugins_admin($key)
131    set blah [split $val "Š"]
132    set rexp [lindex $blah 0]
133    set flags [lindex $blah 1]
134    set callback [lindex $blah 2]
135    set language [lindex $blah 3]
136    if {[string match $lang $language] || ($language == "any")} {
137      if [regexp -nocase $rexp $text] {
138        array donesearch bMotion_plugins_admin $s
139        return "${flags}Š$callback"
140      }
141    }
142  }
143  array donesearch bMotion_plugins_admin $s
144  return ""
145}
146
147
148## Load a complex plugin
149proc bMotion_plugin_add_complex { id match chance callback language } {
150  global bMotion_plugins_complex plugins bMotion_testing
151  if {$bMotion_testing == 0} {
152    catch {
153      set test $bMotion_plugins_complex($id)
154      putlog "bMotion: ALERT! Complex plugin $id is defined more than once"
155      return 0
156    }
157  }
158  if [bMotion_plugin_check_allowed "complex:$id"] {
159    set bMotion_plugins_complex($id) "${match}Š$chanceŠ$callbackŠ$language"
160    bMotion_putloglev 2 * "bMotion: added complex plugin: $id"
161    append plugins "$id,"
162    return 1
163  }
164  bMotion_putloglev d * "bMotion: ignoring disallowed plugin complex:$id"
165
166}
167
168## Find a complex plugin plugin
169proc bMotion_plugin_find_complex { text lang } {
170  global bMotion_plugins_complex botnicks
171  set s [array startsearch bMotion_plugins_complex]
172  set result [list]
173
174  while {[set key [array nextelement bMotion_plugins_complex $s]] != ""} {
175    if {$key == "dummy"} { continue }
176    set val $bMotion_plugins_complex($key)
177    set blah [split $val "Š"]
178    set rexp [lindex $blah 0]
179    set chance [lindex $blah 1]
180    set callback [lindex $blah 2]
181    set language [lindex $blah 3]
182    if {[string match $lang $language] || ($language == "any")} {
183    set rexp [bMotionInsertString $rexp "%botnicks" "${botnicks}"]
184      if [regexp -nocase $rexp $text] {
185        set c [rand 100]
186        if {$chance > $c} {
187          lappend result $callback
188        }
189      }
190    }
191  }
192  array donesearch bMotion_plugins_complex $s
193  return $result
194}
195
196
197## Load an output plugin
198proc bMotion_plugin_add_output { id callback enabled language } {
199  global bMotion_plugins_output plugins bMotion_testing
200
201  if {$bMotion_testing == 0} {
202    catch {
203      set test $bMotion_plugins_output($id)
204      putlog "bMotion: ALERT! Output plugin $id is defined more than once"
205      return 0
206    }
207  }
208  if [bMotion_plugin_check_allowed "output:$id"] {
209    set bMotion_plugins_output($id) "$callbackŠ$enabledŠ$language"
210    bMotion_putloglev 2 * "bMotion: added output plugin: $id"
211    append plugins "$id,"
212    return 1
213  }
214  bMotion_putloglev d * "bMotion: ignoring disallowed plugin output:$id"
215}
216
217proc bMotion_plugin_find_output { lang } {
218  global bMotion_plugins_output botnicks
219  set s [array startsearch bMotion_plugins_output]
220  set result [list]
221
222  while {[set key [array nextelement bMotion_plugins_output $s]] != ""} {
223    if {$key == "dummy"} { continue }
224    set val $bMotion_plugins_output($key)
225    set blah [split $val "Š"]
226    set callback [lindex $blah 0]
227    set enabled [lindex $blah 1]
228    set language [lindex $blah 2]
229    if {[string match $lang $language] || ($language == "any")} {
230      if {$enabled == 1} {
231        lappend result $callback
232      }
233    }
234  }
235  array donesearch bMotion_plugins_output $s
236  return $result
237}
238
239
240## Load a simple action plugin
241proc bMotion_plugin_add_action_simple { id match chance response language } {
242  global bMotion_plugins_action_simple plugins bMotion_testing
243
244  if {$bMotion_testing == 0} {
245    catch {
246      set test $bMotion_plugins_action_simple($id)
247      putlog "bMotion: ALERT! Simple plugin $id is defined more than once"
248      return 0
249    }
250  }
251  if [bMotion_plugin_check_allowed "action_simple:$id"] {
252    set bMotion_plugins_action_simple($id) "${match}Š${chance}Š$responseŠ$language"
253    bMotion_putloglev 2 * "bMotion: added simple action plugin: $id"
254    append plugins "$id,"
255    return 1
256  }
257  bMotion_putloglev d * "bMotion: ignoring disallowed plugin action_simple:$id"
258
259}
260
261
262## Find a simple action plugin
263proc bMotion_plugin_find_action_simple { text lang } {
264  global bMotion_plugins_action_simple botnicks
265  set s [array startsearch bMotion_plugins_action_simple]
266
267  while {[set key [array nextelement bMotion_plugins_action_simple $s]] != ""} {
268    if {$key == "dummy"} { continue }
269    set val $bMotion_plugins_action_simple($key)
270    set blah [split $val "Š"]
271    set rexp [lindex $blah 0]
272    set chance [lindex $blah 1]
273    set response [lindex $blah 2]
274    set language [lindex $blah 3]
275    if {[string match $lang $language] || ($language == "any")} {
276      set rexp [bMotionInsertString $rexp "%botnicks" "${botnicks}"]
277      if [regexp -nocase $rexp $text] {
278        set c [rand 100]
279        if {$chance > $c} {
280          array donesearch bMotion_plugins_action_simple $s
281          return $response
282        }
283      }
284    }
285  }
286  array donesearch bMotion_plugins_action_simple $s 
287  return ""
288}
289
290
291## Load a complex action plugin
292proc bMotion_plugin_add_action_complex { id match chance callback language } {
293  global bMotion_plugins_action_complex plugins bMotion_testing
294  if {$bMotion_testing == 0} {
295    catch {
296      set test $bMotion_plugins_action_complex($id)
297      putlog "bMotion: ALERT! Complex action plugin $id is defined more than once"
298      return 0
299    }
300  }
301  if [bMotion_plugin_check_allowed "action_complex:$id"] {
302    set bMotion_plugins_action_complex($id) "${match}Š$chanceŠ$callbackŠ$language"
303    bMotion_putloglev 2 * "bMotion: added complex action plugin: $id"
304    append plugins "$id,"
305    return 1
306  }
307  bMotion_putloglev d * "bMotion: ignoring disallowed plugin action_complex:$id"
308
309}
310
311## Find a complex action plugin plugin
312proc bMotion_plugin_find_action_complex { text lang } {
313  global bMotion_plugins_action_complex botnicks
314  set s [array startsearch bMotion_plugins_action_complex]
315  set result [list]
316
317  while {[set key [array nextelement bMotion_plugins_action_complex $s]] != ""} {
318    if {$key == "dummy"} { continue }
319    set val $bMotion_plugins_action_complex($key)
320    set blah [split $val "Š"]
321    set rexp [lindex $blah 0]
322    set chance [lindex $blah 1]
323    set callback [lindex $blah 2]
324    set language [lindex $blah 3]
325    if {[string match $language $lang] || ($language == "any")} {
326      set rexp [bMotionInsertString $rexp "%botnicks" "${botnicks}"]
327      if [regexp -nocase $rexp $text] {
328        set c [rand 100]
329        if {$chance > $c} {
330          lappend result $callback
331        }
332      }
333    }
334  }
335  array donesearch bMotion_plugins_action_complex $s
336  return $result
337}
338
339
340###############################################################################
341
342proc bMotion_plugin_check_depend { depends } {
343
344  #pass a string in the format "type:plugin,type:plugin,..."
345
346  if {$depends == ""} {
347    return 1
348  }
349
350  set result 1
351
352  set blah [split $depends ","]
353  foreach depend $blah {
354    set blah2 [split $depend ":"]
355    set t [lindex $blah2 0]
356    set id [lindex $blah2 1]
357    set a "bMotion_plugins_$t"
358    upvar #0 $a ar
359    bMotion_putloglev 1 * "bMotion: checking $a for $id ..."
360    set temp [array names ar $id]
361    if {[llength $temp] == 0} {
362      set result 0
363      bMotion_putloglev d * "bMotion: Missing dependency $t:$id"
364    }
365  }
366  return $result
367}
368
369
370
371###############################################################################
372
373proc bMotion_plugin_check_allowed { name } {
374
375  #pass a string in the format "type:plugin"
376  #setting in config should be "type:plugin,type:plugin,..."
377
378  global bMotionSettings
379
380  set disallowed ""
381
382  catch {
383    set disallowed $bMotionSettings(noPlugin)
384  }
385
386  if {$disallowed == ""} {
387    return 1
388  }
389
390  bMotion_putloglev 4 * "bMotion: checking $name against $disallowed"
391
392  set blah [split $disallowed ","]
393  foreach plugin $blah {
394    if {$plugin == $name} {
395      return 0
396    }
397  }
398  return 1
399}
400
401
402
403################################################################################
404
405## Load the simple plugins
406set plugins ""
407catch { source "$bMotionPlugins/simple.tcl" }
408#set plugins [string range $plugins 0 [expr [string length $plugins] - 2]]
409#bMotion_putloglev d * "bMotion: simple plugins loaded: $plugins"
410
411## Load the admin plugins
412set plugins ""
413catch { source "$bMotionPlugins/admin.tcl" }
414#set plugins [string range $plugins 0 [expr [string length $plugins] - 2]]
415#bMotion_putloglev d * "bMotion: admin plugins loaded: $plugins"
416
417## Load the complex plugins
418set plugins ""
419catch { source "$bMotionPlugins/complex.tcl" }
420#set plugins [string range $plugins 0 [expr [string length $plugins] - 2]]
421#bMotion_putloglev d * "bMotion: complex plugins loaded: $plugins"
422
423## Load the output plugins
424set plugins ""
425catch { source "$bMotionPlugins/output.tcl" }
426#set plugins [string range $plugins 0 [expr [string length $plugins] - 2]]
427#bMotion_putloglev d * "bMotion: output plugins loaded: $plugins"
428
429## Load the simple action plugins
430catch { source "$bMotionPlugins/action_simple.tcl" }
431
432## Load the complex action plugins
433catch { source "$bMotionPlugins/action_complex.tcl" }
434
435bMotion_putloglev d * "Installed bMotion plugins: (some may be inactive)\r"
436bMotion_putloglev d * "(one moment...)\r"
437foreach t {simple complex admin output action_simple action_complex} {
438  set arrayName "bMotion_plugins_$t"
439  upvar #0 $arrayName cheese
440  set plugins [array names cheese]
441  set output "$t: "
442  foreach n $plugins {
443    if {$n != "dummy"} {
444      append output "$n, "
445    }
446  }
447  set output [string range $output 0 [expr [string length $output] - 3]]
448  bMotion_putloglev d * "$output\r"
449}
450
451### null plugin routine for faking plugins
452proc bMotion_plugin_null { {a ""} {b ""} {c ""} {d ""} {e ""} } {
453  return 0
454}
455
456bMotion_putloglev d * "bMotion: plugins module loaded"
457
Note: See TracBrowser for help on using the repository browser.