def join(self, channel, password=None, nick=None): """ join conference. """ if channel.startswith("#"): return logging.warn("joining %s" % channel) try: if not nick: nick = channel.split('/')[1] except IndexError: nick = self.cfg.nick or "jsonbot" self.timejoined[channel] = time.time() chan = ChannelBase(channel, self.botname) chan.data['nick'] = nick if password: chan.data['key'] = password if chan.data['key'] and not password: password = chan.data['key'] if not chan.data.has_key('cc'): chan.data['cc'] = self.cfg['defaultcc'] or '!' if channel not in self.state['joinedchannels']: self.state['joinedchannels'].append(channel) self.state.save() if channel in self.channels409: self.channels409.remove(channel) chan.save() channel = channel.split('/')[0] q = Queue.Queue() presence = Presence({'to': channel + '/' + nick, "xmlns": 'http://jabber.org/protocol/muc' }) if password: presence.x = {} presence.x['password'] = password self.send(presence) return 1
def join(self, channel, password=None, nick=None): """ join conference. """ if channel.startswith("#"): return logging.warn("joining %s" % channel) try: if not nick: nick = channel.split('/')[1] except IndexError: nick = self.cfg.nick or "jsonbot" self.timejoined[channel] = time.time() chan = ChannelBase(channel, self.botname) chan.data['nick'] = nick if password: chan.data['key'] = password if chan.data['key'] and not password: password = chan.data['key'] if not chan.data.has_key('cc'): chan.data['cc'] = self.cfg['defaultcc'] or '!' if channel not in self.state['joinedchannels']: self.state['joinedchannels'].append(channel) self.state.save() if channel in self.channels409: self.channels409.remove(channel) chan.save() channel = channel.split('/')[0] q = Queue.Queue() presence = Presence({ 'to': channel + '/' + nick, "xmlns": 'http://jabber.org/protocol/muc' }) if password: presence.x = {} presence.x['password'] = password self.send(presence) return 1
def sendpresence(self): """ send presence based on status and status text set by user. """ if self.state: if self.state.has_key('status') and self.state['status']: status = self.state['status'] else: status = "" if self.state.has_key('show') and self.state['show']: show = self.state['show'] else: show = "" else: status = "" show = "" logging.debug('%s - keepalive - %s - %s' % (self.cfg.name, show, status)) if show and status: p = Presence({'to': self.cfg.user, 'show': show, 'status': status}) elif show: p = Presence({'to': self.cfg.user, 'show': show}) elif status: p = Presence({'to': self.cfg.user, 'status': status}) else: p = Presence({'to': self.cfg.user}) self.send(p)
def part(self, channel): """ leave conference. """ if channel.startswith("#"): return presence = Presence({'to': channel}) presence.type = 'unavailable' self.send(presence) if channel in self.state['joinedchannels']: self.state['joinedchannels'].remove(channel) self.state.save() return 1
def quit(self): """ send unavailable presence. """ if self.error: return presence = Presence({'type': 'unavailable' ,'to': self.cfg.user}) if self.state: for i in self.state.data.joinedchannels: presence.to = i self.send(presence) presence = Presence({'type': 'unavailable', 'to': self.cfg.user}) presence['from'] = self.cfg.user self.send(presence)
def quit(self): """ send unavailable presence. """ if self.error: return presence = Presence({'type': 'unavailable' ,'to': self.jid}) if self.state: for i in self.state.data.joinedchannels: presence.to = i self.send(presence) presence = Presence({'type': 'unavailable', 'to': self.jid}) presence['from'] = self.me self.send(presence)
def _keepchannelsalive(self): """ channels keep alive method. """ nrsec = 0 p = Presence({'to': self.me, 'txt': '' }) while not self.stopped: time.sleep(1) nrsec += 1 if nrsec < 600: continue else: nrsec = 0 for chan in self.state['joinedchannels']: if chan not in self.channels409: p = Presence({'to': chan}) self.send(p)
def join(self, channel, password=None, nick=None): """ join conference. """ if channel.startswith("#"): return try: if not nick: nick = channel.split('/')[1] except IndexError: nick = self.nick channel = channel.split('/')[0] q = Queue.Queue() self.errorwait.register("409", q, 3) self.errorwait.register("401", q, 3) self.errorwait.register("400", q, 3) presence = Presence({'to': channel + '/' + nick}) if password: presence.x.password = password self.send(presence) errorobj = waitforqueue(q, 3000) if errorobj: err = errorobj[0].error logging.error('%s - error joining %s - %s' % (self.name, channel, err)) if err >= '400': if channel not in self.channels409: self.channels409.append(channel) return err self.timejoined[channel] = time.time() chan = ChannelBase(channel, self.botname) chan.data['nick'] = nick if password: chan.data['key'] = password if not chan.data.has_key('cc'): chan.data['cc'] = self.cfg['defaultcc'] or '!' if channel not in self.state['joinedchannels']: self.state['joinedchannels'].append(channel) self.state.save() if channel in self.channels409: self.channels409.remove(channel) return 1
def setstatus(self, status, show=""): """ send status presence. """ if self.error: return if self.state: self.state['status'] = status self.state['show'] = show self.state.save() presence = Presence({'status': status, 'show': show ,'to': self.jid}) self.send(presence)
def main(): motor = Motor() print("> Motor initialized") presence = Presence() print("> Presence initialized") while True: if presence.has_presence() == True: motor.on() # donaldduck.play(DONALD_TRACK) p = vlc.MediaPlayer(AUDIO_PATH + "/" + DONALD_TRACK) p.play() start = time.time() while audio.info.length > (time.time() - start): if ((time.time() - start) > 0.0) and ( (time.time() - start) < 2.0): motor.on() print("> Motor ON!") elif ((time.time() - start) > 2.1) and ( (time.time() - start) < 20.0): motor.off() print("> Motor OFF!") elif ((time.time() - start) > 20.1) and ( (time.time() - start) < 23.0): motor.on() print("> Motor ON!") elif ((time.time() - start) > 23.1) and ( (time.time() - start) < 30.0): motor.off() print("> Motor OFF!") elif ((time.time() - start) > 39.1) and ( (time.time() - start) < 40.0): motor.on() print("> Motor ON!") elif ((time.time() - start) > 40.1): motor.off() print("> Motor OFF!") print("> Playing Track!") motor.off() print("> Motor OFF!") time.sleep(0.5)
def handle_presence(self, data, *args, **kwargs): """ message handler. """ try: p = Presence() p.parse(data, self) frm = p.fromm nickk = "" nick = p.nick if self.cfg.user in frm: self.pongcheck = True if nick: self.userhosts[nick] = stripped(frm) nickk = nick jid = p.fromm if nickk and jid and self.cfg.fulljids: channel = p.channel if not self.jids.has_key(channel): self.jids[channel] = {} self.jids[channel][nickk] = jid self.userhosts[nickk] = stripped(jid) logging.debug('%s - setting jid of %s (%s) to %s' % (self.cfg.name, nickk, channel, self.userhosts[nickk])) if p.type == 'subscribe': pres = Presence({'to': p.fromm, 'type': 'subscribed'}) self.send(pres) pres = Presence({'to': p.fromm, 'type': 'subscribe'}) self.send(pres) nick = p.resource if p.type != 'unavailable': p.joined = True p.type = 'available' elif self.cfg.user in p.userhost: try: del self.jids[p.channel] logging.debug('%s - removed %s channel jids' % (self.cfg.name, p.channel)) except KeyError: pass else: try: del self.jids[p.channel][p.nick] logging.debug('%s - removed %s jid' % (self.cfg.name, p.nick)) except KeyError: pass self.put(p) except Exception, ex: logging.error(str(ex))
def __init__(self, presence = None): super(ContextMonitor, self).__init__() # Create the local context. self._context = Context() # Subscribe to Presence announcements. if presence == None: self._presence = Presence() self._presence.connect() else: self._presence = presence self._presence.subscribe('scavenger', self.receive_announcement)
def handle_presence(self, data, *args, **kwargs): """ message handler. """ try: p = Presence() p.parse(data, self) frm = p.fromm nickk = "" nick = p.nick if self.cfg.user in frm: self.pongcheck = True if nick: self.userhosts[nick] = stripped(frm) nickk = nick jid = p.fromm if nickk and jid and self.cfg.fulljids: channel = p.channel if not self.jids.has_key(channel): self.jids[channel] = {} self.jids[channel][nickk] = jid self.userhosts[nickk] = stripped(jid) logging.debug( '%s - setting jid of %s (%s) to %s' % (self.cfg.name, nickk, channel, self.userhosts[nickk])) if p.type == 'subscribe': pres = Presence({'to': p.fromm, 'type': 'subscribed'}) self.send(pres) pres = Presence({'to': p.fromm, 'type': 'subscribe'}) self.send(pres) nick = p.resource if p.type != 'unavailable': p.joined = True p.type = 'available' elif self.cfg.user in p.userhost: try: del self.jids[p.channel] logging.debug('%s - removed %s channel jids' % (self.cfg.name, p.channel)) except KeyError: pass else: try: del self.jids[p.channel][p.nick] logging.debug('%s - removed %s jid' % (self.cfg.name, p.nick)) except KeyError: pass self.put(p) except Exception, ex: logging.error(str(ex))
class ContextMonitor(object): def __init__(self, presence = None): super(ContextMonitor, self).__init__() # Create the local context. self._context = Context() # Subscribe to Presence announcements. if presence == None: self._presence = Presence() self._presence.connect() else: self._presence = presence self._presence.subscribe('scavenger', self.receive_announcement) def receive_announcement(self, peer_name, peer_address, service): peer_name = peer_name.strip('\x00 ') cpu_strength, cpu_cores, active_tasks, network_media = struct.unpack("!fIII", service.data) peer = ScavengerPeer(peer_name, (peer_address, service.port), cpu_strength, cpu_cores, active_tasks, network_media) self._context.add(peer) def get_peers(self): return self._context.get_peers() def has_peer(self, name): return self._context.has_peer(name) def increment_peer_activity(self, name): self._context.increment_peer_activity(name) def decrement_peer_activity(self, name): self._context.decrement_peer_activity(name) def shutdown(self): self._presence.shutdown(True)
token_backend = FileSystemTokenBackend(token_path='.', token_filename='my_token.txt') account = Account(credentials, auth_flow_type='public', protocol=protocol, token_backend=token_backend) scopes = ['https://graph.microsoft.com/Presence.Read'] colors = { 'Offline': 'blue', 'Busy': 'red', 'DoNotDisturb': 'red', 'Available': 'green', 'Away': 'yellow', 'BeRightBack': 'yellow' } presence = Presence(con=account.connection, protocol=protocol) while True: now = datetime.now() # if later than 17.00 or later than Friday (4) => sleep if now.hour > 17 or now.weekday() > 4: print('time to rest!') tomorrow = now + timedelta(days=1) t = tomorrow tomorrow_morning = datetime(t.year, t.month, t.day, 8, 0) seconds_to_sleep = (tomorrow_morning - now).total_seconds() time.sleep(seconds_to_sleep) if not account.is_authenticated: account.authenticate(scopes=scopes)
#!/bin/python3 from presence import Presence import time import os # change dir to where the script is DIR = os.path.dirname(os.path.realpath(__file__)) os.chdir(DIR) client_id = '451491740499705857' # My ID, please no abuse :( RPC = Presence(client_id) RPC.connect() # Magic PyPresence stuff, no idea :( line1 = os.popen('bash ./gimp-rpc.sh line1').read() # status line2 = os.popen('bash ./gimp-rpc.sh line2').read() # filename version = os.popen('bash ./gimp-rpc.sh version').read() line2old = line2 initresult = os.popen('bash ./init-script.sh').read() # Initialize the script if initresult.find("ERROR_WMCTRL_NOT_INSTALLED") != -1: date = os.popen('echo -n $(date +%s)').read() print( date + ' - wmctrl is not installed. Refer to README.md for more information.') exit() elif initresult.find("ERROR_SCRIPT_ALREADY_RUNNING") != -1: date = os.popen('echo -n $(date +%s)').read() print(date + ' - The script is already running! Stopping.') exit()
def handle_presence(self, data): """ presence handler. """ p = Presence(data) p.parse() frm = p.fromm nickk = "" nick = p.nick if self.cfg.user in frm: self.pongcheck = True if nick: self.userhosts[nick] = stripped(frm) nickk = nick jid = None for node in p.subelements: try: jid = node.x.item.jid except (AttributeError, TypeError): continue if nickk and jid and self.cfg.fulljids: channel = p.channel if not self.jids.has_key(channel): self.jids[channel] = {} self.jids[channel][nickk] = jid self.userhosts[nickk] = stripped(jid) logging.debug('%s - setting jid of %s (%s) to %s' % (self.cfg.name, nickk, channel, self.userhosts[nickk])) if p.type == 'subscribe': pres = Presence({'to': p.fromm, 'type': 'subscribed'}) self.send(pres) pres = Presence({'to': p.fromm, 'type': 'subscribe'}) self.send(pres) nick = p.resource if p.type != 'unavailable': p.joined = True p.type = 'available' elif self.cfg.user in p.userhost: try: del self.jids[p.channel] logging.debug('%s - removed %s channel jids' % (self.cfg.name, p.channel)) except KeyError: pass else: try: del self.jids[p.channel][p.nick] logging.debug('%s - removed %s jid' % (self.cfg.name, p.nick)) except KeyError: pass if p.type == 'error': for node in p.subelements: try: err = node.error.code except (AttributeError, TypeError): err = 'no error set' try: txt = node.text.data except (AttributeError, TypeError): txt = "" if err: logging.error('%s - error - %s - %s' % (self.cfg.name, err, txt)) try: method = getattr(self,'handle_' + err) try: method(p) except: handle_exception() except AttributeError: pass self.doevent(p)
def handle_presence(self, data): """ presence handler. """ p = Presence(data) p.parse() frm = p.fromm nickk = "" nick = p.nick if self.me in p.userhost: return 0 if nick: self.userhosts[nick] = str(frm) nickk = nick jid = None for node in p.subelements: try: jid = node.x.item.jid except (AttributeError, TypeError): continue if nickk and jid: channel = p.channel if not self.jids.has_key(channel): self.jids[channel] = {} self.jids[channel][nickk] = jid self.userhosts[nickk] = str(jid) logging.debug('%s - setting jid of %s (%s) to %s' % (self.name, nickk, channel, jid)) if p.type == 'subscribe': pres = Presence({'to': p.fromm, 'type': 'subscribed'}) self.send(pres) pres = Presence({'to': p.fromm, 'type': 'subscribe'}) self.send(pres) nick = p.resource if p.type != 'unavailable': p.joined = True p.type = 'available' elif self.me in p.userhost: try: del self.jids[p.channel] logging.debug('%s - removed %s channel jids' % (self.name, p.channel)) except KeyError: pass else: try: del self.jids[p.channel][p.nick] logging.debug('%s - removed %s jid' % (self.name, p.nick)) except KeyError: pass if p.type == 'error': for node in p.subelements: try: err = node.error.code except (AttributeError, TypeError): err = 'no error set' try: txt = node.text.data except (AttributeError, TypeError): txt = "" if err: logging.error('%s - error - %s - %s' % (self.name, err, txt)) self.errorwait.check(p) try: method = getattr(self,'handle_' + err) try: method(p) except: handle_exception() except AttributeError: pass self.doevent(p)
def invite(self, jid): pres = Presence({'to': jid, 'type': 'subscribe'}) self.send(pres) time.sleep(2) pres = Presence({'to': jid}) self.send(pres)