Exemplo n.º 1
0
def home():

    if request.method == 'POST':
        original_message = request.form['content']
        rendered_message = html.escape(str(original_message))

        m = Message(content=rendered_message)
        m.save()

    body = """
<html>
<body>
<h1>Class Message Board</h1>
<h2>Contribute to the Knowledge of Others</h2>
<form method="POST">
    <textarea name="content"></textarea>
    <input type="submit" value="Submit">
</form>

<h2>Wisdom From Your Fellow Classmates</h2>
"""
    
    for m in Message.select():
        body += """
<div class="message">
{}
</div>
""".format(m.content)

    return body 
Exemplo n.º 2
0
def send_message():
    # Handles sending a message to a specific user.

    # create a message field in the db.
    message = request.form.get('message')
    current_user_id = session['user_id']
    posting_user = request.form.get('posting_user')

    Message.add_message(current_user_id, posting_user, message)

    # Send an alert via sms to the posting
    # user that someone is interested in their food.
    food_listing_id = request.form.get('food_listing')
    food_listing = Food.query.get(food_listing_id)
    poster_name = food_listing.user.fname

    phone_number = food_listing.phone_number
    if phone_number:
        phone_number = "+1"+phone_number
        send_text(phone_number,
                  ("Hi, %s! Someone's interested in your post " +
                   "on Make Less Mush! Sign in and check your " +
                   "messages!") % (poster_name))

    flash('Your message has been sent.')

    return redirect('/home')
Exemplo n.º 3
0
 def post(self):
     message_text = self.request.get("message")
     message = Message()
     message.message_text = message_text
     message.put()
     return self.render_template("hello.html",
                                 params={"message": message_text})
Exemplo n.º 4
0
def home():

    if request.method == 'POST':
        m = Message(content=request.form['content'])
        m.save()

    body = """
<html>
<body>
<h1>Class Message Board</h1>
<h2>Contribute to the Knowledge of Others</h2>
<form method="POST">
    <textarea name="content"></textarea>
    <input type="submit" value="Submit">
</form>
<h2>Wisdom From Your Fellow Classmates</h2>
"""
    # Replacing str with HTML char. sequences (Unicode Hexadecimal)
    for m in Message.select():
        body += """
<div class="message">
{}
</div>
""".format(m.content.replace('<', '&#x003C;').replace('>', '&#x003E;')
    .replace('(', '&#x0028;').replace(')', '&#x0029;').replace("'", '&#x0027;')
    .replace("[", '&#x005B;').replace("]", '&#x005D;').replace("{", '&#x007B;')
    .replace("}", '&#x007D;').replace("!", '&#x0021;'))

    return body 
Exemplo n.º 5
0
def home():

    if request.method == 'POST':
        m = Message(content=request.form['content'])
        m.save()

    body = """
<html>
<body>
<h1>Class Message Board</h1>
<h2>Contribute to the Knowledge of Others</h2>
<form method="POST">
    <textarea name="content"></textarea>
    <input type="submit" value="Submit">
</form>

<h2>Wisdom From Your Fellow Classmates</h2>
"""

    for m in Message.select():
        body += """
    <div class="message">
    {}
    </div>
    """.format(html.escape(m.content))
    # Ref: https://docs.python.org/3/library/html.html
    # Method from lecture, handles only < > characters:
    # """.format(m.content.replace('<', '&lt;').replace('>', '&gt;'))

    return body
Exemplo n.º 6
0
def home():

    if request.method == 'POST':
        m = Message(content=request.form['content'])
        m.save()

    body = """
<html>
<body>
<h1>Class Message Board</h1>
<h2>Contribute to the Knowledge of Others</h2>
<form method="POST">
    <textarea name="content"></textarea>
    <input type="submit" value="Submit">
</form>

<h2>Wisdom From Your Fellow Classmates</h2>
"""

    for m in Message.select():
        body += """
<div class="message">
{}
</div>
""".format(html.escape(m.content))

# a simple approach below to replace < or >
# m.content.replace('<', '&lt;').replace('>', '&gt;'))
# html.escape does the same replacement, but also looks for other control characters in html

    return body
Exemplo n.º 7
0
def home():

    if request.method == 'POST':
        m = Message(content=request.form['content'])
        m.save()

    body = """
<html>
<body>
<h1>Class Message Board</h1>
<h2>Contribute to the Knowledge of Others</h2>
<form method="POST">
    <textarea name="content"></textarea>
    <input type="submit" value="Submit">
</form>

<h2>Wisdom From Your Fellow Classmates</h2>
"""

    for m in Message.select():
        body += """
<div class="message">
{}
</div>
""".format(m.content.replace('<', '&lt;').replace('>', '&gt;'))

    return body
 def test_delete(self):
     self.test_new()
     e = ZVent(ztype=events.MESG_DELETE, data={"id": TestMessage.id, "user": TestMessage.user_id})
     ZVent.publish(e)
     m = Message()
     m.load(TestMessage.id)
     assert m.deleted == True
Exemplo n.º 9
0
 def choose_deck(self, type_ids=None, base_units=None):
     message = Message(type="pick", turn=self.get_current_turn(), info=None)
     if type_ids is not None:
         message.info = {"units": type_ids}
     elif base_units is not None:
         message.info = {"units": [unit.type_id for unit in base_units]}
     self.queue.put(message)
Exemplo n.º 10
0
def home():
    if 'csrf_token' not in session:
        session['csrf_token'] = str(random.randint(10000000, 99999999))

    if request.method == 'POST':
        m = Message(content=request.form['content'])
        m.save()

    body = """
<html>
<body>
<h1>Class Message Board</h1>
<h2>Contribute to the Knowledge of Others</h2>
<form method="POST">
    <textarea name="content"></textarea>
    <input type="submit" value="Submit">
</form>

<h2>Wisdom From Your Fellow Classmates</h2>
"""

    for m in Message.select():
        body += """
<div class="message">
{}
</div>
""".format(m.content)

    return body
Exemplo n.º 11
0
    def send_message(user_id, user_message, talk_to):
        user_name = ""
        # Find user's name with user_id
        for user in server_data_container.user_list:
            if user['user_id'] == user_id:
                user_name = user['user_name']
                break

        format_msg = Message(user_id, talk_to, user_message)

        # Group chat here
        if talk_to == 0:

            print('Group chat', format_msg)
            format_msg.content = "<%s>: %s" % (user_name, format_msg.content)
            for user in server_data_container.user_list:
                if user['user_id'] != user_id:
                    user['message_queue'].put(format_msg)

        # Individual talk here
        else:
            print('Individual chat', format_msg)

            for user in server_data_container.user_list:
                if user['user_id'] == talk_to and user_id != talk_to:
                    user['message_queue'].put(format_msg)
            db = MySQL()
            db.modify(
                "INSERT INTO Message (user_id, content, destination) VALUES (%s,%s,%s)",
                int(user_id), user_message, int(talk_to))
Exemplo n.º 12
0
def create_room_message(room_id):
    '''Create a Message object from the POST data'''
    # Can call curl with --data-binary and retrieve with request.data
    # API test: curl --data "data=Hi, I am Sally&user_id=3" http://localhost:5001/api/rooms/1/messages
    # print request.form

    #import pdb; pdb.set_trace()
    main_room = db.session.query(Room).get(room_id)
    data = request.form.get('data')
    data = bleach.clean(data)
    uid = int(request.form.get('user_id'))
    user = db.session.query(User).get(uid)
    # If nothing is left in the message, don't return anything
    # if data == '':
    #     return
    msg = Message(user=user, room=main_room, data=data)

    # Create the date fields using the database
    db.session.add(msg)
    db.session.commit()

    # Put the event on the bus
    print "Throwing message_created_event"
    bus.notify(Event(
        Event.Types.message_created_event, 
        msg.as_json()))


    # To access the newly created object
    # msgs = Message.query.order_by(Message.message_id).all()
    # msg = msgs[-1]

    return jsonify({"messages": main_room.messages_as_json()})
Exemplo n.º 13
0
def create_room_message(room_id):
    '''Create a Message object from the POST data'''
    # Can call curl with --data-binary and retrieve with request.data
    # API test: curl --data "data=Hi, I am Sally&user_id=3" http://localhost:5001/api/rooms/1/messages
    # print request.form

    #import pdb; pdb.set_trace()
    main_room = db.session.query(Room).get(room_id)
    data = request.form.get('data')
    data = bleach.clean(data)
    uid = int(request.form.get('user_id'))
    user = db.session.query(User).get(uid)
    # If nothing is left in the message, don't return anything
    # if data == '':
    #     return
    msg = Message(user=user, room=main_room, data=data)

    # Create the date fields using the database
    db.session.add(msg)
    db.session.commit()

    # Put the event on the bus
    print "Throwing message_created_event"
    bus.notify(Event(Event.Types.message_created_event, msg.as_json()))

    # To access the newly created object
    # msgs = Message.query.order_by(Message.message_id).all()
    # msg = msgs[-1]

    return jsonify({"messages": main_room.messages_as_json()})
 def set_up(self):
     u = Message()
     path = u.path(self.id)
     try:
         os.remove(path)
     except Exception, e:
         pass
 def set_up(self):
     u = Message()
     path = u.path(self.id)
     try:
         os.remove(path)
     except Exception, e:
         pass
Exemplo n.º 16
0
def post_message():
    # verbose check for security purpose, actually not necessary
    if session.get('isLogin') != 'true':
        return 'permission_denied'

    msg_type = request.form.get('type')
    headline = request.form.get('headline')
    content = request.form.get('content')
    drafted = bool(int(request.form.get('drafted')))

    if not (msg_type and headline and content):
        return 'invalid'

    if request.method == 'POST':
        # post a new message
        try:
            msg_id = Message.insert_message(session.get('user_id'), msg_type,
                                            headline, content, drafted)
            return str(msg_id)
        except IOError:
            return 'failed'
    elif request.method == 'PUT':
        # update a message
        msg_id = request.form.get('msg_id')
        if not msg_id:
            return 'invalid'

        try:
            return str(
                Message.update_message(msg_id, msg_type, headline, content,
                                       drafted))
        except IOError:
            return 'failed'
 def all(self):
     messagers = []
     for id, data in self._db.items():
         m = Message(data['name'], data['message'])
         m.id = str(id)
         messagers.append(m)
     messagers.sort(key=lambda x: x.id)
     return messagers
Exemplo n.º 18
0
def commentscreate():
    data = request.forms.getunicode('data')
    data = json.loads(data)
    Comment.create(data)
    openid = data['openid']
    user = User.find_by_id(openid)
    data['type'] = 'comment'
    Message.create(user, '', data)
 def test_delete(self):
     self.test_new()
     e = ZVent(ztype=events.MESG_DELETE, data={'id':TestMessage.id,
         'user':TestMessage.user_id})
     ZVent.publish(e)
     m = Message()
     m.load(TestMessage.id)
     assert m.deleted == True
 def test_update(self):
     self.test_new()
     new_text = "foo the bar out of here"
     e = ZVent(ztype=events.MESG_NEW, data={"id": TestMessage.id, "text": new_text, "user": TestMessage.user_id})
     ZVent.publish(e)
     m = Message()
     m.load(TestMessage.id)
     assert m.id == TestMessage.id, "expect id to be %s but it was %s" % (TestMessage.id, u.id)
     assert m.text == new_text
Exemplo n.º 21
0
def messages():
    if request.method == 'POST':
        sender = request.get_json()['sender']
        content = request.get_json()['content']
        m = Message(sender, content)
        m.save()
        return jsonify(m.__dict__), 201
    else:
        return jsonify([m.__dict__ for m in Message.get_all()])
Exemplo n.º 22
0
 def get(self, *args):
     content = self.request.get("content")
     content = cgi.escape(content);
     len_after_encode = len(content.encode('utf-8'))
     if len_after_encode == 0 or len_after_encode > 1400:
         self.response.out.write('')
         return
     message = Message(content = content)
     message.put()
     self.response.out.write(message.color)
Exemplo n.º 23
0
def reply_to():
    # Reply to a message.

    reply_to_user = request.form['send_message_to']
    current_user = session['user_id']
    message = request.form['message']

    Message.add_message(current_user, reply_to_user, message)

    return "Your message has been sent."
Exemplo n.º 24
0
def send_message():
    i = ctx.request.input(to="", subject="", content="")
    user_to = User.find_first("where t_emailaddr=?", i.to)
    to_id = user_to.t_uid
    from_id = ctx.request.user.t_uid
    subject = valid_secure_data(i.subject)
    content = valid_secure_data(i.content)
    msg = Message(t_to=to_id, t_from=from_id, t_title=subject, t_content=content, t_read=0, t_time=time.time())
    msg.insert()
    return dict()
Exemplo n.º 25
0
def collectpost(pid):
    openid = request.forms.getunicode('openid')
    postuserid = request.forms.getunicode('postuserid')
    user = User.find_by_id(openid)
    flag = user.collectpost(pid)
    if flag:
        data = {'type': 'collect', 'index': pid}
        Message.create(user, postuserid, data)
    else:
        user.uncollectpost(pid)
Exemplo n.º 26
0
def add_message():
    if request.method == "GET":
        return render_template("add_message.html")
    else:
        msg = Message()
        msg.sender = request.form["username"]
        msg.contents = request.form["contents"]
        db.session.add(msg)
        db.session.commit()
        return redirect("/")
    def test_new(self):

        e = ZVent(ztype=events.MESG_NEW, data={'id':TestMessage.id, 
            'text':TestMessage.text, 'user':TestMessage.user_id})
        ZVent.publish(e)
        m = Message()
        m.load(TestMessage.id)
        assert m.id == TestMessage.id, 'expect id to be %s but it was %s' % (
                TestMessage.id, u.id)
        assert m.text == TestMessage.text
    def test_new(self):

        e = ZVent(
            ztype=events.MESG_NEW, data={"id": TestMessage.id, "text": TestMessage.text, "user": TestMessage.user_id}
        )
        ZVent.publish(e)
        m = Message()
        m.load(TestMessage.id)
        assert m.id == TestMessage.id, "expect id to be %s but it was %s" % (TestMessage.id, u.id)
        assert m.text == TestMessage.text
Exemplo n.º 29
0
    def post(self):
        conn = sqlite3.connect('lbg.db')
        cursor = conn.cursor()

        messages = cursor.execute('select * from messages')

        res = []
        for message in messages:
            m = Message(message)
            res.append(m.getFullInfo())
        return jsonify(res)
 def test_update(self):
     self.test_new()
     new_text = 'foo the bar out of here'
     e = ZVent(ztype=events.MESG_NEW, data={'id':TestMessage.id, 
         'text':new_text, 'user':TestMessage.user_id})
     ZVent.publish(e)
     m = Message()
     m.load(TestMessage.id)
     assert m.id == TestMessage.id, 'expect id to be %s but it was %s' % (
             TestMessage.id, u.id)
     assert m.text == new_text
Exemplo n.º 31
0
 def choose_hand(self, base_units: List[BaseUnit]) -> None:
     message = Message(type="pick", turn=self.get_current_turn(), info=None)
     if base_units is not None:
         for base_unit in base_units:
             if type(base_unit) is not BaseUnit:
                 Logs.show_log("base_units is not an array of BaseUnits")
                 return
         message.info = {"units": [unit.type_id for unit in base_units]}
         self._queue.put(message)
     else:
         Logs.show_log("choose_hand function called with None base_units")
Exemplo n.º 32
0
def commentvote():
    postuserid = request.forms.getunicode('postuserid')
    openid = request.forms.getunicode('openid')
    user = User.find_by_id(openid)
    cid = request.forms.getunicode('cid')
    comment = Comment(cid)
    flag = comment.incrThumbs(openid)
    if flag:
        data = {'type': 'tcomment', 'index': cid}
        Message.create(user, postuserid, data)
    else:
        comment.decrThumbs(openid)
Exemplo n.º 33
0
def index():
    msg_count = []
    msgs = []
    for i in range(len(type_map)):
        msg_count.append(Message.count_msg_of_type(i))
        msgs.append(Message.find_top(i, 3))
    users = User.find_new(4)
    return render_template('index.html',
                           msgs=msgs,
                           users=users,
                           type_map=type_map,
                           msg_count=msg_count)
Exemplo n.º 34
0
    def choose_hand_by_id(self, type_ids):
        message = Message(type="pick", turn=self.get_current_turn(), info=None)
        if type_ids is not None:
            for type_id in type_ids:
                if type(type_id) is not int:
                    Logs.show_log("type_ids are not int")
                    return

            message.info = {"units": type_ids}
            self.queue.put(message)
        else:
            Logs.show_log("choose_hand_by_id function called with None type_eds")
Exemplo n.º 35
0
def postvote():
    openid = request.forms.getunicode('openid')
    postuserid = request.forms.getunicode('postuserid')
    postid = request.forms.getunicode('postid')
    user = User.find_by_id(openid)
    postuser = User.find_by_id(postuserid)
    post = Post.find_by_id(postid)
    flag = post.incrThumbs(user, postuser)
    if flag:
        data = {'type': 'tpost', 'index': postid}
        Message.create(user, postuserid, data)
    else:
        post.decrThumbs(user, postuser)
Exemplo n.º 36
0
def main():
    logging.basicConfig(
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        level=logging.INFO,
        filename=configure.get('logging', None))

    db.connect()
    if not Message.table_exists():
        Message.create_table()

    updater = Updater(configure['token'])
    updater.dispatcher.add_handler(MessageHandler(Filters.all, new_message))
    updater.start_polling()
Exemplo n.º 37
0
    def post(self):
        user = users.get_current_user()

        if not user:
            return self.write("You are not logged in!")

        author = self.request.get("name")
        email = user.email()
        sendto = self.request.get("to-mail")
        subject = self.request.get("to-subject")
        message = self.request.get("message")

        if not author:
            author = "Anonymous"

        if not sendto:
            sendto = "Write your email"

        if not subject:
            subject = "none"

        if "<script>" in message:
            return self.write("insert non JS")

        msg_object = Message(message=message.replace("<script>", ""))
        msg_object.author_name = author
        msg_object.email = email
        msg_object.sendto = sendto
        msg_object.subject = subject
        msg_object.put()

        return self.redirect_to("message-site")
Exemplo n.º 38
0
 def post(self):
     user = users.get_current_user()
     if user:
         to_email = self.request.get("to")
         subject = self.request.get("subject")
         content = self.request.get("content")
         message = Message(from_email=user.email(),
                           to_email=to_email,
                           subject=subject,
                           content=content)
         message.put()
         return self.redirect_to("home")
     else:
         return self.render_template(
             "login.html", params={"loginUrl": users.create_login_url("/")})
Exemplo n.º 39
0
def api_mark_read(msg_id):
    msg = Message.get(msg_id)
    if msg is None:
        raise NotFound()
    msg.t_read = 1
    msg.update()
    return dict()
Exemplo n.º 40
0
 def get(self):
     self.response.out.write('UID\tTID\tMId\tRTime\tERTime\twC\tunsub\tedu\tsent\tis_html\n')
     for thread in Thread.gql('WHERE is_processed = TRUE AND is_queued = FALSE AND estimated_reading_time > 0'):
         message = Message.get_by_key_name(thread.last_message_id, None)
         if message != None:
             self.response.out.write('%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n' % (message.user_email, 
                 message.thread_id, message.message_id, thread.reading_time, thread.estimated_reading_time, 
                 message.word_count, message.has_unsubscribe, 'edu' in message.addresses, message.is_sent, message.is_html))
Exemplo n.º 41
0
def create_message():
    
    if request.method == 'POST':

        params = request.json
        match_id = params['match_id']
        user_id = params['user_id']
        text = params['text']
        new_message = Message(match_id=match_id,user_id=user_id,text=text)
        g.db.add(new_message)
        g.db.flush()
        g.socketio.emit('message',new_message.serialize(),namespace='/chat'+str(match_id))
        return resource_created(new_message)

    else:
        
        return invalid_method()
Exemplo n.º 42
0
def receive_message():
    user = ctx.request.user
    if not user:
        return dict()
    uid = user.t_uid
    total = Message.count_by("where t_to=?", uid)
    page = Page(total, page_index=_get_page_index(), page_size=5)
    msg_list = Message.find_by("where t_to=? order by t_time desc limit ?,?", uid, page.offset, page.limit)
    messages = list()
    for m in msg_list:
        u = User.find_first("where t_uid=?", m.t_from)
        m.t_from = u.t_username
        m["t_fromid"] = u.t_uid
        m["t_fromemail"] = u.t_emailaddr
        messages.append(m)

    return dict(messages=messages, page=page)
Exemplo n.º 43
0
def message():
    if conf.MESSAGES_BLOCKED:
        abort(410)
    msg = Message.from_request(request)
    try:
        msg.save()
    except ValidationError, e:
        return dumps({'status': 'error', 'message': e.message})
Exemplo n.º 44
0
  def rcv_message(self, message):
    #we connect, then they send us a message with all old messages as payload
    if message.is_new_connect():
      missed = pickle.loads(message.content)
      for missed_message in missed:
        if not (missed_message.timestamp, missed_message) in self.msg_stack:
          self.msg_stack.append((missed_message.timestamp, missed_message))
          self.acked_messages[missed_message.get_hash()] = message
      self.say(Message.ack_for(message))
      self.data_changed_ptr()
  
    elif not (message.timestamp, message) in self.msg_stack:
      self.msg_stack.append((message.timestamp, message))
      self.new_content_message(message)

    self.acked_messages[message.get_hash()] = message
    self.say(Message.ack_for(message))
    self.data_changed_ptr()
Exemplo n.º 45
0
 def buffer_new_data(self, data):
   self.buffer += data
   while self.buffer:
     new_msg, leftover = Message.deserialize(self.buffer)
     if new_msg:
       self.rcv_message(new_msg)
       self.buffer = leftover
     else:
       #we have part of a message
       break
Exemplo n.º 46
0
    def get(self, id):
        message = MessageModel.find(id=id)

        model = {
            'message': {
                'id': message.id,
                'type': message.type,
                'from_user': message.from_user_id,
                'to_user': message.to_user_id
            }
        }
        return jsonify(model)
Exemplo n.º 47
0
 def get(self):
     
     message_query = Message.query(
         ancestor=messages_key('Messages')).order(-Message.date)
     messages = message_query.fetch(5) 
    
     template_values = {
         'name': CURRENT_USER_NAME,
         'messages': messages
     }
     
     template = JINJA_ENVIRONMENT.get_template('html/news.html')                   
     self.response.write(template.render(template_values))
Exemplo n.º 48
0
 def _persist_message(self):
     if session.query(Message).filter(Message.id == self.messageid).count() != 0:
         print 'Message with ID = (' + self.messageid + ') has already been persisted.  Skipping.'
         raise Exception()
     m = Message()
     m.id = self.messageid
     m.inreplyto = self.inreplyto
     m.fromcontact_email = self.msgfrom['email']
     m.size = self.size
     m.subject = self.subject
     m.msgdate = self.date
     session.add(m)
     session.commit()
Exemplo n.º 49
0
 def _store_message(self, mail_message):
     receiver_name, receiver_address = parseaddr(mail_message.to)
     account = Account.get_by_email(receiver_address)
     if not account or account.valid_until < datetime.now():
         return
     sender_name, sender_address = parseaddr(mail_message.sender)
     body = mail_message.body.decode() if hasattr(mail_message, 'body') else None
     html = clean_html(mail_message.html.decode()) if hasattr(mail_message, 'html') else None
     db_message = Message(
         parent=account.key,
         sender_name=sender_name,
         sender_address=sender_address,
         receiver_name=receiver_name,
         receiver_address=receiver_address,
         reply_to=getattr(mail_message, 'reply_to', None),
         cc=getattr(mail_message, 'cc', None),
         bcc=getattr(mail_message, 'bcc', None),
         subject=getattr(mail_message, 'subject', None),
         date=datetime.now(),
         body=body,
         html=html
     )
     db_message.put()
     self._store_attachments(mail_message, db_message)
Exemplo n.º 50
0
 def post(self, *args, **kwargs):
     """ 删除消息
     """
     mid = kwargs.get('mid')
     message = Message.get(id=mid, status__no=Message._status_del)
     if message.user_id != self.user.id or not message:
         self.write({
             'status': 0
         })
         return
     message.status = Message._status_del
     message.save()
     self.write({
         'status': 1,
         'msg_id': message.id,
     })
Exemplo n.º 51
0
    def get(self):
        messagelist = []

        for message in MessageModel.list():

            model = {
                'id': message.id,
                'to_user': message.to_user,
                'from_user': message.from_user,
                'type': message.type,
                'read': message.read
            }

            messagelist.append(model)

        return jsonify({'assets': messagelist})
Exemplo n.º 52
0
    def get(self):
        entityType = self.request.get('entityType')
        email = self.request.get('email')
        if entityType == 'User':
            logging.info('Remove User %s' % email);
            user = User.get_by_key_name(key_names = email)
            if not user:
                logging.error('no such User %s' % email);
                return 
            user.delete()
            for m in Message.gql('WHERE user_email=:1',email):
                m.delete()

            for t in Thread.gql('WHERE user_email=:1',thread):
                t.delete()
            StorageByKeyName(CredentialsModel, user_email, 'credentials').locked_delete()
            logging.info('Remove User successfully')
Exemplo n.º 53
0
def processReadingThread(user_ctx_id, user_email, thread_ids):
    ctxio = ContextIOConn.getInstance().ctxio
    account = contextIO2.Account(ctxio, {'id' : user_ctx_id})
    index = 0
    try:
        for index, thread_id in enumerate(thread_ids):
            thread = Thread.get_or_insert(key_name = thread_id, thread_id = thread_id, user_email = user_email)
            if thread.is_processed:
                continue
            if not thread.is_queued:
                continue

            thread_data = account.get_message_thread('gm-' + thread_id, include_body=True)
            thread.last_date = 0
            thread.estimated_reading_time = 0
            if not thread_data or not 'messages' in thread_data:
                thread.is_queued = False
                thread.estimated_reading_time = 1000
                thread.put()    
                return
            for message_data in thread_data['messages']:
                message_id = message_data['gmail_message_id']
                message = Message.get_or_insert(key_name = message_id, message_id = message_id, thread_id=thread_id, user_email=user_email)
                if not message.is_processed:
                    message.is_html, plain_text = getPlainText(message_data['body'])
                    message.word_count = getWordCount(stripPlainTextSignature(plain_text))
                    message.addresses = json.dumps(message_data['addresses'])
                    message.date = message_data['date']
                    message.is_sent = '\\Sent' in message_data['folders']
                    message.has_unsubscribe = 'unsubscribe' in plain_text.lower()
                    message.is_processed = True
                    message.put()            
                if message.date > thread.last_date:
                    thread.last_date = message.date
                    thread.last_message_id = message_id
                    thread.estimated_reading_time += estimateReadingTime(message)
            thread.is_queued = False
            thread.is_processed = True
            thread.put()
    except DeadlineExceededError:
        deferred.defer(processReadingThread, user_ctx_id, user_email, thread_ids[index:], _queue='reading-queue', _target='crawler')
    except Exception as e:
        logging.error('thread_id: %s user_email:%s %s' % (thread_id, user_email, traceback.print_exc()))
        raise e
Exemplo n.º 54
0
 def post(self):
     new_message = Message(parent=messages_key('Messages'))
     new_message.populate(author = self.request.get('author'),
                          email = self.request.get('email'),
                          title = self.request.get('title'),
                          message = self.request.get('message'),
                          )
     new_message.put()
     
     message_query = Message.query(
         ancestor=messages_key('Messages')).order(-Message.date)
     messages = message_query.fetch(5) 
     
     template_values = {
         'name': CURRENT_USER_NAME,
         'messages': messages
     }
     
     template = JINJA_ENVIRONMENT.get_template('html/news.html')                   
     self.response.write(template.render(template_values))
Exemplo n.º 55
0
def do_sync(name):

    if not name in config.stores:
        print('Invalid store')
    else:
        print('Syncing %s' % name)

        store = config.stores[name]
        uri = store['uri']

        if not os.path.exists(uri):
            print('Invalid uri: %s' % uri)
            return

        # TODO: merge service interfaces
        utils = IndexUtils()

        # TODO: move that to a maildir utils interface
        mails = maildir.get_mails(uri)
        for (uid, mail) in mails.items():
            if not utils.message.exists(uid):
                filename = mails.get_file(uid)._file.name
                (display, addr) = maildir.get_sender(mail)
                subject = maildir.get_subject(mail)

                msg = Message() 
                msg.uid = uid
                msg.sender = utils.recipient.lookup_recipient(display, addr, True)
                msg.subject = subject
                msg.path = Path(filename)
                msg.thread = Thread()

                utils.message.add(msg)
            else:
                # assumes emails are immutable
                pass

        utils.close()
Exemplo n.º 56
0
 def buffer_new_data(self):
   while self.buf:
     new_msg, leftover = Message.deserialize(self.buf)
     if new_msg:
       self.server.incoming_message(new_msg, self.wfile)
       self.buf = leftover
Exemplo n.º 57
0
class retry_tester(object):
    def __init__(self):
        self.valid = 4

    @retry_with_backoff("success", 2, 1, 2)
    def some_method(self, arg1, arg2):
        print arg1, arg2, self.valid

    def success(self, arg1, arg2):
        print "trying to validate", arg1, arg2
        self.valid -= 1
        return self.valid == 0


m = Message("rcoh", "hey", 0)
ms = list(m.serialize())
md, leftover = Message.deserialize(ms)
assert m == md

m2 = Message("rcoh", "hey2", 0)

total = ms + list(m2.serialize())
m1d, left = Message.deserialize(total)
m2d, more = Message.deserialize(left)
assert m == m1d
assert m2d == m2

m = Message("blah", "blah", 5)
print m.get_hash()
Exemplo n.º 58
0
def response():
    action = cgi_get("action", choices=["join", "activate", "login", "contact", "edit", "email"])
    if action == "join":
        email = cgi_get("email")
        if CTUser.query(CTUser.email == email).get():
            fail("this email is already in use")
        user_type = cgi_get("utype")
        u = db.get_model(user_type)(email=email,
            firstName=cgi_get("firstName"), lastName=cgi_get("lastName"),
            **cgi_get("extras"))
        u.put() # to generate created timestamp
        u.password = db.hashpass(cgi_get("password"), u.created)
        rule = config.ctuser.activation.get(user_type, config.ctuser.activation.ctuser)
        if rule == "auto":
            u.active = True
        else: # assumes config.mailer (otherwise, don't change activation "auto" default)
            usk = u.key.urlsafe()
            if rule == "confirm":
                send_mail(to=u.email, subject="activation required",
                    body=JOIN%(usk,))
            else: # email admin to handle it
                send_mail(to=rule, subject="activation required", body=JOINED%(email, usk))
        u.put()
        succeed(u.data())
    elif action == "activate":
        u = db.get(cgi_get("key"))
        if u and not u.active: # else, don't even trip
            u.active = True
            u.put()
        send_mail(to=u.email, subject="account activated",
            body=ACTIVATE)
        redirect("/", "you did it!")
    elif action == "login":
        u = CTUser.query(CTUser.email == cgi_get("email"),
            CTUser.active == True).get()
        if not u or u.password != db.hashpass(cgi_get("password"), u.created):
            fail()
        succeed(u.data())
    elif action == "contact":
        sender = db.get(cgi_get("user"))
        message = cgi_get("message")
        convokey = cgi_get("conversation", required=False)
        if convokey:
            conversation = db.get(convokey)
        else:
            conversation = Conversation()
            conversation.topic = cgi_get("topic")
            conversation.participants = [sender.key, db.KeyWrapper(cgi_get("recipient"))]
            conversation.put()
        m = Message(sender=sender.key, conversation=conversation.key, body=message)
        m.put()
        for recipient in conversation.participants:
            if recipient != sender.key:
                send_mail(to=recipient.get().email,
                    subject="message from %s"%(sender.firstName,),
                    body=CONTACT%(sender.fullName(), message,
                        sender.firstName, sender.key.urlsafe(), conversation.key.urlsafe()))
        succeed(convokey and m.key.urlsafe() or conversation.key.urlsafe())
    elif action == "edit":
        changes = cgi_get("changes")
        changes["key"] = cgi_get("user")
        edit(changes)
    elif action == "email":
        sender = db.get(cgi_get("user"))
        if not sender.admin:
            fail()
        recips = cgi_get("recipients", default=[])
        if not recips:
            if config.wpmail:
                log("no recipients specified -- WP mode enabled -- building recipient list...")
                recips = getWPmails()
            else:
                fail("no recipients specified -- can't email nobody")
        batch(recips, lambda chunk : send_mail(bcc=chunk,
            subject=cgi_get("subject"), body=cgi_get("body")), chunk=100)
Exemplo n.º 59
0
    def receive(self, mail_message):
        logging.info("Received a message from: " + mail_message.sender)
        decoded_html = ""

        confirmation_subject_regex = re.compile('.*Gmail Forwarding Confirmation \- Receive Mail from (.*)')

        sender_match = confirmation_subject_regex.match(mail_message.subject)
        if sender_match:
            for content_type, body in mail_message.bodies('text/plain'):
                decoded_text = body.decode()
                logging.info(decoded_text)
                
                reply_email = sender_match.group(1)
                link_match = re.search('(^https://.*?mail.google.com/mail/.*$)', decoded_text, re.MULTILINE)
                if link_match:
                    link = link_match.group(1)
                    sender_address = "Sydney Resistance Watch <*****@*****.**>"
                    subject = "Gmail Forwarding Confirmation - to [email protected]"
                    body = """
                        Please confirm your forwarding setting by
                        clicking on the link below:

                        %s
                        """ % link

                    mail.send_mail(sender_address, reply_email, subject, body)

        try:
            owner_regex = re.compile('([a-zA-Z0-9]+?),<br/>')
            link_regex = re.compile("Your Link has been destroyed by (.*?) at (.*?) hrs\..*?<a href\=\"http:\/\/www\.ingress\.com\/intel\?latE6=(.*?)&.*?lngE6=(.*?)&.*?\">View start location\<\/a\>.*?<a href=\"http:\/\/www\.ingress\.com\/intel\?latE6=(.*?)&.*?lngE6=(.*?)&.*?\">View end location<\/a>")
            item_regex = re.compile("(\d+?) ([a-zA-Z0-9()]+?) were destroyed by (.*?) at (.*?) hrs\..*?<a href\=\"http:\/\/www\.ingress\.com\/intel\?latE6=(.*?)&.*?lngE6=(.*?)&.*?\">View location\<\/a\>")
            for content_type, body in mail_message.bodies('text/html'):
                decoded_html = body.decode()

                owner_match = owner_regex.match(decoded_html)
                owner = "Unknown"
                if owner_match:
                    owner = owner_match.group(1)
                else:
                    logging.debug(decoded_html)
                
                match = link_regex.findall(decoded_html)
                for group in match:
                    attacker = group[0]
                    attack_time = datetime.time(hour=int(group[1].split(':')[0]), minute=int(group[1].split(':')[1]))
                    start_latitude = float(group[2])/1000000
                    start_longitude = float(group[3])/1000000
                    end_latitude = float(group[4])/1000000
                    end_longitude = float(group[5])/1000000
                    link_start_location = db.GeoPt(lat=start_latitude, lon=start_longitude)
                    link_end_location = db.GeoPt(lat=end_latitude, lon=end_longitude)
                    activity = LinkDestroyActivity(link_creator=owner, attacker=attacker, attack_time=attack_time, link_start_location=link_start_location, link_end_location=link_end_location)
                    activity.put()

                match = item_regex.findall(decoded_html)
                for group in match:
                    item_amount = int(group[0])
                    item_type = group[1]
                    attacker = group[2]
                    attack_time = datetime.time(hour=int(group[3].split(':')[0]), minute=int(group[3].split(':')[1]))
                    latitude = float(group[4])/1000000
                    longitude = float(group[5])/1000000
                    item_location = db.GeoPt(lat=latitude, lon=longitude)
                    activity = ItemDestroyActivity(item_owner=owner, item_amount=item_amount, item_type=item_type, attacker=attacker, attack_time=attack_time, item_location=item_location)
                    activity.put()
                    taskqueue.add(url='/notifications/check', params={ 'lat' : str(latitude),
                                                                        'lon' : str(longitude),
                                                                        'attacker' : attacker})
        except Exception, e:
            logging.debug(mail_message.original)
            logging.debug(decoded_html)
            message = Message(mail=pickle.dumps(mail_message), analyzed=False)
            message.put()
            raise e