Beispiel #1
0
def output_json():
    # CSV => List
    greetings = []
    questions = []
    thoughts = []
    with open(CSV_PATH) as f:
        reader = csv.reader(f)
        for i, row in enumerate(reader):
            if i != 0:
                if len(row[1]) != 0:
                    greetings.append(Message(i - 1, row[1]))
                if len(row[2]) != 0:
                    questions.append(Message(i - 1, row[2]))
                if len(row[3]) != 0:
                    thoughts.append(Message(i - 1, row[3]))

    # List => messages
    messages = Messages(greetings, questions, thoughts)
    with open(OUTPUT_PATH, 'w') as f:
        json.dump(
            {'data': messages},
            f,
            default=default_method,
            ensure_ascii=False,
            indent=2,
        )
Beispiel #2
0
def disconnected_callback(peer_id):
    print("Peer", peer_id, "disconnected.")
    if is_relay:
        if len(list(peers.keys())) > 1:
            dest_peer_id = list(peers.keys())[0] if list(
                peers.keys())[0] is not peer_id else list(peers.keys())[1]
            if not peers.get(dest_peer_id)["soft_disconnected"]:
                send_to_single(
                    Packet(action=DisconnectAction(), message=Message()),
                    dest_peer_id)
    else:
        broadcast(
            Packet(action=ServiceAction(),
                   message=Message(text="{} disconnected.".format(peer_id))),
            [peer_id])
    async def readmeme(self, message):
        logging.info('Processing incoming meme.')
        current_user = message.author
        current_server = message.server

        if (current_server is None or current_user is None):
            logging.info('Missing info. Discarding.')
            return

        db_server = self.session.query(Server).filter(
            Server.discord_id == current_server.id).first()
        if (db_server is None): return
        if (not self.is_correct_channel_and_message(message, db_server)):
            logging.info('Not in selected channel. Discarding.')
            return

        db_user = self.session.query(User).filter(
            User.discord_id == current_user.id).first()
        if (db_user is None):
            db_user = User(current_user.id, current_user.name,
                           current_user.display_name)
            self.session.add(db_user)

        content = self.get_message_content(message)

        self.session.add(
            Message(message.id, db_server, db_user, content, message.timestamp,
                    1, 1))
        self.session.commit()
        logging.info('Wrote new meme.')
Beispiel #4
0
def create():

    # if MessageBody.query.get(request.json["body"]):

    try:
        msg = Message(body=request.json['body'],
                      sender=request.json['sender'],
                      destination=request.json['destination'],
                      customer_id=request.json['customer_id'],
                      parameters=request.json['parameters'])
        db.session.add(msg)
        db.session.commit()

        return jsonify(msg.serialize())
    except KeyError:
        return jsonify("key error"), 400
    except TypeError:
        return jsonify("wrong content-type"), 400
Beispiel #5
0
 def from_json_obj(json_obj):
     packet = Packet(
         action=Action.from_json_obj(
             json_obj=json_obj["action"]),
         message=Message.from_json_obj(
             json_obj=json_obj["message"])
     )
     if json_obj["data"]:
         packet.data = Data.from_json_obj(json_obj=json_obj["data"])
     return packet
Beispiel #6
0
def chat():
    content = request.form.get('content', '')
    sender_id = int(request.form.get('sender', DM_ID))
    sender = character_id_to_messages(sender_id)
    recipient_ids = map(int, request.form.getlist("recipients[]"))
    recipients = map(character_id_to_messages, recipient_ids)
    message = Message(content, sender, recipients)
    db.session.add(message)
    db.session.commit()
    add_message_update(message, sender_id, recipient_ids)

    return {'updates': get_updates(sender_id)}
    def update_reactions(self, reaction, user):
        current_user = user
        current_message = reaction.message
        current_server = reaction.message.server

        db_server = self.session.query(Server).filter(
            Server.discord_id == str(current_server.id)).first()
        if (db_server is None):
            # Lol, gotem
            logging.info('Server not found, bot not configured for: ' +
                         str(current_server.id) + ':' + current_server.name)
            return
        if (not self.is_correct_channel_and_message(current_message,
                                                    db_server)):
            return

        db_user = self.session.query(User).filter(
            User.discord_id == str(current_user.id)).first()
        if (db_user is None):
            db_user = User(current_user.id, current_user.name,
                           current_user.display_name)
            self.session.add(db_user)

        db_message = self.session.query(Message).filter(
            Message.discord_id == str(current_message.id)).first()
        if (db_message is None):
            content = self.get_message_content(current_message)
            db_message = Message(str(current_message.id), db_server, db_user,
                                 content, current_message.timestamp, 0, 0)
            self.session.add(db_message)

        if (str(reaction.emoji) == db_server.rx1):
            db_message.rx1_count = reaction.count
        elif (str(reaction.emoji) == db_server.rx2):
            db_message.rx2_count = reaction.count

        self.session.commit()
        logging.info('Updated reactions.')
def dispatch_message():
    channel_name = request.form.get('channel_name')
    admin_key = request.form.get('admin_key')
    title = request.form.get('title')
    body = request.form.get('message')

    if not channel_name:
        return to_error_json("channel_name is required")
    elif not title:
        return to_error_json("title is required")
    elif not body:
        return to_error_json("message is required")

    if Channel.query.filter(Channel.name == channel_name).first() is None:
        return to_error_json("Channel {} does not exist".format(channel_name))

    if Channel.query.filter(Channel.admin_key == admin_key).first() is None:
        return to_error_json("Admin Key does not match any channel")

    message = Message(channel_name=channel_name, title=title, message=body)
    db.session.add(message)
    db.session.commit()
    send_message(message=message, channel_name=channel_name, admin_key=admin_key)
    return jsonify(message.dict())
Beispiel #9
0
 def post(self, curr_user):
     data = request.get_json()
     member = curr_user.member
     message_content = data.get("content")
     topic_id = data.get("topic_id")
     if not message_content or not topic_id:
         json_abort(400, "תוכן ההודעה ריק")
     new_message = Message(content=message_content,
                           member_email=member.email,
                           topic_id=topic_id)
     if not new_message:
         json_abort(500, "יצירת הודעה חדשה לא הצליחה")
     db.session.add(new_message)
     db.session.commit()
     response = make_response(jsonify(message='ההודעה התווספה בהצלחה'), 200)
     return response
Beispiel #10
0
def server_connect(remote_host: str,
                   remote_port: int,
                   peer_id_override: str = None) -> tuple:
    if len(peers.keys()) == 1024:
        return "Reached maximum connections limit", None

    address = (remote_host, remote_port)

    if address in sockets.keys():
        connection, connected = sockets[address]["connection"], sockets[
            address]["used"]
        if connected:
            return "Already connected to {}:{}".format(address[0],
                                                       address[1]), None
        sockets[address]["used"] = True
    else:
        connection = socket.socket()
        try:
            connection.connect(address)
        except ConnectionRefusedError:
            return "Server is offline", None
        except socket.gaierror as e:
            return str(e), None
        connection.settimeout(30)
        sockets[address] = {"connection": connection, "used": True}

    peer = Server(remote_host, remote_port)

    if peer_id_override:
        peer.peer_id = peer_id_override

    incoming_message_thread = threading.Thread(
        target=server_new_message_listener, args=[peer, connection])
    incoming_message_thread.setDaemon(True)

    peers[peer.peer_id] = {
        "peer": peer,
        "thread": incoming_message_thread,
        "socket": connection
    }

    incoming_message_thread.start()

    send_message(peer.peer_id, Packet(action=ConnectAction(),
                                      message=Message()))
    print('Connected to server')
    return None, peer
Beispiel #11
0
def exit_handler(sig, frame):
    print("\nGot exit signal")
    broadcast(
        Packet(action=ServiceAction(),
               message=Message(text="Server shutdown.")), [])
    peer_ids = copy.deepcopy(list(peers.keys()))

    for peer_id in peer_ids:
        print("disconnecting peer", peer_id)
        peers[peer_id]["thread"].join(0)
        peers[peer_id]["socket"].shutdown(socket.SHUT_RDWR)
        peers[peer_id]["socket"].close()
        peers.pop(peer_id)

    sock.detach()
    sock.close()
    sys.exit(0)
    async def write_to_db(self, temp_cache):
        new_users = []
        user_cache = {}
        server = temp_cache['server']

        for user in temp_cache['new_users']:
            new_user = User(user.id, user.name, user.display_name)
            self.session.add(new_user)
            new_users.append(new_user)

        for message in temp_cache['new_messages']:
            if (message['user_in_db']
                    and message['author'].id not in user_cache):
                msg_user = self.session.query(User).filter(
                    User.discord_id == message['author'].id).first()
                user_cache[message['author'].id] = msg_user
            elif (message['user_in_db']):
                msg_user = user_cache[message['author'].id]
            else:
                msg_user = new_users[message['user_index']]

            rx1 = [d for d in message['rxns'] if str(d.emoji) == server.rx1]
            rx2 = [d for d in message['rxns'] if str(d.emoji) == server.rx2]

            if (len(rx1) == 0 or len(rx2) == 0):
                logging.info('Skipping due to no reactions.')
                continue
            else:
                rx1, rx2 = rx1[0], rx2[0]

            new_message = Message(message['id'], temp_cache['server'],
                                  msg_user, message['content'],
                                  message['timestamp'], rx1.count, rx2.count)
            self.session.add(new_message)

        self.session.commit()
        await self.bot.send_message(temp_cache['ctx'].message.channel,
                                    'Wrote messages to the database.')

        logging.info('Writing to database')
Beispiel #13
0
 def post(self, curr_user):
     data = request.get_json()
     member = curr_user.member
     topic_title = data.get("title")
     cluster_title = data.get("cluster_title")
     message = data.get("message")
     if not topic_title or not cluster_title:
         json_abort(400, "אחד או יותר מהנושאים חסרים")
     new_topic = Topic(title=topic_title,
                       member_email=member.email,
                       cluster_title=cluster_title)
     db.session.add(new_topic)
     db.session.flush()  # To obtain id.
     if not new_topic:
         json_abort(500, "תהליך יצירת נושא חדש נכשל")
     new_message = Message(content=message,
                           member_email=member.email,
                           topic_id=new_topic.id)
     db.session.add(new_message)
     db.session.commit()
     response = make_response(jsonify(message='הפוסט הוסף בהצלחה'), 200)
     return response
Beispiel #14
0
def incoming_message_listener(connection, peer: Client):
    while peer.peer_id in peers.keys():
        if peers[peer.peer_id]["muted"]:
            continue

        try:
            data = connection.recv(METADATA_LEN)
        except OSError as error:
            if debug:
                print("Error in message thread (peer {})".format(peer.peer_id))
                print(error)
            continue

        if debug:
            print("Data:", data)

        if data == b'':
            if not peers[peer.peer_id]["soft_disconnected"]:
                disconnected_callback(peer.peer_id)
            if peer.peer_id in peers.keys():
                peers.pop(peer.peer_id)
            break

        decoded_data: str = data.decode("utf8")
        if decoded_data.startswith(chr(64)) and decoded_data.endswith(chr(64)):

            def is_number(string: str):
                for char in string:
                    if ord(char) not in range(48, 58):
                        return False
                return True

            if is_number(decoded_data[1:METADATA_LEN - 1]):
                packet_len = int(decoded_data[1:METADATA_LEN - 1])
                data = connection.recv(packet_len)

        try:
            packet, status_code = layers.socket_handle_received(
                connection, data, loaded_modules, lambda m, e: print(
                    f"Error in module {m.__class__.__name__} on receive:\n{e}")
            )
            if not (packet and status_code == STATUS_OK):
                continue
            if packet.action.action == DisconnectAction().action:
                peers[peer.peer_id]["soft_disconnected"] = True
                disconnected_callback(peer.peer_id)
                peers[peer.peer_id]["wrote"] = True
                continue
            elif packet.action.action == ConnectAction().action:
                connected_callback(peer.peer_id)
                peers[peer.peer_id]["wrote"] = True
                continue
            elif packet.action.action == PeerInfoAction().action:
                peer_port = packet.data.content["port"]
                peer_nickname = packet.data.content["nickname"]
                peers[peer.peer_id]["peer"].nickname = peer_nickname
                peers[peer.peer_id]["peer"].port = peer_port
            else:
                if not peers[peer.peer_id]["wrote"]:
                    send_to_single(
                        Packet(action=ServiceAction(),
                               message=Message(text="Wrong button, buddy :)")),
                        peer.peer_id)
                    kick(peer.peer_id)
                    break
            peers[peer.peer_id]["wrote"] = True
            message_received(packet, peer)
            print("Message from", peer.peer_id, ": ", packet.message.text)
        except UnicodeDecodeError as error:
            print("Corrupted packet from", peer.peer_id)
            if debug:
                print(error)
        except (JSONDecodeError, KeyError, TypeError) as error:
            print("Invalid packet from", peer.peer_id)
            if debug:
                print(error)