Userhost requests

The KSirc Handbook
Prev | Chapter 5. Programming Your Sirc Client | Next

# 5.4. Userhost requests

Sometimes in a function you need to know the full username and hostname for some nick. If this happens in a hook, and the nick is the one who did the action, then the nick is in `$who` and the userhost data is already in `$user` and `$host`.

Otherwise, you have to call the perl function `&userhost` giving it three arguments: the nickname, what you want evaluated when the data is available, and what you want evaluated if the nick is not found on IRC; if the third argument is ommited, sirc will print the default message `"*?* somenickname not found on IRC"`.

Unlike with earlier versions of sirc, it is possible to do more than one userhost request in a short time before getting the answers from the server.

Example: a function that prints someone's country code

     # country code

     sub printcountry {		# prints $host's country code

       if ($host =˜ /\.([^.]+)$/) {  # match the last part of the host
         local($c)=($1); 		# put it in local var $c
         $c="USA" if $c =˜ /^edu$/i;	# if it's a .edu, say it's USA
         $c="USA (probably)" if $c =˜ /^com$/i || $c =˜ /^org$/i || $c =˜ /^net$/i;
     				# if it's a .org, .com or .net, it's
     				# probably in the USA too
         if ($c =˜ /^\d+$/) {	# if it's a number
           &tell("*** out of luck, $who has an IP address :p");
     				# complain, it's an IP
         } else {			# otherwise
           &tell("*** $who is in $c");
     				# announce the result
         }
       }
     }

     sub cmd_country {		# this is the command

       &getarg;			# get the argument in $newarg
       if ($newarg) {		# if it's there
         &userhost($newarg, "&printcountry;");
     				# request a userhost with &printcountry as
     				# action to take
       } else {			# otherwise
         &tell("*** Whose?");	# complain
       }
     }

     &addcmd("country");		# install the command

Prev | Home | Next
SET variables | Up | Timers