Hadd

From Scriptwiki
Jump to: navigation, search

Adds an item to an existing hash table.

/hadd -smbczuN <name> <item> [data | &binvar]

Using the -s switch makes the command display the result (e.g. * Added item 'Dana' to hash table 'users')

The -m switch makes /hadd create the hash table if it doesn't already exist.

The -uN switch unsets the item after N seconds.

The -b indicates that you're adding a &binvar item to the hash table.

The -c switch chops the &binvar up to the first null value and treats it as plain text.

The -z switch decreases hash item once per second until it reaches zero and then unsets it.

Note that if the item you're adding already exists, the old item is replaced.

The /hinc and /hdec commands use the same parameters as /hadd and increase or decrease the number value of an item.

When used with /hinc or /hdec, the -c switch increases or decreases the value once per second.

Example

hadd -m users Dana $address(Dana,5)

This example will, if it doesn't already exist, make a new hashtable called "users" and add Dana as item and Dana's address as value to it.


The next, more complex example, will add all users, who are currently in #help.script and their addresses to a hashtable called users.

; make a new hashtable called users
hmake users 
var %i = 1
; begin to loop through all users in #help.script
while (%i <= $nick(#help.script,0)) {
 ; actually add it to the hashtable
 hadd users $nick(#help.script,%i) $address($nick(#help.script,%i),5)
 ; increase looping-variable
 inc %i
}

Be sure that your IAL is up-to-date, if not, execute /who #help.script before.


on *:text:!help:#: {
  if ($hget(anti-flood,$nick)) { return } ;; If user already in hash table, wait a bit until we speak to him.
  hadd -mu10 anti-flood $nick No flood    ;; Add user to hash table for 10 seconds.
  ;;Commands after here will only get ran if the user isn't in the antiflood hash table.
  notice $nick You'll need more than me for the help you need!
}

The above example will only send the !help to the user once every 10 seconds.


;;Every 15 seconds check if we there is people who need to be looked up.
on *:connect: { .timer 0 15 CheckUsers }

on *:join:#: {
  ;;If it's you who is joining, add the whole channel to the lookup list.
  if ($me == $nick) { set %wholookup.chans $addtok(%wholookup.chans,$chan,44) }
  ;;If the user who is joining the channel has +x set, no need to use who, grab their QAuth from their host.
  elseif (*.users.quakenet.org iswm $site) { hadd -m UserAuths $nick $gettok($site,1,46) }

  ;;If we dont know their QAuth add them to the lookup list.
  else { set %wholookup.users $addtok(%wholookup.users,$nick,44) }
}

alias CheckUsers {
  ;;If we have any chans that need to be looked up, look them up.
  if (%wholookup.chans) { who %wholookup.chans c%nat,55 }
  ;;If we have any users that need to be looked up, look them up.
  if (%wholookup.users) { who %wholookup.users n%nat,55 }
  ;;Clear the lookup vars.
  unset %wholookup.*
}

;;If user is authed then add user to UserAuthed hash table.
raw 354:$($me 55 & &): { if ($2 != 0) { hadd -m UserAuths $1 $2 } | halt }

The above example creates a hash table of all the users and their qauth's.