Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # REPEAT
- # Author: Bry8Star. <bry 8 s tar a.t ya hoo d.o.t c om>
- #
- # Copyright (C) 2013 Bright Star, Bry8Star.
- #
- # You Must obtain explicit permission from Author, before using this code.
- #
- # This "REPEAT" plugin for XChat IRC Client Software can Repeat a
- # (or set of) task either with fixed-length time, or with variable
- # (and random) amount of time. This Plugin can be turned off or on,
- # max repeatation number can be set, fixed or variable amount of time
- # duration can also be set, main xchat command-name for this plugin
- # can also be changed, etc.
- #
- #
- # Search (Ctrl+F) for the word YOUR-CODES
- # then add your own custom codes, in that section,
- # which you want to repeat.
- #
- #
- # Many "prnt" command lines added for debug purpose, that is, to show
- # what is going on, once working fine or expected, then disable those
- # lines by placing a "#" symbol in front of "prnt" command.
- # or use this command to turn of messages:
- # /COMMAND_NAME ShowDbgMsg 0
- #
- #
- # If you will use same plugin for multiple IRC network, then you will
- # have to change /COMMAND name. or else they will conflict.
- # Simple way would be to add _NetworkName after /COMMAND
- # For example, if my IRC network list entry for freenode is
- # "Freenode-Bry8Star", then i would set command like this:
- # /REPEAT_FN_Bry8
- # or, /RECONNECT_FN_Bry8
- # or, /AUTO_RECONNECT_FN_Bry8
- # (keep command name under 33 characters)
- #
- #
- # ToDO:
- # Add a "CheckViaPM" method, for checking irc network server connection
- # status. Then IRC server which are behind BNC would also work via
- # using this script/plugin.
- # Detect the "* Stopped previous connection attempt..." message and
- # then attempt to issue "RECONNECT" command.
- # Change "Counting"/"Showing-number" into "Checking Server Connection Status"
- # Show actual local system time when showing elapsed time.
- #
- use strict;
- use warnings;
- use Xchat qw(:all);
- # sub-prototypes:
- sub rInt(); # convert any +/- non+/-fractional into correct +/- Integer.
- sub ms2sec(); # converts ms(milli-seconds) into sec with fractional NUMBER.
- sub ms2iSec(); # converts ms into sec(seconds) with no fraction, NUMBER.
- sub ms2fSec(); # converts ms into sec STRING with 3 fraction after point.
- sub sec2ms(); # converts sec into ms number, 1 sec is 1000 ms.
- sub usrFSec2ms(); # convert user given fractional-second into ms.
- sub prnt_on(); # prnt_on( $r->{sDbgMsg} , "message" , $r->{NtwrkLstNm} );
- # Controlling Global Variables: (requires perl 5.10+):
- my $r //= { #
- cmdNm => "AUTO_RECONNECT_YourIRC", # main command name of this XChat plugin,
- # for FN-bry8 IRC Net (Freenode-for-bry8star).
- # my($mainCmd)="REPEAT"; # name of XChat main command, for this plugin.
- enbl => 0, # indicator for REPEAT: Enable(1) or Disable(0) ?
- upto => 10, # initially max number of repeatation amount is set on 10.
- # if $r->{upto} is 0, then plugin will repeat unlimited number of times.
- tmrTyp => 2, # 1 = fixed-length timer, 2 = variable-length timer.
- rptTm => 62000, # user given main repeat time interval. 60000ms=60sec.
- varTm => 5000, # variable fluctuating time amount. Max Half of $timeIntrvl.
- rndmTm => 62000, # random time = (+/-~$r->{varTm} + $r->{rptTm} ).
- sDbgMsg => 2, # show debug message. 1 = yes. 0 = no. 2 = network-servr-tab.
- # if $r->{sDbgMsg} = 2 then show dbg msg only on given network's main/srvr tab
- countr => 0, # it stores counting/loop number.
- tmrExist => 0, # Whan a hook_timer is created then this is set to 1.
- hkPnt => 0, # used for storing timer's ref-/mem-location-point.
- #
- # Enter name of IRC NETWORK NAME which you used inside your
- # XChat NETWORK LIST, (or SERVER LIST) for repeatedly checking
- # if its connected or not, with IRC Server, if not connected
- # then this plugin will RECONNECT back.
- NtwrkLstNm => 'YourIRC', # target irc net name to check & reconnect
- #
- cntxLocOfNtwrkSvTb => "", # Target IRC Network's main server tab location
- usrContxtPosn => "", # user's current context position
- elapsTm => 0, # elapsed time, in between now and previous repeatation
- };
- $r->{usrContxtPosn} => Xchat::get_context();
- $r->{elapsTm} = ( $r->{tmrTyp} == 2 )? $r->{rndmTm} : $r->{rptTm};
- Xchat::register($r->{cmdNm} ,'1.0.6',"$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) allows to repeat tasks. Such as checking servr connection status. More info, type: /". $r->{cmdNm} );
- prnt("\x{2}". $r->{cmdNm} ."\x{2} plugin \(for ". $r->{NtwrkLstNm} ."\) for XChat, started. \(A plugin for repeating tasks\). To see more info, you may type: /". $r->{cmdNm} );
- # This portion developed by Bry8Star:
- Xchat::hook_command( $r->{cmdNm} , sub{
- # Xchat::hook_command( "COUNTER", sub{
- # Xchat::hook_command( "REPEAT", sub{
- my($mS)="";
- my($userDat1)="";
- my($userDat2)="";
- my($uV)=0;
- if (not defined $_[0][1]) {
- # User did not specify any sub-command option or value after main command:
- # Main command is /REPEAT
- # or, Main command is /$r->{cmdNm}
- $mS .= "\x{2}". $r->{cmdNm} ."\x{2} --> Currently this REPEAT \(". $r->{cmdNm} ."\) Plugin is \x{2}". (($r->{enbl} )? "ON" : "OFF") ."\x{2} \(for \"". $r->{NtwrkLstNm} ."\" IRC Network\)\.\n";
- $mS .= "Command Examples\:\n";
- $mS .= " \x{2}/$r->{cmdNm} ON\x{2} <-- Enable this \"Repeat\" plugin\. It will start to repeat ". (($r->{upto} >= 1 )? "upto $r->{upto} " : "unlimited ") ."times/number, and will check connection status with given IRC Server \(or repeat/do your other codes\)\.\n";
- $mS .= " \x{2}/$r->{cmdNm} OFF\x{2} <-- disables/stops repeatations/counting and stops checking connection status, \(or if you have added your codes, then it will stop executing those codes\)\.\n";
- $mS .= " \x{2}/$r->{cmdNm} upto 20\x{2} <-- will do total 20 repeatations for showing counting numbers upto 20, or it will do your codes 20 times\. And then stop\. Current \"upto\" value is ". (($r->{upto} >= 1 )? "$r->{upto} \(" : "$r->{upto} \(means, unlimited number of ") ."repeatations\)\.\n";
- $mS .= " \x{2}/$r->{cmdNm} variable 5\x{2} <-- Start randomly add or subtract variable time-legth in between +/- 5 seconds with main Interval time of ". ms2fSec($r->{rptTm}) ." seconds \(this given \"5\" seconds will be accepted if it is not more than the half of main Interval time\)\. Current value is ". ms2fSec($r->{varTm}) ." seconds\. It can be upto +/- ". ms2fSec("". ($r->{rptTm}/ 2)) ." \(half of main Time Interval ". ms2fSec($r->{rptTm}) ."\) seconds\.\n";
- $mS .= " \x{2}/$r->{cmdNm} interval 30\x{2} <-- start to repeatedly check connection status with given IRC server, after each 30 seconds of main Time Interval, if/when enabled\. Current value is ". ms2fSec($r->{rptTm}) ." seconds\.\n";
- $mS .= " \x{2}/$r->{cmdNm} timerType 2\x{2} <-- use variable-length of time for checking connection status with given IRC server, \(or for doing your codes\)\. Current value is $r->{tmrTyp} \(". (($r->{tmrTyp} == 2 )? "variable-time" : "fixed-time") ."\)\.\n";
- $mS .= " \x{2}/$r->{cmdNm} showDbgMsg 0\x{2} <-- turn off showing debug messages on screen\. To turn on use 1, or, 2\. Current value is ". $r->{sDbgMsg} ." \(". (($r->{sDbgMsg} == 2 )? ("Show Debug Messages inside main/server tab of \"". $r->{NtwrkLstNm} ."\" IRC Network") : (($r->{sDbgMsg} == 1 )? "Show Debug Messages inside user\'s/your current tab position" : "Do Not Show Debug Messages")) ."\)\.\n";
- $mS .= " \x{2}/$r->{cmdNm} showDbgMsg 2\x{2} <-- show debug messages only inside the \"". $r->{NtwrkLstNm} ."\" IRC network\'s main/server tab\. \(If mentioned destination IRC Network\'s tab is not found, then messages will be shown inside user\'s current tab/context location\)\.\n";
- prnt($mS);
- $mS = "";
- } else { # Section: /CMD Sub-Cmd Value
- # User has specified a sub-command (option) or a value after main command:
- $userDat1 = lc((not defined $_[0][1] )? "" : $_[0][1]);
- # Checking single sub-command (SSC) or single value (SV) based options:
- if ($userDat1 eq 'off' or $userDat1 eq '0') {
- $r->{enbl} = 0; # disable REPEAT ( $r->{cmdNm} )
- if ($r->{hkPnt} or $r->{tmrExist} ) {
- if ($r->{sDbgMsg} ) {
- $mS=""; $mS .= "$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> Stopped a previously running Timer. \(\$r->{tmrExist}=$r->{tmrExist} , \$r->{hkPnt}=$r->{hkPnt} \) \(h_c\)";
- prnt_on( $r->{sDbgMsg}, $mS, $r->{NtwrkLstNm} );
- };
- Xchat::unhook( $r->{hkPnt} );
- # $r->{hkPnt} = 0;
- $r->{tmrExist} = 0; # 0 = No. 1 = yes.
- };
- prnt("\x{2}". $r->{cmdNm} ."\x{2} \(for ". $r->{NtwrkLstNm} ."\) --> is now \x{2}OFF\x{2}/disabled. Repeatation/Counting task is stopped, \(for checking connection status with \"". $r->{NtwrkLstNm} ."\" IRC Network\)\.");
- $r->{countr} = 0;
- return EAT_ALL;
- } elsif ($userDat1 eq 'on' or $userDat1 eq '1') {
- if ($r->{hkPnt} or $r->{tmrExist} ) {
- if ($r->{sDbgMsg} ) {
- $mS=""; $mS .= "$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> Stopped a previously running Timer. \(\$r->{tmrExist}=$r->{tmrExist} , \$r->{hkPnt}=$r->{hkPnt} \) \(h_c\)";
- prnt_on( $r->{sDbgMsg}, $mS, $r->{NtwrkLstNm} );
- };
- Xchat::unhook( $r->{hkPnt} );
- # $r->{hkPnt} = 0;
- $r->{tmrExist} = 0;
- };
- $r->{enbl} = 1; # enable REPEAT ( $r->{cmdNm} ).
- $r->{countr} = 0;
- prnt("\x{2}". $r->{cmdNm} ."\x{2} \(for ". $r->{NtwrkLstNm} ."\) --> is now \x{2}ON\x{2}/enabled, \(for \"". $r->{NtwrkLstNm} ."\" IRC Network\)\. Starting to repeat ". (($r->{upto} >= 1 )? ("upto ". $r->{upto} ) : "unlimited number of") ." times, after every ". (($r->{tmrTyp} == 2 )? "$r->{rndmTm} +/- ~$r->{varTm} seconds of variable-time" : "$r->{rptTm} seconds of fixed-time") ." based repeatations\.");
- &start_timer();
- return EAT_ALL;
- # } elsif ($userDat1 eq 'new-option') { list your other SSC, SV options #
- } else { # Section: Sub-Cmd Value
- # Checking options which requires a sub-command and also requires a value:
- if (defined $_[0][2]) { # Section: Value (after Sub-Cmd exist)
- # User has specified a non-empty value for sub-commands:
- $userDat2 = lc((not defined $_[0][2] )? "" : $_[0][2]);
- # Start to check given sub-commands, and their given values:
- if ($userDat1 eq "upto") {
- # Grabbing only positive integer portion:
- $uV=0; $uV = int(abs(0 + $userDat2));
- if ($uV >= 1) {
- $r->{upto} = $uV;
- prnt("$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> will repeat upto $r->{upto} times, if/when enabled\.");
- } elsif ($uV == 0) {
- $r->{upto} = $uV;
- prnt("$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> plugin will repeat unlimited times, for checking connection status with given IRC Server without stopping, unless you set it to any limited value, like\: 1\.");
- } else {
- prnt("$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> This \"". $userDat2 ."\" is empty or not a valid value for \"upto\" sub-command, please specify \(number of limited/finite repeatation number\) 1 or more\. If you specify 0, then plugin will repeat without stopping, for unlimited number of times\. Current \"upto\" value is\: $r->{upto} \(". (($r->{upto} >= 0 )? "means, unlimited number of " : "") ." repeatations\)\.");
- };
- return EAT_ALL;
- } elsif ($userDat1 eq "showdbgmsg") {
- # Grabbing only positive integer portion:
- $uV=0; $uV = int(abs(0 + $userDat2));
- if ($uV == 1 or $uV == 0) {
- $r->{sDbgMsg} = $uV;
- prnt("$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> will ". (($r->{sDbgMsg} )? "" : "not ") ."show debug messages on screen, if/when enabled\.");
- } elsif ($uV == 2) {
- $r->{sDbgMsg} = $uV;
- prnt("$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> will show debug messages only inside the \"". $r->{NtwrkLstNm} ."\" IRC Network\'s main/server tab, if/when enabled\.");
- } else {
- prnt("$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> This \"". $userDat2 ."\" is empty or not a valid value for \"showDbgMsg\" sub-command, please specify either 0 or 1\. Current value is\: $r->{sDbgMsg} \(". (($r->{sDbgMsg} == 2 )? ("Show Debug Messages inside main/server tab of \"". $r->{NtwrkLstNm} ."\" IRC Network") : (($r->{sDbgMsg} == 1 )? "Show Debug Messages on user\'s/your current tab position" : "Do Not Show Debug Messages")) ."\)\.\n");
- };
- return EAT_ALL;
- } elsif ($userDat1 eq "variable") {
- $uV=0; $uV = usrFSec2ms( ("".$userDat2) );
- if ($uV) {
- if ($uV <= rInt( ($r->{rptTm} / 2) )) {
- $r->{varTm} = sec2ms($uV);
- prnt("$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> plugin will start to randomly add or subtract variable time-length in betwenn +/- ". ms2fSec($r->{varTm} ) ." seconds with main time interval of ". ms2fSec($r->{rptTm} ) ." seconds, and then check IRC server connection status for ". (($r->{upto} >= 1 )? ("upto $r->{upto} " ) : "limitless number of ") ."times\. Currently \"TimerType\" option has\: $r->{tmrTyp} \(". (($r->{tmrTyp} == 2 )? "variable-timer" : "fixed-timer") ."\), means this \"Variable\" time will ". (($r->{tmrTyp} == 2 )? "" : "not ") ."be used\.");
- } else {
- prnt("$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> acceptable value \(for \"variable\"\) can be maximum ". ms2fSec($r->{rptTm} / 2) ." seconds \(which is, Half of current main Time Interval\)\. \(If you want to, you can set \"timerType\" sub-command\'s value to 1, to use fixed-length time for each repeatation\)\. Currently \"variable\" has value of\: ". ms2fSec($r->{varTm} ) ." seconds\.");
- };
- # } elsif ($userDat1 eq 'new-option') { list your other options #
- } else {
- prnt("$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> you/user have/has specified this \"". $userDat2 ."\" is empty or not a valid value for \"variable\" sub-command, please specify a different number\. \(If you want to, you can set \"timerType\" sub-command\'s value to 1, to use fixed-length time for each repeatation\)\. Current \"variable\" time value is\: ". ms2fSec($r->{varTm} ) ." seconds\.");
- };
- return EAT_ALL;
- } elsif ($userDat1 eq "interval") {
- $uV=0; $uV = usrFSec2ms($userDat2);
- if ($uV >= 2) {
- $r->{rptTm} = sec2ms($uV);
- prnt("$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> plugin will start to check connection status repeatedly \(for given IRC server, if/when enabled\), after each main Time Interval of around ~ ". ms2fSec($r->{rptTm} ) ." seconds, ". (($r->{upto} >= 1 )? ("until counting or max-repeatation number reaches ". $r->{upto} ) : "for unlimited number of times") ."\.");
- if ($r->{varTm} > rInt($r->{rptTm} / 2)) {
- $mS="";
- $mS .= "$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> previous \"Variable\" Time amount was ". ms2fSec($r->{varTm} ) ." seconds \($r->{varTm} milli-seconds\), and it was higher than half of currently specified main Time Interval amount, so lowered Variable Time and set to ";
- $r->{varTm} = rInt($r->{rptTm} / 2);
- $mS .= "". ms2fSec($r->{varTm} ) ." seconds \($r->{varTm} milli-seconds\)\.";
- prnt($mS);
- $mS="";
- };
- # On first repeatation, $r->{rndmTm} should be = to $r->{rptTm} :
- $r->{rndmTm} = $r->{rptTm};
- } else {
- prnt("$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> This \(sub-command\) value \"". $userDat2 ."\" is empty or not a valid \(sub-command\) value for \"interval\" sub-command, please specify a different number\. Number must be at-least\: 2 \(seconds\), or more. Current \"interval\" value is\: ". ms2fSec($r->{rptTm} ) ." seconds\.");
- };
- return EAT_ALL;
- } elsif ($userDat1 eq "timertype") {
- $uV=0; $uV = int(abs(0 + $userDat2));
- if ($uV >= 1 and $uV <= 2) {
- $r->{tmrTyp} = $uV;
- prnt("$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> plugin will use ". (($r->{tmrTyp} == 2 )? "random and variable-time" : "fixed-time") ." for each repeatation \(". (($r->{tmrTyp} == 2 )? (ms2fSec($r->{rptTm} ) ." +/- 0 to ". ms2fSec($r->{varTm} )) : ms2fSec($r->{rptTm} )) ." seconds\) before checking connection status with \(given\) IRC Server \(or before doing your codes\)\.");
- } else {
- prnt("$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> This \(sub-command\) value \"". $userDat2 ."\" is empty or not a valid \(sub-command\) value for \"timerType\" sub-command, please specify a different number\. Number 1 tells this plugin to use a fixed-length time for each repeatation, and number 2 tells the plugin to use variable-length time on each repeatation\. Current \"timerType\" value is\: $r->{tmrTyp} \(". (($r->{tmrTyp} == 2 )? "variable-time" : "fixed-time") ."\)\.");
- };
- return EAT_ALL;
- } else { # Section: /CMD No-Value_or_No-Sub-Cmd
- # User has not specified a valid/known sub-command/option,
- # or user has not specified a known value after main command:
- prnt("$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> This \"". ((defined $_[0][1] )? $_[0][1] : "") ."\" is empty or not a valid value or not a valid sub-command for this Plugin\. Valid sub-commands can be \"upto\", \"variable\", \"interval\", \"timerType\", \"showDbgMsg\", etc \(and then each of these sub-command also requires you/user specify at-least it\'s one valid value\)\. And \(other\) valid value after this plugin\'s main command currently can be\: ON, 1, OFF, 0, to enable or disable this plugin, etc\.");
- return EAT_ALL;
- }; # END of Section: /CMD No-Value_or_No-Sub-Cmd
- } # END of Section: Value (after Sub-Cmd exist)
- else { # Section: Value (after Sub-Cmd does not exist)
- # User has specified empty or non-valid or no value for sub-commands:
- prnt("$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> This \"". ((defined $_[0][2] )? $_[0][2] : "") ."\" is empty or not a valid value, for these sub-commands\: \"upto\", \"variable\", \"interval\", \"timerType\", \"showDbgMsg\", etc\. Please specify positive integer number without \+ symbol\. Negative numbers are not accepted\.");
- }; # END of Section: Value (after Sub-Cmd does not exist)
- }; # END of Section: Sub-Cmd Value
- }; # END of Section: /CMD Sub-Cmd Value
- return EAT_ALL;
- }, {data => "", help_text => "$r->{cmdNm} \: Plugin for repeating any tasks\. Like, checking connection status to \"". $r->{NtwrkLstNm} ."\"\. To see more info, type /$r->{cmdNm} "});
- # END of hook_command( $r->{cmdNm} , ...)
- # Below sections developed by Bry8Star:
- #
- # Simple small (and frequently used) functions:
- sub rInt ($) {
- # Convert given number or string, into a correctly
- # rounded INTEGER number:
- # (Output wil have no fraction, that is, no number after point)
- my($n) = ((defined $_[0] )? (((0 + $_[0]) != 0 )? (0 + $_[0]) : 0) : 0);
- # Thanks to user RET for below main code, then modified by Bry8Star:
- return int($n + (($n != 0 )? ($n / abs($n * 2)) : 0));
- }
- sub ms2sec ($) {
- # convert milli-seconds to seconds (including fractions) :
- return ((defined $_[0] )? (((0 + $_[0]) != 0 )? ((0 + $_[0]) / 1000) : 0) : 0);
- }
- sub ms2iSec ($) {
- # Convert milli-seconds into non-fractional seconds:
- # First, converting given STRING sec, into NUMBER sec:
- my($f) = ((defined $_[0] )? (((0 + $_[0]) != 0 )? ((0 + $_[0]) / 1000) : 0.0) : 0.0);
- # return int(( 0 + $_[0] )? ((0 + $_[0]) / 1000) : 0); # simple (but not correct)
- # converting into correctly rounded (non-fractional) integer number:
- return rInt($f);
- }
- sub ms2fSec ($) {
- # convert milli-seconds to fractional seconds
- # with upto max 3 numbers after dot/point:
- my($fsec) = 0.0;
- if (defined $_[0] ) {
- if ((0 + $_[0]) != 0 ) {
- $fsec = (0 + $_[0]) / 1000;
- };
- };
- # round it to fraction with upto 3 numbers only:
- my($sec3f) = 0.000;
- if ($fsec != 0) {
- $sec3f = sprintf("%.3f", ($fsec * 1.0));
- };
- # converting to string:
- my($sT) = "". $sec3f;
- # replacing multiple leading zero with one zero:
- $sT =~ s/^[0]+/"0"/e;
- # removing that one leading zero if a digit exist after it:
- $sT =~ s/^[0]([1-9])/$1/;
- # adding .000 text-string after number
- # which only had non-fraction portion:
- $sT =~ s/^(\d+)$/$1.".000"/e;
- # adding 2 zeroes (like .N00) after a fractional number
- # which only had 1 digit after the point:
- $sT =~ s/(\d+\.[1-9])$/$1."00"/e;
- # adding 1 zero (like .NN0) after a fractional-second
- # which only had 2 digits after the point:
- $sT =~ s/(\d+\.\d[1-9])$/$1."0"/e;
- # adding a zero in front when there is no zero before a point:
- $sT =~ s/^(\.\d+)/"0".$1/e;
- return $sT;
- }
- sub usrFSec2ms ($) {
- # Convert user given fractional second into milli-seconds:
- # (round & accept only 3 digits after point, if point exist)
- # First converting user given string input into a
- # fractional positive number:
- my($fsec) = 0.0;
- if (defined $_[0] ) {
- if ((0 + $_[0]) != 0 ) {
- $fsec = abs(0 + $_[0]);
- };
- };
- # Rounding it to fraction with upto 3 numbers only after the point:
- my($sec3f) = 0.0;
- if ($fsec != 0) {
- $sec3f = sprintf("%.3f", $fsec);
- };
- # converting into a milli-second NUMBER, and returning it back:
- my($iMS) = 0;
- if ($sec3f != 0 ) {
- $iMS = int($sec3f * 1000);
- };
- return $iMS;
- }
- sub sec2ms ($) {
- # convert seconds into milli-seconds :
- my($iMS) = 0;
- if (defined $_[0] ) {
- if ((0 + $_[0]) != 0 ) {
- $iMS = int((0 + $_[0]) * 1000);
- };
- };
- return $iMS;
- }
- # This section developed by Bry8Star:
- # portion of codes were contributed by nightfrog, Khisanth:
- sub prnt_on {
- my($sMsg, $uMsg, $ircNetNm) = @_;
- # my($sMsg)=(defined $_[0][1] )? (0 + $_[0][1]) : 0;
- if (defined $sMsg) { if ($sMsg) {
- # my($uMsg)=((defined $_[0][2] )? "". $_[0][2] : "");
- # my($rt) = 0;
- if ($sMsg == 1) {
- #$rt = prnt($uMsg);
- prnt($uMsg);
- #return $rt;
- } else {
- if ($sMsg == 2) {
- # my($ircNetNm)=((defined $_[0][3] )? "". $_[0][3] : "");
- my($uCurContxtLoc)= Xchat::get_context();
- if (defined $ircNetNm) {
- if ($ircNetNm) {
- if ((not defined $r->{cntxLocOfNtwrkSvTb} ) or not $r->{cntxLocOfNtwrkSvTb} ) {
- # This/below "for" loop portion of codes are based on script
- # from user "nightfrog". Which in turn based on codes from
- # user "Khisanth".
- for my $tab (Xchat::get_list "channels") {
- if ($tab->{type} == 1) {
- if (lc($tab->{network} ) eq lc($ircNetNm)) {
- $r->{cntxLocOfNtwrkSvTb} = $tab->{context};
- last; # by Bry8Star.
- };
- };
- }; # END of for loop
- };
- };
- };
- if (defined $r->{cntxLocOfNtwrkSvTb} ) {
- if ( $r->{cntxLocOfNtwrkSvTb} ) {
- prnt($uMsg) if (Xchat::set_context($r->{cntxLocOfNtwrkSvTb} ));
- #$rt = prnt($uMsg) if (Xchat::set_context($r->{cntxLocOfNtwrkSvTb} ));
- };
- } else {
- prnt($uMsg);
- #$rt = prnt($uMsg);
- };
- Xchat::set_context( $uCurContxtLoc );
- #return $rt;
- };
- };
- }; };
- return Xchat::EAT_NONE;
- } # END of sub prnt_on
- # This portion developed by Bry8Star:
- sub call_and_do_something {
- my($mS)="";
- $r->{countr} = $r->{countr} +1;
- #
- # BEGIN or place user's codes or YOUR-CODES or other
- # codes here, which you want to execute repeatedly based
- # on this REPEAT ($r->{cmdNm} ) plugin.
- # ...
- if ($r->{sDbgMsg} ) {
- $mS .= "$r->{countr} \(\$r->{hkPnt}=$r->{hkPnt} , \$r->{rndmTm}=$r->{rndmTm} , \$r->{rptTm}=$r->{rptTm} \) \(c_a_d_s\)";
- prnt_on( $r->{sDbgMsg}, $mS, $r->{NtwrkLstNm} );
- };
- $r->{elapsTm} = ($r->{tmrTyp} == 2 )? $r->{rndmTm} : $r->{rptTm};
- $r->{usrContxtPosn} = Xchat::get_context();
- if ((not defined $r->{cntxLocOfNtwrkSvTb} ) or not $r->{cntxLocOfNtwrkSvTb} ) {
- # Below "for" loop coding portion contributed by nightfrog,
- # which was based on user Khisanth's script.
- # Then further modified by Bry8Star.
- for my $tab (Xchat::get_list "channels") {
- if ($tab->{type} == 1) {
- if (lc( $tab->{network} ) eq lc( $r->{NtwrkLstNm} )) {
- $r->{cntxLocOfNtwrkSvTb} = $tab->{context};
- last; # by Bry8Star.
- };
- };
- }; # END of for loop
- };
- Xchat::set_context( $r->{cntxLocOfNtwrkSvTb} );
- if (defined Xchat::get_info "server") {
- if ($r->{sDbgMsg} ) {
- $mS=""; $mS .= "Internet connection status checking \(to IRC server\) is\: \x{2}ok\x{2} \:\) \(Last repeatation time period or duration was around ". ms2fSec($r->{elapsTm} ) ." seconds\) \(Checked total $r->{countr} times\) <-- \x{2}". $r->{cmdNm} ."\x{2} \(for ". $r->{NtwrkLstNm} ."\)";
- prnt_on( $r->{sDbgMsg}, $mS, $r->{NtwrkLstNm} );
- };
- Xchat::set_context( $r->{usrContxtPosn} );
- } else {
- if ($r->{sDbgMsg} ) {
- $mS=""; $mS .= "Internet connection status checking indicating\: \x{2}disconnected or Stopped\x{2} ! \:\( Trying to reconnect... \(Last repeatation time period or duration was around ". ms2fSec($r->{elapsTm} ) ." seconds\) \(Checked total $r->{countr} times\) <-- \x{2}". $r->{cmdNm} ."\x{2} \(for ". $r->{NtwrkLstNm} ."\)";
- prnt_on( $r->{sDbgMsg}, $mS, $r->{NtwrkLstNm} );
- };
- Xchat::command("RECONNECT") if (Xchat::set_context($r->{cntxLocOfNtwrkSvTb} ));
- Xchat::set_context( $r->{usrContxtPosn} );
- };
- # ...
- # END of YOUR-CODES.
- #
- # Adding below codes as safe-guard for not allowing a timer
- # to keep on running. Even if there is a coding mistake,
- # a timer should stop by itself and to force it to stop.
- # (But if $r->{upto} is set on to 0, then looping will be done
- # unlimited number of times: unless plugin is disabled/OFF,
- # or, unless a limited/finite number is set inside $r->{upto} ).
- if ($r->{enbl} == 0 or ($r->{upto} >= 1 and $r->{countr} >= $r->{upto} )) {
- # If REPEAT/$r->{cmdNm} is set to OFF, or when loop exceeds, then:
- if ($r->{sDbgMsg} ) {
- $mS=""; $mS .= "\x{2}". $r->{cmdNm} ."\x{2} \(for ". $r->{NtwrkLstNm} ."\) --> Repeatation/Counting \x{2}finished\x{2}\. \(c_a_d_s\)";
- prnt_on( $r->{sDbgMsg}, $mS, $r->{NtwrkLstNm} );
- };
- $r->{enbl} = 0; # disable REPEAT/$r->{cmdNm} plugin.
- if ($r->{hkPnt} or $r->{tmrExist} ) {
- if ($r->{sDbgMsg} ) {
- $mS=""; $mS .= "$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> Stopped a running Timer\. \(\$r->{tmrExist}=$r->{tmrExist} , \$r->{hkPnt}=$r->{hkPnt} \) \(c_a_d_s\)";
- prnt_on( $r->{sDbgMsg}, $mS, $r->{NtwrkLstNm} );
- };
- Xchat::unhook( $r->{hkPnt} );
- # $r->{hkPnt} = 0;
- $r->{tmrExist} = 0; # 0 = no. 1 = yes.
- };
- } else {
- \&start_timer();
- };
- } # END of sub call_and_do_something
- # This portion developed by Bry8Star:
- sub start_timer {
- # if $r->{tmrTyp} or $_[0] is 1, then keep on loop with
- # fixed time length based timer.
- # if $r->{tmrTyp} or $_[0] is 2, then loop with variable timer
- # once only, and then close previous timer and start another
- # timer with different time length again.
- # When variable-length timer is used for once, then it is
- # easier to stop them, and also easier to start with new
- # values if user has changed any options/parameters/values.
- my($mS)="";
- if ($r->{enbl} == 1 && ($r->{upto} == 0 or $r->{countr} < $r->{upto} )) {
- if ($r->{hkPnt} or $r->{tmrExist} ) {
- if ($r->{sDbgMsg} ) {
- $mS=""; $mS .= "$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> Stopped a previously running \(". (($r->{tmrTyp} == 2 )? "variable-time-length" : "fixed-timed") ."\) Timer\. \(\$r->{tmrExist}=$r->{tmrExist} , \$r->{hkPnt}=$r->{hkPnt} \) \(s_t\)";
- prnt_on( $r->{sDbgMsg}, $mS, $r->{NtwrkLstNm} );
- };
- Xchat::unhook( $r->{hkPnt} );
- # $r->{hkPnt} = 0;
- $r->{tmrExist} = 0;
- };
- if ($r->{tmrTyp} == 2) {
- # Creating a random amount of time in between from 0
- # to $r->{varTm} amount of time, then randomly either
- # adding or subtracting that from/with $r->{rptTm} ,
- # and then placing result inside $r->{rndmTm} :
- variate_repeat_time();
- };
- if ($r->{sDbgMsg} ) {
- $mS=""; $mS .= "$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> Creating a new \(". (($r->{tmrTyp} == 2 )? "variable-time-length" : "fixed-timed") ."\) Timer for ". ms2fSec(($r->{tmrTyp} == 2 )? $r->{rndmTm} : $r->{rptTm} ) ." seconds \(". (($r->{tmrTyp} == 2 )? $r->{rndmTm} : $r->{rptTm} ) ." milli-seconds\)\. \(s_t\)";
- prnt_on( $r->{sDbgMsg}, $mS, $r->{NtwrkLstNm} );
- };
- # WARNING:
- # Make sure to use "\" and "&" immediately before the sub-function
- # name, used inside the hook_timer command, to create one timer only,
- # or else, it will create $r->{upto} number ($r->{upto} = 0 means,
- # "unlimited" number) of multiple Timers at the same time and XChat
- # may crash or or become non-responsive very soon or instantly.
- $r->{tmrExist} += 1;
- $r->{hkPnt} = Xchat::hook_timer ( int(($r->{tmrTyp} == 2 )? $r->{rndmTm} : $r->{rptTm} ) , \&call_and_do_something ,{data => ""});
- }; # End of checking if repeatation allowed or not
- # Completely stop even the last timer, when followings are true:
- if ($r->{enbl} == 0 or ($r->{upto} >= 1 and $r->{countr} >= $r->{upto} )) {
- if ($r->{sDbgMsg} ) {
- $mS=""; $mS .= "$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> Repeatation \x{2}finished\x{2}\. \(s_t\)";
- prnt_on( $r->{sDbgMsg}, $mS, $r->{NtwrkLstNm} );
- };
- $r->{enbl} = 0;
- if ($r->{hkPnt} or $r->{tmrExist} ) {
- if ($r->{sDbgMsg} ) {
- $mS=""; $mS .= "$r->{cmdNm} \(for ". $r->{NtwrkLstNm} ."\) --> Stopped a previously running Timer\. \(\$r->{tmrExist}=$r->{tmrExist} , \$r->{hkPnt}=$r->{hkPnt} \) \(s_t\)";
- prnt_on( $r->{sDbgMsg}, $mS, $r->{NtwrkLstNm} );
- };
- Xchat::unhook( $r->{hkPnt} );
- # $r->{hkPnt} = 0;
- $r->{tmrExist} = 0;
- };
- };
- # return 0;
- return Xchat::EAT_NONE;
- } # END of sub start_timer
- # This portion developed by Bry8Star:
- sub variate_repeat_time {
- # Creating a random time-duration between 0 to $r->{varTm}
- # milli-seconds, and then randomly either subtracting it or
- # adding it with user's given main time-duration $r->{rptTm} ,
- # to variate/fluctuate total timer duration.
- # XChat internally uses milli-seconds.
- my($maxFluctuateRange) = int(1 + ((abs($r->{varTm} ) )? (abs($r->{varTm} )) : 0 )); # using milli-seconds
- # Generate a random (1st-)number, from 0 to 10000 (milli-seconds):
- my($randomNumB2inZroAndVarTime) = rand( $maxFluctuateRange );
- # Create another (2nd-)random/any number starting from 0 to 7:
- my($randomPlusMinus) = rand( 8);
- # If 2nd random Num is from 0 to 3 only then (try to) multiply with -1:
- if ($randomPlusMinus <= 3) {
- # If 1st random Num is not 0, then multiply with -1:
- if ($randomNumB2inZroAndVarTime > 0) {
- # Converting to a Negative number:
- $r->{rndmTm} = -1 * $randomNumB2inZroAndVarTime;
- };
- } else {
- $r->{rndmTm} = $randomNumB2inZroAndVarTime;
- };
- # Add +/- 0 to $r->{varTm} amount of variation, with main $r->{rptTm} :
- $r->{rndmTm} = int( abs( $r->{rndmTm} + abs( $r->{rptTm} ) ));
- return Xchat::EAT_NONE;
- } # END of sub variate_repeat_time
- # summary/short form : my($enabled) = 0; my($hookPoint) = 0; my($variableTime) = 10000; my($randomTime) = $variableTime; Xchat::hook_command("REPEAT", sub{ my($t)=0; if ($_[0][1] eq 'on') { $enabled=1; Xchat::unhook($hookPoint); &start_timer();}; if ($_[0][1] eq 'off') { Xchat::unhook($hookPoint); $enabled=0;}; $t=int(0 + $_[0][2]); if ($_[0][1] eq 'variableTime' and $t >= 2) { $variableTime = int($t * 1000);}; return Xchat::EAT_ALL;}); sub call_and_do_something { my_codes_which_needed_to_be_repeated(); if ($enabled) { \&start_timer();}; } sub start_timer { Xchat::unhook($hookPoint); variate_time(); if ($enabled) { $hookPoint = Xchat::hook_timer ( int($randomTime) , \&call_and_do_something );}; return Xchat::EAT_NONE; } sub variate_time { my($rT) = rand($variableRandomTime * 0.4); $randomTime = $variableTime; if(rand(1)){ $randomTime = $randomTime+$rT;} else{ $randomTime = $randomTime-$rT;}; return Xchat::EAT_NONE; }
Advertisement
Add Comment
Please, Sign In to add comment