def from_user(self, mask, o=None, *_): # TODO: If the "o" parameter is passed only operators are returned # according to the <mask> supplied. # TODO: If there is a list of parameters supplied # with a WHO message, a RPL_ENDOFWHO MUST be sent # after processing each list item with <name> being # the item. resp = [] if Channel.exists(mask): channel = Channel.get(mask) for channel_user in channel.users: resp.append( RPL_WHOREPLY(self.actor, channel_user, str(channel)) ) else: if mask == '0': mask = '*' parser = abnf.wildcard(mask) for user in User.all(): # TODO: add check for servername if any([abnf.parse(str, parser) for str in [user.hostname, user.realname, user.nickname]]): resp.append(RPL_WHOREPLY(self.actor, user, mask)) #resp.append(RPL_ENDOFWHO(self.user, str(channel))) return resp
def get_channel(channel_name): if Channel.exists(channel_name): channel = Channel.get(channel_name) else: channel = Channel(channel_name) channel.save() return channel
def user_channel_post(channel_name, username): new_user_channel = None try: contact = request.args.get('contact') if contact is None or contact == '': return error_view(400, "invalid contact") channel = Channel.get(channel_name) user = User.get(username) if UserChannel.exists(user.username, channel.name): return error_view(500, "this channel is already linked") new_user_channel = UserChannel.new(user.username, channel.name, contact) new_user_channel.insert() template = CHANNEL_VERIFY_TEMPLATE template.set_format(token=new_user_channel.token, channel=channel.name) send(contact, channel, template) return user_ch_created_view(new_user_channel) except (MailSendingError, TelegramSendingError): if new_user_channel is not None: new_user_channel.delete() return error_view(500, "error sending token to this contact")
def from_user(self, receivers=None, text=None, *_): if receivers is None: return ERR_NORECIPIENT(self.command, self.actor) if text is None: return ERR_NOTEXTTOSEND(self.actor) resp = [] # TODO: check for ERR_TOOMANYTARGETS for receiver in receivers.split(','): if Channel.exists(receiver): users = [user for user in Channel.get(receiver).users if user is not self.user] resp.append(M( ActorCollection(users), self.command, str(receiver), text, prefix=str(self.user))) elif User.exists(receiver): resp.append(M( Actor.by_user(User.get(receiver)), self.command, str(receiver), text, prefix=str(self.user))) # TODO: Implement wildcards # TODO: check for ERR_WILDTOPLEVEL, RPL_AWAY, ERR_NOTOPLEVEL else: resp.append(ERR_NOSUCHNICK(receiver, self.actor)) return resp
def test_get_telegram(self): name = channel_telegram['_key'] Channel._get = MagicMock(return_value=channel_telegram) ch = Channel.get(name) self.assertEqual(ch.name, name) self.assertEqual(ch.infos.bot_token, channel_telegram['infos']['bot_token'])
def alert_all_process(dn_list): """ Sent the DN list to all registered Users :param dn_list: the DN list to send :return: """ data = [] for dn in dn_list: data.append([ dn['domain_name'], dn['last_ip_address'], dn['current_ip_address'], ]) columns = ['Domain name', 'Last IP address', 'Current IP address'] df = pandas.DataFrame(data=data, columns=columns) # fixme template = ALERT_LIST_TEMPLATE template.set_format(url_alerts="/alerts", url_channels="/channels", table=df.to_markdown(index=False)) # fixme # users = User.list() users = [User.get("user")] for u in users: if u.role != UserRole.USER.value and u.role != UserRole.ADMIN.value: continue user_channels = UserChannel.list_from_username(u.username) for u_ch in user_channels: channel = Channel.get(u_ch.channel_name) send(u_ch.contact, channel, template)
def _handle_privmsg_to_channel(self, message, receiver, user): if not Channel.exists(receiver): return channel = Channel.get(receiver) msg_content = message.parameters[-1] message = "[%s] <%s>: %s" % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M'), user.nickname, msg_content) self._message(channel.name, message)
def post(self, *args, **kwargs): try: data = json.loads(self.request.body) #将json格式文件变成字典 c = Channel(data['_id'], body=data) channel = c.get() yield self.db.Channel.insert(channel) self.write_response("") except Exception as e: print e self.write_response("", status=0, error="channel 添加失败") get_traceback()
def post(self, *args, **kwargs): try: data = json.loads(self.request.body) channel = yield self.db.Channel.find_one({"_id": data["_id"]}) if channel: c = Channel(_id=channel['_id'], body=data) channel = c.get() yield self.db.Channel.save(channel) except: self.write_response("", status=0) get_traceback()
def ch_put(name): infos = request.json if infos is None or infos == '': return error_view(400, "missing parameter") if not Channel.exists(name): return error_view(404, f"channel {name} not found") ch = Channel.get(name) ch.update(infos) return ch_admin_put_view(ch)
def test_get_email(self): name = channel_email['_key'] Channel._get = MagicMock(return_value=channel_email) ch = Channel.get(name) self.assertEqual(ch.name, name) self.assertEqual(ch.infos.smtp_host, channel_email['infos']['smtp_host']) self.assertEqual(ch.infos.smtp_port, channel_email['infos']['smtp_port']) self.assertEqual(ch.infos.sender_email, channel_email['infos']['sender_email']) self.assertEqual(ch.infos.sender_password, channel_email['infos']['sender_password'])
def users_channel_list(): username = get_jwt_identity() out = [] user_channels = UserChannel.list_from_username(username) for u_ch in user_channels: channel = Channel.get(u_ch.channel_name) out.append({ 'user_channel': u_ch, 'channel': channel }) return user_ch_list_view(out)
def ch_delete(name): if name == Channel.DEFAULT: return error_view(403, f"cannot modify default channel") if not Channel.exists(name): return error_view(404, f"channel {name} not found") ch = Channel.get(name) user_channel_list = UserChannel.list_from_channel(ch.name) for user_ch in user_channel_list: user_ch.delete() ch.delete() return ch_admin_delete_view(ch)
def from_user(self, receivers=None, text=None, *_): if receivers is None: return ERR_NORECIPIENT(self.command, self.actor) if text is None: return ERR_NOTEXTTOSEND(self.actor) resp = [] # TODO: check for ERR_TOOMANYTARGETS for receiver in receivers.split(','): if Channel.exists(receiver): channel_log = '%s/%s.log' % (config.get( 'server', 'channel_log_dir'), receiver.replace('#', '')) # if not PrivmsgCommand.channel_log_files.get(channel_log): # PrivmsgCommand.channel_log_files[channel_log] = open(channel_log,'a') # PrivmsgCommand.channel_log_files[channel_log].write("%s::%s::%s::%s\n" % ( # time.time(), time.strftime('%Y-%m-%d %H:%I:%S'), self.user.nickname, text # )) # PrivmsgCommand.channel_log_files[channel_log].flush() with open(channel_log, 'a') as f: f.write("%s::%s::%s::%s\n" % (time.time(), time.strftime('%Y-%m-%d %H:%I:%S'), self.user.nickname, text)) f.flush() users = [ user for user in Channel.get(receiver).users if user is not self.user ] resp.append( M(ActorCollection(users), self.command, str(receiver), text, prefix=str(self.user))) elif User.exists(receiver): resp.append( M(Actor.by_user(User.get(receiver)), self.command, str(receiver), text, prefix=str(self.user))) # TODO: Implement wildcards # TODO: check for ERR_WILDTOPLEVEL, RPL_AWAY, ERR_NOTOPLEVEL else: resp.append(ERR_NOSUCHNICK(receiver, self.actor)) return resp
def invite(): user_pending = None try: body = request.json if body is None: return error_view(400, "invalid JSON in body") email = body.get('email') if email is None: return error_view(400, "invalid email value") # check if mail is already used if User.exists_from_email(email): return error_view(500, f"email already used by an existing user") if UserRequest.exists(email): return error_view(500, f"a request for this email already exists") if UserPending.exists_from_email(email): return error_view( 500, f"a user with this email has already been invited") # create a new pending user in database user_pending = UserPending.new(email) user_pending.insert() # send a mail with the token default_channel = Channel.get(Channel.DEFAULT) template = INVITE_TEMPLATE template.set_format(token=user_pending.token) send(user_pending.email, default_channel, template) return user_pending_created_view(user_pending) except ObjectNotFound as o: return error_view(404, str(o)) except (MailSendingError, TelegramSendingError): # in case the mail cannot be sent, abort the invitation and delete the pending user in database if user_pending is not None: user_pending.delete() return error_view(500, f"error sending the invitation")
def from_user(self, channel_name, topic=None, *_): # TODO: ERR_NOCHANMODES, ERR_CHANOPRIVSNEEDED if not Channel.exists(channel_name): return ERR_NOSUCHCHANNEL(channel_name, self.actor) channel = Channel.get(channel_name) if self.user not in channel.users: return ERR_NOTONCHANNEL(channel_name, self.actor) if topic is None: if channel.topic is None: return RPL_NOTOPIC(self.actor, channel) else: return RPL_TOPIC(self.actor, channel) elif topic == '': channel.topic = None else: channel.topic = topic # Forward message to others on the channel self.message.target = ActorCollection(channel.users) return self.message
def channel_test(channel_name): try: username = get_jwt_identity() if request.method == 'GET': user = User.get(username) channel = Channel.get(channel_name) user_channel = UserChannel.get(user.username, channel.name) template = TEST_TEMPLATE template.set_format(date=str(datetime.now().date())) send(user_channel.contact, channel, template) return user_ch_test_view(user_channel) except ObjectNotFound: return error_view(404, "object not found") except (MailSendingError, TelegramSendingError): return error_view(500, "failed to send test")
def from_user(self, channels, msg='leaving', *_): channels = channels.split(',') ret = [] for channel_name in channels: if not Channel.exists(channel_name): ret.append(ERR_NOSUCHCHANNEL(channel_name, self.actor)) continue channel = Channel.get(channel_name) if self.user not in channel.users: ret.append(ERR_NOTONCHANNEL(channel_name, self.actor)) continue ret.append(M(ActorCollection(channel.users), 'PART', str(channel), msg, prefix=str(self.user) )) self.user.part(channel) return ret
def from_user(self, receivers=None, text=None, *_): if receivers is None: return ERR_NORECIPIENT(self.command, self.actor) if text is None: return ERR_NOTEXTTOSEND(self.actor) resp = [] # TODO: check for ERR_TOOMANYTARGETS for receiver in receivers.split(','): if Channel.exists(receiver): channel_log = '%s/%s.log' % ( config.get('server', 'channel_log_dir'), receiver.replace('#','')) # if not PrivmsgCommand.channel_log_files.get(channel_log): # PrivmsgCommand.channel_log_files[channel_log] = open(channel_log,'a') # PrivmsgCommand.channel_log_files[channel_log].write("%s::%s::%s::%s\n" % ( # time.time(), time.strftime('%Y-%m-%d %H:%I:%S'), self.user.nickname, text # )) # PrivmsgCommand.channel_log_files[channel_log].flush() with open(channel_log,'a') as f: f.write("%s::%s::%s::%s\n" % ( time.time(), time.strftime('%Y-%m-%d %H:%I:%S'), self.user.nickname, text )) f.flush() users = [user for user in Channel.get(receiver).users if user is not self.user] resp.append(M( ActorCollection(users), self.command, str(receiver), text, prefix=str(self.user) )) elif User.exists(receiver): resp.append(M( Actor.by_user(User.get(receiver)), self.command, str(receiver), text, prefix=str(self.user) )) # TODO: Implement wildcards # TODO: check for ERR_WILDTOPLEVEL, RPL_AWAY, ERR_NOTOPLEVEL else: resp.append(ERR_NOSUCHNICK(receiver, self.actor)) return resp
def from_user(self, channels, msg='leaving', *_): channels = channels.split(',') ret = [] for channel_name in channels: if not Channel.exists(channel_name): ret.append(ERR_NOSUCHCHANNEL(channel_name, self.actor)) continue channel = Channel.get(channel_name) if self.user not in channel.users: ret.append(ERR_NOTONCHANNEL(channel_name, self.actor)) continue ret.append( M(ActorCollection(channel.users), 'PART', str(channel), msg, prefix=str(self.user))) self.user.part(channel) return ret
def register(): try: body = request.json if body is None: return error_view(400, "invalid JSON in body") username = body.get('username') password = body.get('password') token = request.args.get('token') if username is None or password is None or token is None: return error_view(400, "invalid parameters") if token == '' or username == '' or password == '': return error_view(400, "missing parameters") if User.exists(username): return error_view(500, f"user with username `{username}` already exists") user_pending = UserPending.get(token) created_user = User.new(username, password, user_pending.email) created_user.insert() user_pending.delete() default_channel = Channel.get(Channel.DEFAULT) user_channel = UserChannel.new( created_user.username, default_channel.name, created_user.email ) user_channel.verified = True user_channel.insert() return user_created_view(created_user) except ObjectNotFound as o: return error_view(404, str(o))
def channel_get(channel_name): try: ch = Channel.get(channel_name) return ch_get_view(ch) except ObjectNotFound: return error_view(500, "channel not found")
def ch_get(name): if not Channel.exists(name): return error_view(404, f"channel {name} not found") ch = Channel.get(name) return ch_admin_get_view(ch)