logo

dotfiles

My dotfiles, one branch per machine, rebased on base git clone https://hacktivis.me/git/dotfiles.git

buffer_autoclose.py (5148B)


  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (c) 2009 by xt <xt@bash.no>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. #
  19. # (this script requires WeeChat 0.3.0 or newer)
  20. #
  21. # History:
  22. # 2018-04-10, Sébastien Helleu <flashcode@flashtux.org>
  23. # version 0.5: fix infolist_time for WeeChat >= 2.2 (WeeChat returns a long
  24. # integer instead of a string)
  25. # 2016-02-05, ixti
  26. # version 0.4: Add Python3 support
  27. # 2009-12-15, xt
  28. # version 0.3: moved around some control structures to not be as noisy
  29. # 2009-12-02, xt
  30. # version 0.2: bugfix, more printing
  31. # 2009-12-01, xt <xt@bash.no>
  32. # version 0.1: initial release
  33. import weechat as w
  34. import time
  35. SCRIPT_NAME = "buffer_autoclose"
  36. SCRIPT_AUTHOR = "xt <xt@bash.no>"
  37. SCRIPT_VERSION = "0.5"
  38. SCRIPT_LICENSE = "GPL3"
  39. SCRIPT_DESC = "Automatically close inactive private message buffers"
  40. settings = {
  41. 'interval': '1', # How often in minutes to check
  42. 'age_limit': '30', # How old in minutes before auto close
  43. 'ignore': '', # Buffers to ignore (use full name: server.buffer_name)
  44. }
  45. if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
  46. SCRIPT_DESC, "", ""):
  47. for option, default_value in settings.items():
  48. if not w.config_is_set_plugin(option):
  49. w.config_set_plugin(option, default_value)
  50. w.hook_timer(\
  51. int(w.config_get_plugin('interval')) * 1000 * 60,
  52. 0,
  53. 0,
  54. "close_time_cb",
  55. '')
  56. def get_all_buffers():
  57. '''Returns list with pointers of all open buffers.'''
  58. buffers = []
  59. infolist = w.infolist_get('buffer', '', '')
  60. while w.infolist_next(infolist):
  61. buffer_type = w.buffer_get_string(w.infolist_pointer(infolist, 'pointer'), 'localvar_type')
  62. if buffer_type == 'private': # we only close private message buffers for now
  63. buffers.append(w.infolist_pointer(infolist, 'pointer'))
  64. w.infolist_free(infolist)
  65. return buffers
  66. def get_last_line_date(buffer):
  67. date = '1970-01-01 01:00:00'
  68. infolist = w.infolist_get('buffer_lines', buffer, '')
  69. while w.infolist_prev(infolist):
  70. date = w.infolist_time(infolist, 'date')
  71. # since WeeChat 2.2, infolist_time returns a long integer instead of
  72. # a string
  73. if not isinstance(date, str):
  74. date = time.strftime('%F %T', time.localtime(int(date)))
  75. if date != '1970-01-01 01:00:00':
  76. # Some lines like "Day changed to" message doesn't have date
  77. # set so loop until we find a message that does
  78. break
  79. w.infolist_free(infolist)
  80. return date
  81. def is_in_hotlist(buffer):
  82. ''' Returns true if buffer is in hotlist, false if not'''
  83. hotlist = w.infolist_get('hotlist', '', '')
  84. found = False
  85. while w.infolist_next(hotlist):
  86. thebuffer = w.infolist_pointer(hotlist, 'buffer_pointer')
  87. if thebuffer == buffer:
  88. found = True
  89. name = w.buffer_get_string(thebuffer, 'short_name')
  90. break
  91. w.infolist_free(hotlist)
  92. return found
  93. def close_time_cb(buffer, args):
  94. ''' Callback for check for inactivity and close '''
  95. for buffer in get_all_buffers():
  96. name = w.buffer_get_string(buffer, 'name')
  97. date = get_last_line_date(buffer)
  98. date = time.mktime(time.strptime(date, '%Y-%m-%d %H:%M:%S'))
  99. now = time.time()
  100. seconds_old = now - date
  101. if seconds_old > int(w.config_get_plugin('age_limit'))*60:
  102. if is_in_hotlist(buffer):
  103. #w.prnt('', '%s: Not closing buffer: %s: it is in hotlist' %(SCRIPT_NAME, name))
  104. continue
  105. if name in w.config_get_plugin('ignore').split(','):
  106. #w.prnt('', '%s: Not closing buffer: %s: it is in ignore list' %(SCRIPT_NAME, name))
  107. continue
  108. if buffer == w.current_buffer():
  109. # Never close current buffer
  110. #w.prnt('', '%s: Not closing buffer: %s: it is in currently active' %(SCRIPT_NAME, name))
  111. continue
  112. if len(w.buffer_get_string(buffer, 'input')):
  113. # Don't close buffers with text on input line
  114. #w.prnt('', '%s: Not closing buffer: %s: it has input' %(SCRIPT_NAME, name))
  115. continue
  116. w.prnt('', '%s: Closing buffer: %s' %(SCRIPT_NAME, name))
  117. w.command(buffer, '/buffer close')
  118. #else:
  119. # w.prnt('', '%s: Not closing buffer: %s: it is too new: %s' %(SCRIPT_NAME, name, seconds_old))
  120. return w.WEECHAT_RC_OK