NAME

Sirc::Util - Utility sirc functions

SYNOPSIS

# sirc functions
use Sirc::Util ':sirc';
# overrides:
addhelp $cmd, $usage_line, $rest;
timer $delay, $code_string_or_ref, [$reference];

# user messages
arg_count_error undef, $want, [@arg];	# or 1st arg $name
tell_error $msg;
tell_question $msg;
xtell $msg;

# miscellaneous
$pattern = ban_pattern $nick, $user, $host;
$boolean = by_server [$who, $user, $host];
eval_this $code, [@arg];
eval_verbose $name, code$, [@arg];
$boolean = have_ops $channel;
$boolean = have_ops_q $channel;
$boolean = ieq $a, $b;
$re = mask_to_re $mask;
$unused_timer = newtimer;
optional_channel or return;
$boolean = plausible_channel $channel;
$boolean = plausible_nick $nick;
$arg = xgetarg;
$restricted = xrestrict;

# /settables
settable name, $var_ref, $setter_ref;
settable_boolean $name, $var_ref, [$validate_ref];
settable_int $name, $var_ref, [$validate_ref];
settable_str $name, $var_ref, [$validate_ref];

# hooks
add_hook_type $name;
add_hook $name, $code;
run_hook $name, [@arg];

DESCRIPTION

This module provides a bunch of utility functions for sirc.

It also allows you to import from it all of the standard sirc API functions, so that you can more simply write your script as a module.

Nothing is exported by default.

STANDARD SIRC FUNCTIONS

You can import the standard SIRC API functions individually or, using the tag :sirc, as a group. The available functions are:

  • accept addcmd addhelp addhook addset connect deltimer describe docommand doset dosplat dostatus eq getarg getuserline getuserpass listen load me msg newfh notice print remhook remsel resolve say sl tell timer userhost yetonearg

Some of these are actually enhanced versions of the routines that sirc provides, see below for information about them.

STANDARD MESSAGE FORMS

These functions provide for a few standard message forms which are shown to the user via main::tell().

arg_count_error name, want, [arg...]

This prints an error appropriate to an incorrect number of arguments. name is the name to report as having been invoked incorrectly. If it's undef (which is the usual case) it's set to the caller's function name. want is how many arguments were desired and the remaining arg arguments are the arguments which were actually received.

tell_error msg

This formats msg as an error message and passes it to main::tell. It's appropriate for errors caused by the system or an invalid invocation of your code.

tell_question msg

This formats msg as an error message for something the user did wrong. The message is passed to main::tell.

xtell msg

This is just main::tell "*** $msg".

MISCELLANEOUS FUNCTIONS

These are some functions which don't fall nicely into groups like those following do.

addcmd command

This is an enhanced version of sirc's addcmd(). It lets you define commands whose names contain non-alphanumeric characters.

addhelp command, help
addhelp command, usage line, rest

This is an enhanced version of sirc's addhelp(). It arranges for the new command to appear in the master help list.

Additionally, there's a new 3-arg syntax. When called with 2 args it uses the regular addhelp() command. I hate the way this makes you hardcode the standard form for help info, though, so I added the second form. This form takes the usage info which appears after the command as its first arg, and the bulk of the help as its 3rd arg.

ban_pattern nick, user, host

This returns a pattern suitable for banning the given nick, user and host.

The current implementation is this: Any nick is always matched. If the user has a ~ at the start (that is, it didn't come from identd) all user names are matched, else just the one given matches. If the host is an IP address, it bans a class C sized chunk of IP space, otherwise part of it is wildcarded (how much depends on how many parts it has).

For example:

qw(Nick  user 1.2.3.4)		*!user@1.2.3.*
qw(Nick ~user 1.2.3.4)		*!*@1.2.3.*
qw(Nick  user host.foo.com)		*!user@*.foo.com
qw(Nick ~user host.foo.com)		*!*@*.foo.com
qw(Nick  user foo.com)		*!user@*foo.com
qw(Nick ~user foo.com)		*!*@*foo.com
by_server [who, user, host]

If the given who, user, host corresponds to a server rather than a user, return the server name, else return undef. If these aren't specified the global $::who, $::user, and $::host are used, which is what you usually want anyway.

eval_this code, [arg...]

This evals code with arg as arguments. The code can be either a code reference or a string. In either case the args will be available in @_. The return value is whatever the code returns. $@ will be set if an exception was raised.

eval_verbose name, code, [arg...]

This is like eval_this except that if an exception is raised it is passed along to tell_error (with a message indicating it's from name).

have_ops channel

This function returns true if you have ops on the specified channel. If you don\'t have ops it prints an error message and returns false.

have_ops_q channel

This is like have_ops except that no message is printed, it just returns true or false depending on whether you have ops on the specified channel.

ieq $a, $b

This sub returns true if its two args are eq, ignoring case.

mask_to_re glob

Convert the given "mask" (an IRC-style glob pattern) to a regular expression. The only special characters in IRC masks are * and ? (there's no way to escape one of these). The returned pattern always matches case insensitively and is anchored at the front and back (as IRC does it).

optional_channel

This sub examines $::args to see if the first word in it looks like a channel. If it doesn't then $::talkchannel is inserted there. If there was no channel present and you're not on a channel then an error message is printed and false is returned, otherwise true is returned.

Here's a replacement for /names which runs /names for your current channel if you don't provide any args.

    sub main::cmd_names {
	optional_channel or return;
	docommand "/names $::args";
    }
    addcmd 'names';
newtimer

Return an unused timer number.

plausible_channel channel

This returns true if channel is syntactically valid as a channel name.

plausible_nick nick

This returns true if nick is syntactically valid as a nick name. Originally I used the RFC 1459 definition here, but that turns out to be no longer valid. I don't know what definition modern IRC servers are using. This sub allows characters in the range [!-~].

timer @args

This is an enhanced version of sirc's timer(). It allows you to use a code reference as the code arg.

xgetarg

This is like main::getarg, but it returns the new argument (in addition to setting $::newarg).

xrestrict

This just returns $::restrict.

/SET COMMANDS

These commands provide a simplified interface to adding /set variables.

settable name, var-ref, setter-ref

This sub adds a user-settable option. name is its name, var-ref is a reference to the place it will be stored, and setter-ref is a reference to code to validate and save new values. The code will be called as $rsetter-($rvar, $name, $value)>. $name will be in upper case. The code needs to set both $$rvar and $::set{$name}. (The values in %set are user-visible.)

settable_boolean name, var-ref, [validate-ref]

This adds a /settable boolean called name. var-ref is a reference to the scalar which will store the value.

validate-ref, if provided, will be called to validate a new value is legal. It will receive both the name and the new value (as a boolean, not as the user typed it) as arguments. It should return a boolean to indicate whether the value is okay.

settable_int name, var-ref, [validate-ref]

This function adds a /settable integer called name. var-ref is a reference to the scalar which will store the value.

validate-ref, if provided, will be called to validate a new value is legal. It will receive both the name and the new value as arguments. Before it is called the new value will have been vetted for number-hood. It should return a boolean to indicate whether the value is okay.

settable_str name, var-ref, [validate-ref]

This function adds a /settable called name. var-ref is a reference to the scalar which will store the value.

validate-ref, if provided, will be called to validate the a new value is legal. It will receive both the name and the new value as arguments. It should return a boolean to indicate whether the value is okay.

HOOKS

Sirc::Util provides functionality for creating, adding code to and running hooks.

add_hook_type name

This creates a new hook called name.

add_hook name, code

Add code to the name hook. The name must already have been created with add_hook_type(). The code can be either a string or a code reference.

run_hook name, [arg...]

Run the name hook, passing the args to each hook member via @_.

AVAILABILITY

Check CPAN or http://www.argon.org/~roderick/ for the latest version.

AUTHOR

Roderick Schertler <roderick@argon.org>

SEE ALSO

sirc(1), perl(1), Sirc::Chantrack(3pm).