Re: Script Help

Tamara Temple <[email protected]> Mon, 5 Jul 2010 09:32:36 -0500
Newsgroups gmane.network.irc.irssi.user
Message-ID <[email protected]>
First off, you're not getting the parameters passed to the function  
when it is triggered.

sub time_printer(
    my ($server, $data, $nick, $address, $target) = @_;

Next, you need to check the chatnet and channel name to ensure you're  
only targetting a specific channel. These are in the server structure  
that is passed to the function when it's called. I've outlined the  
server structure here: http://wiki.tamaratemple.com/?n=Main.ServerStructure 
  to show what it contains. Specifically, you want to check:

   if ($server->{chatnet} eq $mychatnet) && ($target eq $mychannel) {
      # process signal

Then test the data that is passed to the function to see if it  
contains the appropriate trigger.

  if ($data =~ /^!thetime$/) {
      # format and emit the time...

Finally, you'll send a command to the particular server indicating  
what channel to put the message on. Skip the "!msg " in your fomatting  
of $thetime (just format the time string itself). Then, issue:

   $server->command("ACTION $target $thetime");

(I use ACTION instead of MSG when emitting things from scripts to  
channels/queries so it doesn't appear as normal conversation from me.)


For an example of a script which implements these elements, visit http://public.tamaratemple.com/irssi/showtitle2.pl.txt 
  -- it doesn't do exactly what your script it trying to do, but it  
has all the elements you'd need to accomplish what you're trying to do.

A perl idiom you might not be aware of is when you have a single  
statement controlled by an if clause, the way to write that is:

   $hour -= 12 if ($hour > 12);

although you could consider something like this:

   if (hour > 12) {
     $hour -= 12;
     $daypart = "PM";
   } else {
     $daypart = "AM";
   }

and add in an indicator if it's AM or PM.

Also, you can accomplish the formatting to $thetime with a single  
sprintf() function call rather than making a whole lot of function  
calls and concatenating them together:

   $thetime = sprintf("!msg %02d.%02d.%02d %02d-%02d-%02d",
        $hour, $min, $sec, $mday, $mon+1, $year+1900);


Tamara Temple
	-- aka tamouse__
[email protected]


"May you never see a stranger's face in the mirror."

On Jul 4, 2010, at 10:33 PM, Ivan Panarusky wrote:

> I'm trying to write a script so that when a message is sent to a
> certain that contains "thetime", I will automatically reply with "!msg
> 11:27:04 4-7-2010".
>
> Here is what I have already
>
> sub time_printer {
> ($sec,$min,$hour,$mday,$mon,$year,$wday, $yday, 
> $isdst)=localtime(time);
> if ($hour>12) {$hour-=12;}
> $thetime = "!msg ".sprintf("%02d", $hour).":".sprintf("%02d",
> $min).":".sprintf("%02d", $sec)." ".sprintf("%02d",
> $mday)."-".sprintf("%02d", $mon+1)."-".sprintf("%02d", $year+1900);
> print $thetime;
> }
>
> Irssi::signal_add("message public", "time_printer");
>
> Right now it just prints the time in the status window anytime
> somebody says something. How do I get it to print it in a certain
> channel, only when a message containing "thetime" is sent?
>
> Thanks in advance
>