2016-04-05 14:18:22 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-04-02 00:50:29 +02:00
|
|
|
import logging
|
2016-04-04 00:47:14 +02:00
|
|
|
import time
|
|
|
|
|
import random
|
2016-04-02 00:50:29 +02:00
|
|
|
import config
|
2016-04-05 14:18:22 +02:00
|
|
|
from plugin_system import pluginfunction, ptypes
|
2016-04-07 20:17:43 +02:00
|
|
|
|
2016-04-05 18:40:31 +02:00
|
|
|
log = logging.getLogger(__name__)
|
2016-04-04 00:47:14 +02:00
|
|
|
|
2016-04-05 18:13:47 +02:00
|
|
|
comment_joins_strings = [
|
|
|
|
|
"%s: please consider to fix your internet connection"
|
|
|
|
|
]
|
|
|
|
|
|
2016-04-05 18:40:31 +02:00
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('comment_joins', 'comments frequent joins', ptypes.MUC_ONLINE)
|
|
|
|
|
@config.config_locked
|
2016-04-04 00:47:14 +02:00
|
|
|
def comment_joins(**args):
|
|
|
|
|
# max elapsed time between the latest and the N latest join
|
2016-04-07 20:17:43 +02:00
|
|
|
timespan = 120
|
|
|
|
|
max_joins = config.runtime_config_store
|
2016-04-04 00:47:14 +02:00
|
|
|
|
|
|
|
|
current_timestamp = int(time.time())
|
|
|
|
|
|
|
|
|
|
arg_user = args['reply_user']
|
|
|
|
|
arg_user_key = arg_user.lower()
|
|
|
|
|
|
|
|
|
|
if arg_user_key not in config.runtime_config_store['user_joins']:
|
2016-04-07 20:17:43 +02:00
|
|
|
config.runtime_config_store['user_joins'][arg_user_key] = [current_timestamp]
|
2016-04-04 00:47:14 +02:00
|
|
|
config.runtimeconf_persist()
|
|
|
|
|
return None
|
|
|
|
|
|
2016-04-07 20:17:43 +02:00
|
|
|
user_joins = []
|
|
|
|
|
|
2016-04-05 18:13:47 +02:00
|
|
|
for ts in config.runtime_config_store['user_joins'][arg_user_key]:
|
|
|
|
|
if current_timestamp - int(ts) <= timespan:
|
|
|
|
|
user_joins.append(ts)
|
2016-04-04 00:47:14 +02:00
|
|
|
|
2016-04-05 18:13:47 +02:00
|
|
|
print(user_joins)
|
2016-04-04 00:47:14 +02:00
|
|
|
|
|
|
|
|
if len(user_joins) >= max_joins:
|
|
|
|
|
config.runtime_config_store['user_joins'].pop(arg_user_key)
|
|
|
|
|
config.runtimeconf_persist()
|
2016-04-05 18:13:47 +02:00
|
|
|
log.info("send comment on join")
|
2016-04-07 20:17:43 +02:00
|
|
|
return {'msg': random.choice(comment_joins_strings) % arg_user}
|
2016-04-04 00:47:14 +02:00
|
|
|
else:
|
2016-04-05 18:13:47 +02:00
|
|
|
user_joins.append(current_timestamp)
|
|
|
|
|
config.runtime_config_store['user_joins'][arg_user_key] = user_joins
|
2016-04-04 00:47:14 +02:00
|
|
|
config.runtimeconf_persist()
|