Example #1
0
 def on_message(self, message):
     super(self.__class__, self).on_message(message)
     if 'user' not in self._message_json.keys():
         self._message_json['user'] = 'Unknown'
         message = json.dumps(self._message_json)
     message_id = yield Message.insert(self._message_json)
     self.send_message(message)
Example #2
0
def upload(request):
	if request.method == 'POST':
		
		try:
			i = File(filename = request.FILES['file'].name,
					nickname = request.session['nickname'])
			
			i.save() #save it to get id
			
			destination = open(settings.FILES_PATH + '/' + str(i.id), 'wb+')
			for chunk in request.FILES['file'].chunks():
				destination.write(chunk)
			
			destination.close()
			
			#send link to the file to appropriate chatroom
			c=Chatroom.objects.get(id=request.GET['id'],is_private=False)
			
			m=Message(chatroom=c,
					  nickname='system',
					  message=request.session['nickname'] + ' uploaded file: '+settings.DOMAIN+'/download/'+str(i.id))
			m.save()
		except:
			try:
				if len(code) > 0:
					os.remove(settings.FILES_PATH + '/' + i.id)
			except:
				#there might be a problem with removing folder/file
				None
			
			#try removing entry from DB
			try:
				i.delete()
			except:
				#entry not found, apparently we didn't create one, therefore we don't need to remove folder/file
				None
		
		return HttpResponse('File successfully uploaded!')
	else:
		if 'id' in request.GET:
			return render_to_response('upload.html', {'id': request.GET['id']}, context_instance=RequestContext(request))
    def dataReceived(self, data):
        print "Received data: " + str(data)

        topic_window_split = str(data).split(":-:")
        if len(topic_window_split) > 1:
            dashboardname = topic_window_split[0]
            topicname = topic_window_split[1]
            action = topic_window_split[2]
            if action == "add":
                add_topic_window(topicname, dashboardname)
            elif action == "delete":
                deactivate_topic_window(topicname, dashboardname)
            for c in self.factory.clients:
                messageFormatted = "<{}> {}".format(self.transport.getHost(), dashboardname
                                            + ":-:" + topicname + ":-:" + action)
                c.sendLine(messageFormatted)
            return

        topic_window_split = str(data).split(":--:")
        if len(topic_window_split) > 1:
            print "Splitting dis/connect message: " + str(topic_window_split)
            dashboardname = topic_window_split[0]
            username = topic_window_split[1]
            action = topic_window_split[2]
            for c in self.factory.clients:
                messageFormatted = "<{}> {}".format(self.transport.getHost(), dashboardname
                                            + ":--:" + username + ":--:" + action)
                c.sendLine(messageFormatted)
            return

        # construct a model object message
        splitinfo = str(data).split("::",3)
        username = splitinfo[2]
        messagetext = splitinfo[3]
        topicname = splitinfo[1]
        dashboardname = splitinfo[0]
        message = Message(msgtext=messagetext,
                          timestamp=datetime.datetime.now(),
                          username=username,
                          dashboardtitle=dashboardname,
                          topic=topicname)
        message.save()


        #to_save_topic = True
        #for topic_window in Topic.objects:
        #    if topic_window.topic_title == topicname:
        #        to_save_topic = False

        #if to_save_topic:
        #    topic = Topic(topic_title = topicname,
        #                  topic_active = True,
        #                  dashboard_title = dashboardname)
        #    topic.save()

        for c in self.factory.clients:
            messageFormatted = "<{}> {}".format(self.transport.getHost(), dashboardname
                                        + "::" + topicname + "::" + username +
                                        "::" + messagetext)
            print "Message formatted is:" + messageFormatted
            c.sendLine(messageFormatted)
Example #4
0
 def get(self):
     # db = self.application.db
     messages = yield Message.find_raw()
     json_messages = bson_dumps(messages)
     self.render('chatapp/chat_angular.html', messages=json_messages)
Example #5
0
 def get(self):
     # db = self.application.db
     messages = yield Message.find_raw()
     self.render('chatapp/chat.html', messages=messages)
Example #6
0
def message(request):
	if not 'nickname' in request.session:
		return HttpResponseRedirect('/nickname')
		
	chatroom = request.POST['chatroom']
	msg = request.POST['msg']
	
	r = {}
	
	if msg[0:7] == '/join #':
		#join chatroom
	
		name = msg[6:]
		
		try:
			room = Chatroom.objects.get(name=name)
			
			if room.is_private == True:
				return HttpResponse(simplejson.dumps({'message':'error'}))
			
			nc = NicknameChatroom(nickname = request.session['nickname'], chatroom = room, ping = datetime.datetime.now())
			nc.save()
		except:
			room = Chatroom(name=name)
			room.save()
			
			nc = NicknameChatroom(nickname = request.session['nickname'], chatroom = room, ping = datetime.datetime.now())
			nc.save()
		
		
		
	elif msg[0:6] == '/leave':
		#leave
		
		if chatroom != '0':
			try:
				room = Chatroom.objects.get(id=chatroom)
				
				nc = NicknameChatroom.objects.filter(chatroom=room,nickname=request.session['nickname'])
				
				nc.delete()
			except:
				r['message'] = 'Error'
			
		else:
			r['message'] = 'You are not in a chatroom!'
		
	elif msg[0:4] == "/msg":
		#msg
		
		nick = msg[5:]
		
		name = request.session['nickname']+'_' + nick
		
		
		room = Chatroom(name=name,is_private=True)
		room.save()
		
		nc = NicknameChatroom(nickname = request.session['nickname'], chatroom = room, ping = datetime.datetime.now())
		nc.save()
		
		nc = NicknameChatroom(nickname = nick, chatroom = room, ping = datetime.datetime.now())
		nc.save()
	
	elif msg[0:5] == '/nick':
		#change nick
		if change_nick(request, msg[6:]):
			r['message'] = 'Your nickname was successfully changed.'
		else:
			r['message'] = 'Selected nickname is too short or already in use'
	
	else:
		#it's a message
		if chatroom != '0':
			try:
				room = Chatroom.objects.get(id=chatroom)
				
				try:
					nc = NicknameChatroom.objects.get(chatroom=room,nickname=request.session['nickname'])
					
					#post message
					m = Message(chatroom=room,nickname=request.session['nickname'], message=msg)
					m.save()
				except:
					r['message'] = 'Wrong chatroom id!'
			except:
				r['message'] = 'Message couldn\'t be send!'
			
		else:
			r['message'] = 'You are not in a chatroom!'
	
	return HttpResponse(simplejson.dumps(r))