Home

Script: yaaa.pl

Auto-away script. (for WeeChat ≥ 0.3.0)
Author: jnbek, version 0.3, GPL3 — added: 2009-11-09, updated: 2009-11-18
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# YaAA: Yet Another Auto Away Script written in Perl
# Copyright (c) 2009 by jnbek <jnbek@yahoo.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#Configuration Options:
#/set plugins.var.perl.yaaa.autoaway 10
#/set plugins.var.perl.yaaa.interval 5
#/set plugins.var.perl.yaaa.message AFK
#/set plugins.var.perl.yaaa.status on
#/set plugins.var.perl.yaaa.debug on
# Changelog:
#  0.1: first version
#  0.2: bug with toggle removed (nils <weechatter@arcor.de>)
#  0.2.1: Check configuration options for changes and apply them..
#  0.3: Fixed more config bugs. Fully Tested.
# TODO: Add a nick changer.

my $version  = '0.3';
my $interval = 5;         # Seconds between checks
my $autoaway = 10;        # Minutes til away
my $message  = "AFK";
my $status   = "on";
my $debug    = "off";
my $description = "YaAA: Set your away status based on inactivity.";
my $help        = "Toggles On/Off status";
weechat::register( "YaAA", "jnbek", $version, "GPL3", $description, "", "" );
weechat::hook_command( "yaaa", $help, "", "", "", "switch_up", "" );
weechat::hook_timer( $interval * 1000, 60, 0, "chk_timer", "" );

sub chk_conf {
    my ( $self, $buffer, $args ) = @_;

    #Check if config exists and write it if it doesn't
    if ( !weechat::config_get_plugin('interval') ) {
        weechat::config_set_plugin( 'interval',, $interval );
    }
    else {
        $interval = weechat::config_get_plugin('interval');
    }
    if ( !weechat::config_get_plugin('autoaway') ) {
        weechat::config_set_plugin( 'autoaway', $autoaway );
    }
    else {
        $autoaway = weechat::config_get_plugin('autoaway');
    }
    if ( !weechat::config_get_plugin('message') ) {
        weechat::config_set_plugin( 'message', $message );
    }
    else {
        $message = weechat::config_get_plugin('message');
    }
    if ( !weechat::config_get_plugin('status') && $status ne 'away') {
        weechat::config_set_plugin( 'status', $status );
    }
    else {
        if ($status ne 'away'){
            $status = weechat::config_get_plugin('status');
        }
    }
    if ( !weechat::config_get_plugin('debug') ) {
        weechat::config_set_plugin( 'debug', $debug );
    }
    else {
        $debug = weechat::config_get_plugin('debug');
    }
    return weechat::WEECHAT_RC_OK;
}

sub switch_up {
    my ( $self, $buffer, $args ) = @_;

    #Check status and set accordingly
    if ( weechat::config_get_plugin("status") eq "on" ) {
        $status = "off";
        weechat::config_set_plugin( "status", $status );
        weechat::print( $buffer, "YaAA: Yet Another AutoAway is now $status" );
    }
    elsif ( weechat::config_get_plugin("status") eq "off" ) {
        $status = "on";
        weechat::config_set_plugin( "status", $status );
        weechat::print( $buffer, "YaAA: Yet Another AutoAway is now $status" );
    }
    return weechat::WEECHAT_RC_OK;
}

sub chk_timer {
    my ( $self, $buffer, $args ) = @_;
    if ( $debug eq 'on' ) {
        weechat::print( $buffer,
            "Inactivity: " . weechat::info_get( "inactivity", "" ) );
        weechat::print( $buffer, "Interval: $interval" );
        weechat::print( $buffer, "Autoaway: $autoaway" );
        weechat::print( $buffer, "Message: $message" );
        weechat::print( $buffer, "Status: $status" );
        weechat::print( $buffer, "Debug: $debug" );
    }
    &chk_conf;
    return weechat::WEECHAT_RC_OK if $status eq 'off';
    if ( weechat::info_get( "inactivity", "" ) >= $autoaway * 60
        && $status ne "away" )
    {
        weechat::command( $buffer, "/away -all $message" );
        $status = "away";
    }
    elsif ( weechat::info_get( "inactivity", "" ) <= $autoaway * 60
        && $status eq "away" )
    {
        weechat::command( $buffer, "/away -all" );
        $status = "unaway";
    }
    return weechat::WEECHAT_RC_OK;
}