def process_cb(returncode, stdout, stderr, url_fetch_data): if returncode == 0: ctx = url_fetch_data['priv_ctx'] info_string = stdout.strip() url_fetch_data['url_title_cache'][url_fetch_data['url']] = \ (time.time() + url_fetch_data['max_cache_time'], info_string) if info_string: ctx.command('/say {}'.format(info_string)) hook_timer(2, url_requests_unlock, url_fetch_data)
def process_cb(returncode, stdout, stderr, url_fetch_data): if returncode == 0: ctx = url_fetch_data['priv_ctx'] info_string = stdout.strip() url_fetch_data['url_title_cache'][url_fetch_data['url']] = \ (time.time() + url_fetch_data['max_cache_time'], info_string) if info_string: ctx.command('/say \x02url_info |\x02 {}'.format(info_string)) hook_timer(2, url_requests_unlock, url_fetch_data)
def clean_cache(url_fetch_data): url_title_cache = url_fetch_data['url_title_cache'] old_len = len(url_title_cache) url_title_cache = { k: v for k, v in url_title_cache.iteritems() if v[0] > time.time() } if old_len > len(url_title_cache): weechat.prnt("", "Cleared {} cached urls.".format( old_len - len(url_title_cache))) url_fetch_data['url_title_cache'] = url_title_cache hook_timer(30*60, clean_cache, url_fetch_data) return weechat.WEECHAT_RC_OK
def privmsg(ctx, pline, signal, url_fetch_data): ctx.to_channel(pline.middle[0]) if url_fetch_data['locked']: return if not ctx.is_channel(pline.middle[0]): return if pline.trailing.startswith('+'): return if re.search(r'https?://', pline.trailing) is None: return if any( pattern.match(pline.prefix.raw) for pattern in url_fetch_data['ignore_list']): # weechat.prnt("", "Ignored {}".format(pline.prefix.mask)) return match = url_fetch_data['url_pattern'].search(pline.trailing) if match is None: return url = match.group('url') if '#' in url: url = url[:url.find('#')] url_fetch_data['locked'] = True # is it still cached? data = url_fetch_data['url_title_cache'].get(url, None) if data: ts, info_string = data if time.time() >= ts: del url_fetch_data['url_title_cache'][url] else: if info_string: ctx.command('/say \x02url_info |\x02 {}'.format(info_string)) hook_timer(2, url_requests_unlock, url_fetch_data) return url_fetch_data['proc_output'] = '' url_fetch_data['proc_errput'] = '' url_fetch_data['url'] = url url_fetch_data['priv_ctx'] = ctx hook_process(['fetch-url-title.py', url], process_cb, userdata=url_fetch_data)
def timer_hook(ctx, pline, userdata): caller = pline.prefix.nick args = pline.trailing.split(None, 2) usage = '/notice {} Invalid syntax: +timer <[ digits "h" ]' \ '[ digits "m" ][ digits "s" ]> <message>'.format(caller) if len(args) < 3: ctx.command(usage) return _, time_string, message = args time_seconds = to_seconds(time_string) if not time_seconds: ctx.command(usage) return if time_seconds > 2147483: ctx.command('/notice {} Too large, maximum is 2147483 seconds or {}' .format(caller, seconds_to_string(2147483))) return _userdata = dict( ctx=ctx, caller=caller, message=message, time_seconds=time_seconds ) _userdata['hook'] = hook_timer(time_seconds, cmd_timer_callback, _userdata) _userdata['tid'] = add_timer(time_seconds, _userdata) dt = datetime.now() + timedelta(seconds=time_seconds) ctx.command('/notice {} timer set to {} seconds ({}, Timer Id: {}, around: {})' .format(caller, time_seconds, seconds_to_string(time_seconds), _userdata['tid'], dt.strftime('%Y-%m-%d %H:%M:%S')))
def timer_hook(ctx, pline, userdata): caller = pline.prefix.nick args = pline.trailing.split(None, 2) usage = '/notice {} Invalid syntax: +timer <[ digits "h" ]' \ '[ digits "m" ][ digits "s" ]> <message>'.format(caller) if len(args) < 3: ctx.command(usage) return _, time_string, message = args time_seconds = to_seconds(time_string) if not time_seconds: ctx.command(usage) return if time_seconds > 2147483: ctx.command( '/notice {} Too large, maximum is 2147483 seconds or {}'.format( caller, seconds_to_string(2147483))) return _userdata = dict(ctx=ctx, caller=caller, message=message, time_seconds=time_seconds) _userdata['hook'] = hook_timer(time_seconds, cmd_timer_callback, _userdata) _userdata['tid'] = add_timer(time_seconds, _userdata) dt = datetime.now() + timedelta(seconds=time_seconds) ctx.command( '/notice {} timer set to {} seconds ({}, Timer Id: {}, around: {})'. format(caller, time_seconds, seconds_to_string(time_seconds), _userdata['tid'], dt.strftime('%Y-%m-%d %H:%M:%S')))
def privmsg(ctx, pline, signal, url_fetch_data): ctx.to_channel(pline.middle[0]) if url_fetch_data['locked']: return if not ctx.is_channel(pline.middle[0]): return if pline.trailing.startswith('+'): return if re.search(r'https?://', pline.trailing) is None: return if any(pattern.match(pline.prefix.raw) for pattern in url_fetch_data['ignore_list']): # weechat.prnt("", "Ignored {}".format(pline.prefix.mask)) return match = url_fetch_data['url_pattern'].search(pline.trailing) if match is None: return url = match.group('url') if '#' in url: url = url[:url.find('#')] url_fetch_data['locked'] = True # is it still cached? data = url_fetch_data['url_title_cache'].get(url, None) if data: ts, info_string = data if time.time() >= ts: del url_fetch_data['url_title_cache'][url] else: if info_string: ctx.command('/say \x02url_info |\x02 {}'.format(info_string)) hook_timer(2, url_requests_unlock, url_fetch_data) return url_fetch_data['proc_output'] = '' url_fetch_data['proc_errput'] = '' url_fetch_data['url'] = url url_fetch_data['priv_ctx'] = ctx hook_process(['fetch-url-title.py', url], process_cb, userdata=url_fetch_data)
def timer_hook(ctx, pline, userdata): caller = pline.prefix.nick usage = ('/notice {} Invalid syntax: +timer <[ digits "d" ][ digits "h" ]' '[ digits "m" ][ digits "s" ]> [<message>] || ' '+timer "<timestamp>" [<message>]' .format(caller)) _, _, arg_string = pline.trailing.partition(" ") # allow first argument to be quoted # while everything following becomes "the message" pattern = re.compile(r'''^(?P<q>["']?)(?P<time_string>[^"']+?)(?P=q)(\s+(?P<message>.*))?$''') match = pattern.match(arg_string.strip()) if match is None: ctx.command(usage) return time_string = match.group('time_string').strip() message = match.group('message') if message is None or not message.strip(): message = '' message = message.strip() # interpret timestamp time_seconds = to_seconds(time_string) if not time_seconds: timestamp = parse_timestamp(time_string) if not timestamp: ctx.command(usage) return delta = timestamp - datetime.now() time_seconds = int(delta.seconds + 86400*delta.days) if time_seconds < 0: ctx.command('/notice Timestamp is in past') return elif time_seconds > MAX_TIMER_LENGTH: ctx.command('/notice {} Too large, maximum is {} seconds or {}' .format(caller, MAX_TIMER_LENGTH, seconds_to_string(MAX_TIMER_LENGTH))) return _userdata = dict( ctx=ctx, caller=caller, message=message, time_seconds=time_seconds ) _userdata['hook'] = hook_timer(time_seconds, cmd_timer_callback, _userdata) _userdata['tid'] = add_timer(time_seconds, _userdata) dt = datetime.now() + timedelta(seconds=time_seconds) ctx.command('/notice {} timer set to {} seconds ({}, Timer Id: {}, around: {})' .format(caller, time_seconds, seconds_to_string(time_seconds), _userdata['tid'], dt.strftime('%Y-%m-%d %H:%M:%S')))
# loading existing timers from file if not os.path.exists(timer_filename): with open(timer_filename, 'w') as fp: json.dump({'timers': [], 'next': 1}, fp) timer_data = load_timers() for timer in timer_data['timers']: now = int(time.time()) _ud = timer['userdata'] fake_context = weechat_utils.Context(_ud['ctx']['server'], _ud['ctx']['channel']) _ud['ctx'] = fake_context _ud['time_seconds'] = timer['time_seconds'] _ud['tid'] = timer['tid'] _ud['_when'] = timer['when'] _ud['hook'] = hook_timer(max(1, timer['when'] - now), cmd_timer_callback, _ud) weechat.prnt(weechat.current_buffer(), u'{}* Loaded timer {tid} for {ctx.server}@{ctx.channel}' .format(weechat.color("yellow,black"), **_ud).encode('utf-8')) @hook_irc_command('+timer') def timer_hook(ctx, pline, userdata): caller = pline.prefix.nick args = pline.trailing.split(None, 2) usage = '/notice {} Invalid syntax: +timer <[ digits "h" ]' \ '[ digits "m" ][ digits "s" ]> <message>'.format(caller) if len(args) < 3: ctx.command(usage) return
# loading existing timers from file if not os.path.exists(timer_filename): with open(timer_filename, 'w') as fp: json.dump({'timers': [], 'next': 1}, fp) timer_data = load_timers() for timer in timer_data['timers']: now = int(time.time()) _ud = timer['userdata'] fake_context = weechat_utils.Context(_ud['ctx']['server'], _ud['ctx']['channel']) _ud['ctx'] = fake_context _ud['time_seconds'] = timer['time_seconds'] _ud['tid'] = timer['tid'] _ud['_when'] = timer['when'] _ud['hook'] = hook_timer(max(1, timer['when'] - now), cmd_timer_callback, _ud) weechat.prnt(weechat.current_buffer(), u'{}* Loaded timer {tid} for {ctx.server}@{ctx.channel}' .format(weechat.color("yellow,black"), **_ud).encode('utf-8')) MAX_TIMER_LENGTH = 60 * 60 * 24 * 7 * 4 # in seconds @hook_irc_command('+timer') def timer_hook(ctx, pline, userdata): caller = pline.prefix.nick usage = ('/notice {} Invalid syntax: +timer <[ digits "d" ][ digits "h" ]' '[ digits "m" ][ digits "s" ]> [<message>] || ' '+timer "<timestamp>" [<message>]' .format(caller)) _, _, arg_string = pline.trailing.partition(" ")