source: trunk/modules/queue.tcl @ 1143

Revision 1083, 5.5 KB checked in by james, 21 months ago (diff)

add option for queue output to be higher priority (for eggdrop)
add yes, you did plugin

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#bMotion - queue functions
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# init our counters
24#TODO: add counters
25
26#A rehash should kill the queue
27set bMotion_queue [list]
28set bMotion_queue_runny 1
29
30# queue format is:
31#  list of:
32#                int: number of seconds until line should be output
33#                str: target
34#                str: content
35
36
37# bMotion_queue_run
38#
39# Processes the queue, reducing all the times by 1. If anything hits or goes below
40# 0 then it is sent to output
41# This also sends stuff to remote bots
42proc bMotion_queue_run { {force 0} } {
43        global bMotion_queue bMotion_queue_runny
44
45        bMotion_putloglev 4 * "bMotion_queue_run $force"
46
47        if {$bMotion_queue_runny == 0} {
48                if {$force == 0} {
49                #queue is frozen
50                        return 0
51                } else {
52                        bMotion_putloglev d * "Running queue once while frozen"
53                }
54        }
55
56        set tempqueue [list]
57        bMotion_putloglev 3 * "Running output queue..."
58        foreach item $bMotion_queue {
59                set sec [lindex $item 0]
60                incr sec -1
61                set target [lindex $item 1]
62                set content [lindex $item 2]
63                if {$sec < 1} {
64                #time to output this
65                        bMotion_putloglev 4 * "queue: NOW $target :$content"
66                        if [regexp {^@([^@]+)?@(.+)} $content matches bot text] {
67                                if {$bot == ""} {
68                                        bMotion_putloglev d * "bMotion: WARNING - tried to send text to a null bot o_O"
69                                } else {
70                                        bMotionSendSayChan $target $text $bot
71                                }
72                        } else {
73                                if {$content != ""} {
74                                        if [bMotion_setting_get "bitlbee"] {
75                                                global bMotionOriginalNick
76                                                #make sure the line doesn't start with the nick already
77                                                if {$bMotionOriginalNick == ""} {
78                                                        set bMotionOriginalNick [bMotion_choose_random_user $target 0 ""]
79                                                }
80                                                if {![regexp -nocase "^$bMotionOriginalNick:" $content]} {
81                                                        set content "$bMotionOriginalNick: $content"
82                                                }
83                                                set bMotionOriginalNick ""
84                                                bMotion_putloglev d * "bitlbee outgoing: $content"
85                                        }
86                                        if {[bMotion_setting_get "outputpriority"] == 1} {
87                                                putserv "PRIVMSG $target :$content"
88                                        } else {
89                                                puthelp "PRIVMSG $target :$content"
90                                        }
91                                        set bMotionCache($target,last) 1
92                                }
93                        }
94                } else {
95                #put it back into queue
96                bMotion_putloglev 4 * "queue: ${sec}s: $target :$content"
97                        lappend tempqueue [list $sec $target $content]
98                }
99        }
100        set bMotion_queue $tempqueue
101}
102
103
104# bMotion_queue_get_delay
105#
106# Returns the number of seconds something to wait to be last in the queue
107proc bMotion_queue_get_delay { } {
108        global bMotion_queue
109        return [expr 2 + [llength $bMotion_queue]]
110}
111
112# bMotion_queue_add
113#
114# Adds some output to the queue
115proc bMotion_queue_add { target content {delay 0} } {
116        global bMotion_queue
117
118        #calculate line delay
119        set delay [expr $delay == 0 ? [bMotion_queue_get_delay] : $delay]
120        bMotion_putloglev 1 * "queuing output '$content' for '$target' with ${delay}s delay"
121        lappend bMotion_queue [list $delay $target $content]
122}
123
124# bMotion_queue_add_now
125#
126# Adds some output to the head of the queue
127proc bMotion_queue_add_now { target content } {
128        global bMotion_queue
129
130        #no delay
131        set delay 0
132        bMotion_putloglev 1 * "queuing output '$content' for '$target' with 0s delay"
133        lappend bMotion_queue [list $delay $target $content]
134}
135
136# bMotion_queue_callback
137#
138# This is the timer function
139proc bMotion_queue_callback { } {
140        global bMotion_queue
141        utimer 2 bMotion_queue_callback
142        if {[llength $bMotion_queue] > 0} {
143                bMotion_queue_run
144        }
145}
146
147# bMotion_queue_size
148#
149# Get the size of the queue in an implementation-independent fashion
150proc bMotion_queue_size { } {
151        global bMotion_queue
152        return [llength bMotion_queue]
153}
154
155# bMotion_queue_flush
156#
157# Clears the queue
158proc bMotion_queue_flush { } {
159        global bMotion_queue
160        set bMotion_queue [list]
161}
162
163# bMotion_queue_freeze
164#
165# Stops queue output
166proc bMotion_queue_freeze { } {
167        global bMotion_queue_runny
168
169        set bMotion_queue_runny 0
170        bMotion_putloglev d * "Freezing output queue"
171}
172
173proc bMotion_queue_thaw { } {
174        global bMotion_queue_runny
175
176        set bMotion_queue_runny 1
177        bMotion_putloglev d * "Thawing output queue"
178}
179
180proc bMotion_queue_dupecheck { text channel } {
181        global bMotion_queue
182
183        bMotion_putloglev 4 * "bMotion_queue_dupecheck $text"
184       
185        set text [string tolower $text]
186
187        # TODO: maybe remove some punctuation, but not things like :)
188
189        set tempqueue [list]
190
191        foreach item $bMotion_queue {
192                if {$channel == [lindex $item 1]} {
193                        lappend tempqueue $item
194                        continue
195                }
196
197                set content [string tolower [lindex $item 2]]
198
199                if {$content == $text} {
200                        # delete this entry by setting it to nothing; the next queue run will remove it
201                        set item [lreplace $item 2 2 ""]
202                        bMotion_putloglev 2 * "queue: removed item '$content' as it's similar to text '$text'"
203                }
204
205                lappend tempqueue $item
206        }
207
208        set bMotion_queue $tempqueue
209}
210
211# init timer
212utimer 1 bMotion_queue_callback
Note: See TracBrowser for help on using the repository browser.