~vanhoof/+junk/znc

« back to all changes in this revision

Viewing changes to modules/extra/binds.tcl

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Matthäi
  • Date: 2009-09-25 08:07:42 UTC
  • mfrom: (1.3.1 upstream) (10.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925080742-vvh22yi4969453r7
Tags: 0.076-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2004-2009  See the AUTHORS file for details.
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify it
 
4
# under the terms of the GNU General Public License version 2 as published
 
5
# by the Free Software Foundation.
 
6
#
 
7
# Binds module to process incoming messages with ZNC modtcl
 
8
#
 
9
# Supported bind types: pubm/pub, time, evnt, nick, bot, dcc, kick
 
10
#  evnt: prerehash,rehash,init-server,disconnect-server
 
11
#
 
12
 
 
13
namespace eval Binds {
 
14
 
 
15
# vars
 
16
        if {![info exists Binds::List]} {
 
17
                variable List [list]
 
18
        }
 
19
 
 
20
# procs
 
21
        proc Add {type flags cmd {procname ""}} {
 
22
                if {![regexp {^(pub|pubm|nick|mode|raw|bot|time|evnt|dcc|kick)$} $type]} {
 
23
                        PutModule "Tcl error: Bind type: $type not supported"
 
24
                        return
 
25
                }
 
26
                # ToDo: Flags check from user info (IsAdmin, etc)
 
27
                if {$procname == ""} {
 
28
                        PutModule "Tcl error: Without a proc binds are useless"
 
29
                        return
 
30
                }
 
31
                # ToDo: bind hit counter
 
32
                if {[lsearch $Binds::List "$type $flags [list $cmd] 0 [list $procname]"] == -1} {
 
33
                        lappend Binds::List "$type $flags [list $cmd] 0 [list $procname]"
 
34
                }
 
35
                return $cmd
 
36
        }
 
37
 
 
38
        proc Del {type flags cmd procname} {
 
39
                if {[set match [lsearch [binds] "$type $flags $cmd 0 $procname"]] != -1 || [set match [lsearch [binds] "$type $flags {${cmd}} 0 $procname"]] != -1} {
 
40
                        set Binds::List [lreplace $Binds::List $match $match]
 
41
                        return $cmd
 
42
                } else {
 
43
                        error "Tcl error: no such binding"
 
44
                }
 
45
        }
 
46
 
 
47
        proc ProcessPubm {nick user handle channel text} {
 
48
                # Loop bind list and execute
 
49
                foreach n [binds pub] {
 
50
                        if {[string match [lindex $n 2] [lindex [split $text] 0]]} {
 
51
                                [lindex $n 4] $nick $user $handle $channel [lrange [join $text] 1 end]
 
52
                        }
 
53
                }
 
54
                foreach {type flags mask hits proc} [join [binds pubm]] {
 
55
                        regsub {^%} $mask {*} mask
 
56
                        if {[ModuleLoaded crypt]} {regsub {^�} $nick {} nick}
 
57
                        if {[string match -nocase $mask "$channel $text"]} {
 
58
                                $proc $nick $user $handle $channel $text
 
59
                        }
 
60
                }
 
61
        }
 
62
 
 
63
        proc ProcessTime {} {
 
64
                if {[clock format [clock seconds] -format "%S"] != 0} {return}
 
65
                set time [clock format [clock seconds] -format "%M %H %d %m %Y"]
 
66
                foreach {mi ho da mo ye} $time {}
 
67
                # Loop bind list and execute
 
68
                foreach n [binds time] {
 
69
                        if {[string match [lindex $n 2] $time]} {
 
70
                                [lindex $n 4] $mi $ho $da $mo $ye
 
71
                        }
 
72
                }
 
73
        }
 
74
 
 
75
        proc ProcessEvnt {event} {
 
76
                foreach n [binds evnt] {
 
77
                        if {[string match [lindex $n 2] $event]} {
 
78
                                [lindex $n 4] $event
 
79
                        }
 
80
                }
 
81
                switch $event {
 
82
                        init-server {
 
83
                                set ::botnick [GetCurNick]
 
84
                                set ::server [GetServer]
 
85
                                set ::server-online [expr [GetServerOnline] / 1000]
 
86
                        }
 
87
                        disconnect-server {
 
88
                                set ::botnick ""
 
89
                                set ::server ""
 
90
                                set ::server-online 0
 
91
                        }
 
92
                }
 
93
        }
 
94
 
 
95
        proc ProcessBot {from-bot cmd text} {
 
96
                foreach n [binds bot] {
 
97
                        if {[string match [lindex $n 2] $cmd]} {
 
98
                                [lindex $n 4] ${from-bot} $cmd $text
 
99
                        }
 
100
                }
 
101
        }
 
102
 
 
103
        proc ProcessNick {nick user handle channel newnick} {
 
104
                if {$nick == $::botnick} {set ::botnick $newnick}
 
105
                foreach n [binds nick] {
 
106
                        if {[string match [lindex $n 2] $newnick]} {
 
107
                                [lindex $n 4] $nick $user $handle $channel $newnick
 
108
                        }
 
109
                }
 
110
        }
 
111
 
 
112
        proc ProcessKick {nick user handle channel target reason} {
 
113
                foreach n [binds kick] {
 
114
                        if {[string match [lindex $n 2 0] $channel] && [string match [lindex $n 2 1] $target]} {
 
115
                                [lindex $n 4] $nick $user $handle $channel $target $reason
 
116
                        }
 
117
                }
 
118
        }
 
119
 
 
120
        proc ProcessDcc {handle idx text} {
 
121
                set match 0
 
122
                foreach n [binds dcc] {
 
123
                        if {[string match -nocase [lindex $n 2] [string range [lindex $text 0] 1 end]]} {
 
124
                                [lindex $n 4] $handle $idx [lrange $text 1 end]
 
125
                                set match 1
 
126
                        }
 
127
                }
 
128
                if {!$match} {
 
129
                        PutModule "Error, dcc trigger '[string range [lindex $text 0] 1 end]' doesnt exist"
 
130
                }
 
131
        }
 
132
}
 
133
 
 
134
# Provide aliases according to eggdrop specs
 
135
proc ::bind {type flags cmd {procname ""}} {Binds::Add $type $flags $cmd $procname}
 
136
proc ::unbind {type flags cmd procname} {Binds::Del $type $flags $cmd $procname}
 
137
proc ::binds {{type ""}} {if {$type != ""} {set type "$type "};return [lsearch -all -inline $Binds::List "$type*"]}
 
138
proc ::bindlist {{type ""}} {foreach bind $Binds::List {PutModule "$bind"}}
 
139
 
 
140
PutModule "modtcl script loaded: Binds v0.1"