def send_mail(self):
        eml_type = self.in_eml_type.text()
        eml_file = self.in_eml_template.text()
        user_group = self.in_recipient_group.text()
        mail_excel = self.in_recipient_excel.text()
        annex_file = self.in_annex_file.text()
        url = self.data_db[5] if self.data_db else 'http://yumail.myvnc.com'

        try:
            if self.cb_scheduler.isChecked():
                my_time = self.in_scheduler.text()+':00'

                client = Client()
                client.send(self.data_smtp[:4], self.data_db[:5], eml_type, eml_file, user_group, mail_excel, annex_file, url, my_time)
                
                QMessageBox.information(self, 'Success!', '排程設定成功!', QMessageBox.Ok)
            else:
                sm = Smtp(self.data_smtp[0], int(self.data_smtp[1]), self.data_smtp[2], self.data_smtp[3]) if self.data_smtp else Smtp()
                db = Database(self.data_db[0], int(self.data_db[1]), self.data_db[2], self.data_db[3], self.data_db[4]) if self.data_db else Database()

                insert_send_mail(eml_type, eml_file, user_group, mail_excel, sm, db, annex=annex_file, url=url)
        
                sm.close()
                db.__disconnect__()
        
                QMessageBox.information(self, 'Success!', '信件寄出成功!', QMessageBox.Ok)
        except:
            QMessageBox.warning(self, 'Failed!', '信件寄出失敗!', QMessageBox.Ok)
Пример #2
0
    def insert_db(self, db: Database) -> None:
        """
        """

        request = "INSERT INTO contacts " + self.get_db_attributes(
        ) + " VALUES (%s, %s, %s, %s)"
        db.set(request, self.get_values())
        print("OK!")
Пример #3
0
def database():
    '''Returns a pre-loaded Database instance'''
    parent = os.path.dirname(__file__)
    path = os.path.join(parent, '..', 'test_data')
    prepare_csv_files(path)
    db = Database()
    db.load(path)
    shutil.rmtree(path)
    return db
Пример #4
0
 def start(self):
     self.cache = ManageCache()
     self.println("Connecting to database...", "info")
     self.database = Database(self)
     if self.database.connect():
         self.startServer = self.getTime()
         self.println("Starting server...", "info")
         self.langues = Langues()
         self.captcha = Captcha(self)
         self.users = Users(self)
         # emails list
         #pool = self.database.execute("SELECT * FROM users")
         #results = self.database.fetchall(pool)
         #for row in results:
         #	if not row["Email"] in list(self.cache.usersByEmail):
         #		self.cache.usersByEmail[row["Email"]] = []
         #	self.cache.usersByEmail[row["Email"]].append("{}#{}".format(row["playerName"], row["playerTag"]))
         self.rooms = Rooms(self, self.users)
         self.users.packetManage = PacketManage(self.users)
         self.users.commands = Commands(self.users, self.rooms)
         self.users.skills = Skills(self.users, self.rooms)
         self.users.shop = Shop(self.users)
         self.users.inventory = Inventory(self.users)
         self.users.missions = Missions(self.users)
         #self.users.updateSeasonRanking()
         self.tribulle = TribulleServer(self, self.users)
         self.dos = DoS(self)
         self.machine = Machine(self)
         self.machine.start()
         self.api = API(self)
         self.api.start()
         self.modopwet = ModoPwet(self)
         self.xenforo = Xenforo(self)
         #self.gameSWF = GameSWF(self)
         #self.gameSWF.start()
         self.discord = Discord
         self.discord.setServer(self)
         self.println(
             "Server loaded in: {}ms".format(
                 int(self.getTime() * 1000 - self.startServer * 1000)),
             "info")
         loop = asyncio.get_event_loop()
         bound_protocol = functools.partial(ClientHandler, self)
         for port in self.config["ports"]:
             coro = loop.create_server(bound_protocol, "0.0.0.0", int(port))
             server = loop.run_until_complete(coro)
             self.ports.append(int(port))
         self.saveJsonFiles()
         self.println("Server online on ports: {}\n".format(self.ports),
                      "info")
         self.println("Connecting to Discord API...", "info")
         self.discord.run(
             "NjQ2MDg0MzIxNzYwNzcyMTI3.Xorx5Q.RZr3oZVbGpviAEizFq63ovVgf-w")
         loop.run_forever()
Пример #5
0
def background_rss():
    mongodb = Database.get_mongodb_database(False)
    if mongodb:
        podcasts = mongodb.podcast.find()
        for podcast in podcasts:
            parser_podcast_rss.delay(podcast['collectionId'],
                                     podcast['feedUrl'])
    def send_mail(self):
        smtp_host = self.smtp_host.text() if self.smtp_host.text(
        ) else 'localhost'
        smtp_port = self.smtp_port.text() if self.smtp_port.text() else 465
        smtp_user = self.smtp_user.text() if self.smtp_user.text(
        ) else '*****@*****.**'
        smtp_pw = self.smtp_pw.text() if self.smtp_pw.text() else 'yucc1234'
        db_host = self.db_host.text() if self.db_host.text() else 'localhost'
        db_port = self.db_port.text() if self.db_port.text() else 3306
        db_user = self.db_user.text() if self.db_user.text() else 'socialmails'
        db_pw = self.db_pw.text() if self.db_pw.text() else 'socialmails123'
        db_db = self.db_db.text() if self.db_db.text() else 'socialmails'
        eml_file = self.eml.text()
        eml_type = self.eml_type.text()
        mails_excel = self.mails.text()
        mails_group = self.mails_group.text()

        if self.add_cb.isChecked():
            my_time = self.datetime_edit.text() + ':00'
            client = Client()
            client.send(eml_type, eml_file, mails_group, mails_excel, my_time)

        else:
            db = Database(db_host, db_port, db_user, db_pw, db_db)
            sm = Smtp(smtp_host, smtp_port, smtp_user, smtp_pw)

            insert_send_mail(eml_type, eml_file, mails_group, mails_excel, sm,
                             db)
Пример #7
0
def _load_podcasts_from_database(term):
    mongodb = Database.get_mongodb_database()
    if mongodb:
        podcasts = mongodb.podcast.find({
            "$or": [{
                "artistName": {
                    "$regex": term,
                    "$options": "i"
                }
            }, {
                "collectionName": {
                    "$regex": term,
                    "$options": "i"
                }
            }, {
                "feedUrl": {
                    "$regex": term,
                    "$options": "i"
                }
            }]
        })
        if podcasts.count() > 0:
            return (True, ujson.loads(dumps(podcasts)))
    else:
        # TODO(Better error handler)
        print("Error: No database found")
    return (False, [])
Пример #8
0
def record_search_term(term):
    mongodb = Database.get_mongodb_database(False)
    if mongodb:
        mongodb.term.replace_one({'term': term}, {'term': term}, upsert=True)
    else:
        # TODO(Better error handler)
        print("Error: No database found")
def insert_log(params, db=None):
    if not db:
        db = Database()

    eml_id = params[0]
    act = params[1]
    recipient_id = params[2]

    if act == '00':
        act = '送信'
    elif act == '01':
        act = '開信'
    elif act == '02':
        act = '點擊連結'
    elif act == '03':
        act = '開啟附件'

    db.insert_log((eml_id, recipient_id, act))
Пример #10
0
 def checking_expirations(self):
     with Database() as db:
         today = str(datetime.date.today())
         db.execute(
             '''DELETE FROM aidebot.daily_reminders WHERE (end_date<'{today}')'''
             .format(today=today))
         db.execute(
             '''DELETE FROM aidebot.receipts WHERE (end_date<'{today}')'''.
             format(today=today))
Пример #11
0
def background_itunes():
    mongodb = Database.get_mongodb_database(False)
    if mongodb:
        podcasts = mongodb.podcast.find()
        for podcast in podcasts:
            success, response = _load_podcast_info_from_itunes(
                podcast['collectionId'])
            if success:
                parser_itunes_response.delay(response)
Пример #12
0
def _load_podcast_episode_by_id(episode_id):
    mongodb = Database.get_mongodb_database()
    if mongodb:
        try:
            return mongodb.podcast_episode.find_one({'id': episode_id},
                                                    {'_id': False})
        except Exception as e:
            # TODO(Better error handler)
            print("Error: No episode found (%s)" % str(e))
            return None
    return None
Пример #13
0
def parser_itunes_response(itunes_response):
    mongodb = Database.get_mongodb_database(False)
    if mongodb:
        podcast_collection = mongodb.podcast
        for item in itunes_response:
            podcast_collection.replace_one(
                {'collectionId': item['collectionId']}, item, upsert=True)
            parser_podcast_rss.delay(item['collectionId'], item['feedUrl'])
    else:
        # TODO(Better error handler)
        print("Error: No database found")
Пример #14
0
 def __init__(self):
     self.config = None
     self.allowed_iniswaps = None
     self.loaded_ips = {}
     self.load_config()
     self.load_iniswaps()
     self.load_gimps()
     self.client_manager = ClientManager(self)
     self.area_manager = AreaManager(self)
     self.serverpoll_manager = ServerpollManager(self)
     self.ban_manager = BanManager()
     self.software = 'tsuserver3'
     self.version = 'tsuserver3dev'
     self.release = 3
     self.major_version = 3
     self.minor_version = 0
     self.char_list = None
     self.char_pages_ao1 = None
     self.music_list = None
     self.music_list_ao2 = None
     self.music_pages_ao1 = None
     self.backgrounds = None
     self.data = None
     self.features = set()
     self.load_characters()
     self.load_music()
     self.load_backgrounds()
     self.load_data()
     self.load_ids()
     self.enable_features()
     self.stats_manager = Database(self)
     self.district_client = None
     self.ms_client = None
     self.rp_mode = False
     self.runner = True
     self.runtime = 0
     logger.setup_logger(debug=self.config['debug'],
                         log_size=self.config['log_size'],
                         log_backups=self.config['log_backups'],
                         areas=self.area_manager.areas)
Пример #15
0
    def init_database(self) -> None:
        """
        Se connecte à la base de données.

        :return:
        """
        try:
            self.db = Database("127.0.0.1", "root", "", "tipe")
            info("Connecté à la base de données")
        except:
            error(
                "Une erreur est survenue lors de la connexion à la base de données"
            )
    def display_logs(self):
        self.data_temp_logs = []
        self.tbw_logs.setRowCount(0)
        self.clear_layout(self.right_layout)
        self.right_layout.addWidget(self.tbw_logs, 1, 3, 11, 8)
        self.right_layout.addWidget(QLabel('查詢 :'), 0, 3, 1, 1)
        self.right_layout.addWidget(self.cmb_logs_choice, 0, 4, 1, 2)
        self.right_layout.addWidget(self.in_logs_data, 0, 6, 1, 3)
        self.right_layout.addWidget(self.btn_logs_search, 0, 9, 1, 2)

        try:
            db = Database(self.data_db[0], int(self.data_db[1]), self.data_db[2], self.data_db[3], self.data_db[4]) if self.data_db[:5] else Database()
            self.data_logs = db.get_logs()
            self.data_temp_logs =  deepcopy(self.data_logs)
            
            if self.data_logs:
                row_num = len(self.data_logs)
                col_num = len(self.data_logs[0])
                col_lst = list(self.data_logs[0].keys())
                self.cmb_logs_choice.clear()
                self.cmb_logs_choice.addItems(col_lst)

                self.tbw_logs.setRowCount(row_num)  
                self.tbw_logs.setColumnCount(col_num)
                self.tbw_logs.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
                self.tbw_logs.setHorizontalHeaderLabels(col_lst)

                for i in range(row_num):
                    row_data = list(self.data_logs[i].values())
                    for j in range(col_num):
                        temp_data = row_data[j]
                        item = QTableWidgetItem(str(temp_data))
                        item.setForeground(QBrush(QColor(144, 182, 240)))
                        self.tbw_logs.setItem(i, j, item)
        except:
            QMessageBox.warning(self, 'Failed!', '資料庫連結失敗!', QMessageBox.Ok)
        else:
            db.__disconnect__()
Пример #17
0
def _load_podcast_info_by_id(id):
    mongodb = Database.get_mongodb_database()
    if mongodb:
        podcast = mongodb.podcast.find_one({'collectionId': id},
                                           {'_id': False})
        episodes = mongodb.podcast_episode.find({
            'collectionId': id
        }, {
            '_id': False
        }).sort([('number', -1)])
        return {
            'info': ujson.loads(dumps(podcast)),
            'episodes': ujson.loads(dumps(episodes))
        }
    return None
Пример #18
0
    def remind_information(self):
        with Database() as db:
            now = datetime.datetime.now()
            before_now = now - datetime.timedelta(minutes=30)
            now = now.strftime('%H:%M:%S')
            before_now = before_now.strftime('%H:%M:%S')

            data = db.query(
                '''SELECT national_code, time, user_id FROM aidebot.daily_reminders WHERE (Taken!=3 and time<='{now}') or (time>='23:00:00' and (Taken=1 or Taken=2) and '{now}'<'01:00:00')'''
                .format(now=now))

            #  data = db.query('''SELECT national_code, time, user_id
            #                            FROM aidebot.daily_reminders
            #                            WHERE time >= '{before_now}' and time<='{now}' and taken=0
            #                            '''.format(before_now=before_now, now=now))

            self.client.send_reminders(data)
Пример #19
0
    def __init__(self, **kwargs):
        self.loop = kwargs.pop('loop', asyncio.get_event_loop())
        self.host = kwargs.pop('host', '127.0.0.1')
        self.port = kwargs.pop('port', 8000)
        self.cloudflare = kwargs.pop('cloudflare', False)

        self.snowflake = SnowflakeGenerator(**kwargs.pop('snowflake', {}))
        self.token = TokenGenerator(**kwargs.pop('token', {}))

        self.database = Database(loop=self.loop,
                                 config=kwargs.pop('database', {}))
        self.httpclient = HTTPClient(loop=self.loop)

        self.router = Router(loop=self.loop)
        self.router.setup_session(kwargs.pop('session_token', {}))

        self.config = kwargs or {}

        self.tools = Tools(self)
Пример #20
0
class Server(asyncio.BaseTransport):
    def __init__(self):
        self.ports = []
        self.clients = []
        self.config = json.loads(open("./json/config.json", "r").read())
        self.shopList = json.loads(open("./json/shoplist.json", "r").read())
        self.words = json.loads(open("./json/words.json", "r").read())
        self.mutes = json.loads(open("./json/mutes.json", "r").read())
        self.bans = json.loads(open("./json/bans.json", "r").read())
        self.visuDone = {}
        self.startServer = 0
        self.langues = None
        self.captcha = None
        self.database = None
        self.rooms = None
        self.users = None
        self.tribulle = None
        self.cache = None
        self.dos = None
        self.api = None
        self.modopwet = None
        self.firewall = None
        self.machine = None
        self.xenforo = None
        self.discord = None
        self.chats = {}
        self.ipBans = {}
        self.gameSWF = None

    def set_protocol(self):
        return self

    def start(self):
        self.cache = ManageCache()
        self.println("Connecting to database...", "info")
        self.database = Database(self)
        if self.database.connect():
            self.startServer = self.getTime()
            self.println("Starting server...", "info")
            self.langues = Langues()
            self.captcha = Captcha(self)
            self.users = Users(self)
            # emails list
            #pool = self.database.execute("SELECT * FROM users")
            #results = self.database.fetchall(pool)
            #for row in results:
            #	if not row["Email"] in list(self.cache.usersByEmail):
            #		self.cache.usersByEmail[row["Email"]] = []
            #	self.cache.usersByEmail[row["Email"]].append("{}#{}".format(row["playerName"], row["playerTag"]))
            self.rooms = Rooms(self, self.users)
            self.users.packetManage = PacketManage(self.users)
            self.users.commands = Commands(self.users, self.rooms)
            self.users.skills = Skills(self.users, self.rooms)
            self.users.shop = Shop(self.users)
            self.users.inventory = Inventory(self.users)
            self.users.missions = Missions(self.users)
            #self.users.updateSeasonRanking()
            self.tribulle = TribulleServer(self, self.users)
            self.dos = DoS(self)
            self.machine = Machine(self)
            self.machine.start()
            self.api = API(self)
            self.api.start()
            self.modopwet = ModoPwet(self)
            self.xenforo = Xenforo(self)
            #self.gameSWF = GameSWF(self)
            #self.gameSWF.start()
            self.discord = Discord
            self.discord.setServer(self)
            self.println(
                "Server loaded in: {}ms".format(
                    int(self.getTime() * 1000 - self.startServer * 1000)),
                "info")
            loop = asyncio.get_event_loop()
            bound_protocol = functools.partial(ClientHandler, self)
            for port in self.config["ports"]:
                coro = loop.create_server(bound_protocol, "0.0.0.0", int(port))
                server = loop.run_until_complete(coro)
                self.ports.append(int(port))
            self.saveJsonFiles()
            self.println("Server online on ports: {}\n".format(self.ports),
                         "info")
            self.println("Connecting to Discord API...", "info")
            self.discord.run(
                "NjQ2MDg0MzIxNzYwNzcyMTI3.Xorx5Q.RZr3oZVbGpviAEizFq63ovVgf-w")
            loop.run_forever()

    def println(self, message, type_):
        today = datetime.date.today()
        print("[{}] [{}] {}".format(today, type_, message))

    def getTime(self):
        return time.time()

    def getHoursDiff(self, endTimeMillis):
        startTime = self.getTime()
        startTime = datetime.datetime.fromtimestamp(float(startTime))
        endTime = datetime.datetime.fromtimestamp(float(endTimeMillis))
        result = endTime - startTime
        seconds = (result.microseconds +
                   (result.seconds + result.days * 24 * 3600) * 10**6) / float(
                       10**6)
        hours = int(int(seconds) / 3600) + 1
        return hours

    def saveJsonFiles(self):
        json.dump(self.config, open("./json/config.json", "w"))
        json.dump(self.words, open("./json/words.json", "w"))
        json.dump(self.mutes, open("./json/mutes.json", "w"))
        json.dump(self.bans, open("./json/bans.json", "w"))
        json.dump(self.rooms.records, open("./json/records.json", "w"))
        json.dump(self.dos.ipBlocks, open("./json/dos.json", "w"))
        t = threading.Timer(60, self.saveJsonFiles)
        t.start()
Пример #21
0
def load_data(app):
    database = Database()
    database.load(app.config['DATA_PATH'])
    app.config.update({'DATABASE': database})
Пример #22
0
 def delete_history(self):
     with Database() as db:
         db.execute(
             '''DELETE FROM aidebot.history WHERE (last_taken_pill<'{date}')'''
             .format(date=datetime.datetime.now() -
                     datetime.timedelta(days=3)))

if __name__ == '__main__':

    eml_type, eml_file = '金融', '測資/test.eml'
    user_group, mail_excel = '智慧資安', '測資/recipients.xlsx'
    annex_file, date = '測資/風險金融商品比較表.doc', '2020-06-03 11:10:00'
    smtp_data = [
        'yumail.myvnc.com', '465', '*****@*****.**', 'yucc1234'
    ]
    db_data = [
        'yumail.myvnc.com', '3306', 'socialmails', 'socialmails123',
        'socialmails'
    ]

    mailserver = Smtp(smtp_data[0], int(smtp_data[1]), smtp_data[2],
                      smtp_data[3])
    database = Database(db_data[0], int(db_data[1]), db_data[2], db_data[3],
                        db_data[4])
    insert_send_mail(eml_type,
                     eml_file,
                     user_group,
                     mail_excel,
                     mailserver,
                     database,
                     annex=annex_file)
    mailserver.close()
    database.__disconnect__()

    # client = Client()
    # client.send(smtp_data, db_data, eml_type, eml_file, user_group, mail_excel, annex_file, date)
Пример #24
0
def parser_podcast_rss(podcast_id, url):
    # Select shows! By order...
    # podcast_episode.find(
    #    {'collectionId': 979020229}).sort( { number: -1 } )[0]
    mongodb = Database.get_mongodb_database(False)
    if mongodb:
        podcast_episode = mongodb.podcast_episode

        parsed = feedparser.parse(url)
        shows = parsed.get('entries', [])
        if len(shows) > 0:
            number = len(shows)
            for show in parsed['entries']:
                audio = None
                size = 0
                ext = 'mp3'
                for link in show.get('links'):
                    if 'audio' in link.get('type', ''):
                        audio_url = link.get('href', '')
                        size = link.get('length', 0)
                        if '.mp3' in audio_url:
                            ext = 'mp3'
                        elif '.ogg' in audio_url:
                            ext = 'ogg'
                        elif '.aac' in audio_url:
                            ext = 'aac'
                        elif '.wma' in audio_url:
                            ext = 'wma'
                        elif '.flac' in audio_url:
                            ext = 'flac'

                # In the future, we could get image from episode using
                # p = BeautifulSoup(show.get('content')[0].get('value'), 'lxml')
                # Hoping that the image bring back by this is the right one
                # p.find('img')

                guid = show.get('guid', str(randint(1000, 99999)) + '_')
                episode = {
                    'collectionId': podcast_id,
                    'id': str(sum([ord(i) for i in guid])),
                    'guid': guid,
                    'number': number,
                    'author': show.get('author', ''),
                    'title': show.get('title', ''),
                    'description': show.get('description', ''),
                    'published': show.get('published', ''),
                    'duration': show.get('itunes_duration', 0),
                    'audio_url': audio_url,
                    'audio_extension': ext,
                    'audio_size': size
                }

                podcast_episode.replace_one(
                    {
                        'collectionId': podcast_id,
                        'guid': episode['guid']
                    },
                    episode,
                    upsert=True)
                number -= 1
    else:
        # TODO(Better error handler)
        print("Error: No database found")
Пример #25
0
class TsuServer3:
    def __init__(self):
        self.config = None
        self.allowed_iniswaps = None
        self.loaded_ips = {}
        self.load_config()
        self.load_iniswaps()
        self.load_gimps()
        self.client_manager = ClientManager(self)
        self.area_manager = AreaManager(self)
        self.serverpoll_manager = ServerpollManager(self)
        self.ban_manager = BanManager()
        self.software = 'tsuserver3'
        self.version = 'tsuserver3dev'
        self.release = 3
        self.major_version = 3
        self.minor_version = 0
        self.char_list = None
        self.char_pages_ao1 = None
        self.music_list = None
        self.music_list_ao2 = None
        self.music_pages_ao1 = None
        self.backgrounds = None
        self.data = None
        self.features = set()
        self.load_characters()
        self.load_music()
        self.load_backgrounds()
        self.load_data()
        self.load_ids()
        self.enable_features()
        self.stats_manager = Database(self)
        self.district_client = None
        self.ms_client = None
        self.rp_mode = False
        self.runner = True
        self.runtime = 0
        logger.setup_logger(debug=self.config['debug'],
                            log_size=self.config['log_size'],
                            log_backups=self.config['log_backups'],
                            areas=self.area_manager.areas)

    def start(self):
        loop = asyncio.get_event_loop()

        bound_ip = '0.0.0.0'
        if self.config['local']:
            bound_ip = '127.0.0.1'

        ao_server_crt = loop.create_server(lambda: AOProtocol(self), bound_ip,
                                           self.config['port'])
        ao_server = loop.run_until_complete(ao_server_crt)

        if self.config['use_district']:
            self.district_client = DistrictClient(self)
            asyncio.ensure_future(self.district_client.connect(), loop=loop)

        if self.config['use_masterserver']:
            self.ms_client = MasterServerClient(self)
            asyncio.ensure_future(self.ms_client.connect(), loop=loop)

        logger.log_debug('Server started.')
        print("Welcome to AOVserver version " + str(self.release) + "." +
              str(self.major_version) + "." + str(self.minor_version))

        loop.run_until_complete(self.load_runner())
        try:
            loop.run_forever()
        except KeyboardInterrupt:
            pass

        logger.log_debug('Server shutting down.')
        self.runner = False
        ao_server.close()
        loop.run_until_complete(ao_server.wait_closed())
        loop.close()

    def get_version_string(self):
        return str(self.release) + '.' + str(self.major_version) + '.' + str(
            self.minor_version)

    def new_client(self, transport):
        ip = transport.get_extra_info('peername')[0]
        c = self.client_manager.new_client(transport)
        if ip not in self.loaded_ips:
            self.loaded_ips[ip] = 0
        self.loaded_ips[ip] += 1
        if self.rp_mode:
            c.in_rp = True
        c.server = self
        c.area = self.area_manager.default_area()
        c.area.new_client(c)
        return c

    def remove_client(self, client):
        client.area.remove_client(client)
        self.client_manager.remove_client(client)

    def get_player_count(self):
        return len(self.client_manager.clients)

    async def running_check(self):
        while self.runner:
            await asyncio.sleep(1)
            self.runtime += 1
            if self.runtime % 300 == 0:
                for area in self.area_manager.areas:
                    area.last_talked = None
                self.stats_manager.save_alldata()

    async def load_runner(self):
        asyncio.ensure_future(self.running_check())

    def load_config(self):
        with open('config/config.yaml', 'r', encoding='utf-8') as cfg:
            self.config = yaml.load(cfg)
            self.config['motd'] = self.config['motd'].replace('\\n', ' \n')
        if 'music_change_floodguard' not in self.config:
            self.config['music_change_floodguard'] = {
                'times_per_interval': 1,
                'interval_length': 0,
                'mute_length': 0
            }
        if 'wtce_floodguard' not in self.config:
            self.config['wtce_floodguard'] = {
                'times_per_interval': 1,
                'interval_length': 0,
                'mute_length': 0
            }
        if 'log_size' not in self.config:
            self.config['log_size'] = 1048576
        if 'log_backups' not in self.config:
            self.config['log_backups'] = 5

    def load_gimps(self):
        with open('config/gimp.yaml', 'r', encoding='utf-8') as cfg:
            self.gimp_list = yaml.load(cfg)

    def load_ids(self):
        self.hdid_list = {}
        # load hdids
        try:
            with open('storage/hd_ids.json', 'r',
                      encoding='utf-8') as whole_list:
                self.hdid_list = json.loads(whole_list.read())
        except:
            logger.log_debug(
                'Failed to load hd_ids.json from ./storage. If hd_ids.json is exist then remove it.'
            )

    def load_characters(self):
        with open('config/characters.yaml', 'r', encoding='utf-8') as chars:
            self.char_list = yaml.load(chars)
        self.build_char_pages_ao1()

    def load_music(self):
        with open('config/music.yaml', 'r', encoding='utf-8') as music:
            self.music_list = yaml.load(music)
        self.build_music_pages_ao1()
        self.build_music_list_ao2()

    def load_data(self):
        with open('config/data.yaml', 'r') as data:
            self.data = yaml.load(data)

    def save_data(self):
        with open('config/data.yaml', 'w') as data:
            json.dump(self.data, data)

    def save_id(self):
        with open('storage/hd_ids.json', 'w') as data:
            json.dump(self.hdid_list, data)

    def get_ipid(self, ip):
        x = ip + str(self.config['server_number'])
        hash_object = hashlib.sha256(x.encode('utf-8'))
        hash = hash_object.hexdigest()[:12]
        return hash

    def load_backgrounds(self):
        with open('config/backgrounds.yaml', 'r', encoding='utf-8') as bgs:
            self.backgrounds = yaml.load(bgs)

    def load_iniswaps(self):
        try:
            with open('config/iniswaps.yaml', 'r',
                      encoding='utf-8') as iniswaps:
                self.allowed_iniswaps = yaml.load(iniswaps)
        except:
            logger.log_debug('cannot find iniswaps.yaml')

    def enable_features(self):
        self.features.add('modcall_reason')

    def build_char_pages_ao1(self):
        self.char_pages_ao1 = [
            self.char_list[x:x + 10] for x in range(0, len(self.char_list), 10)
        ]
        for i in range(len(self.char_list)):
            self.char_pages_ao1[i // 10][i % 10] = '{}#{}&&0&&&0&'.format(
                i, self.char_list[i])

    def build_music_pages_ao1(self):
        self.music_pages_ao1 = []
        index = 0
        # add areas first
        for area in self.area_manager.areas:
            self.music_pages_ao1.append('{}#{}'.format(index, area.name))
            index += 1
        # then add music
        for item in self.music_list:
            self.music_pages_ao1.append('{}#{}'.format(index,
                                                       item['category']))
            index += 1
            for song in item['songs']:
                self.music_pages_ao1.append('{}#{}'.format(
                    index, song['name']))
                index += 1
        self.music_pages_ao1 = [
            self.music_pages_ao1[x:x + 10]
            for x in range(0, len(self.music_pages_ao1), 10)
        ]

    def build_music_list_ao2(self):
        self.music_list_ao2 = []
        # add areas first
        for area in self.area_manager.areas:
            self.music_list_ao2.append(area.name)
            # then add music
        for item in self.music_list:
            self.music_list_ao2.append(item['category'])
            for song in item['songs']:
                self.music_list_ao2.append(song['name'])

    def is_valid_char_id(self, char_id):
        return len(self.char_list) > char_id >= 0

    def get_char_id_by_name(self, name):
        for i, ch in enumerate(self.char_list):
            if ch.lower() == name.lower():
                return i
        raise ServerError('Character not found.')

    def get_song_data(self, music):
        for item in self.music_list:
            if item['category'] == music:
                return item['category'], -1
            for song in item['songs']:
                if song['name'] == music:
                    try:
                        return song['name'], song['length']
                    except KeyError:
                        return song['name'], -1
        raise ServerError('Music not found.')

    def send_all_cmd_pred(self, cmd, *args, pred=lambda x: True):
        for client in self.client_manager.clients:
            if pred(client):
                client.send_command(cmd, *args)

    def broadcast_global(self, client, msg, as_mod=False):
        char_name = client.get_char_name()
        ooc_name = '{}[{}][{}]'.format('<dollar>G', client.area.id, char_name)
        if as_mod:
            ooc_name += '[M]'
        self.send_all_cmd_pred('CT',
                               ooc_name,
                               msg,
                               pred=lambda x: not x.muted_global)
        if self.config['use_district']:
            self.district_client.send_raw_message('GLOBAL#{}#{}#{}#{}'.format(
                int(as_mod), client.area.id, char_name, msg))

    def broadcast_need(self, client, msg):
        char_name = client.get_char_name()
        area_name = client.area.name
        area_id = client.area.id
        self.send_all_cmd_pred(
            'CT',
            '{}'.format(self.config['hostname']),
            '=== Advert ===\r\n{} in {} [{}] needs {}\r\n==============='.
            format(char_name, area_name, area_id, msg),
            pred=lambda x: not x.muted_adverts)
        if self.config['use_district']:
            self.district_client.send_raw_message('NEED#{}#{}#{}#{}'.format(
                char_name, area_name, area_id, msg))