Your IP : 216.73.216.224


Current Path : /home/mudbot/eggdrop-keep/scripts/
Upload File :
Current File : //home/mudbot/eggdrop-keep/scripts/topic-guard1.6.tcl

# t o p i c  -  g u a r d   v 1 . 6 	                                        #
# by:   gozzip@Undernet                                                         #                                    
# mail: 2-lazy@gmx.net                                                          #                     
# ----                                                                          #     
# This is my very first script,                                                 #                               
# so I hope you find it usefull - I sure did.                                   #
# Thanks to the nice ppl at #eggdrop@Undernet                                   #
# and of course my friend furt (#eggdrop.no@Undernet)!                          #            
#                                                                               #
# Features:                                                                     #
# o Topic will never be blank! (for more than 10 min max)                       #
# o You can lock/unlock the topic in each chan you spesify                      #
# o You can have a different default-topic in each chan you spesify (doh!) :P   #
# o Public commands (requires +o flag or @ in the chan)                         #
# o Checks all channels for topic-changes every 10 min and                      #
#   on topic commands (mode changes)                                            #
#                                                                               #
#                                                                               #
# The script has two settings - locked and unlocked.                            #                                                   
# If the topic-mode is set to locked, it wont allow                             #                                                    
# other topics to be set, but if its unlocked the                               #                                                   
# bot will only change the topic if the current one is blank.                   #
# It checks for changes every 10 minutes,                                       #
# on a topic change commands, and on public commands.                           #                                     
#                                                                               # 
# Commands:                                                                     #
# (triggered in the chan - without quotes of course)                            #                                 
# To lock the topicmode in a chan:                ".locktopic"                  #                                          
# To unlock the topicmode in a chan:              ".unlocktopic"                #                                             
# To view the current topicmode:                  ".topicmode"                  #                                       
# To add a new default topic for the chan:        ".def-topic <topic>"          #
# To view the current default topic for the chan: ".def-topic"                  # 
# For help:                                       ".topic-help"                 #
# Aliases:                                        ".topichelp" and ".deftopic"  #
#                                                                               # 
# All chans (can)have their own default topic,                                  #                                             
# and topic mode. Each chan must be in the "topic(chans)"                       #                                                       
# for them to be included. Files will be saves for each                         #                                                      
# chan too - one directory up from the script's locations,                      #                                                         
# usually directly in the eggdrop folder.                                       #                                       
#                                                                               #
# When the bot joins a channel (it doesnt have OPs right away) - it will	#
# produce an error sent to the partyline.. I'm not going to fix this, sorry	#
#										#
# If you want to use different commands than the one I have set,                #
# just change the binds below - but be carefull.                                #
# 										#
# Updates:									#	
# o v1.0  - Released								#
# o v1.2  - Fixed timer (bind time) bug						#
# o v1.3  - Rewrote all the procs, and made som tcl-design changes.		#
#           Changed the bindings.						#
#           Added a help trigger.                                               #
# o v1.4  - Fixed a typo :D                         				#
# o v1.5  - ppl with the +o flag on the chan, can now use the script            #
#           without being OPed at the time.                                     #
# o v1.6  - Fixed it so that .refreshtopic sets the topic again, even if the    #
#           bot sees it allready. This is useful during bad sync situations	#
#	    between the IRC servers.						#
#	    I also made it so that the bot does not check the topic every 	#
#	    $topic(timer) minutes in locked mode, it actually sets it.		#
#	    This is to try and get out of a desync situation.			#
#										#
# happy surfin'									#
#										#
#################################################################################


## C O N F I G :

#Channels the script will apply to
set topic(chans) "#MineCity"   

#The default topic - can of course be set individually for each chan later
set topic(def) "Welcome to the #MineCity IRC channel. For more great MineTest content visit https://MineCity.online"

#Default topic mode. 0: unlocked, 1: locked
set topic(mode) 1

#now that you are set, just load the script, and let it do the work for you :D


## S C R I P T :  
        
                          
#Binds:
bind pub - ".locktopic" 	locktopic
bind pub - ".unlocktopic" 	unlocktopic
bind pub - ".topicmode" 	topicmode
bind pub - ".def-topic" 	def-topic
bind pub - ".deftopic" 		def-topic
bind pub - ".topic-help" 	help
bind pub - ".topichelp" 	help
bind pub - ".refreshtopic" 	refreshtopic
bind pub - ".refresh-topic" 	refreshtopic
bind topc - "*" 		check_topic_topc

#Global variables (not userdefined)
set topic(chans) [string tolower $topic(chans)]
set topic(ID) "topic-guard:"
set topic(timer) 10


#The proc's:

##CHECK_TOPIC_TIME
#bound to a timer, every 10 min it checks the topic..
proc check_topic_time {} {
 
 #load some global vars
 global topic
 
 #check all channels
 foreach chan [split $topic(chans)] {
   
   #set some variables
   set topicmode [load_topicmode $chan]
   set deftopic [load_deftopic $chan]
   set curtopic [topic $chan]
   
   #check if the bot is op, else return
   if {![botisop $chan]} {
      putlog "$topic(ID) Cant guard the topic in $chan if I dont have op there.."
      return 0
   }
   
   #a switch, regarding if the topicmode is locked or unlocked
   switch $topicmode {
      0 {
          if {$curtopic == ""} { 
             putserv "TOPIC $chan :$deftopic" 
             putlog "$topic(ID) Changed topic in $chan because it was empty..."
          }
        } 
      1 {
          if {$curtopic != $deftopic} { 
             putserv "TOPIC $chan :$deftopic"
             putlog "$topic(ID) Changed topic in $chan because it differed from the default topic (locked mode).."
          } else {
             putserv "topic $chan :$deftopic"
          }
        }
   } ;#end switch
 } ;#end foreach
      
 timer $topic(timer) {check_topic_time}

} ;#end proc


##CHECK_TOPIC_TOPC
#bound to topic changes in the channel..
proc check_topic_topc {nick uhost handle chan curtopic} {
  
  #laod some global vars
  global topic
  
  #check if the channel is enabled, if not - return
  set enabled [lsearch -exact $topic(chans) [string tolower $chan]]
  if {$enabled == -1} {return 0}
  
  #check if the bot has OPs
  if {![botisop $chan]} {
     putlog "$topic(ID) Cant guard the topic in $chan if I dont have op there.."
     return 0
  }
  
  #set some variables
  set topicmode [load_topicmode [string tolower $chan]]
  set deftopic [load_deftopic [string tolower $chan]]
  
  #a switch, regarding if the topicmode is locked or unlocked   
  switch $topicmode {
    0 {
        if {$curtopic == ""} {
           putserv "TOPIC $chan :$deftopic"
           putlog "$topic(ID) Changed topic in $chan because it was empty..."
        }
      }
    1 {
        if {$curtopic != $deftopic} {
           putserv "TOPIC $chan :$deftopic"
           putlog "$topic(ID) Changed topic in $chan because it differed from the default topic (locked mode).."
        }
      }
  } ;#end switch
             
} ;#end proc


##DEF-TOPIC
#Bound to ".def-topic" and ".deftopic"
proc def-topic {nick uhost handle chan text} {
  
  #load some global vars
  global topic
  
  #check if the chan is enabled, if not - return
  set enabled [lsearch -exact $topic(chans) [string tolower $chan]]
  if {$enabled == -1} {return 0}
  
  #check if the dude is an op, if not - return
  if {([matchattr $handle o $chan]) || ([isop $nick $chan])} {
  
     #set some vars
     set topicmode [load_topicmode [string tolower $chan]]
     set curtopic [topic [string tolower $chan]]
  
     #do your thing..
     if {$text == ""} {
        set deftopic [load_deftopic [string tolower $chan]]
        puthelp "privmsg $chan :current default topic for $chan is: '$deftopic'"
     } else {
        set deftopic $text
        save_deftopic $chan $deftopic
        putlog "$topic(ID) default topic for $chan changed to '$deftopic'"
        puthelp "privmsg $chan :default topic for $chan changed..."
     
        #return if the topicmode is unlocked
        if {$topicmode == 0} {return}
     
        #check the topic against the saved deftopic
        if {$curtopic == ""} {
           putserv "TOPIC $chan :$deftopic"
           putlog "$topic(ID) Changed topic in $chan because it was empty..."
        }
        if {$curtopic != $deftopic} {
           putserv "TOPIC $chan :$deftopic"
           putlog "$topic(ID) Changed topic in $chan because it differed from the default topic (locked mode).."
        }
     }
  }
  
} ;#end proc


##LOCKTOPIC
#bound to ".locktopic"
proc locktopic {nick uhost handle chan text} {
  
  #load some variables
  global topic
  
  #check if the chan is enabled, if not - return
  set enabled [lsearch -exact $topic(chans) [string tolower $chan]]
  if {$enabled == -1} {return 0}
  
  #check if the dude is an op, if not - return
  if {([matchattr $handle o $chan]) || ([isop $nick $chan])} {
  
     #set some vars
     set topicmode 1
     set curtopic [topic [string tolower $chan]]
     set deftopic [load_deftopic [string tolower $chan]]
  
     #return the msg to the chan
     puthelp "privmsg $chan :topicmode for $chan is now locked."
  
     #save the topic the the file
     save_topicmode [string tolower $chan] $topicmode
  
     #check the current topic, and act on it if nessisary
     if {$curtopic == ""} {
        putserv "TOPIC $chan :$deftopic"
        putlog "$topic(ID) Changed topic in $chan because it was empty..."
     }
     if {$curtopic != $deftopic} {
        putserv "TOPIC $chan :$deftopic"
        putlog "$topic(ID) Changed topic in $chan because it differed from the default topic (locked mode).."
     }
  }
     
} ;#end proc


##UNLOCKTOPIC
#bound to ".unlocktopic"
proc unlocktopic {nick uhost handle chan text} {
  
  #load some variables
  global topic
  
  #check if the chan is enabled, if not - return
  set enabled [lsearch -exact $topic(chans) [string tolower $chan]]
  if {$enabled == -1} {return 0}
  
  #check if the dude is an op, if not - return
  if {([matchattr $handle o $chan]) || ([isop $nick $chan])} {
  
     #set some vars
     set topicmode 0
    
     #save the topic the the file
     save_topicmode [string tolower $chan] $topicmode
  
     #return the msg to the chan
     puthelp "privmsg $chan :topicmode for $chan is now unlocked."
  } 
  
} ;#end proc


##TOPICMODE
#bound to ".topicmode"
proc topicmode {nick uhost handle chan text} {

  #load some vars
  global topic
  
  #check if the channel is enabled, if not - return
  set enabled [lsearch -exact $topic(chans) [string tolower $chan]]
  if {$enabled == -1} {return 0}
  
  #check if the dude is an op, if not - return
  if {([matchattr $handle o $chan]) || ([isop $nick $chan])} {
  
     #set some vars
     set topicmode [load_topicmode [string tolower $chan]]
 
     #convert 1 and 0 to 'locked' and 'unlocked'
     if {$topicmode == 1} {set topicmode "locked"}
     if {$topicmode == 0} {set topicmode "unlocked"}
 
     #display the line in the chan
     puthelp "privmsg $chan :topicmode for $chan is $topicmode"
  }

} ;#end proc


##HELP
#bound to ".topic-help" and ".topichelp"
proc help {nick uhost handle chan text} {

  #load some vars
  global topic
  
  #check if the chan is enabled, if not - return
  set enabled [lsearch -exact $topic(chans) [string tolower $chan]]
  if {$enabled == -1} {return 0}
  
  #check if the dude is an op, if not - return
  if {([matchattr $handle o $chan]) || ([isop $nick $chan])} {
   
     #do your thing
     putquick "notice $nick :the commands, and their functions, are as following:"
     putquick "notice $nick :\002.locktopic\002         - locks the topicmode in a chan (the topic will always be the default-topic)."
     putquick "notice $nick :\002.unlocktopic\002       - unlocks the topicmode in a chan (allow other topics than the default-topic to be set)."
     putquick "notice $nick :\002.topicmode\002         - check the current topicmode status (bot returns either locked or unlocked)."
     putquick "notice $nick :\002.def-topic <topic>\002 - defines a new default-topic."
     putquick "notice $nick :\002.def-topic\002         - returns the current default-topic."

  }

} ;#end proc


##REFRESHTOPIC
proc refreshtopic {nick uhost handle chan text} {

   #load some global vars
   global topic

   #check if the chan is enabled, if not - return
   set enabled [lsearch -exact $topic(chans) [string tolower $chan]]
   if {$enabled == -1} {return 0}

   #check if the dude is an op, if not - return
   if {([matchattr $handle o $chan]) || ([isop $nick $chan])} {
     set deftopic [load_deftopic [string tolower $chan]] 
     putserv "topic $chan :$deftopic"
   }

} ;#end proc
 
##LOAD_DEFTOPIC 
#loads the default topic..
proc load_deftopic {chan} {
  set chan [string tolower $chan]
  set exFile [open ./$chan.topic r]
  set curline "[gets $exFile]"
  close $exFile
  return "[lrange $curline 1 end]"
}


##LOAD_TOPICMODE
#loads the default topicmode
proc load_topicmode {chan} {
  set chan [string tolower $chan]
  set exFile [open ./$chan.topic r]
  set curline "[gets $exFile]"
  close $exFile
  return "[lindex $curline 0]"
}


##SAVE_DEFTOPIC
#saves the default topic
proc save_deftopic {chan deftopic} {
  set chan [string tolower $chan]
  set topicmode [load_topicmode [string tolower $chan]]
  set exFile [open ./$chan.topic w]
  puts $exFile "$topicmode $deftopic"
  close $exFile
}


##SAVE_TOPICMODE
#saves the topicmode
proc save_topicmode {chan topicmode} {
  set chan [string tolower $chan]
  set topic [load_deftopic [string tolower $chan]]
  set exFile [open ./$chan.topic w]
  puts $exFile "$topicmode $topic"
  close $exFile
}

##CHECK_FILE_EXISTS
#creates the files you need for the chans
#and sets a lame default topic
proc check_file_exists {} {
  
  #load some vars
  global topic
  
  #run a loop through all the chans
  foreach chan [split $topic(chans)] {
  
    #if the file exist, return
    if {![file exists ./$chan.topic]} {
    
       #if the deftopic has not been defined, define a lame one
       if {$topic(def) == ""} {set topic(def) "ThiS Is mY VErY lov3lY t0piC! Plz help me configure my script.. anyone?"}
    
       #if the defmode has not been set, set it to locked
       if {$topic(mode) == ""} {set topic(mode) 1}
    
       #do your thing..
       set exFile [open ./$chan.topic w]
       puts $exFile "$topic(mode) $topic(def)"
       close $exFile
       putlog "$topic(ID) created topic-file for $chan"
    }
  } ;#end foreach

} ;#end proc

#execute a check for topic-files
check_file_exists

#after waiting 1min, start the prosess with checking for a topic-change every 10 min
timer 1 "check_topic_time"


#gives me som credits :P
putlog "topic-guard v1.6 by gozzip loaded.."