def get(self, projectid, ip, page, do): def getname(id): return urlde(db.ct("project", "name", "id=" + id)['name']) if ip and projectid: num = db.c('host', "hostip='" + ip + "' and projectid='" + projectid + "'") if num: page = str(page and (int(page) - 1 > 0 and (int(page) - 1 < num and int(page) - 1 or 0) or 0) or 0) host = db.ct( "host", "*", "hostip = '" + ip + "' and projectid='" + projectid + "' order by id desc limit " + page + ",1") if do == 'del': db.d("host", "id=" + str(host['id'])) if num == 1: hostlist = json.loads( db.ct("project", "hosts", "id=" + projectid)['hosts']) hostlist.remove(ip) db.u("project", "hosts='" + json.dumps(hostlist) + "'", "id=" + projectid) self.redirect("http://" + URL + "/project/" + projectid) self.redirect("http://" + URL + "/host/" + projectid + "/" + ip) else: self.render( "host.html", heads=[ { 'name': getname(projectid), 'title': 'Go to ' + getname(projectid), 'url': 'project/' + projectid }, { 'name': ip, 'title': '', 'url': '' }, ], username=self.get_secure_cookie("username"), datainfo=db.datainfo(), systeminfo=systeminfo(), urlde=urlde, timede=timede, url=URL, urljson=urljson, host=host, num=range(num), page=int(page) + 1, ) else: self.render('404.html') else: self.render('404.html')
def list(self, user_id: str, agent=0): agent = int(agent) if agent: result = list( r.table('conversation').order_by(r.desc('stampdate')).run( db.c())) else: result = list( r.table('conversation').filter({ 'user_id': user_id }).order_by(r.desc('stampdate')).run(db.c())) for row in result: row['date'] = arrow.get(row.get('stampdate')).datetime summary = self.summary(user_id=user_id, conversation_id=row['conversation_id']) row['unread'] = summary['unread'] row['updated'] = summary['updated'] return result
def change(self, conversation_id: str, user_id: str): results = list( r.table('message').filter({ 'conversation_id': conversation_id }).order_by('stampdate').run(db.c())) if results: r.table('lastseen').filter({ 'user_id': user_id, "conversation_id": conversation_id }).delete().run(db.c()) r.table('lastseen').insert({ 'user_id': user_id, 'conversation_id': conversation_id, 'message_id': results[-1]['message_id'] }).run(db.c()) wamp.publish('conversationsummary.%s' % (conversation_id), {'conversation_id': conversation_id}) return results
def new(self, user_id, conversation_id, newmessage): try: previousmessage = r.table('message').filter({ 'conversation_id': conversation_id }).max('stampdate').run(db.c()) except r.errors.ReqlNonExistenceError: previousmessage = None conversation = r.table('conversation').filter({ 'conversation_id': conversation_id }).run(db.c()) result = r.table('message').insert([{ 'user_id': user_id, 'conversation_id': conversation_id, 'message': newmessage, 'stampdate': arrow.utcnow().datetime }]).run(db.c()) wamp.publish('conversation.%s' % (conversation_id), {'conversation_id': conversation_id}) wamp.publish('conversationsummary.%s' % (conversation_id), {'conversation_id': conversation_id}) if arrow.utcnow().datetime - arrow.get( previousmessage['stampdate']).datetime > datetime.timedelta( minutes=15): try: doyatelegram.send( 'lemongroup', "New Message to old Conversation: %s. Go to https://masterpenny.com/lemonchat" % (conversation['subject'])) except telegram.error.TimedOut: pass return result['generated_keys'][0] # message_id
def countunread(self, user_id: str, conversation_id: str): # Now determine how many unread messages there are., lastseen_message_ids = list( r.table('lastseen').filter({ 'conversation_id': conversation_id, 'user_id': user_id }).run(db.c())) if lastseen_message_ids: lastseen_message_id = lastseen_message_ids[-1]['message_id'] allmessages = r.table('message').filter({ 'conversation_id': conversation_id }).order_by('stampdate').run(db.c()) message_ids = [x['message_id'] for x in allmessages] try: pos = message_ids.index(lastseen_message_id) except ValueError: pos = -1 unread = len(message_ids) - pos - 1 else: unread = 0 return unread
def DELETE(message_id): """Creates a new conversation""" input = cherrypy.request.json inputParams = {} for key, value in input.items(): inputParams[key] = str(value) result = r.table('message').delete([{ 'conversation_id': inputParams.get('conversation_id') }]).run(db.c()) # wamp.publish('conversation.%s' % (conversation_id), {'conversation_id': conversation_id}) return result['generated_keys'][0] # message_id
def summary(self, user_id: str, conversation_id: str): """Returns only the last updated datetime and the number of messages unread""" try: updated = r.table('message').filter({ 'conversation_id': conversation_id }).has_fields('stampdate').max('stampdate').run( db.c()).get('stampdate') except Exception as e: updated = None return { 'updated': updated, 'unread': self.countunread(user_id, conversation_id) }
def get(self, projectid, ip, page, do): def getname(id): return urlde(db.ct("project", "name", "id=" + id)["name"]) if ip and projectid: num = db.c("host", "hostip='" + ip + "' and projectid='" + projectid + "'") if num: page = str(page and (int(page) - 1 > 0 and (int(page) - 1 < num and int(page) - 1 or 0) or 0) or 0) host = db.ct( "host", "*", "hostip = '" + ip + "' and projectid='" + projectid + "' order by id desc limit " + page + ",1", ) if do == "del": db.d("host", "id=" + str(host["id"])) if num == 1: hostlist = json.loads(db.ct("project", "hosts", "id=" + projectid)["hosts"]) hostlist.remove(ip) db.u("project", "hosts='" + json.dumps(hostlist) + "'", "id=" + projectid) self.redirect("http://" + URL + "/project/" + projectid) self.redirect("http://" + URL + "/host/" + projectid + "/" + ip) else: self.render( "host.html", heads=[ { "name": getname(projectid), "title": "Go to " + getname(projectid), "url": "project/" + projectid, }, {"name": ip, "title": "", "url": ""}, ], username=self.get_secure_cookie("username"), datainfo=db.datainfo(), urlde=urlde, timede=timede, url=URL, urljson=urljson, host=host, num=range(num), page=int(page) + 1, ) else: self.render("404.html") else: self.render("404.html")
def new(self, user_id: str, subject: str): result = r.table('conversation').insert([{ 'user_id': user_id, 'subject': subject, 'stampdate': arrow.utcnow().datetime }]).run(db.c()) wamp.publish( 'conversations' ) # @TODO a user should only listen to conversation that involve them doyatelegram.send( 'lemongroup', "New Conversation: %s. Go to https://masterpenny.com/lemonchat" % (subject, )) return {"conversation_id": result['generated_keys'][0]}
def get(self, projectid, ip, page, do): def getname(id): return urlde(db.ct("project","name","id="+id)['name']) if ip and projectid: num = db.c('host', "hostip='"+ip+"' and projectid='"+projectid+"'") if num: page = str(page and (int(page)-1>0 and (int(page)-1<num and int(page)-1 or 0) or 0) or 0) host = db.ct( "host", "*", "hostip = '"+ip+"' and projectid='"+projectid+"' order by id desc limit "+page+",1") if do == 'del': db.d("host", "id="+str(host['id'])) if num == 1: hostlist = json.loads(db.ct("project", "hosts", "id="+projectid)['hosts']) hostlist.remove(ip) db.u("project", "hosts='"+json.dumps(hostlist)+"'", "id="+projectid) self.redirect("http://"+URL+"/project/"+projectid) self.redirect("http://"+URL+"/host/"+projectid+"/"+ip) else: self.render( "host.html", heads=[ {'name':getname(projectid), 'title': 'Go to ' + getname(projectid), 'url': 'project/'+projectid}, {'name': ip, 'title': '', 'url': ''}, ], username=self.get_secure_cookie("username"), datainfo=db.datainfo(), systeminfo=systeminfo(), urlde=urlde, timede=timede, url=URL, urljson=urljson, host=host, num=range(num), page=int(page)+1, ) else: self.render('404.html') else: self.render('404.html')
def POST(self): """Creates a new conversation""" input = cherrypy.request.json inputParams = {} for key, value in input.items(): inputParams[key] = str(value) result = r.table('message').insert([{ 'user_id': inputParams.get('user_id'), 'conversation_id': inputParams.get('conversation_id'), 'message': inputParams.get('message'), 'stampdate': arrow.utcnow().datetime }]).run(db.c()) # wamp.publish('conversation.%s' % (conversation_id), {'conversation_id': conversation_id}) return result['generated_keys'][0] # message_id
def gethostn(hostip): return db.c("host", "hostip='"+hostip+"'")
def get(self, id, do): def gethostn(hostip): return db.c("host", "hostip='" + hostip + "'") def getmcustom(code): code = urlde(code) s = re.findall("({set\..*})", code) return s if id: row = db.ct("project", "*", "id=" + id) if row: if do == "del": db.d("project", "id=" + id) self.redirect("http://" + URL + "/project") elif do == "edit": modules = db.cts("module", "*", "1=1") pmodules = json.loads(urlde(db.ct("project", "module", "id=" + id)["module"])) self.render( "project_edit.html", heads=[ {"name": "Project", "title": "Project list", "url": "project"}, {"name": urlde(row["name"]), "title": urlde(row["name"]) + "view", "url": "project/" + id}, ], username=self.get_secure_cookie("username"), datainfo=db.datainfo(), systeminfo=systeminfo(), urlde=urlde, getmcustom=getmcustom, row=row, url=URL, modules=modules, pmodules=pmodules, ) else: x = [] hosts = [] hostlist = json.loads(db.ct("project", "hosts", "id=" + id)["hosts"]) hostn = 0 if hostlist: for i in hostlist: x.append( db.ct( "host", "id", "hostip='" + i + "' and projectid=" + id + " order by id desc limit 1" )["id"] ) x.sort(reverse=True) hostn = len(x) for i in x: hosts.append( db.ct("host", "hostip,information,online,addtime,projectid,id", "id=" + str(i)) ) self.render( "project_select.html", heads=[ {"name": "Project", "title": "Project list", "url": "project"}, {"name": urlde(row["name"]), "title": "", "url": ""}, ], username=self.get_secure_cookie("username"), datainfo=db.datainfo(), systeminfo=systeminfo(), urlde=urlde, hostn=hostn, hosts=hosts, gethostn=gethostn, url=URL, timede=timede, urljson=urljson, getaddr=getaddr, ) else: self.render("404.html") else: if do == "add": modules = db.cts("module", "*", "1=1") self.render( "project_add.html", heads=[ {"name": "Project", "title": "Project list", "url": "project"}, {"name": "Add", "title": "", "url": ""}, ], username=self.get_secure_cookie("username"), datainfo=db.datainfo(), systeminfo=systeminfo(), url=URL, urlde=urlde, modules=modules, getmcustom=getmcustom, ) else: prows = db.cts("project", "*", "1=1 order by id") # 所有的project hrown = {} # host数目 hrowno = {} # host online 数目 if prows: for i in prows: hrown[i["id"]] = db.c("host", "projectid=" + str(i["id"])) hrowno[i["id"]] = db.c("host", "projectid=" + str(i["id"]) + " and online=1") self.render( "project.html", heads=[{"name": "Project", "title": "", "url": ""}], prows=prows, prown=len(prows), hrown=hrown, hrowno=hrowno, username=self.get_secure_cookie("username"), datainfo=db.datainfo(), systeminfo=systeminfo(), url=URL, urlde=urlde, timede=timede, )
def get(self, id, do): def gethostn(hostip): return db.c("host", "hostip='"+hostip+"'") def getmcustom(code): code = urlde(code) s = re.findall("({set\.[^}]*})", code) return s if id: row = db.ct("project", "*", "id="+id) if row: if do == 'del': db.d("project", "id="+id) self.redirect("http://"+URL+"/project") elif do == 'edit': modules = db.cts("module", "*", "1=1") pmodules = json.loads(urlde(db.ct("project", "module", "id="+id)['module'])) self.render( "project_edit.html", heads=[ {'name':'Project', 'title':'Project list', 'url':'project'}, {'name':urlde(row['name']), 'title':urlde(row['name']) + 'view', 'url':'project/'+id}, ], username=self.get_secure_cookie("username"), datainfo=db.datainfo(), urlde=urlde, getmcustom=getmcustom, row=row, url=URL, modules=modules, pmodules=pmodules, ) else: x = [] hosts = [] hostlist = json.loads(db.ct("project", "hosts", "id="+id)['hosts']) hostn = 0 if hostlist: for i in hostlist: x.append(db.ct( "host", "id", "hostip='"+i+"' and projectid="+id+" order by id desc limit 1")['id']) x.sort(reverse=True) hostn = len(x) for i in x: hosts.append(db.ct( "host", "hostip,information,online,addtime,projectid,id,addr", "id="+str(i))) self.render( "project_select.html", heads=[ {'name':'Project', 'title':'Project list', 'url':'project'}, {'name':urlde(row['name']), 'title':'', 'url':''}, ], username=self.get_secure_cookie("username"), datainfo=db.datainfo(), urlde=urlde, hostn=hostn, hosts=hosts, gethostn=gethostn, url=URL, timede=timede, urljson=urljson, getaddr=getaddr, ) else: self.render('404.html') else: if do == 'add': modules = db.cts("module", "*", "1=1") self.render( "project_add.html", heads=[ {'name': 'Project', 'title': 'Project list', 'url': 'project'}, {'name': 'Add', 'title': '', 'url': ''}, ], username=self.get_secure_cookie("username"), datainfo=db.datainfo(), url=URL, urlde=urlde, modules=modules, getmcustom=getmcustom, ) else: prows = db.cts("project", "*", "1=1 order by id") #所有的project hrown = {} #host数目 hrowno = {} #host online 数目 if prows: for i in prows: hrown[i['id']] = db.c("host", "projectid="+str(i['id'])) hrowno[i['id']] = db.c("host", "projectid="+str(i['id'])+" and online=1") self.render( "project.html", heads=[{'name': 'Project', 'title': '', 'url':''}], prows=prows, prown=len(prows), hrown=hrown, hrowno=hrowno, username=self.get_secure_cookie("username"), datainfo=db.datainfo(), url=URL, urlde=urlde, timede=timede, )
def get(self, id, do): def gethostn(hostip): return db.c("host", "hostip='" + hostip + "'") def getmcustom(code): code = urlde(code) s = re.findall("({set\..*})", code) return s if id: row = db.ct("project", "*", "id=" + id) if row: if do == 'del': db.d("project", "id=" + id) self.redirect("http://" + URL + "/project") elif do == 'edit': modules = db.cts("module", "*", "1=1") pmodules = json.loads( urlde( db.ct("project", "module", "id=" + id)['module'])) self.render( "project_edit.html", heads=[ { 'name': 'Project', 'title': 'Project list', 'url': 'project' }, { 'name': urlde(row['name']), 'title': urlde(row['name']) + 'view', 'url': 'project/' + id }, ], username=self.get_secure_cookie("username"), datainfo=db.datainfo(), systeminfo=systeminfo(), urlde=urlde, getmcustom=getmcustom, row=row, url=URL, modules=modules, pmodules=pmodules, ) else: x = [] hosts = [] hostlist = json.loads( db.ct("project", "hosts", "id=" + id)['hosts']) hostn = 0 if hostlist: for i in hostlist: x.append( db.ct( "host", "id", "hostip='" + i + "' and projectid=" + id + " order by id desc limit 1")['id']) x.sort(reverse=True) hostn = len(x) for i in x: hosts.append( db.ct( "host", "hostip,information,online,addtime,projectid,id", "id=" + str(i))) self.render( "project_select.html", heads=[ { 'name': 'Project', 'title': 'Project list', 'url': 'project' }, { 'name': urlde(row['name']), 'title': '', 'url': '' }, ], username=self.get_secure_cookie("username"), datainfo=db.datainfo(), systeminfo=systeminfo(), urlde=urlde, hostn=hostn, hosts=hosts, gethostn=gethostn, url=URL, timede=timede, urljson=urljson, getaddr=getaddr, ) else: self.render('404.html') else: if do == 'add': modules = db.cts("module", "*", "1=1") self.render( "project_add.html", heads=[ { 'name': 'Project', 'title': 'Project list', 'url': 'project' }, { 'name': 'Add', 'title': '', 'url': '' }, ], username=self.get_secure_cookie("username"), datainfo=db.datainfo(), systeminfo=systeminfo(), url=URL, urlde=urlde, modules=modules, getmcustom=getmcustom, ) else: prows = db.cts("project", "*", "1=1 order by id") #所有的project hrown = {} #host数目 hrowno = {} #host online 数目 if prows: for i in prows: hrown[i['id']] = db.c("host", "projectid=" + str(i['id'])) hrowno[i['id']] = db.c( "host", "projectid=" + str(i['id']) + " and online=1") self.render( "project.html", heads=[{ 'name': 'Project', 'title': '', 'url': '' }], prows=prows, prown=len(prows), hrown=hrown, hrowno=hrowno, username=self.get_secure_cookie("username"), datainfo=db.datainfo(), systeminfo=systeminfo(), url=URL, urlde=urlde, timede=timede, )