Colour Defender

By RusselB on Mar 25, 2009

This was written up in response to a request on the forum, and then the requester suggested that I submit it as a snippet.

On a channel by channel basis (and works over multiple networks), you can set the number of lines that a person can use in colour before the script sets mode +S

Settings are stored in a hash table. Settings can be controlled via text or menu.
When using text to control the settings, you must be an op in the channel.

Warning: Original code will also be triggered by bold, underline and reverse character codes, not just colour codes. Code has been edited to allow for these extra codes.

on *:text:*:#:{ 
  var %text = $1-
  tokenize 32 $strip($1-) 
  if $nick isop $chan { 
    if $1 == !help { 
      .msg $nick Available commands: 
      .msg $nick !help -> Brings up this help. 
      .msg $nick !colour <on/off> 
      .msg $nick !color <on/off> 
      .msg $nick !lines <number of lines allowed> 
      .msg $nick !delay <time in minutes> 
    } 
    if $1 == !colour || $1 == !color { 
      if $istok(on off,$2,32) { 
        .hadd -m Colour $+($network,.,$chan) $iif($2 == on,$true,$iif($2 == off,$false)) 
        .onotice $chan Colour Defender for $chan is now $2 $+ .  Set by $nick 
      } 
    } 
    elseif $1 == !lines { 
      if $2 isnum 1- { 
        .hadd -m Colour $+($network,.,$chan,.Lines) $2 
        .onotice $chan Number of coloured lines allowed in $chan has been set to $2 by $nick 
      } 
    } 
    elseif $1 == !delay { 
      if $2 isnum 1- { 
        .hadd -m Colour $+($network,.,$chan,.Delay) $calc($2 * 60) 
        .onotice $chan Time delay to automatic removal of Colour stripping set to $2 minutes by $nick 
      } 
    } 
  } 
  if $1- != $strip(%text,c) { 
    .hinc -m Colour $+($network,.,$chan,.,$address) 
    if $hget(Colour,$+($network,.,$chan,.,$address)) >= $hget(Colour,$+($network,.,$chan,.Lines)) { 
      if S !isincs $chan($chan).mode { 
        .mode $chan +S 
        .timer 1 $hget(Colour,$+($network,.,$chan,.Delay)) mode $chan -S 
        .onotice $chan $nick activated mode +S by using $hget(Colour,$+($network,.,$chan,.,$address)) coloured lines.
      } 
    } 
  } 
} 
menu channel { 
  Colour Defender 
  .Turn $iif($hget(Colour,$+($network,.,$chan)),Off,On) :{ 
    .hadd -m Colour $+($network,.,$chan) $iif($hget(Colour,$+($network,.,$chan)),$false,$true) 
    if !$hget(Colour,$+($network,.,$chan,.Lines)) {      .hadd -m Colour $+($network,.,$chan,.Lines) 2    } 
    if !$hget(Colour,$+($network,.,$chan,.Delay)) {      .hadd -m Colour $+($network,.,$chan,.Delay) 10    } 
  } 
  .Lines $hget(Colour,$+($network,.,$chan,.Lines)) : .hadd -m Colour $+($network,.,$chan,.Lines) $?="Number of coloured lines allowed" 
  .Delay $hget(Colour,$+($network,.,$chan,.Delay)) : {
    var %d.time = $?="Time Delay in minutes"
    .hadd -m Colour $+($network,.,$chan,.Delay) $calc(%d.time * 60)
  }
} 
on *:start:{ 
  if !$hget(Colour) { .hmake Colour 100 } 
  if $exists($qt($scriptdirColour.hsh)) { 
    .hload Colour $qt($scriptdirColour.hsh) 
  } 
} 
on *:disconnect:{ 
  .hsave Colour $qt($scriptdirColour.hsh) 
} 
on *:exit:{ 
  .hsave Colour $qt($scriptdirColour.hsh) 
} 

Comments

Sign in to comment.
RusselB   -  Sep 25, 2010

The access methods required for ini files prevent you from doing what you're looking for directly. One option is to use the -i switch in the /hload command, then search the hash table.
A couple of additional items that you may not have thought of, is that ini files are, realistically, limited to 64k in size. While it is possible to create and use ini files that are bigger than 64k, there is no guarantee that you won't have data loss. The 64k limit is a Windows limit not mIRC.
Also, by using an ini file, the file has to be opened and closed every time you access it, thus causing wear & tear on your hard drive and subjecting your data to the relatively slow speed of your hard drive vs. the speed of RAM in your system (which a hash table uses.)

less chance of it being lost on a mIRC crash/dc/etc. This is the set of scenarios that are normally handled by the ON DISCONNECT and ON EXIT events in my script.
The only way that a system could crash and not have at least one of those triggering is if there was a sudden power outage where the system had a complete power failure within (about) 2 seconds of the power supply failing.

 Respond  
Lucius   -  Sep 25, 2010

¬_¬ not to fire things up after over a year, but I prefer ini because it instantly saves information and there is less chance of it being lost on a mIRC crash/dc/etc. but saying that, I use hash too sometimes, because it allows the multiple access of sections, matching the content and then retrieving the title. I'm not sure if you can do this with ini too but if you can, I don't know how. I'll give you an example.
someone buys a cookie
it gets saved in hash:
nick - cookie
but in an ini
[nick]
[bought]
cookie=1

How do you search for the cookies and list who has bought one in an ini?
(This is the only reason so far which stops me from using ini exclusively so if there is a way, I would be grateful if someone informed me.)

 Respond  
RusselB   -  Mar 27, 2009

Referencing my earlier post, you probably won't notice a problem until you hit (about)250,000 entries. Basically the amount of RAM measured in k is the number of entries before a slow down is noticed for a system with a 600mhz processor.

 Respond  
FordLawnmower   -  Mar 27, 2009

Yea, my code was to count the total records. I guess there just isn't a magic solution to this ;/ I do know that hashtables are definitely the most efficient way to store any large set of data. When I first started scripting with msl, I wrote a couple of scripts using ini's. They eventually got so big that they crashed mirc. I've never had any trouble with hashtables and I have some with over 50 thousand records.

 Respond  
RusselB   -  Mar 27, 2009

No. The number at the end of the /hmake statement is the number of slots that are opened, not the amount of memory that is allocated.
Even if it was the amount of memory, your code should look like this

alias counthash {
  var %a = $hget(0), %c
  while %a {
    inc %c $hget(%a).size
    dec %a
  }
  $iif($isid,return,echo -a) %c
}

While this is closer to the amount of memory actually, there's still a discrepancy, since the number of slots specified in the /hmake statement is usually about 1/10th of the number of items you think you're going to have, also the fact that an entry that has 400 characters in it is going to need more memory to hold the information than an entry of 10 characters (for example.)

 Respond  
FordLawnmower   -  Mar 26, 2009

I was thinking there was a way to calculate it with a equation. I just made this to count the total number of records.

alias CountHash {
  var %TableCount = $hget(0), %TotalCount
  While %TableCount {
    %TotalCount = $calc(%TotalCount + $hget(%TableCount,0).item)
    dec %TableCount
  }
  return %TotalCount
}

I'm sure this would be useless in calculating anything, since the amount of memory is set at the end of the hmake statement. I'm guessing if you always use 10, it would be
(10 $hget(0)) (Whatever amount of memory = 1 in hmake)

 Respond  
RusselB   -  Mar 26, 2009

I use a program called MemTurbo to monitor the amount of RAM is available.
By checking the amount of RAM available with no hash tables, then creating the table, then adding item(s) and watching the numbers in MemTurbo, I can determine how much memory is being used at each step.
The version of MemTurbo that I have is a purchased version, and allows me to configure the display to show Gig, Meg, k, or bytes. Note that the shareware version does not allow this customization.

 Respond  
FordLawnmower   -  Mar 26, 2009

I have a bot running a similar amount of hash tables RusselB. Do you know how to calculate how much memory is being used by hashtables?? I've had people cry to me that some of my scripts that use hashtables are slowing down their computer. I have a machine that's running two mirc bots with a combined 21 hashtable scripts and it never complains.
It's a P3 with only 256MB of ram ;/

 Respond  
RusselB   -  Mar 26, 2009

@The_Almighty_Duelist: I have been thinking about your request while at work today (as well as the comments received before I went to work), and I can and will write an alternative version.
@Aucun50: If you feel the need to add the () around the if statements, be my guest. The script works fine either way.
To all regarding hash tables and memory usage: I have a bot that runs 24/7 on a PIII-600 with 512M ram and a 120G hard drive. This bot contains 10 hash tables. Of those 10, 4 contain between 10,000 and 15,000 entries. The remaining 6 all have under 1,000 entries.
A bit of quick simple math gives a total of about 45,000 - 65,000 entries for all 10 tables. The amount of RAM required to hold all of those entries, is less than 2M.
Now, I realize that the actual memory required will vary depending on the specifics of the hash table name, item and data that are stored, but this gives a general idea of the memory usage for a fair number of tables/entries.
At one point it was noticed that the bot was slowing down, and I checked the hash tables, to find that the tables (at that time) were holding nearly 10x the current settings.

 Respond  
Aucun50   -  Mar 26, 2009

I always find it hard to read it will no "()" around "$1 == !colour" other then that nice work.

 Respond  
Jethro   -  Mar 26, 2009

Hash can easily be converted into ini if you know how to do it.

 Respond  
Tamaki   -  Mar 26, 2009

rule 14, prime example of it Neptune, stop patronizing me, its a simple request, if you feel like being mean, i would recommend taking it to a channel called #insult?

i am not bashing hash tables, even though i don't use them myself, you're right on one thing...

There is no point of making an .ini version when this one works perfectly.

there is not point on making any script, we just make them to make life easier.

 Respond  
^Neptune   -  Mar 26, 2009

"h...and i don't know hash tables, i learned them once, and realized how un-useful they were"

Sorry what? Hash tables are almost exactly like an .ini file, with all topics in a single section. They are far faster and easier to manage, and don't take up file space on your computer.

There is no point of making an .ini version when this one works perfectly.

 Respond  
Tamaki   -  Mar 26, 2009

looking like a troll is obv least of you concern...what incorrect information? i wasn't talking about you

and yes...it was a good snippet

 Respond  
Pass   -  Mar 26, 2009

Looking like a troll is the least of my concern (although, you should definitely read up on the definition of 'troll'), as I was pointing out incorrect information that users could, and most likely would have mistaken for legit knowledge.

RusselB, excuse the spam, and I enjoyed the snippet. Worked well.

 Respond  
Tamaki   -  Mar 26, 2009

omg <---seriously | in any case....i didn't say it was a bad script, and i didn't give it a bad rating.

if you still feeling like making a .ini version lemme know, if not, i'll make one myself and publish it when i publish Staff Bot 3. Sound fair?

if you still feel like being a troll, don't talk, just don't do it, responding to this comment will just make you look like a troll...truth. oh well

oh...and i don't know hash tables, i learned them once, and realized how un-useful they were; i know that seems trollish, but after using .ini files, .ini files seem sooo much more useful. I don't really care how fast they go. I have a fast computer. So it doesn't matter to me.

 Respond  
Pass   -  Mar 26, 2009

This is not the forum, if you have a request, take it there.

 Respond  
Tamaki   -  Mar 26, 2009

why so serious? and no, i will not shut up...i didn't do anything wrong and i got attacked because i wanted a .ini version of the script :'(

 Respond  
Pass   -  Mar 26, 2009

The_Almighty_Duelist: Please kindly shut the fuck up. I wasn't responding to any of your comments.

 Respond  
Tamaki   -  Mar 26, 2009

i don't really care what you think about hash tables and .ini files. I simply asked him if he could make a .ini version of the script. Ever thought about that?

 Respond  
Pass   -  Mar 26, 2009

Cheiron, you're missing the point. In actuality, hash tables are more efficient for older PCs, not newer ones because mIRC does NOT use a lot of RAM, no matter the size of the table. Stop with this 'hash tables require modern pcs' propaganda. You're spreading false information for others to believe.

 Respond  
Cheiron   -  Mar 26, 2009

hash tables store their information in ram to save on the accessing of the hard drive all the time that items like ini and txt files do.

can you remove all other non contributing comments please RusselB from this thread from myself. i wont comment anymore on this one. edited comment

 Respond  
Pass   -  Mar 26, 2009

Cheiron, that's ridiculous. Using hash tables does not require any significant amount of RAM. I've a little over 700mb and can run hundreds(/thousands, if you count broken loops while creating tables) together. If anything, INI files would require more up-to-date systems to run smoother due to the constant writing to the hard drive disk.

 Respond  
Cheiron   -  Mar 26, 2009

just need to have a decent pc with a good amount of ram. older pc's with under a gig will struggle when using hash based scripts owing to the way it works. but yeah i totally agree. hash is the way to go

 Respond  
Jethro   -  Mar 26, 2009

I'm not sure why some people reject the use of hash tables when in fact it's superior than ini and text file. Learning hash tables is not as difficult as you think, people...when you can take an effort to read into how hash tables work. Everything will eventually fall into place as you start to get a hint of them.

 Respond  
Cheiron   -  Mar 26, 2009

i have been using this script for the last 6 hours now since RusselB made it for me following a help query in the forums that i submitted.
script was very easy to set up , once i had made a couple of boobs heh,and functioned very well with no errors. gets a thumping 10 and a like from me. many thanks RusselB and thanks for posting it here also :) log excerpt below from my running test.

12<20112:234am12> Sleeping_On_The_Job looks in very quickly before hitting bed to tes a new script
12<20112:234am12> !ahelp
12<20112:235am12> !colour off
12<20112:235am12> -Titan_Bot:@#gothic- Colour Defender for #gothic is now off. Set by Sleeping_On_The_Job
12<20112:235am12> !lines 4
12<20112:235am12> -Titan_Bot:@#gothic- Number of coloured lines allowed in #gothic has been set to 4 by Sleeping_On_The_Job
12<20112:235am12> !delay 1:00
12<20112:236am12> -S
12<20112:236am12> someone type 5 lines in colour a sec?
12<20112:237am12> <Ayla[EL]{t^c}{a}> colour
12<20112:237am12> <Ayla[EL]{t^c}{a}> colour
12<20112:237am12> <Ayla[EL]{t^c}{a}> colour
12<20112:237am12> <Ayla[EL]{t^c}{a}> colour
12<20112:237am12> <Ayla[EL]{t^c}{a}> colour
12<20112:237am12> hmm
12<20112:237am12> color
12<20112:237am12> !colour on
12<20112:237am12> -Titan_Bot:@#gothic- Colour Defender for #gothic is now on. Set by Sleeping_On_The_Job
12<20112:237am12> colour
12<20112:237am12> ah
12<20112:237am12> now try lol
12<20112:237am12>
lynxrecovering pokes Ms Ayla mewing questionly up at Her.
12<20112:237am12> !delay 60
12<20112:237am12> -Titan_Bot:@#gothic- Time delay to automatic removal of Colour stripping set to 60 minutes by Sleeping_On_The_Job
12<20112:237am12> !delay 1
12<20112:237am12> -Titan_Bot:@#gothic- Time delay to automatic removal of Colour stripping set to 1 minutes by Sleeping_On_The_Job
12<20112:238am12> 4 boo
12<20112:238am12> 4 boo
12<20112:238am12> 4 boo
12<20112:238am12> 4 boo
12<20112:238am12> 4 boo
12<20112:238am12> Titan_Bot sets mode: +S
12<20112:238am12> O.o
12<20112:238am12>
channy{S^} laughs and pokes Sleeping_On_The_Job
12<20112:238am12> colour
12<20112:238am12> Sleeping_On_The_Job looks at his pm box
12<20112:238am12> 12<20112:238am12> Sleeping_On_The_Job used 4 coloured lines in #gothic on (server name). Activating mode +S
12<20112:238am12> oh colored lines...not the word
12<20112:238am12>
Sleeping_On_The_Job grins
12<20112:239am12> * Titan_Bot sets mode: -S

 Respond  
slacker   -  Mar 26, 2009

nice work RusselB
@ The_Almighty_Duelist why would he want to use ini files? when hash works alot better and faster.

 Respond  
Tamaki   -  Mar 26, 2009

looks good...see if you can make a version using .ini files

(if you don't know how to use them, use the Help Connection Center snippet i posted to connect to me)

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.