예제 #1
0
파일: twitter.py 프로젝트: buzzworkers/tl
def handle_twittercmnd(bot, ievent):
    """ arguments: <API cmnd> - do a twitter API cmommand. """
    go = getauth()
    if not go: ievent.reply("the twitter plugin needs the credentials.py file in the .tl/config dir. see the examples dir") ; return
    if not ievent.args: ievent.missing('<API cmnd>') ; return
    target =  strippedtxt(ievent.args[0])
    try:
        from tl.utils.twitter import get_token
        token = get_token(ievent.user.data.name)
        if not token: ievent.reply("you are not logged in yet .. run the twitter-auth command.") ; return 
        key, secret = getcreds(getdatadir())
        token = tweepy.oauth.OAuthToken(key, secret).from_string(token)
        twitter = twitterapi(key, secret, token)
        cmndlist = dir(twitter)
        cmnds = []
        for cmnd in cmndlist:
            if cmnd.startswith("_") or cmnd == "auth": continue
            else: cmnds.append(cmnd)
        if target not in cmnds: ievent.reply("choose one of: %s" % ", ".join(cmnds)) ; return
        try: method = getattr(twitter, target)
        except AttributeError: ievent.reply("choose one of: %s" % ", ".join(cmnds)) ; return
        result = method()
        res = []
        for item in result:
            try: res.append("%s - %s" % (item.screen_name, item.text))
            except AttributeError:
                try: res.append("%s - %s" % (item.screen_name, item.description))
                except AttributeError:
                    try: res.append(str(item.__getstate__()))
                    except AttributeError: res.append(dir(i)) ; res.append(str(item))
        ievent.reply("result of %s: " % target, res) 
    except KeyError: ievent.reply('you are not logged in yet. see the twitter-auth command.')
    except (tweepy.TweepError, urllib.error.HTTPError) as e: ievent.reply('twitter failed: %s' % (str(e),))
예제 #2
0
파일: udp.py 프로젝트: buzzworkers/tl
 def handle(self, input, addr):
     """  handle an incoming udp packet. """
     if cfg['udpseed']:
         data = ""
         for i in range(len(input)/16):
             try: data += crypt.decrypt(input[i*16:i*16+16])
             except Exception as ex:
                 logging.warn("udp - can't decrypt: %s" % str(ex))
                 data = input
                 break
     else: data = input
     if cfg['udpstrip']: data = strippedtxt(data)
     # check if udp is enabled and source ip is in udpallow list
     if cfg['udp'] and (addr[0] in cfg['udpallow'] or _inmask(addr[0])):
         # get printto and passwd data
         header = re.search('(\S+) (\S+) (.*)', data)
         if header:
             # check password
             if header.group(1) == cfg['udppassword']:
                 printto = header.group(2)    # is the nick/channel
                 # check if printto is in allowednicks
                 
                 if cfg['udpalloednicks'] and not printto in cfg['udpallowednicks']:
                     logging.warn("udp - udp denied %s" % printto )
                     return
                 logging.debug('udp - ' + str(addr[0]) +  " - udp allowed")
                 text = header.group(3)    # is the text
                 self.say(printto, text)
             else: logging.warn("udp - can't match udppasswd from " + str(addr))
         else: logging.warn("udp - can't match udp from " + str(addr[0]))
     else: logging.warn('udp - denied udp from ' + str(addr[0]))
예제 #3
0
파일: botbase.py 프로젝트: buzzworkers/tl
 def normalize(self, what):
     """ convert markup to IRC bold. """
     if not what: return what
     txt = strippedtxt(what, ["\002", "\003"])
     txt = re.sub("\s+", " ", what)
     txt = stripcolor(txt)
     txt = txt.replace("\002", "*")
     txt = txt.replace("<b>", "")
     txt = txt.replace("</b>", "")
     txt = txt.replace("<i>", "")
     txt = txt.replace("</i>", "")
     txt = txt.replace("&lt;b&gt;", "*")
     txt = txt.replace("&lt;/b&gt;", "*")
     txt = txt.replace("&lt;i&gt;", "")
     txt = txt.replace("&lt;/i&gt;", "")
     return txt
예제 #4
0
파일: bot.py 프로젝트: buzzworkers/tl
 def normalize(self, what):
     what = strippedtxt(what)
     what = what.replace("<b>", self.BLUE)
     what = what.replace("</b>", self.ENDC)
     what = what.replace("<i>", self.YELLOW)
     what = what.replace("</i>", self.ENDC)
     what = what.replace("<h2>", self.GREEN)
     what = what.replace("</h2>", self.ENDC)
     what = what.replace("<h3>", self.GREEN)
     what = what.replace("</h3>", self.ENDC)
     what = what.replace("&lt;b&gt;", self.BOLD)
     what = what.replace("&lt;/b&gt;", self.ENDC)
     what = what.replace("<br>", "\n")
     what = what.replace("<li>", "* ")
     what = what.replace("</li>", "\n")
     if what.count(self.ENDC) % 2: what = "%s%s" %  (self.ENDC, what)
     return what
예제 #5
0
파일: bot.py 프로젝트: buzzworkers/tl
 def normalize(self, txt):
     txt = stripcolor(txt)
     txt = txt.replace("\n", "<br>");
     txt = txt.replace("<", "&lt;")
     txt = txt.replace(">", "&gt;")
     txt = strippedtxt(txt)
     txt = txt.replace("&lt;br&gt;", "<br>")
     txt = txt.replace("&lt;b&gt;", "<b>")
     txt = txt.replace("&lt;/b&gt;", "</b>")
     txt = txt.replace("&lt;i&gt;", "<i>")
     txt = txt.replace("&lt;/i&gt;", "</i>")   
     txt = txt.replace("&lt;h2&gt;", "<h2>")
     txt = txt.replace("&lt;/h2&gt;", "</h2>")
     txt = txt.replace("&lt;h3&gt;", "<h3>")
     txt = txt.replace("&lt;/h3&gt;", "</h3>")
     txt = txt.replace("&lt;li&gt;", "<li>")
     txt = txt.replace("&lt;/li&gt;", "</li>")
     return txt
예제 #6
0
파일: irc.py 프로젝트: buzzworkers/tl
 def normalize(self, what):
     txt = what.replace("\n", " ")
     txt = strippedtxt(txt, ["\001", "\002", "\003"])
     txt = txt.replace("<b>", "\002")
     txt = txt.replace("</b>", "\002")
     txt = txt.replace("<i>", "\0032")
     txt = txt.replace("</i>", "\003")
     txt = txt.replace("<li>", "\0033*\003 ")
     txt = txt.replace("</li>", "")
     txt = txt.replace("<br><br>", " - ")
     txt = txt.replace("<br>", " [!] ")
     txt = txt.replace("&lt;b&gt;", "\002")
     txt = txt.replace("&lt;/b&gt;", "\002")
     txt = txt.replace("&lt;i&gt;", "\003")
     txt = txt.replace("&lt;/i&gt;", "")
     txt = txt.replace("&lt;h2&gt;", "\0033")
     txt = txt.replace("&lt;/h2&gt;", "\003")
     txt = txt.replace("&lt;h3&gt;", "\0034")
     txt = txt.replace("&lt;/h3&gt;", "\003")
     txt = txt.replace("&lt;li&gt;", "\0034")
     txt = txt.replace("&lt;/li&gt;", "\003")
     return txt
예제 #7
0
파일: irc.py 프로젝트: buzzworkers/tl
 def _readloop(self):
     """ loop on the socketfile. """
     self.stopreadloop = False
     self.stopped = False
     self.goreconnect = True
     timeout = 1
     logging.debug('%s - starting readloop' % self.cfg.name)
     prevtxt = ""
     while not self.stopped and not self.stopreadloop and self.sock and self.fsock:
         try:
             time.sleep(0.01)
             if 'ssl' in self.cfg and self.cfg['ssl']: intxt = self.sock.read().split('\n')
             else: intxt = self.fsock.readline().split('\n')
             if self.stopreadloop or self.stopped:
                 self.goreconnect = 0
                 break
             if intxt == ["",]:
                 logging.error("remote disconnected")
                 self.goreconnect = 1
                 break
             if prevtxt:
                 intxt[0] = prevtxt + intxt[0]
                 prevtxt = ""
             if intxt[-1] != '':
                 prevtxt = intxt[-1]
                 intxt = intxt[:-1]
             for r in intxt:
                 if not r: continue
                 try:
                     r = strippedtxt(r.rstrip(), ["\001", "\002", "\003"])
                     rr = str(fromenc(r.rstrip(), self.encoding))
                 except UnicodeDecodeError:
                     logging.warn("decode error - ignoring (%s)" % self.cfg.name)
                     continue
                 if not rr: continue
                 res = rr
                 try:
                     ievent = IrcEvent().parse(self, res)
                 except Exception as ex:
                     handle_exception()
                     continue
                 try:
                     if int(ievent.cmnd) > 400: logging.error("< %s - %s" % (res, self.cfg.name))
                     elif int(ievent.cmnd) >= 300: logging.info("< %s - %s" % (res, self.cfg.name))
                 except ValueError:
                     if not res.startswith("PING") and not res.startswith("NOTICE"): logging.warn("< %s - %s" % (res, self.cfg.name))
                     else: logging.info("< %s - %s" % (res, self.cfg.name))
                 if ievent: self.handle_ievent(ievent)
                 timeout = 1
         except UnicodeError as ex:
             self.error = str(ex)  
             logging.warn(self.error)
             handle_exception()
             continue
         except socket.timeout as ex:
             self.error = str(ex)
             logging.warn(self.error)
             if self.stopped or self.stopreadloop: break
             timeout += 1
             if timeout > 2:
                 self.goreconnect = True
                 logging.warn('no pong received (%s)' % self.cfg.name)
                 break
             pingsend = self.ping()
             if not pingsend: self.goreconnect = True ; break
             continue
         except ssl.SSLError as ex:
             self.error = str(ex)  
             logging.warn(self.error)
             if self.stopped or self.stopreadloop: break
             if not ex.errno == ETIMEOUT:
                 handle_exception()
                 self.goreconnect = True
                 break
             timeout += 1
             if timeout > 2:
                 self.goreconnect = True
                 logging.warn('no pong received (%s)' % self.cfg.name)
                 break
             logging.warn("socket timeout (%s)" % self.cfg.name)
             pingsend = self.ping()
             if not pingsend: self.goreconnect = True ; break
             handle_exception()
         except IOError as ex: 
             self.error = str(ex)
             logging.warn(self.error)
             if ex.errno in [EAGAIN, EPIPE, EBADF]: time.sleep(1) ; continue
             if not self.stopped:
                 logging.error('connecting error: %s (%s)' % (str(ex), self.cfg.name))
                 handle_exception()
                 self.goreconnect = True
             break
         except socket.error as ex:
             self.error = str(ex)  
             logging.warn(self.error)
             if ex.errno in [EAGAIN, EPIPE, EBADF]: time.sleep(0.5) ; continue
             if not self.stopped:
                 logging.error('connecting error: %s (%s)' % (str(ex), self.cfg.name))
                 self.goreconnect = True
             break
         except Exception as ex:
             self.error = str(ex)
             if self.stopped or self.stopreadloop: break
             self.goreconnect = True
             break
     logging.warn('readloop stopped - %s (%s)' % (self.error, self.cfg.name))
     self.connectok.clear()
     self.connected = False
     if self.goreconnect and not self.stopped:
         self.goreconnect = False
         time.sleep(2)
         self.reconnect()