summaryrefslogtreecommitdiff
path: root/scripts/python/autoaway.py
blob: b61d74dfeaed447890922440edd60fa4b6e3954e (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# --------------------------------------------------------------------
#
# Copyright (c) 2006 by Gwenn Gueguen <weechat@grumly.info>
#
# 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
#
# --------------------------------------------------------------------
# This script automatically sets away after a period of inactivity
# --------------------------------------------------------------------


import weechat


"""
/autoaway [time [message]]

/autoaway whithout any parameter disables autoaway

Script config:
  time:
    number of minutes of inactivity after which a user will be marked as away
  message:
    message that will be passed to the /away command
  enabled:
    is autoaway enabled ?
"""


SCRIPT_NAME="autoaway"
SCRIPT_VERSION="0.2"
SCRIPT_DESC="autoaway script for weechat"


# Names of the settings
CONFIG_TIME="time"
CONFIG_MESSAGE="message"
CONFIG_ENABLED="enabled"

# Name of the command
CMD_AUTOAWAY="autoaway"

# Default settings
DEFAULT_TIME="15"
DEFAULT_MESSAGE="idle"

# Interval (in seconds) between checks
TIMER_VALUE=15


def print_settings():
    weechat.prnt("AutoAway settings:")
    weechat.prnt("  time: %s minute(s)" % weechat.get_plugin_config(CONFIG_TIME))
    weechat.prnt("  message: %s" % weechat.get_plugin_config(CONFIG_MESSAGE))
    if weechat.get_plugin_config(CONFIG_ENABLED) == "true":
        weechat.prnt("  enabled")
    else:
        weechat.prnt("  disabled")


def autoaway(server, args):
    weechat.remove_timer_handler("timer_handler")

    params = args.split(None, 1)
    if len(params) == 0:
        weechat.set_plugin_config(CONFIG_ENABLED, "false")
    else:
        weechat.set_plugin_config(CONFIG_ENABLED, "true")
        weechat.set_plugin_config(CONFIG_TIME, params[0])
        if len(params) > 1:
            weechat.set_plugin_config(CONFIG_MESSAGE, params[1])

        previous_inactivity = int(weechat.get_info("inactivity"))
        weechat.add_timer_handler(TIMER_VALUE, "timer_handler")

    print_settings()

    return weechat.PLUGIN_RC_OK


def timer_handler():
    global previous_inactivity, previous_state

    # Get current away status
    away_flag = int(weechat.get_info("away"))

    # Get number of seconds of inactivity
    idle_time = int(weechat.get_info("inactivity"))

    if away_flag == previous_state:
        # away flag was not changed outside this script
        if away_flag and idle_time < previous_inactivity:
            # Inactivity was reset (or overflowed ?)
            weechat.command("/away -all")
        elif not away_flag and idle_time >= (60 * int(weechat.get_plugin_config(CONFIG_TIME))):
            # Time to go away
            weechat.command("/away -all %s" % weechat.get_plugin_config(CONFIG_MESSAGE))

    previous_state = int(weechat.get_info("away"))
    previous_inactivity = idle_time

    return weechat.PLUGIN_RC_OK


if weechat.register(SCRIPT_NAME, SCRIPT_VERSION, "", SCRIPT_DESC):

    try:
        previous_state = int(weechat.get_info("away"))
    except ValueError:
        previous_state = 0

    # Set config to default values if undefined
    try:
        idle_time = int(weechat.get_plugin_config(CONFIG_TIME))
    except ValueError:
        weechat.set_plugin_config(CONFIG_TIME, DEFAULT_TIME)
    
    if weechat.get_plugin_config(CONFIG_MESSAGE) == None:
        weechat.set_plugin_config(CONFIG_MESSAGE, DEFAULT_MESSAGE)
    
    if weechat.get_plugin_config(CONFIG_ENABLED) == None:
        weechat.set_plugin_config(CONFIG_ENABLED, "false")


    # Display a summary of the settings
    print_settings()


    # Start the timer if necessary
    if weechat.get_plugin_config(CONFIG_ENABLED) == "true":
        previous_inactivity = int(weechat.get_info("inactivity"))
        weechat.add_timer_handler(TIMER_VALUE, "timer_handler")


    weechat.add_command_handler(CMD_AUTOAWAY, "autoaway", "Set autoaway", 
      "[time [message]]",
      "time: number of minutes before being marked as away\n"
      + "message: away message\n"
      + "\n"
      + "whithout any argument, autoaway will be disabled\n")