···11+# -*- coding: utf-8 -*-
22+#
33+# Copyright (c) 2009 by xt <xt@bash.no>
44+#
55+# This program is free software; you can redistribute it and/or modify
66+# it under the terms of the GNU General Public License as published by
77+# the Free Software Foundation; either version 3 of the License, or
88+# (at your option) any later version.
99+#
1010+# This program is distributed in the hope that it will be useful,
1111+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1212+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1313+# GNU General Public License for more details.
1414+#
1515+# You should have received a copy of the GNU General Public License
1616+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717+#
1818+1919+#
2020+# (this script requires WeeChat 0.3.0 or newer)
2121+#
2222+# History:
2323+# 2018-04-10, Sébastien Helleu <flashcode@flashtux.org>
2424+# version 0.5: fix infolist_time for WeeChat >= 2.2 (WeeChat returns a long
2525+# integer instead of a string)
2626+# 2016-02-05, ixti
2727+# version 0.4: Add Python3 support
2828+# 2009-12-15, xt
2929+# version 0.3: moved around some control structures to not be as noisy
3030+# 2009-12-02, xt
3131+# version 0.2: bugfix, more printing
3232+# 2009-12-01, xt <xt@bash.no>
3333+# version 0.1: initial release
3434+3535+import weechat as w
3636+import time
3737+3838+SCRIPT_NAME = "buffer_autoclose"
3939+SCRIPT_AUTHOR = "xt <xt@bash.no>"
4040+SCRIPT_VERSION = "0.5"
4141+SCRIPT_LICENSE = "GPL3"
4242+SCRIPT_DESC = "Automatically close inactive private message buffers"
4343+4444+settings = {
4545+ 'interval': '1', # How often in minutes to check
4646+ 'age_limit': '30', # How old in minutes before auto close
4747+ 'ignore': '', # Buffers to ignore (use full name: server.buffer_name)
4848+}
4949+5050+if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
5151+ SCRIPT_DESC, "", ""):
5252+ for option, default_value in settings.items():
5353+ if not w.config_is_set_plugin(option):
5454+ w.config_set_plugin(option, default_value)
5555+ w.hook_timer(\
5656+ int(w.config_get_plugin('interval')) * 1000 * 60,
5757+ 0,
5858+ 0,
5959+ "close_time_cb",
6060+ '')
6161+6262+6363+def get_all_buffers():
6464+ '''Returns list with pointers of all open buffers.'''
6565+ buffers = []
6666+ infolist = w.infolist_get('buffer', '', '')
6767+ while w.infolist_next(infolist):
6868+ buffer_type = w.buffer_get_string(w.infolist_pointer(infolist, 'pointer'), 'localvar_type')
6969+ if buffer_type == 'private': # we only close private message buffers for now
7070+ buffers.append(w.infolist_pointer(infolist, 'pointer'))
7171+ w.infolist_free(infolist)
7272+ return buffers
7373+7474+def get_last_line_date(buffer):
7575+ date = '1970-01-01 01:00:00'
7676+ infolist = w.infolist_get('buffer_lines', buffer, '')
7777+ while w.infolist_prev(infolist):
7878+ date = w.infolist_time(infolist, 'date')
7979+ # since WeeChat 2.2, infolist_time returns a long integer instead of
8080+ # a string
8181+ if not isinstance(date, str):
8282+ date = time.strftime('%F %T', time.localtime(int(date)))
8383+ if date != '1970-01-01 01:00:00':
8484+ # Some lines like "Day changed to" message doesn't have date
8585+ # set so loop until we find a message that does
8686+ break
8787+ w.infolist_free(infolist)
8888+ return date
8989+9090+def is_in_hotlist(buffer):
9191+ ''' Returns true if buffer is in hotlist, false if not'''
9292+9393+ hotlist = w.infolist_get('hotlist', '', '')
9494+ found = False
9595+ while w.infolist_next(hotlist):
9696+ thebuffer = w.infolist_pointer(hotlist, 'buffer_pointer')
9797+ if thebuffer == buffer:
9898+ found = True
9999+ name = w.buffer_get_string(thebuffer, 'short_name')
100100+ break
101101+102102+ w.infolist_free(hotlist)
103103+ return found
104104+105105+def close_time_cb(buffer, args):
106106+ ''' Callback for check for inactivity and close '''
107107+108108+ for buffer in get_all_buffers():
109109+ name = w.buffer_get_string(buffer, 'name')
110110+111111+112112+ date = get_last_line_date(buffer)
113113+ date = time.mktime(time.strptime(date, '%Y-%m-%d %H:%M:%S'))
114114+ now = time.time()
115115+ seconds_old = now - date
116116+ if seconds_old > int(w.config_get_plugin('age_limit'))*60:
117117+ if is_in_hotlist(buffer):
118118+ #w.prnt('', '%s: Not closing buffer: %s: it is in hotlist' %(SCRIPT_NAME, name))
119119+ continue
120120+ if name in w.config_get_plugin('ignore').split(','):
121121+ #w.prnt('', '%s: Not closing buffer: %s: it is in ignore list' %(SCRIPT_NAME, name))
122122+ continue
123123+ if buffer == w.current_buffer():
124124+ # Never close current buffer
125125+ #w.prnt('', '%s: Not closing buffer: %s: it is in currently active' %(SCRIPT_NAME, name))
126126+ continue
127127+ if len(w.buffer_get_string(buffer, 'input')):
128128+ # Don't close buffers with text on input line
129129+ #w.prnt('', '%s: Not closing buffer: %s: it has input' %(SCRIPT_NAME, name))
130130+ continue
131131+132132+ w.prnt('', '%s: Closing buffer: %s' %(SCRIPT_NAME, name))
133133+ w.command(buffer, '/buffer close')
134134+ #else:
135135+ # w.prnt('', '%s: Not closing buffer: %s: it is too new: %s' %(SCRIPT_NAME, name, seconds_old))
136136+137137+ return w.WEECHAT_RC_OK