Пример #1
0
 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)
Пример #2
0
 def get_channel(channel_name):
     if Channel.exists(channel_name):
         channel = Channel.get(channel_name)
     else:
         channel = Channel(channel_name)
         channel.save()
     return channel
Пример #3
0
    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
Пример #4
0
 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
Пример #5
0
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)
Пример #6
0
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)
Пример #7
0
    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
Пример #8
0
 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
Пример #9
0
    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
Пример #10
0
    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
Пример #11
0
    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
Пример #12
0
def ch_post(name):
    try:
        ch_type = request.args.get('type')
        if ch_type is None or ch_type == '':
            return error_view(400, "missing parameter")

        infos = request.json
        if infos is None or infos == '':
            return error_view(400, "missing json")

        if Channel.exists(name):
            return error_view(500, "a channel with this name already exists")

        new_ch = Channel.new(name, ch_type, infos)
        new_ch.insert()

        return ch_admin_post_view(new_ch)

    except KeyError:
        return error_view(500, "error parsing json input")

    except ChannelTypeError:
        return error_view(400, "invalid channel type")
Пример #13
0
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)