| 1 | ## plugins engine for bMotion |
|---|
| 2 | # |
|---|
| 3 | |
|---|
| 4 | ############################################################################### |
|---|
| 5 | # bMotion - an 'AI' TCL script for eggdrops |
|---|
| 6 | # Copyright (C) James Michael Seward 2000-2008 |
|---|
| 7 | # |
|---|
| 8 | # This program is free software; you can redistribute it and/or modify |
|---|
| 9 | # it under the terms of the GNU General Public License as published by |
|---|
| 10 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 11 | # (at your option) any later version. |
|---|
| 12 | # |
|---|
| 13 | # This program is distributed in the hope that it will be useful, but |
|---|
| 14 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 16 | # General Public License for more details. |
|---|
| 17 | # |
|---|
| 18 | # You should have received a copy of the GNU General Public License |
|---|
| 19 | # along with this program; if not, write to the Free Software |
|---|
| 20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 21 | ############################################################################### |
|---|
| 22 | |
|---|
| 23 | # "Constants" |
|---|
| 24 | set BMOTION_PLUGIN_SIMPLE_MATCH 0 |
|---|
| 25 | set BMOTION_PLUGIN_SIMPLE_CHANCE 1 |
|---|
| 26 | set BMOTION_PLUGIN_SIMPLE_RESPONSE 2 |
|---|
| 27 | set BMOTION_PLUGIN_SIMPLE_LANGUAGE 3 |
|---|
| 28 | |
|---|
| 29 | set BMOTION_PLUGIN_MANAGEMENT_MATCH 0 |
|---|
| 30 | set BMOTION_PLUGIN_MANAGEMENT_FLAGS 1 |
|---|
| 31 | set BMOTION_PLUGIN_MANAGEMENT_CALLBACK 2 |
|---|
| 32 | set BMOTION_PLUGIN_MANAGEMENT_HELPCALLBACK 3 |
|---|
| 33 | |
|---|
| 34 | set BMOTION_PLUGIN_COMPLEX_MATCH 0 |
|---|
| 35 | set BMOTION_PLUGIN_COMPLEX_CHANCE 1 |
|---|
| 36 | set BMOTION_PLUGIN_COMPLEX_CALLBACK 2 |
|---|
| 37 | set BMOTION_PLUGIN_COMPLEX_LANGUAGE 3 |
|---|
| 38 | |
|---|
| 39 | set BMOTION_PLUGIN_OUTPUT_CALLBACK 0 |
|---|
| 40 | set BMOTION_PLUGIN_OUTPUT_ENABLED 1 |
|---|
| 41 | set BMOTION_PLUGIN_OUTPUT_LANGUAGE 2 |
|---|
| 42 | set BMOTION_PLUGIN_OUTPUT_PRIORITY 3 |
|---|
| 43 | |
|---|
| 44 | set BMOTION_PLUGIN_EVENT_TYPE 0 |
|---|
| 45 | set BMOTION_PLUGIN_EVENT_MATCH 1 |
|---|
| 46 | set BMOTION_PLUGIN_EVENT_CHANCE 2 |
|---|
| 47 | set BMOTION_PLUGIN_EVENT_CALLBACK 3 |
|---|
| 48 | set BMOTION_PLUGIN_EVENT_LANGUAGE 4 |
|---|
| 49 | |
|---|
| 50 | ## Simple plugins |
|---|
| 51 | if [info exists bMotion_plugins_simple] { unset bMotion_plugins_simple } |
|---|
| 52 | array set bMotion_plugins_simple {} |
|---|
| 53 | |
|---|
| 54 | ## Admin plugins (.bmotion) |
|---|
| 55 | if [info exists bMotion_plugins_admin] { unset bMotion_plugins_admin } |
|---|
| 56 | array set bMotion_plugins_admin {} |
|---|
| 57 | |
|---|
| 58 | ## complex plugins |
|---|
| 59 | if [info exists bMotion_plugins_complex] { unset bMotion_plugins_complex } |
|---|
| 60 | array set bMotion_plugins_complex {} |
|---|
| 61 | |
|---|
| 62 | ## output plugins |
|---|
| 63 | if [info exists bMotion_plugins_output] { unset bMotion_plugins_output } |
|---|
| 64 | array set bMotion_plugins_output {} |
|---|
| 65 | |
|---|
| 66 | ## action simple plugins |
|---|
| 67 | if [info exists bMotion_plugins_action_simple] { unset bMotion_plugins_action_simple } |
|---|
| 68 | array set bMotion_plugins_action_simple {} |
|---|
| 69 | |
|---|
| 70 | ## action complex plugins |
|---|
| 71 | if [info exists bMotion_plugins_action_complex] { unset bMotion_plugins_action_complex } |
|---|
| 72 | array set bMotion_plugins_action_complex {} |
|---|
| 73 | |
|---|
| 74 | ## irc_event plugins |
|---|
| 75 | if [info exists bMotion_plugins_irc_event] { unset bMotion_plugins_irc_event } |
|---|
| 76 | array set bMotion_plugins_irc_event {} |
|---|
| 77 | |
|---|
| 78 | ## management plugins |
|---|
| 79 | if [info exists bMotion_plugins_management] { unset bMotion_plugins_management } |
|---|
| 80 | array set bMotion_plugins_management {} |
|---|
| 81 | |
|---|
| 82 | # |
|---|
| 83 | # Load a simple plugin |
|---|
| 84 | proc bMotion_plugin_add_simple { id match chance response language} { |
|---|
| 85 | global bMotion_plugins_simple plugins bMotion_testing bMotion_noplugins |
|---|
| 86 | global BMOTION_PLUGIN_SIMPLE_MATCH BMOTION_PLUGIN_SIMPLE_CHANCE BMOTION_PLUGIN_SIMPLE_RESPONSE BMOTION_PLUGIN_SIMPLE_LANGUAGE |
|---|
| 87 | |
|---|
| 88 | if {$bMotion_testing == 0} { |
|---|
| 89 | catch { |
|---|
| 90 | set test $bMotion_plugins_simple($id) |
|---|
| 91 | bMotion_putloglev d * "bMotion: ALERT! Simple plugin $id is defined more than once" |
|---|
| 92 | return 0 |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | if [bMotion_plugin_check_allowed "simple:$id"] { |
|---|
| 96 | set bMotion_plugins_simple($id) [list $match $chance $response $language] |
|---|
| 97 | bMotion_putloglev 2 * "bMotion: added simple plugin: $id" |
|---|
| 98 | append plugins "$id," |
|---|
| 99 | return 1 |
|---|
| 100 | } |
|---|
| 101 | bMotion_putloglev d * "bMotion: ignoring disallowed plugin simple:$id" |
|---|
| 102 | set bMotion_noplugins 1 |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | ## Find a simple plugin |
|---|
| 107 | proc bMotion_plugin_find_simple { text lang { debug 0 }} { |
|---|
| 108 | bMotion_putloglev 3 * "bMotion_plugin_find_simple: text = $text, lang = $lang" |
|---|
| 109 | global bMotion_plugins_simple botnicks |
|---|
| 110 | global BMOTION_PLUGIN_SIMPLE_MATCH BMOTION_PLUGIN_SIMPLE_CHANCE BMOTION_PLUGIN_SIMPLE_RESPONSE BMOTION_PLUGIN_SIMPLE_LANGUAGE |
|---|
| 111 | |
|---|
| 112 | # TODO: bias |
|---|
| 113 | |
|---|
| 114 | set s [lsort [array names bMotion_plugins_simple]] |
|---|
| 115 | |
|---|
| 116 | foreach key $s { |
|---|
| 117 | set val $bMotion_plugins_simple($key) |
|---|
| 118 | set rexp [lindex $val $BMOTION_PLUGIN_SIMPLE_MATCH] |
|---|
| 119 | set chance [lindex $val $BMOTION_PLUGIN_SIMPLE_CHANCE] |
|---|
| 120 | set response [lindex $val $BMOTION_PLUGIN_SIMPLE_RESPONSE] |
|---|
| 121 | set language [lindex $val $BMOTION_PLUGIN_SIMPLE_LANGUAGE] |
|---|
| 122 | |
|---|
| 123 | if {[string match $lang $language] || ($language == "any")} { |
|---|
| 124 | set rexp [bMotionInsertString $rexp "%botnicks" "${botnicks}"] |
|---|
| 125 | if [regexp -nocase $rexp $text] { |
|---|
| 126 | set c [rand 100] |
|---|
| 127 | if {$debug} { |
|---|
| 128 | set c 0 |
|---|
| 129 | } |
|---|
| 130 | bMotion_putloglev 4 * "simple plugin $key matches" |
|---|
| 131 | if {[bMotion_plugins_settings_get "system" "last_simple" "" ""] == $key} { |
|---|
| 132 | bMotion_putloglev 3 * "trying to trigger same simple plugin twice in a row, aborting" |
|---|
| 133 | return "" |
|---|
| 134 | } |
|---|
| 135 | bMotion_plugins_settings_set "system" "last_simple" "" "" $key |
|---|
| 136 | if {$chance > $c} { |
|---|
| 137 | bMotion_putloglev 4 * " `- firing" |
|---|
| 138 | return $response |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | return "" |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | ## Load management plugin: TODO: Still generating dups? |
|---|
| 148 | proc bMotion_plugin_add_management { id match flags callback { language "" } { helpcallback "" } } { |
|---|
| 149 | global bMotion_plugins_management plugins bMotion_testing bMotion_noplugins |
|---|
| 150 | |
|---|
| 151 | if {$bMotion_testing == 0} { |
|---|
| 152 | catch { |
|---|
| 153 | set test $bMotion_plugins_management($id) |
|---|
| 154 | bMotion_putloglev d * "bMotion: ALERT! management plugin $id is defined more than once ($bMotion_testing)" |
|---|
| 155 | return 0 |
|---|
| 156 | } |
|---|
| 157 | if [bMotion_plugin_check_allowed "management:$id"] { |
|---|
| 158 | #set bMotion_plugins_management($id) "${match}Š${flags}Š${callback}Š${helpcallback}" |
|---|
| 159 | set bMotion_plugins_management($id) [list $match $flags $callback $helpcallback] |
|---|
| 160 | bMotion_putloglev 2 * "bMotion: added management plugin: $id" |
|---|
| 161 | append plugins "$id," |
|---|
| 162 | return 1 |
|---|
| 163 | } |
|---|
| 164 | bMotion_putloglev d * "bMotion: ignoring disallowed plugin management:$id" |
|---|
| 165 | set bMotion_noplugins 1 |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | ## Find management plugin |
|---|
| 170 | proc bMotion_plugin_find_management { text } { |
|---|
| 171 | global bMotion_plugins_management |
|---|
| 172 | global BMOTION_PLUGIN_MANAGEMENT_MATCH BMOTION_PLUGIN_MANAGEMENT_FLAGS BMOTION_PLUGIN_MANAGEMENT_CALLBACK BMOTION_PLUGIN_MANAGEMENT_HELPCALLBACK |
|---|
| 173 | |
|---|
| 174 | foreach key [array names bMotion_plugins_management] { |
|---|
| 175 | set val $bMotion_plugins_management($key) |
|---|
| 176 | set rexp [lindex $val $BMOTION_PLUGIN_MANAGEMENT_MATCH] |
|---|
| 177 | set flags [lindex $val $BMOTION_PLUGIN_MANAGEMENT_FLAGS] |
|---|
| 178 | set callback [lindex $val $BMOTION_PLUGIN_MANAGEMENT_CALLBACK] |
|---|
| 179 | if [regexp -nocase $rexp $text] { |
|---|
| 180 | return [list $flags $callback] |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | return "" |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | #find a management plugin's help callback |
|---|
| 187 | proc bMotion_plugin_find_management_help { name } { |
|---|
| 188 | global bMotion_plugins_management |
|---|
| 189 | global BMOTION_PLUGIN_MANAGEMENT_MATCH BMOTION_PLUGIN_MANAGEMENT_FLAGS BMOTION_PLUGIN_MANAGEMENT_CALLBACK BMOTION_PLUGIN_MANAGEMENT_HELPCALLBACK |
|---|
| 190 | |
|---|
| 191 | foreach key [array names bMotion_plugins_management] { |
|---|
| 192 | if [string match -nocase $name $key] { |
|---|
| 193 | set blah $bMotion_plugins_management($key) |
|---|
| 194 | set helpcallback [lindex $blah $BMOTION_PLUGIN_MANAGEMENT_HELPCALLBACK] |
|---|
| 195 | return $helpcallback |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | return "" |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | # |
|---|
| 202 | # Load a complex plugin |
|---|
| 203 | proc bMotion_plugin_add_complex { id match chance callback language } { |
|---|
| 204 | global bMotion_plugins_complex plugins bMotion_testing bMotion_noplugins |
|---|
| 205 | if {$bMotion_testing == 0} { |
|---|
| 206 | catch { |
|---|
| 207 | set test $bMotion_plugins_complex($id) |
|---|
| 208 | bMotion_putloglev d * "bMotion: ALERT! Complex plugin $id is defined more than once" |
|---|
| 209 | return 0 |
|---|
| 210 | } |
|---|
| 211 | if [bMotion_plugin_check_allowed "complex:$id"] { |
|---|
| 212 | #set bMotion_plugins_complex($id) "${match}Š${chance}Š${callback}Š${language}" |
|---|
| 213 | set bMotion_plugins_complex($id) [list $match $chance $callback $language] |
|---|
| 214 | |
|---|
| 215 | bMotion_putloglev 2 * "bMotion: added complex plugin: $id" |
|---|
| 216 | append plugins "$id," |
|---|
| 217 | return 1 |
|---|
| 218 | } |
|---|
| 219 | bMotion_putloglev d * "bMotion: ignoring disallowed plugin complex:$id" |
|---|
| 220 | set bMotion_noplugins 1 |
|---|
| 221 | } |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | # |
|---|
| 225 | # Find a complex plugin plugin |
|---|
| 226 | proc bMotion_plugin_find_complex { text lang { debug 0 }} { |
|---|
| 227 | global bMotion_plugins_complex botnicks |
|---|
| 228 | global BMOTION_PLUGIN_COMPLEX_MATCH BMOTION_PLUGIN_COMPLEX_CHANCE BMOTION_PLUGIN_COMPLEX_CALLBACK BMOTION_PLUGIN_COMPLEX_LANGUAGE |
|---|
| 229 | set result [list] |
|---|
| 230 | |
|---|
| 231 | bMotion_putloglev 5 * "bMotion_plugin_find_complex $text $lang $debug" |
|---|
| 232 | |
|---|
| 233 | set bias [bMotion_setting_get "bias"] |
|---|
| 234 | if {$bias == ""} { |
|---|
| 235 | set bias 1 |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | foreach key [lsort [array names bMotion_plugins_complex]] { |
|---|
| 239 | set val $bMotion_plugins_complex($key) |
|---|
| 240 | set rexp [lindex $val $BMOTION_PLUGIN_COMPLEX_MATCH] |
|---|
| 241 | set callback [lindex $val $BMOTION_PLUGIN_COMPLEX_CALLBACK] |
|---|
| 242 | set language [lindex $val $BMOTION_PLUGIN_COMPLEX_LANGUAGE] |
|---|
| 243 | |
|---|
| 244 | if {[string match $lang $language] || ($language == "any") || ($language == "all")} { |
|---|
| 245 | set rexp [bMotionInsertString $rexp "%botnicks" "${botnicks}"] |
|---|
| 246 | if [regexp -nocase $rexp $text] { |
|---|
| 247 | set chance [lindex $val $BMOTION_PLUGIN_COMPLEX_CHANCE] |
|---|
| 248 | set c [rand 100] |
|---|
| 249 | if {$debug} { |
|---|
| 250 | set c 0 |
|---|
| 251 | } |
|---|
| 252 | set chance [expr $chance * $bias] |
|---|
| 253 | bMotion_putloglev 4 * "matched complex:$key, chance is $chance, c is $c" |
|---|
| 254 | if {$chance > $c} { |
|---|
| 255 | bMotion_putloglev 4 * "chance is high enough, adding $callback" |
|---|
| 256 | lappend result $callback |
|---|
| 257 | } |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | return $result |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | # |
|---|
| 266 | # Load an output plugin |
|---|
| 267 | proc bMotion_plugin_add_output { id callback enabled language { priority 11 } } { |
|---|
| 268 | global bMotion_plugins_output plugins bMotion_testing bMotion_noplugins |
|---|
| 269 | |
|---|
| 270 | if {$bMotion_testing == 0} { |
|---|
| 271 | catch { |
|---|
| 272 | set test $bMotion_plugins_output($id) |
|---|
| 273 | bMotion_putloglev d * "bMotion: ALERT! Output plugin $id is defined more than once" |
|---|
| 274 | return 0 |
|---|
| 275 | } |
|---|
| 276 | if [bMotion_plugin_check_allowed "output:$id"] { |
|---|
| 277 | set bMotion_plugins_output($id) [list $callback $enabled $language $priority] |
|---|
| 278 | bMotion_putloglev 2 * "bMotion: added output plugin: $id" |
|---|
| 279 | append plugins "$id," |
|---|
| 280 | set bMotion_plugins_output_perchan($id) [list ] |
|---|
| 281 | return 1 |
|---|
| 282 | } |
|---|
| 283 | bMotion_putloglev d * "bMotion: ignoring disallowed plugin output:$id" |
|---|
| 284 | set bMotion_noplugins 1 |
|---|
| 285 | } |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | # |
|---|
| 289 | # Return a list of callbacks of output plugins |
|---|
| 290 | # Sorted by priority then name |
|---|
| 291 | # Includes plugins only enabled for the given channel |
|---|
| 292 | proc bMotion_plugin_find_output { lang { channel "" } { min_priority 0} { max_priority 100 } {name "" } } { |
|---|
| 293 | global bMotion_plugins_output botnicks |
|---|
| 294 | global bMotion_plugins_output_perchan |
|---|
| 295 | global BMOTION_PLUGIN_OUTPUT_PRIORITY BMOTION_PLUGIN_OUTPUT_ENABLED BMOTION_PLUGIN_OUTPUT_LANGUAGE BMOTION_PLUGIN_OUTPUT_CALLBACK |
|---|
| 296 | set result [list] |
|---|
| 297 | |
|---|
| 298 | bMotion_putloglev 5 * "bMotion_plugin_find_output lang=$lang channel=$channel min=$min_priority max=$max_priority name=$name" |
|---|
| 299 | |
|---|
| 300 | foreach key [lsort [array names bMotion_plugins_output]] { |
|---|
| 301 | set val $bMotion_plugins_output($key) |
|---|
| 302 | set callback [lindex $val $BMOTION_PLUGIN_OUTPUT_CALLBACK] |
|---|
| 303 | set enabled [lindex $val $BMOTION_PLUGIN_OUTPUT_ENABLED] |
|---|
| 304 | set language [lindex $val $BMOTION_PLUGIN_OUTPUT_LANGUAGE] |
|---|
| 305 | set priority [lindex $val $BMOTION_PLUGIN_OUTPUT_PRIORITY] |
|---|
| 306 | |
|---|
| 307 | if {($name != "") && ($name != $key)} { |
|---|
| 308 | bMotion_putloglev 3 * "macro: ignoring $key on name" |
|---|
| 309 | continue |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | if {!(($priority >= $min_priority) && ($priority <= $max_priority))} { |
|---|
| 313 | bMotion_putloglev 3 * "macro: ignoring $key on priority" |
|---|
| 314 | continue |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | if {(![string match $lang $language] && ($language != "any") && ($language != "all"))} { |
|---|
| 318 | bMotion_putloglev 3 * "macro: ignoring $key on language" |
|---|
| 319 | bMotion_putloglev 3 * "macro: plugin is $language, want $lang" |
|---|
| 320 | continue |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | if {$enabled == 1} { |
|---|
| 324 | lappend result [list $callback $priority] |
|---|
| 325 | } else { |
|---|
| 326 | bMotion_putloglev 1 * "Searching $key for channel $channel" |
|---|
| 327 | if {$channel != ""} { |
|---|
| 328 | catch { |
|---|
| 329 | set chanlist $bMotion_plugins_output_perchan($key) |
|---|
| 330 | if {[lsearch $chanlist $channel] > -1} { |
|---|
| 331 | lappend result [list $callback $priority] |
|---|
| 332 | bMotion_putloglev d * "Plugin $key is enabled for $channel" |
|---|
| 333 | } |
|---|
| 334 | } |
|---|
| 335 | } |
|---|
| 336 | } |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | # Sort by priority |
|---|
| 340 | set result [lsort -index 1 $result] |
|---|
| 341 | set result2 [list] |
|---|
| 342 | foreach entry $result { |
|---|
| 343 | lappend result2 [lindex $entry 0] |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | bMotion_putloglev 4 * "Returning list of plugins for $channel: $result2" |
|---|
| 347 | return $result2 |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | # |
|---|
| 351 | # Globally enable or disable an output plugin |
|---|
| 352 | proc bMotion_plugin_set_output { id enabled } { |
|---|
| 353 | global bMotion_plugins_output |
|---|
| 354 | global BMOTION_PLUGIN_OUTPUT_ENABLED |
|---|
| 355 | |
|---|
| 356 | if {($enabled == 0) || ($enabled == 1)} { |
|---|
| 357 | lset bMotion_plugins_output($id) $BMOTION_PLUGIN_OUTPUT_ENABLED $enabled |
|---|
| 358 | return 1 |
|---|
| 359 | } |
|---|
| 360 | return 0 |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | # |
|---|
| 364 | # Enable or disable an output plugin on a channel |
|---|
| 365 | proc bMotion_plugin_set_output_channel { id channel enabled } { |
|---|
| 366 | global bMotion_plugins_output_perchan |
|---|
| 367 | set channel [string tolower $channel] |
|---|
| 368 | |
|---|
| 369 | set current [list] |
|---|
| 370 | catch { |
|---|
| 371 | set current $bMotion_plugins_output_perchan($id) |
|---|
| 372 | } |
|---|
| 373 | if {$enabled == 1} { |
|---|
| 374 | set current [lappend current $channel] |
|---|
| 375 | } else { |
|---|
| 376 | set index [lsearch $current $channel] |
|---|
| 377 | if {$index > -1} { |
|---|
| 378 | set current [lreplace $current $index $index] |
|---|
| 379 | } |
|---|
| 380 | } |
|---|
| 381 | set current [lsort -unique $current] |
|---|
| 382 | |
|---|
| 383 | set bMotion_plugins_output_perchan($id) $current |
|---|
| 384 | return 1 |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | ## Load a simple action plugin |
|---|
| 389 | proc bMotion_plugin_add_action_simple { id match chance response language } { |
|---|
| 390 | global bMotion_plugins_action_simple plugins bMotion_testing bMotion_noplugins |
|---|
| 391 | |
|---|
| 392 | if {$bMotion_testing == 0} { |
|---|
| 393 | catch { |
|---|
| 394 | set test $bMotion_plugins_action_simple($id) |
|---|
| 395 | bMotion_putloglev d * "bMotion: ALERT! Simple plugin $id is defined more than once" |
|---|
| 396 | return 0 |
|---|
| 397 | } |
|---|
| 398 | if [bMotion_plugin_check_allowed "action_simple:$id"] { |
|---|
| 399 | #set bMotion_plugins_action_simple($id) "${match}Š${chance}Š${response}Š$language" |
|---|
| 400 | set bMotion_plugins_action_simple($id) [list $match $chance $response $language] |
|---|
| 401 | bMotion_putloglev 2 * "bMotion: added simple action plugin: $id" |
|---|
| 402 | append plugins "$id," |
|---|
| 403 | return 1 |
|---|
| 404 | } |
|---|
| 405 | bMotion_putloglev d * "bMotion: ignoring disallowed plugin action_simple:$id" |
|---|
| 406 | set bMotion_noplugins 1 |
|---|
| 407 | } |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | ## Find a simple action plugin |
|---|
| 412 | proc bMotion_plugin_find_action_simple { text lang { debug 0 } } { |
|---|
| 413 | global bMotion_plugins_action_simple botnicks |
|---|
| 414 | global BMOTION_PLUGIN_SIMPLE_MATCH BMOTION_PLUGIN_SIMPLE_CHANCE BMOTION_PLUGIN_SIMPLE_RESPONSE BMOTION_PLUGIN_SIMPLE_LANGUAGE |
|---|
| 415 | |
|---|
| 416 | foreach key [lsort [array names bMotion_plugins_action_simple]] { |
|---|
| 417 | set val $bMotion_plugins_action_simple($key) |
|---|
| 418 | set language [lindex $val $BMOTION_PLUGIN_SIMPLE_LANGUAGE] |
|---|
| 419 | if {[string match $lang $language] || ($language == "any")|| ($language == "all")} { |
|---|
| 420 | set rexp [lindex $val $BMOTION_PLUGIN_SIMPLE_MATCH] |
|---|
| 421 | set rexp [bMotionInsertString $rexp "%botnicks" "${botnicks}"] |
|---|
| 422 | if [regexp -nocase $rexp $text] { |
|---|
| 423 | set chance [lindex $val $BMOTION_PLUGIN_SIMPLE_CHANCE] |
|---|
| 424 | set c [rand 100] |
|---|
| 425 | if {$debug} { |
|---|
| 426 | set c 0 |
|---|
| 427 | } |
|---|
| 428 | if {$chance > $c} { |
|---|
| 429 | set response [lindex $val $BMOTION_PLUGIN_SIMPLE_RESPONSE] |
|---|
| 430 | return $response |
|---|
| 431 | } |
|---|
| 432 | } |
|---|
| 433 | } |
|---|
| 434 | } |
|---|
| 435 | return "" |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | ## Load a complex action plugin |
|---|
| 440 | proc bMotion_plugin_add_action_complex { id match chance callback language } { |
|---|
| 441 | global bMotion_plugins_action_complex plugins bMotion_testing bMotion_noplugins |
|---|
| 442 | if {$bMotion_testing == 0} { |
|---|
| 443 | catch { |
|---|
| 444 | set test $bMotion_plugins_action_complex($id) |
|---|
| 445 | bMotion_putloglev d * "bMotion: ALERT! Complex action plugin $id is defined more than once" |
|---|
| 446 | return 0 |
|---|
| 447 | } |
|---|
| 448 | if [bMotion_plugin_check_allowed "action_complex:$id"] { |
|---|
| 449 | #set bMotion_plugins_action_complex($id) "${match}Š${chance}Š${callback}Š${language}" |
|---|
| 450 | set bMotion_plugins_action_complex($id) [list $match $chance $callback $language] |
|---|
| 451 | bMotion_putloglev 2 * "bMotion: added complex action plugin: $id" |
|---|
| 452 | append plugins "$id," |
|---|
| 453 | return 1 |
|---|
| 454 | } |
|---|
| 455 | bMotion_putloglev d * "bMotion: ignoring disallowed plugin action_complex:$id" |
|---|
| 456 | set bMotion_noplugins 1 |
|---|
| 457 | } |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | ## Find a complex action plugin plugin |
|---|
| 461 | proc bMotion_plugin_find_action_complex { text lang { debug 0 } } { |
|---|
| 462 | global bMotion_plugins_action_complex botnicks |
|---|
| 463 | global BMOTION_PLUGIN_COMPLEX_MATCH BMOTION_PLUGIN_COMPLEX_CHANCE BMOTION_PLUGIN_COMPLEX_CALLBACK BMOTION_PLUGIN_COMPLEX_LANGUAGE |
|---|
| 464 | set result [list] |
|---|
| 465 | |
|---|
| 466 | foreach key [lsort [array names bMotion_plugins_action_complex]] { |
|---|
| 467 | set val $bMotion_plugins_action_complex($key) |
|---|
| 468 | set language [lindex $val $BMOTION_PLUGIN_COMPLEX_LANGUAGE] |
|---|
| 469 | if {[string match $language $lang] || ($language == "any")|| ($language == "all")} { |
|---|
| 470 | set rexp [lindex $val $BMOTION_PLUGIN_COMPLEX_MATCH] |
|---|
| 471 | set rexp [bMotionInsertString $rexp "%botnicks" "${botnicks}"] |
|---|
| 472 | if [regexp -nocase $rexp $text] { |
|---|
| 473 | set chance [lindex $val $BMOTION_PLUGIN_COMPLEX_CHANCE] |
|---|
| 474 | bMotion_putloglev 4 * "matched: $key" |
|---|
| 475 | set c [rand 100] |
|---|
| 476 | if {$debug} { |
|---|
| 477 | set c 0 |
|---|
| 478 | } |
|---|
| 479 | if {$chance > $c} { |
|---|
| 480 | set callback [lindex $val $BMOTION_PLUGIN_COMPLEX_CALLBACK] |
|---|
| 481 | lappend result $callback |
|---|
| 482 | } |
|---|
| 483 | } |
|---|
| 484 | } |
|---|
| 485 | } |
|---|
| 486 | return $result |
|---|
| 487 | } |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | ############################################################################### |
|---|
| 491 | |
|---|
| 492 | proc bMotion_plugin_check_depend { depends } { |
|---|
| 493 | #pass a string in the format "type:plugin,type:plugin,..." |
|---|
| 494 | if {$depends == ""} { |
|---|
| 495 | return 1 |
|---|
| 496 | } |
|---|
| 497 | |
|---|
| 498 | set result 1 |
|---|
| 499 | |
|---|
| 500 | set blah [split $depends ","] |
|---|
| 501 | foreach depend $blah { |
|---|
| 502 | set blah2 [split $depend ":"] |
|---|
| 503 | set t [lindex $blah2 0] |
|---|
| 504 | set id [lindex $blah2 1] |
|---|
| 505 | set a "bMotion_plugins_$t" |
|---|
| 506 | upvar #0 $a ar |
|---|
| 507 | bMotion_putloglev 1 * "bMotion: checking $a for $id ..." |
|---|
| 508 | set temp [array names ar $id] |
|---|
| 509 | if {[llength $temp] == 0} { |
|---|
| 510 | set result 0 |
|---|
| 511 | bMotion_putloglev d * "bMotion: Missing dependency $t:$id" |
|---|
| 512 | } |
|---|
| 513 | } |
|---|
| 514 | return $result |
|---|
| 515 | } |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | ############################################################################### |
|---|
| 520 | |
|---|
| 521 | proc bMotion_plugin_check_allowed { name } { |
|---|
| 522 | #pass a string in the format "type:plugin" |
|---|
| 523 | #setting in config should be "type:plugin,type:plugin,..." |
|---|
| 524 | global bMotionSettings |
|---|
| 525 | |
|---|
| 526 | set disallowed "" |
|---|
| 527 | |
|---|
| 528 | catch { |
|---|
| 529 | set disallowed $bMotionSettings(noPlugin) |
|---|
| 530 | } |
|---|
| 531 | |
|---|
| 532 | if {$disallowed == ""} { |
|---|
| 533 | return 1 |
|---|
| 534 | } |
|---|
| 535 | |
|---|
| 536 | bMotion_putloglev 4 * "bMotion: checking $name against $disallowed" |
|---|
| 537 | |
|---|
| 538 | set blah [split $disallowed ","] |
|---|
| 539 | foreach plugin $blah { |
|---|
| 540 | if {$plugin == $name} { |
|---|
| 541 | return 0 |
|---|
| 542 | } |
|---|
| 543 | } |
|---|
| 544 | return 1 |
|---|
| 545 | } |
|---|
| 546 | |
|---|
| 547 | ################################################################################ |
|---|
| 548 | |
|---|
| 549 | ## dev: simsea |
|---|
| 550 | ## Load an irc event response plugin |
|---|
| 551 | proc bMotion_plugin_add_irc_event { id type match chance callback language } { |
|---|
| 552 | if {![regexp -nocase "nick|join|quit|part|split" $type]} { |
|---|
| 553 | bMotion_putloglev d * "bMotion: ALERT! IRC Event plugin $id has an invalid type $type" |
|---|
| 554 | return 0 |
|---|
| 555 | } |
|---|
| 556 | |
|---|
| 557 | global bMotion_plugins_irc_event plugins bMotion_testing bMotion_noplugins |
|---|
| 558 | if {$bMotion_testing == 0} { |
|---|
| 559 | catch { |
|---|
| 560 | set test $bMotion_plugins_irc_event($id) |
|---|
| 561 | bMotion_putloglev d * "bMotion: ALERT! IRC Event plugin $id is defined more than once" |
|---|
| 562 | return 0 |
|---|
| 563 | } |
|---|
| 564 | if [bMotion_plugin_check_allowed "irc:$id"] { |
|---|
| 565 | #set bMotion_plugins_irc_event($id) "$typeŠ${match}Š$chanceŠ$callbackŠ$language" |
|---|
| 566 | set bMotion_plugins_irc_event($id) [list $type $match $chance $callback $language] |
|---|
| 567 | bMotion_putloglev 2 * "bMotion: added IRC event plugin: $id" |
|---|
| 568 | append plugins "$id," |
|---|
| 569 | return 1 |
|---|
| 570 | } |
|---|
| 571 | bMotion_putloglev d * "bMotion: ignoring disallowed plugin irc:$id" |
|---|
| 572 | set bMotion_noplugins 1 |
|---|
| 573 | } |
|---|
| 574 | } |
|---|
| 575 | |
|---|
| 576 | ## Find an IRC Event response plugin plugin |
|---|
| 577 | proc bMotion_plugin_find_irc_event { text type lang { debug 0 } } { |
|---|
| 578 | if {![regexp -nocase "nick|join|quit|part|split" $type]} { |
|---|
| 579 | bMotion_putloglev d * "bMotion: IRC Event search type $type is invalid" |
|---|
| 580 | return 0 |
|---|
| 581 | } |
|---|
| 582 | global bMotion_plugins_irc_event botnicks |
|---|
| 583 | global BMOTION_PLUGIN_EVENT_TYPE BMOTION_PLUGIN_EVENT_MATCH BMOTION_PLUGIN_EVENT_CHANCE BMOTION_PLUGIN_EVENT_CALLBACK BMOTION_PLUGIN_EVENT_LANGUAGE |
|---|
| 584 | set s [lsort [array names bMotion_plugins_irc_event]] |
|---|
| 585 | set result [list] |
|---|
| 586 | |
|---|
| 587 | foreach key $s { |
|---|
| 588 | set val $bMotion_plugins_irc_event($key) |
|---|
| 589 | set etype [lindex $val $BMOTION_PLUGIN_EVENT_TYPE] |
|---|
| 590 | if {[string match $type $etype]} { |
|---|
| 591 | set language [lindex $val $BMOTION_PLUGIN_EVENT_LANGUAGE] |
|---|
| 592 | if {[string match $language $lang] || ($language == "any") || ($language == "all")} { |
|---|
| 593 | set rexp [lindex $val $BMOTION_PLUGIN_EVENT_MATCH] |
|---|
| 594 | if [regexp -nocase $rexp $text] { |
|---|
| 595 | set chance [lindex $val $BMOTION_PLUGIN_EVENT_CHANCE] |
|---|
| 596 | set c [rand 100] |
|---|
| 597 | if {$debug} { |
|---|
| 598 | set c 0 |
|---|
| 599 | } |
|---|
| 600 | if {$chance > $c} { |
|---|
| 601 | set callback [lindex $val $BMOTION_PLUGIN_EVENT_CALLBACK] |
|---|
| 602 | lappend result $callback |
|---|
| 603 | } |
|---|
| 604 | } |
|---|
| 605 | } |
|---|
| 606 | } |
|---|
| 607 | } |
|---|
| 608 | return $result |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | |
|---|
| 612 | ################################################################################ |
|---|
| 613 | |
|---|
| 614 | ## Load the simple plugins |
|---|
| 615 | catch { source "$bMotionPlugins/simple.tcl" } |
|---|
| 616 | |
|---|
| 617 | ## Load the admin (management) plugins |
|---|
| 618 | catch { source "$bMotionPlugins/admin.tcl" } |
|---|
| 619 | |
|---|
| 620 | ## Load the complex plugins |
|---|
| 621 | catch { source "$bMotionPlugins/complex.tcl" } |
|---|
| 622 | |
|---|
| 623 | ## Load the output plugins |
|---|
| 624 | catch { source "$bMotionPlugins/output.tcl" } |
|---|
| 625 | |
|---|
| 626 | ## Load the simple action plugins |
|---|
| 627 | catch { source "$bMotionPlugins/action_simple.tcl" } |
|---|
| 628 | |
|---|
| 629 | ## Load the complex action plugins |
|---|
| 630 | catch { source "$bMotionPlugins/action_complex.tcl" } |
|---|
| 631 | |
|---|
| 632 | ## Load the irc event plugins |
|---|
| 633 | catch { source "$bMotionPlugins/irc_event.tcl" } |
|---|
| 634 | |
|---|
| 635 | ## clean this up, not used again |
|---|
| 636 | unset bMotion_noplugins |
|---|
| 637 | |
|---|
| 638 | ### null plugin routine for faking plugins |
|---|
| 639 | proc bMotion_plugin_null { {a ""} {b ""} {c ""} {d ""} {e ""} } { |
|---|
| 640 | return 0 |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | # bMotion_plugin_history_add |
|---|
| 644 | # |
|---|
| 645 | # adds a plugin name to the history list, keeping the list to 10 items |
|---|
| 646 | # will not add the plugin if the last one is identical |
|---|
| 647 | proc bMotion_plugin_history_add { channel type plugin } { |
|---|
| 648 | global bMotionPluginHistory |
|---|
| 649 | |
|---|
| 650 | set historyEntry "$channel:$type:$plugin" |
|---|
| 651 | if {$historyEntry == [lindex $bMotionPluginHistory end]} { |
|---|
| 652 | bMotion_putloglev 2 * "Skipping duplicate plugin history entry $historyEntry" |
|---|
| 653 | return 0 |
|---|
| 654 | } |
|---|
| 655 | |
|---|
| 656 | bMotion_putloglev 2 * "Added $historyEntry to plugin history" |
|---|
| 657 | lappend bMotionPluginHistory $historyEntry |
|---|
| 658 | |
|---|
| 659 | if {[llength $bMotionPluginHistory] > 10} { |
|---|
| 660 | set bMotionPluginHistory [lreplace $bMotionPluginHistory end-10 end] |
|---|
| 661 | } |
|---|
| 662 | return 1 |
|---|
| 663 | } |
|---|
| 664 | |
|---|
| 665 | # bMotion_plugin_history_check |
|---|
| 666 | # |
|---|
| 667 | # returns 0 if the plugin hasn't fired recently in the channel |
|---|
| 668 | # else returns position in list |
|---|
| 669 | proc bMotion_plugin_history_check { channel type plugin } { |
|---|
| 670 | global bMotionPluginHistory |
|---|
| 671 | |
|---|
| 672 | return [expr [lsearch $bMotionPluginHistory "$channel:$type:$plugin"] + 1] |
|---|
| 673 | } |
|---|
| 674 | |
|---|
| 675 | bMotion_putloglev d * "bMotion: plugins module loaded" |
|---|
| 676 | |
|---|