Beispiel #1
0
class RSS(Object):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._cfg = Config(rss)
        config  = storage.last("cfg", "rss")
        if config: self._cfg.update(config)
        objs.seen = Object()
        objs.seen.list = []
        objs.seen.seen = "rss"
        objs.seen.prefix = "seen"
        obj = storage.last("seen", "rss")
        if obj: objs.seen = obj
        logging.warn("# seen %s" % len(objs.seen.list))

    def start(self):
        repeater = Repeater(600, self.fetcher)
        return launcher.launch(repeater.start)

    def stop(self): launcher.kill("RSS")
       
    def fetcher(self):
        thrs = []
        for obj in storage.find("rss"):
            if not obj.rss: continue
            thr = launcher.launch(self.fetch, obj)
            thrs.append(thr)
        result = launcher.waiter(thrs)
        if result: logging.info("! fetched %s" % (sj(*[str(x) for x in result if x]) or 0))
        objs.seen.sync()

    def fetch(self, obj):
        nr = 0
        for o in get_feed(obj.rss):
            if o.link in objs.seen.list: continue
            objs.seen.list.append(o.link)
            o.prefix = "feeds"
            o.services = "rss"
            if "published" in o:
                try: date = file_time(to_time(o.published)) ; o.save(date)
                except ENODATE as ex: logging.warn("EDATE %s" % str(ex))
            else: o.save()
            for bot in fleet: bot.announce(self.display(o))
            nr += 1
        return nr 

    def display(self, obj):
        result = ""
        for key in self._cfg.display_list:
            data = obj.get(key, None)
            if data: result += "%s - " % strip_html(data.rstrip())
        for check in self._cfg.descriptions:
            if check in obj.link:
                summary = obj.get("summary", None)
                if summary: result += "%s - " % summary
        if result: return result[:-3].rstrip()
Beispiel #2
0
 def __init__(self):
     super().__init__(self)
     self._cfg = Config(udp)
     self._cfg.load("udp")
     self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
     self._sock.setblocking(1)
     self._status = "start"
     self._starttime = time.time()
Beispiel #3
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self._cfg = Config(rss)
     config  = storage.last("cfg", "rss")
     if config: self._cfg.update(config)
     objs.seen = Object()
     objs.seen.list = []
     objs.seen.seen = "rss"
     objs.seen.prefix = "seen"
     obj = storage.last("seen", "rss")
     if obj: objs.seen = obj
     logging.warn("# seen %s" % len(objs.seen.list))
Beispiel #4
0
class UDP(Object):
    """ UDP class to echo txt through the bot, use the meds-udp program to send. """
    def __init__(self):
        super().__init__(self)
        self._cfg = Config(udp)
        self._cfg.load("udp")
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
        self._sock.setblocking(1)
        self._status = "start"
        self._starttime = time.time()

    def start(self):
        """ start the UDP server. """
        logging.info("! start udp %s:%s" % (self._cfg.host, self._cfg.port))
        self._sock.bind((self._cfg.host, self._cfg.port))
        self.ready()
        self._status = "ok"
        while self._status:
            (txt, addr) = self._sock.recvfrom(64000)
            if not self._status: break
            data = str(txt.rstrip(), "utf-8")
            if not data: break
            self.output(data, addr)
        logging.info("! stop udp %s:%s" % (self._cfg.host, self._cfg.port))

    def exit(self):
        """ shutdown the UDP server. """
        self._status = ""
        self._sock.settimeout(1.0)
        self._sock.sendto(bytes("bla", "utf-8"),
                          (self._cfg.host, self._cfg.port))

    def output(self, txt, addr=None):
        """ output to all bots on fleet. """
        (passwd, text) = txt.split(" ", 1)
        text = text.replace("\00", "")
        if passwd == self._cfg.password:
            for bot in fleet:
                bot.announce(text)
Beispiel #5
0
def edit(event):
    if not meds.core.users.allowed(event.origin, "CONFIG"):
        event.reply("you are not allowed to change the config.")
        return
    obj = meds.core.objs
    args = event._parsed.args
    if not args:
        event.reply("edit <item> <key> <value>")
        return
    name = args[0]
    o = getattr(obj, name, None)
    if not o:
        obj = meds.core.cfgs
        o = getattr(obj, name, None)
        if not o:
            o = Config().load(name)
            if not o:
                template = templates.get(name, None)
                if template:
                    o = Config(template)
            if o:
                obj[name] = o
    if not o:
        event.reply("no %s object available." % name)
        return
    else:
        o = o.get("_cfg", o)
    try:
        key = args[1]
    except IndexError:
        event.reply(o)
        return
    if len(args) < 3:
        event.reply("i need a value to set.")
        return
    if len(args) == 3:
        val = args[2]
    else:
        val = args[2:]
    try:
        o[key] = ast.literal_eval(val)
    except (TypeError, ValueError):
        o[key] = val
    o.sync()
    event.reply(o)
Beispiel #6
0
def parse_cli(name="MEDS", version=__version__, eggs=False, workdir=""):
    from meds.log import loglevel
    from meds.cfg import main
    import meds.core
    opts, args = make_opts(opts_defs)
    cfg = Config(main)
    cfg.name = name
    cfg.args = args
    cfg.update(vars(opts))
    loglevel(cfg.loglevel, colors=cfg.colors)
    if not cfg.workdir: cfg.workdir = os.path.expanduser(workdir or "~/.meds")
    if cfg.scan or eggs:
        for fn in os.listdir(os.getcwd()):
            if fn.endswith(".egg") and fn not in sys.path:
                logging.warn("# load %s" % fn.split()[-1])
                sys.path.insert(0, os.path.abspath(fn))
    meds.core.cfg.update(cfg)
    return cfg
Beispiel #7
0
#
#
""" objects stash, a central place to store objects. """

from meds.object import Object, OOL
from meds.launcher import Launcher
from meds.aliases import Aliases
from meds.storage import Storage
from meds.runner import Runner
from meds.kernel import Kernel
from meds.users import Users
from meds.cmnds import Cmnds
from meds.cfg import Config, main

#:
cfg = Config(main)
#:
aliases = Aliases()
#:
cmnds = Cmnds()
#:
fleet = []
#:
kernel = Kernel()
#:
launcher = Launcher()
#:
names = OOL()
#:
runner = Runner()
#:
Beispiel #8
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.channels = []
     self._cfg = Config()
     self._cfg.load(sname(self).lower())
     if not self._cfg.server: self._cfg.server = "host"
Beispiel #9
0
class Bot(Engine):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.channels = []
        self._cfg = Config()
        self._cfg.load(sname(self).lower())
        if not self._cfg.server: self._cfg.server = "host"

    def announce(self, txt):
        if self._cfg.silent: return
        if not self.channels:
            self.out(txt)
        for channel in self.channels:
            self.say(channel, txt)

    def cmnd(self, txt):
        event = Event()
        event._server = self._cfg.server
        event.btype = sname(self)
        event.origin = "user@bot"
        event.txt = txt
        self.dispatch(event)
        return event

    def connect(self):
        pass

    def dispatch(self, event):
        event._server = self._cfg.server
        event.parse()
        event.dispatch()
        event.wait()
        #self.show(event)
        event.show()

    def join(self, channel):
        pass

    def joinall(self):
        for channel in self.channels:
            self.join(channel)

    def out(self, txt):
        print(txt)

    def prompt(self):
        if kernel._cfg.colors: txt = "%s>%s " % (YELLOW, ENDC)
        else: txt = "> "
        sys.stdout.write(txt)
        sys.stdout.flush()

    def run_forever(self):
        self.start()
        if not kernel._cfg.shell:
            if kernel._cfg.args:
                self.cmnd(" ".join(kernel._cfg.args))
        else:
            self.prompt()
            self.wait()

    def say(self, channel, txt):
        self.out(txt)

    def show(self, event):
        for txt in event._result:
            self.say(event.channel, txt)

    def start(self):
        if self not in fleet: fleet.append(self)
        super().start()