Exemple #1
0
def user_join(c_user):
	try:
		user = db.getUser(id=c_user.id)
	except KeyError as e:
		user = None

	if user is not None:
		if user.isBlacklisted():
			return rp.Reply(rp.types.ERR_BLACKLISTED, reason=user.blacklistReason, contact=blacklist_contact)
		elif user.isJoined():
			return rp.Reply(rp.types.USER_IN_CHAT)
		# user rejoins
		with db.modifyUser(id=user.id) as user:
			user.setLeft(False)
		logging.info("%s rejoined chat", user)
		return rp.Reply(rp.types.CHAT_JOIN)

	# create new user
	user = User()
	user.defaults()
	user.id = c_user.id
	updateUserFromEvent(user, c_user)
	if not any(db.iterateUserIds()):
		user.rank = RANKS.admin

	logging.info("%s joined chat", user)
	db.addUser(user)
	ret = [rp.Reply(rp.types.CHAT_JOIN)]

	motd = db.getSystemConfig().motd
	if motd != "":
		ret.append(rp.Reply(rp.types.CUSTOM, text=motd))

	return ret
Exemple #2
0
def main(configpath, importpath):
    with open(configpath, "r") as f:
        config = yaml.load(f)

    logging.basicConfig(
        format="[%(asctime)s] %(message)s", datefmt="%Y-%m-%d %H:%M", level=logging.INFO
    )

    db = open_db(config)

    with open(importpath, "r") as f:
        data = json.load(f)

    had_ids = set()
    for j in data["users"]:
        u = User()
        u.id = j["id"]
        u.username = j.get("username", None)
        u.realname = j.get("realname", "")
        u.rank = j["rank"]
        u.joined = safe_time(0)
        if j.get("left", False) != False:
            u.left = safe_time(j["left"] // 1000)
        u.lastActive = u.joined
        if "banned" in j.keys():
            u.cooldownUntil = safe_time(j["banned"] // 1000)
        if "reason" in j.keys():
            u.blacklistReason = j["reason"]
        u.warnings = j.get("warnings", 0)
        if u.warnings > 0:
            u.warnExpiry = safe_time(j["warnUpdated"] // 1000) + timedelta(
                hours=WARN_EXPIRE_HOURS
            )
        u.karma = j.get("karma", 0)
        u.hideKarma = j.get("hideKarma", False)
        u.debugEnabled = j.get("debug", False)

        if u.id in had_ids:
            logging.warning("%s is duplicate, dropping the second one", u)
        else:
            db.addUser(u)
            had_ids.add(u.id)

    c = SystemConfig()
    c.motd = data["system"]["motd"]
    db.setSystemConfig(c)

    logging.info("Success.")
    db.close()