def __init__(self, master, electroshock=False): print '* Initializing' self.master = master self.settings = master.settings self.secrets = master.secrets self.channels = self.secrets.channels self.context = self.secrets.primary_channel self.personality = self.settings.bot self.enabled = self.settings.plugins.values().pop(0) metacortex.botnick = self.personality.nick print '* Exciting neurons' Neurons.cortex = self print '* Connecting to datastore' connectdb() print '* Fondly remembering daddy' admin = Id(self.secrets.owner) if not admin.password: print '*' * 40 print 'Hey %s! You haven\'t set a password yet! As my daddy you really need a password.' % self.secrets.owner tmp_pass = getpass('Before I can continue, please enter a password: '******'See? Was that so hard?' admin.setpassword(tmp_pass, True) tmp_pass = None print '* Loading brainmeats' self.loadbrains(electroshock)
def _create_position(self, ptype): whom = self.lastsender try: quantity = int(self.values[0]) symbol = self.values[1] except: self.chat("That's not right") return if quantity <= 0: self.chat("Do you think this is a muthafuckin game?") return stock = Stock(symbol) if not stock: self.chat("Stock not found") return if stock.exchange.upper() not in self.config.exchanges: self.chat("Stock exchange %s DENIED!" % stock.exchange) return if stock.price < 0.01: self.chat("No penny stocks") return drinker = Id(whom) if not drinker.cash: drinker.cash = self.config.startupcash if not drinker.positions: drinker.positions = [] cost = stock.price * quantity if cost > drinker.cash: self.chat("You is poor") return position = Position(symbol=stock.symbol, price=stock.price, quantity=quantity, date=datetime.utcnow(), type=ptype) drinker.positions.append(position) drinker.cash -= cost # drinker.save() verb = 'bought' if ptype == 'long' else 'shorted' self.chat("%s %s %d shares of %s (%s) at %s" % (drinker.nick, verb, position.quantity, stock.company, position.symbol, position.price))
def addphone(self): if not self.values: self.chat("What number?") return phone = self.values[0] if not re.search("^[0-9]{10}$", phone): self.chat("Just one good ol'merican ten-digit number, thank ya kindly.") return whom = Id(self.lastid) whom.phone = phone self.chat("Number updated.")
def identify(self): if not self.values: self.chat('Enter a password.') return if self.context_is_channel: self.chat('Not in the channel, you twit.') return whom = Id(self.lastid) if not whom.identify(' '.join(self.values)): self.chat("I don't know you... go away...") return self.chat('Welcome back %s' % whom.name)
def passwd(self): whom = Id(self.lastid) if not whom.is_authenticated: self.chat('STRANGER DANGER!') return if not self.values: self.chat('Enter a password.') return if self.context_is_channel: self.chat('Not in the channel, you twit.') return whom.setpassword(' '.join(self.values)) self.chat('All clear.')
def adduser(self): if not self.values: self.chat("Who are you trying to add?") return whom = Id(self.lastid) if not whom.is_authenticated: self.chat("I'm sorry, Dave, I'm afraid I can't do that") return new_user = Id(self.values[0]) tmp_pass = str(totp.now()) new_user.setpassword(tmp_pass, True) self.chat('Hi %s, your temporary password is %s. Please set up your user ' 'by identifying yourself to me via private message (.identify %s) and ' 'then changing your password (.passwd <newpass>).' % (new_user.nick, tmp_pass, tmp_pass), target=new_user.nick)
def add_comment(model_name,postid,data): comment=Hifcomment(); comment.id=Id.get_next_id('commentid') comment.content = data['content'] comment.author=Hifuser.objects.get(id=int(data['author_id'])) comment.save() model = Model[model_name].objects.get(id=int(postid)) model.comments.append(comment) model.save() return True
def portfolio(self): if not self.values: whom = self.lastsender else: whom = self.values[0] drinker = Id(whom) if not drinker.is_recognized: self.chat("%s doesn't exist" % whom) return if not drinker.positions: self.chat("%s doesn't have one" % drinker.name) else: drinker.positions.sort(key=lambda p: p.symbol) self.chat("%8s %10s %10s %10s %10s %10s" % ('type', 'symbol', 'qty', 'price', 'last', 'gain')) total = 0 for p in drinker.positions: stock = Broker(p.symbol) if p.type == 'long': net = p.quantity * (stock.price - p.price) else: net = p.quantity * (p.price - stock.price) if net >= 10000000: # Get rid of stupid twitter positions continue self.chat("%8s %10s %10d %10.02f %10.02f %10.02f" % (p.type, p.symbol, p.quantity, p.price, stock.price, net)) total += net self.chat("%8s %10s %10s %10s %10s %10.02f" % ('', '', '', '', '', total))
def _cmd_PRIVMSG(self, source, args): # Parse the incoming message for a command with the selected command prefix match = re.search( '^{0}(\w+)[ ]?(.+)?'.format(self.settings.bot.command_prefix), args[-1]) if not match: return (source, args) # Parse the user, targer, command and arguments information user = Id(source) target = args[0] if args[0] != self.name else user.nick command = match.group(1) arguments = match.group(2) print "*** target: %s; user: %s" % (target, user.nick) # Only listen to authenticated users if not user.is_authenticated: self.send('PRIVMSG %s :My daddy says not to listen to you.' % target) return (source, args) # If there was no command specified, return the source and args so any bound # Receptors can get triggered with the same information if not command: return (source, args) # Butler that command out yo if arguments: self.cx.values = arguments else: self.cx.values = False self.cx.context = target self.cx.butler.do(self.cx.command, (source, args[-1])) return (source, args)
def sms(self): if self.context not in self.cx.channels: self.chat('No private sms abuse. Tsk tsk.') return if not self.values: self.chat('-sms <number> <message>') return to = self.values[0] msg = ' '.join(self.values[1:]) if not re.search('^[+0-9]+$', to): user = Id(to) if not user or not user.phone: self.chat("Don't know who that is :(") return else: to = user.phone try: params = {'to': to, 'from_': self.secrets.number} regex = '^http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+#]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+$' match = re.search(regex, msg) if match: params['media_url'] = msg else: params['body'] = msg message = self.client.messages.create(**params) self.chat('Message sent: %s (%s)' % (msg, message.sid)) except Exception as e: self.chat(str(e)) return
def salias(self): whom = self.lastsender name = self.values[0] evil = ['salias', 'ralias', 'lalias', 'dalias'] definition = ' '.join(self.values[1:]) drinker = Id(self.lastid) # drinker = Drinker.objects(name=whom).first() if not drinker.is_authenticated: self.chat("Nope.") return if any(sin in definition for sin in evil): self.chat("You're trying to hurt me aren't you?") return #if not drinker: # drinker = Drinker(name=whom) new_alias = Alias(name=name, definition=definition) drinker.aliases.append(new_alias) # drinker.save() self.chat(name + " saved.")
def __init__(self, value, children): self.id = Id.getNewId() self.value = value self.children = children
def __init__(self, parent): self.id = Id.getNewId() self.symbols = {} self.parent = parent
def tourettes(self, target, source, args): sentence = args[-1] whom = Id(source) nick = whom.nick if "mom" in sentence.translate(string.maketrans("", ""), string.punctuation).split(): open("%s/mom.log" % self.cx.settings.directory.logdir, 'a').write(sentence + '\n') # This could be more like a dict if sentence.lower().find("oh snap") != -1: self.chat("yeah WHAT?? Oh yes he DID", target=target) return if sentence.lower() == 'boom': self.chat(u'(\u2022_\u2022)', target=target) self.chat(u'( \u2022_\u2022)>\u2310 \u25A1-\u25A1', target=target) self.chat(u'(\u2310 \u25A1_\u25A1)', target=target) return if sentence.lower() == "sup": self.chat("chillin", target=target) return if sentence.lower().find("murica") != -1: self.chat("f**k yeah", target=target) return if sentence.lower().find("hail satan") != -1: self.chat(u"\u26E7\u26E7\u26E7\u26E7\u26E7", target=target) return if sentence.lower().find("race condition") != -1: self.chat("It's never a race condition.", target=target) return if sentence.lower().find("rimshot") != -1: self.chat("*ting*", target=target) return if sentence.lower().find( "stop") == len(sentence) - 4 and len(sentence) != 3: stops = [ 'Hammertime', "Children, what's that sound", 'Collaborate and listen', ] self.chat(random.choice(stops), target=target) return if sentence.lower().find("idk") != -1: self.chat( u'\u00AF\u005C\u005F\u0028\u30C4\u0029\u005F\u002F\u00AF', target=target) return if sentence.lower().strip( ) in self.config.frustration or sentence.lower().find('stupid') == 0: self.chat(self.cx.commands.get('table')(), target=target) return if sentence.strip() in ['ls', 'jjk', ':wq', 'ifconfig']: self.chat('Wrong window.') return inquiries = [ sentence.lower().find(t) != -1 for t in self.config.questions ] if self.config.smartass and True in inquiries: # Naively parse out the question being asked try: smartassery = sentence.lower().split( self.config.questions[inquiries.index(True)])[1] except: return responses = self.config.ithelp # Dynamic cases need to be appended responses.append('http://lmgtfy.com/?q=' + smartassery.replace(' ', '+')) self.chat(random.choice(responses), target=target) return # There's a very good reason for this. if sentence == "oh shit its your birthday erikbeta happy birthday" and nick == "jcb": self._act(" slaps jcb") self.chat("LEAVE ERIK ALONE!", target=target) return
from id import Id from friends import Friends name = input('Имя пользователя: ') client = Id(name) uid = client.execute() friends_client = Friends(uid) friends = friends_client.execute() for (age, count) in friends: print('{} {}'.format(int(age), '#' * count))
def _cmd_PRIVMSG(self, source, args): user = Id(source) target = args[0] if args[0] != self.name else user.nick self.cx.lastid = user.fullid self.cx.lastsender = user.nick self.cx.context = target if target in self.cx.channels \ and 'spy' in self.cx.channels[target]['mods']: self.cx.chat('%s: %s' % (target, args[-1]), self.cx.secrets.primary_channel) return if target not in self.cx.channels: self.cx.lastprivate = args[-1] else: self.cx.lastpublic = args[-1] self.cx.lastchat = args[-1] # Parse the incoming message for a command with the selected command prefix match = re.search( '^[\{0}|\{1}](\w*)[ ]?(.+)?'.format( self.settings.bot.command_prefix, self.settings.bot.multi_command_prefix), args[-1]) if not match: return (target, source, args) # Parse the user, target, command and arguments information command = match.group(1) arguments = match.group(2) if not command: command = self.cx.lastcommand # Only listen to authenticated users if not user.is_authenticated \ and command not in self.cx.public_commands: self.send('PRIVMSG %s :My daddy says not to listen to you.' % target) # If we recognize the username, at least let them know that we know if user.is_recognized: self.send( 'PRIVMSG %s :I recognize you, but first user %sidentify...' % (user.nick, self.settings.bot.command_prefix)) return (target, source, args) # If there was no command specified, return the source and args so any bound # Receptors can get triggered with the same information if not command: return (target, source, args) # Butler that command out yo if arguments: self.cx.values = arguments else: self.cx.values = False butler = Butler(self.cx) butler.do(self.cx.command, (user.name, target, args[-1])) #self.cx.butler.do(self.cx.command, (user.name, args[-1])) return (target, source, args)
def _close_position(self, ptype): whom = self.lastsender try: quantity = int(self.values[0]) symbol = self.values[1] except: self.chat("That's not right") return if quantity <= 0: self.chat("Do you think this is a muthafuckin game?") return stock = Stock(symbol) if not stock: self.chat("Stock not found") return drinker = Id(whom) if not drinker.is_authenticated: self.chat("You don't have a portfolio") return check = [] keep = [] for p in drinker.positions: if p.symbol == stock.symbol and p.type == ptype: check.append(p) else: keep.append(p) if not check: self.chat("I don't see %s in your portfolio" % stock.symbol) return check.sort(key=lambda x: x.date) verb = 'sold' if ptype == 'long' else 'covered' for p in check: if quantity <= 0: keep.append(p) continue q = min(quantity, p.quantity) basis = p.price * q value = stock.price * q if ptype == 'long': drinker.cash += value net = value - basis else: net = basis - value drinker.cash += basis + net quantity -= q p.quantity -= q if p.quantity > 0: keep.append(p) self.chat("%s %s %d shares of %s at %s (net: %.02f)" % (drinker.nick, verb, q, stock.symbol, stock.price, net)) drinker.positions = keep
def _close_position(self, ptype): whom = self.lastsender try: quantity = int(self.values[0]) symbol = self.values[1].upper() except: self.chat("That's not right") return if quantity <= 0: self.chat("Do you think this is a muthafuckin game?") return stock = Broker(symbol) if not stock: self.chat("Stock not found") return drinker = Id(self.lastid) if not drinker.is_authenticated: self.chat("You don't have a portfolio") return check = [] keep = [] for p in drinker.positions: if p.symbol.upper() == stock.symbol and p.type == ptype: check.append(p) else: keep.append(p) if not check: self.chat("I don't see %s in your portfolio" % stock.symbol) return check.sort(key=lambda x: x.date) verb = 'sold' if ptype == 'long' else 'covered' for p in check: if quantity <= 0: keep.append(p) continue q = min(quantity, p.quantity) basis = p.price * q value = stock.price * q if ptype == 'long': drinker.cash += value net = value - basis else: net = basis - value drinker.cash += basis + net quantity -= q p.quantity -= q if p.quantity > 0: keep.append(p) self.chat("%s %s %d shares of %s at %s (net: %.02f)" % (drinker.nick, verb, q, stock.symbol, stock.price, net)) drinker.positions = keep
def smsticker(self): self.current = mktime(localtime()) # Only check every 10 seconds if self.current < self.next_: return self.next_ += 10 try: messages = self.client.sms.messages.list(to=self.secrets.number) except: print "Error fetching" return while messages: item = messages.pop() sid = item.sid if sid in self.incoming: continue self.incoming.append(sid) # Don't parse previously received messages until the # incoming list has been fully parsed if not self.loaded: continue clipped = item.from_[2:] drinker = Id(phone=clipped) # Note that this trusts the phone number, on the grounds # there must have been access to the system to get the # number in there. Normal ident auths can't be applied. if drinker.name: from_ = drinker.name else: from_ = item.from_ self.cx.context = self.cx.secrets.primary_channel message = 'SMS from %s: %s' % (from_, item.body) self.announce(message) name = drinker.name numba = drinker.phone bypass = False if clipped in self.secrets.bypass: bypass = True name = clipped numba = clipped # Check if the incoming message contained a command match = re.search( '^\{0}(\w+)[ ]?(.+)?'.format( self.cx.settings.bot.command_prefix), item.body) if match and (drinker.name or bypass): command = match.group(1) arguments = match.group(2) try: resp = self.cx.command(name, self.cx.secrets.primary_channel, item.body, silent=True) message = self.client.messages.create( body=resp, to=numba, from_=self.secrets.number) self.chat('Message sent: ' + message.sid) except Exception as e: self.chat(str(e)) self.loaded = True