def __init__(self, id, botname=None, type="notset", needexist=False): if not id: raise NoChannelSet() if not botname: Persist.__init__(self, getdatadir() + os.sep + 'channels' + os.sep + stripname(id), needexist=needexist) else: Persist.__init__(self, getdatadir() + os.sep + 'fleet' + os.sep + stripname(botname) + os.sep + 'channels' + os.sep + stripname(id), needexist=needexist) self.id = id self.type = type self.lastmodified = time.time() self.data.id = id self.data.enable = self.data.enable or False self.data.ops = self.data.ops or [] self.data.taglist = self.data.taglist or [] self.data.silentcommands = self.data.silentcommands or [] self.data.allowcommands = self.data.allowcommands or [] self.data.feeds = self.data.feeds or [] self.data.forwards = self.data.forwards or [] self.data.allowwatch = self.data.allowwatch or [] self.data.watched = self.data.watched or [] self.data.passwords = self.data.passwords or {} self.data.cc = self.data.cc or "" self.data.nick = self.data.nick or "jsb" self.data.key = self.data.key or "" self.data.denyplug = self.data.denyplug or [] self.data.createdfrom = whichmodule() self.data.cacheindex = 0 self.data.tokens = self.data.tokens or [] self.data.webchannels = self.data.webchannels or []
def __init__(self, name): self.name = name if not os.path.exists(getdatadir() + os.sep + 'gatekeeper'): os.mkdir(getdatadir() + os.sep + 'gatekeeper') Persist.__init__(self, getdatadir() + os.sep + 'gatekeeper' + os.sep + name) self.data.whitelist = self.data.whitelist or []
def __init__(self, name): self.name = name try: import waveapi except: if not os.path.exists(getdatadir() + os.sep +'gatekeeper'): os.mkdir(getdatadir() + os.sep + 'gatekeeper') Persist.__init__(self, getdatadir() + os.sep + 'gatekeeper' + os.sep + name) self.data.whitelist = self.data.whitelist or []
def oldname(name): from jsb.lib.datadir import getdatadir if name.startswith("-"): name[0] = "+" name = name.replace("@", "+") if os.path.exists(getdatadir() + os.sep + name): return name name = name.replace("-", "#") name = prevchan.replace("+", "@") if os.path.exists(getdatadir() + os.sep + name): return name return ""
def handle_alive(bot, event): if len(event.args) < 1: event.missing("<plugname> [<search>]") ; return if event.options and event.options.all: collection = PersistCollection(getdatadir()) else: collection = PersistCollection(getdatadir() + os.sep + 'plugs') filenames = collection.filenames(event.args) result = {} for fn in filenames: result[fn.split(os.sep)[-1]] = accessed(fn) event.reply("alive results: ", result)
def get_token(username): global users if not users: users = TwitterUsers(getdatadir() + os.sep + "twitter" + os.sep + "users") if not users: raise Exception("can't get twitter users object") ; return key, secret = getcreds(getdatadir()) if not key: raise Exception(getdatadir()) if not secret: raise Exception(getdatadir()) token = twittertoken(key, secret, users, username) if not token: raise Exception("%s %s" % (str(user), username)) api = twitterapi(key, secret, token) if not api: raise Exception("%s %s" % (str(user), name)) return (token, api)
def handle_alive(bot, event): if len(event.args) < 1: event.missing("<plugname> [<search>]") return if event.options and event.options.all: collection = PersistCollection(getdatadir()) else: collection = PersistCollection(getdatadir() + os.sep + 'plugs') filenames = collection.filenames(event.args) result = {} for fn in filenames: result[fn.split(os.sep)[-1]] = accessed(fn) event.reply("alive results: ", result)
def handle_chanupgrade(bot, event): """ no arguments - upgrade the channel. """ prevchan = event.channel # 0.4.1 if prevchan.startswith("-"): prevchan[0] = "+" prevchan = prevchan.replace("@", "+") prev = Persist(getdatadir() + os.sep + "channels" + os.sep + prevchan) if prev.data: event.chan.data.update(prev.data) ; event.chan.save() ; event.reply("done") else: prevchan = event.channel prevchan = prevchan.replace("-", "#") prevchan = prevchan.replace("+", "@") prev = Persist(getdatadir() + os.sep + "channels" + os.sep + prevchan) if prev.data: event.chan.data.update(prev.data) ; event.chan.save() ; event.reply("done") else: event.reply("can't find previous channel data")
def handle_twitterfriends(bot, ievent): """ do a twitter API cmommand. """ if not go: ievent.reply( "the twitter plugin needs the credentials.py file in the .jsb/data/config dir. see .jsb/data/examples" ) return try: twitteruser = TwitterUsers("users") token = twitteruser.data.get(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 = oauth.OAuthToken(key, secret).from_string(token) twitter = twitterapi(key, secret, token) method = getattr(twitter, "friends_timeline") # do the thing result = method() res = [] for item in result: try: res.append("%s - %s" % (item.author.screen_name, item.text)) #logging.warn("twitter - %s" % dir(item.author)) #res.append(unicode(item.__getstate__())) except Exception, ex: handle_exception() ievent.reply("results: ", res)
def __init__(self, cfg=None, usersin=None, plugs=None, botname=None, nick=None, bottype=None, nocbs=None, *args, **kwargs): logging.debug("type is %s" % str(type(self))) if cfg: self.cfg = cfg ; botname = botname or self.cfg.name if not botname: botname = u"default-%s" % str(type(self)).split('.')[-1][:-2] if not botname: raise Exception("can't determine botname") self.fleetdir = u'fleet' + os.sep + stripname(botname) if not self.cfg: self.cfg = Config(self.fleetdir + os.sep + u'config') self.cfg.name = botname or self.cfg.name if not self.cfg.name: raise Exception("name is not set in %s config file" % self.fleetdir) logging.debug("name is %s" % self.cfg.name) LazyDict.__init__(self) logging.debug("created bot with config %s" % self.cfg.tojson(full=True)) self.ecounter = 0 self.ids = [] self.aliases = getaliases() self.reconnectcount = 0 self.plugs = coreplugs self.gatekeeper = GateKeeper(self.cfg.name) self.gatekeeper.allow(self.user or self.jid or self.cfg.server or self.cfg.name) self.starttime = time.time() self.type = bottype or "base" self.status = "init" self.networkname = self.cfg.networkname or self.cfg.name or "" from jsb.lib.datadir import getdatadir datadir = getdatadir() self.datadir = datadir + os.sep + self.fleetdir self.maincfg = getmainconfig() self.owner = self.cfg.owner if not self.owner: logging.debug(u"owner is not set in %s - using mainconfig" % self.cfg.cfile) self.owner = self.maincfg.owner self.users = usersin or getusers() logging.debug(u"owner is %s" % self.owner) self.users.make_owner(self.owner) self.outcache = outcache self.userhosts = LazyDict() self.nicks = LazyDict() self.connectok = threading.Event() self.reconnectcount = 0 self.cfg.nick = nick or self.cfg.nick or u'jsb' try: if not os.isdir(self.datadir): os.mkdir(self.datadir) except: pass self.setstate() self.outputlock = thread.allocate_lock() try: self.outqueue = Queue.PriorityQueue() self.eventqueue = Queue.PriorityQueue() except AttributeError: self.outqueue = Queue.Queue() self.eventqueue = Queue.Queue() self.laterqueue = Queue.Queue() self.encoding = self.cfg.encoding or "utf-8" self.cmndperms = getcmndperms() self.outputmorphs = outputmorphs self.inputmorphs = inputmorphs try: if nocbs: self.nocbs = nocbs.split(",") except ValueError: logging.error("cannot determine %s nocbs argument" % self.nocbs) self.lastiter = 0
def __init__(self, dbname=None, dbhost=None, dbuser=None, dbpasswd=None, dbtype=None, ddir=None, doconnect=True): self.datadir = ddir or getdatadir() self.datadir = self.datadir + os.sep + "db" + os.sep if hasattr(os, 'mkdir'): if not os.path.isdir(self.datadir): try: os.mkdir(self.datadir) except OSError: pass cfg = getmainconfig() self.dbname = dbname or cfg.dbname if not self.dbname: raise Exception("no db name") self.dbhost = dbhost or cfg.dbhost or "" self.dbuser = dbuser or cfg.dbuser or "" self.dbpasswd = dbpasswd or cfg.dbpasswd or "" self.connection = None self.timeout = 15 self.dbtype = dbtype or cfg.dbtype or 'sqlite' self.error = "" if doconnect: self.connect()
def handle_twitterfriends(bot, ievent): """ no arguments - show friends timeline (your normal twitter feed). """ if not go: ievent.reply( "the twitter plugin needs the credentials.py file in the .jsb/data/config dir. see .jsb/data/examples" ) return try: 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) method = getattr(twitter, "friends_timeline") result = method() res = [] for item in result: try: res.append("%s - %s" % (item.author.screen_name, item.text)) except Exception, ex: handle_exception() ievent.reply("results: ", res)
def handle_twittercmnd(bot, ievent): """ arguments: <API cmnd> - do a twitter API cmommand. """ if not go: ievent.reply("the twitter plugin needs the credentials.py file in the .jsb/data//config dir. see .jsb/data/examples") ; return if not ievent.args: ievent.missing('<API cmnd>') ; return target = strippedtxt(ievent.args[0]) try: from jsb.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(unicode(item.__getstate__())) except AttributeError: res.append(dir(i)) ; res.append(unicode(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, urllib2.HTTPError), e: ievent.reply('twitter failed: %s' % (str(e),))
def getfleet(datadir=None, new=False): if not datadir: from jsb.lib.datadir import getdatadir datadir = getdatadir() global fleet if not fleet or new: fleet = Fleet(datadir) return fleet
def enablelogging(botname, channel): """ set loglevel to level_name. """ global loggers global logfiles LOGDIR = initlog(getdatadir()) logging.warn("enabling on (%s,%s)" % (botname, channel)) channel = stripname(channel) logname = "%s_%s" % (botname, channel) #if logname in loggers: logging.warn("there is already a logger for %s" % logname) ; return logfile = LOGDIR + os.sep + logname + ".log" try: filehandler = logging.handlers.TimedRotatingFileHandler(logfile, 'midnight') formatter = logging.Formatter(format) filehandler.setFormatter(formatter) logfiles[logfile] = time.time() except IOError: filehandler = None chatlogger = logging.getLoggerClass()(logname) chatlogger.setLevel(logging.INFO) if chatlogger.handlers: for handler in chatlogger.handlers: chatlogger.removeHandler(handler) if filehandler: chatlogger.addHandler(filehandler) ; logging.warn("%s - logging enabled on %s" % (botname, channel)) else: logging.error("no file handler found - not enabling logging.") global lastlogger lastlogger = chatlogger loggers[logname] = lastlogger
def get_token(username): global users if not users: users = TwitterUsers(getdatadir() + os.sep + "twitter" + os.sep + "users") if not users: raise Exception("can't get twitter users object") return key, secret = getcreds(getdatadir()) if not key: raise Exception(getdatadir()) if not secret: raise Exception(getdatadir()) token = twittertoken(key, secret, users, username) if not token: raise Exception("%s %s" % (str(user), username)) api = twitterapi(key, secret, token) if not api: raise Exception("%s %s" % (str(user), name)) return (token, api)
def handle_twitterfriends(bot, ievent): """ do a twitter API cmommand. """ if not go: ievent.reply("the twitter plugin needs the credentials.py file in the .jsb/data/config dir. see .jsb/data/examples") return try: twitteruser = TwitterUsers("users") token = twitteruser.data.get(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 = oauth.OAuthToken(key, secret).from_string(token) twitter = twitterapi(key, secret, token) method = getattr(twitter, "friends_timeline") # do the thing result = method() res = [] for item in result: try: res.append("%s - %s" % (item.author.screen_name, item.text)) #logging.warn("twitter - %s" % dir(item.author)) #res.append(unicode(item.__getstate__())) except Exception, ex: handle_exception() ievent.reply("results: ", res)
def __init__(self, *args, **kwargs): self.plugname = calledfrom(sys._getframe()) logging.debug('persiststate - initialising %s' % self.plugname) PersistState.__init__( self, getdatadir() + os.sep + 'state' + os.sep + 'plugs' + os.sep + self.plugname + os.sep + 'state')
def handle_twitter_confirm(bot, ievent): """ arguments: <PIN code> - confirm auth with PIN. """ from jsb.utils.twitter import go if not go: ievent.reply( "the twitter plugin needs the credentials.py file in the %s/config dir. see .jsb/data/examples" % getdatadir()) return pin = ievent.args[0] if not pin: ievent.missing("<PIN> .. see the twitter-auth command.") return try: access_token = getauth(getdatadir()).get_access_token(pin) except (tweepy.TweepError, urllib2.HTTPError), e: ievent.reply('twitter failed: %s' % (str(e), )) return
def getversion(txt=""): """ return a version string. """ try: tip = open(getdatadir() + os.sep + "TIP", 'r').read() except: tip = None if tip: version2 = version + " " + tip else: version2 = version if txt: return "JSONBOT %s %s" % (version2, txt) else: return "JSONBOT %s" % version2
def markovlearnlog(chan): """ learn a log """ lines = 0 logfiles = os.listdir(getdatadir() + os.sep + 'chatlogs') for filename in logfiles: if chan[1:] not in filename: continue logging.warn("opening %s" % filename) for line in open(getdatadir() + os.sep + 'chatlogs' + os.sep + filename, 'r'): if lines % 10 == 0: time.sleep(0.001) if not line: continue lines += 1 try: txt = ' '.join(line.strip().split()[2:]) # log format is: 2011-08-07 00:02:16 <botfather> love, peace and happiness markovtalk_learn(txt) except IndexError: continue logging.warn('learning %s log done. %s lines' % (chan, lines)) return lines
def __init__(self): Pdod.__init__( self, os.path.join( getdatadir() + os.sep + 'plugs' + os.sep + 'jsb.plugs.sockets.mpd', 'mpd')) self.running = False self.lastsong = -1
def handle_twitter_confirm(bot, ievent): """ arguments: <PIN code> - confirm auth with PIN. """ if not go: ievent.reply("the twitter plugin needs the credentials.py file in the %s/config dir. see .jsb/data/examples" % getdatadir()) ; return pin = ievent.args[0] if not pin: ievent.missing("<PIN> .. see the twitter-auth command.") ; return try: access_token = getauth(getdatadir()).get_access_token(pin) except (TweepError, urllib2.HTTPError), e: ievent.reply('twitter failed: %s' % (str(e),)) ; return twitteruser = TwitterUsers("users") twitteruser.add(ievent.user.data.name, access_token.to_string()) ievent.reply("access token saved.")
def getplus(target): credentials = _import_byfile("credentials", getdatadir() + os.sep + "config" + os.sep + "credentials.py") url = "https://www.googleapis.com/plus/v1/people/%s/activities/public?alt=json&pp=1&key=%s" % (target, credentials.googleclient_apikey) result = geturl2(url) data = json.loads(result) res = [] for item in data['items']: i = LazyDict(item) res.append("%s - %s - %s" % (i.actor['displayName'], i['title'], item['url'])) return res
def handle_twittercmnd(bot, ievent): """ arguments: <API cmnd> - do a twitter API cmommand. """ if not go: ievent.reply( "the twitter plugin needs the credentials.py file in the .jsb/data//config dir. see .jsb/data/examples" ) return if not ievent.args: ievent.missing('<API cmnd>') return target = strippedtxt(ievent.args[0]) try: from jsb.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(unicode(item.__getstate__())) except AttributeError: res.append(dir(i)) res.append(unicode(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, urllib2.HTTPError), e: ievent.reply('twitter failed: %s' % (str(e), ))
def getcreds(datadir=None): if not datadir: datadir = getdatadir() try: mod = _import_byfile("credentials", datadir + os.sep + "config" + os.sep + "credentials.py") global go go = True except (IOError, ImportError): logging.warn("the twitter plugin needs the credentials.py file in the %s/config dir. see %s/examples" % (datadir, datadir)) return (None, None) logging.warn("found credentials.py") return mod.CONSUMER_KEY, mod.CONSUMER_SECRET
def handle_twitter_auth(bot, ievent): """ get auth url. """ if not go: ievent.reply("the twitter plugin needs the credentials.py file in the .jsb/data/config dir. see .jsb/data/examples") return try: auth_url = getauth(getdatadir()).get_authorization_url() except (TweepError, urllib2.HTTPError), e: ievent.reply('twitter failed: %s' % (str(e),)) return
def handle_twitter_auth(bot, ievent): """ no arguments - get url to get the auth PIN needed for the twitter-confirm command. """ if not go: ievent.reply("the twitter plugin needs the credentials.py file in the .jsb/data/config dir. see .jsb/data/examples") ; return try: auth_url = getauth(getdatadir()).get_authorization_url() except (TweepError, urllib2.HTTPError), e: ievent.reply('twitter failed: %s' % (str(e),)) ; return if bot.type == "irc": bot.say(ievent.nick, "sign in at %s" % auth_url) bot.say(ievent.nick, "use the provided code in the twitter-confirm command.") else: ievent.reply("sign in at %s" % auth_url) ievent.reply("use the provided code in the twitter-confirm command.")
def outnocb(self, waveid, txt, result=[], event=None, origin="", dot=", ", *args, **kwargs): """ output to the root id. """ if not self.domain in self._server_rpc_base: credentials = _import_byfile("credentials", getdatadir() + os.sep + "config" + os.sep + "credentials.py") rpc_base = credentials.RPC_BASE[waveid.split("!")[0]] self._server_rpc_base = rpc_base logging.warn("%s - %s - server_rpc_base is %s" % (self.name, waveid, self._server_rpc_base)) if not event: logging.error("wave - event not set - %s" % calledfrom(sys._getframe(0))) logging.warn("wave - creating new event.") wave = Wave(waveid) wave.say(self, txt)
def mcboot(): if not getmainconfig().memcached: logging.warn("memcached is disabled") ; return try: import jsb.contrib.memcache as memcache rundir = getdatadir() + os.sep + 'run' sock = os.path.abspath(rundir + os.sep + "memcached.socket") global mc mc = memcache.Client(["unix:%s" % sock], debug=0) return isactive(sock) except ImportError, ex: logging.warn("using builtin cache - %s" % str(ex)) except Exception, ex: logging.warn("error starting memcached client: %s" % str(ex))
def getaliases(ddir=None, force=True): """ return global aliases. """ global aliases if not aliases or force: from jsb.lib.persist import Persist from jsb.utils.lazydict import LazyDict d = ddir or getdatadir() p = Persist(d + os.sep + "run" + os.sep + "aliases") if not p.data: p.data = LazyDict() aliases = p.data return aliases
def savealiases(ddir=None): """ return global aliases. """ global aliases if aliases: logging.warn("saving aliases") from jsb.lib.persist import Persist from jsb.utils.lazydict import LazyDict d = ddir or getdatadir() p = Persist(d + os.sep + "run" + os.sep + "aliases") p.data = aliases p.save() return aliases
def markovlearnlog(chan): """ learn a log """ lines = 0 logfiles = os.listdir(getdatadir() + os.sep + 'chatlogs') for filename in logfiles: if chan[1:] not in filename: continue logging.warn("opening %s" % filename) for line in open( getdatadir() + os.sep + 'chatlogs' + os.sep + filename, 'r'): if lines % 10 == 0: time.sleep(0.001) if not line: continue lines += 1 try: txt = ' '.join( line.strip().split()[2:] ) # log format is: 2011-08-07 00:02:16 <botfather> love, peace and happiness markovtalk_learn(txt) except IndexError: continue logging.warn('learning %s log done. %s lines' % (chan, lines)) return lines
def mcboot(): if not getmainconfig().memcached: return logging.warn("memcached is enabled") try: import jsb.contrib.memcache as memcache rundir = getdatadir() + os.sep + 'run' sock = os.path.abspath(rundir + os.sep + "memcached.socket") global mc mc = memcache.Client(["unix:%s" % sock], debug=0) return isactive(sock) except ImportError, ex: logging.warn("using builtin cache - %s" % str(ex)) except Exception, ex: logging.warn("error starting memcached client: %s" % str(ex))
def __init__(self, name, url="", owner="", itemslist=['title', 'link'], watchchannels=[], running=1): filebase = getdatadir() + os.sep + 'plugs' + os.sep + 'jsb.plugs.wave.hubbub' + os.sep + name Persist.__init__(self, filebase + os.sep + name + '.core') if not self.data: self.data = {} self.data = LazyDict(self.data) self.data['name'] = self.data.name or str(name) self.data['url'] = self.data.url or str(url) self.data['owner'] = self.data.owner or str(owner) self.data['watchchannels'] = self.data.watchchannels or list(watchchannels) self.data['running'] = self.data.running or running self.itemslists = Pdol(filebase + os.sep + name + '.itemslists') self.markup = Pdod(filebase + os.sep + name + '.markup')
def handle_twitter_auth(bot, ievent): """ get auth url. """ if not go: ievent.reply( "the twitter plugin needs the credentials.py file in the .jsb/data/config dir. see .jsb/data/examples" ) return try: auth_url = getauth(getdatadir()).get_authorization_url() except (TweepError, urllib2.HTTPError), e: ievent.reply('twitter failed: %s' % (str(e), )) return
def handle_twitter_auth(bot, ievent): """ no arguments - get url to get the auth PIN needed for the twitter-confirm command. """ from jsb.utils.twitter import go if not go: ievent.reply( "the twitter plugin needs the credentials.py file in the .jsb/data/config dir. see .jsb/data/examples" ) return try: auth_url = getauth(getdatadir()).get_authorization_url() except (tweepy.TweepError, urllib2.HTTPError), e: ievent.reply('twitter failed: %s' % (str(e), )) return
def api_log(bot, event): channel = event.upath.split("/")[-1] if not channel: event.error(500) ; return logging.warn("in api_log function .. channel is %s" % channel) LOGDIR = initlog(getdatadir()) channel = stripname(channel) logname = "%s_-%s.log" % ("default-irc", channel) logfile = LOGDIR + os.sep + logname f = open(logfile, "r") logdata = f.read() event.reply(logdata) event.finish() f.close()
def postmsg(username, txt): try: result = splittxt(txt, 139) twitteruser = TwitterUsers("users") key, secret = getcreds(getdatadir()) token = twittertoken(key, secret, twitteruser, username) if not token: raise TweepError("Can't get twitter token") twitter = twitterapi(key, secret, token) for txt in result: status = twitter.update_status(txt) logging.info("logged %s tweets for %s" % (len(result), username)) except TweepError, ex: logging.error("twitter - error: %s" % str(ex))
def getplus(target): credentials = _import_byfile( "credentials", getdatadir() + os.sep + "config" + os.sep + "credentials.py") url = "https://www.googleapis.com/plus/v1/people/%s/activities/public?alt=json&pp=1&key=%s" % ( target, credentials.googleclient_apikey) result = geturl2(url) data = json.loads(result) res = [] for item in data['items']: i = LazyDict(item) res.append("%s - %s - %s" % (i.actor['displayName'], i['title'], item['url'])) return res
def postmsg(username, txt): try: result = splittxt(txt, 139) twitteruser = TwitterUsers("users") key, secret = getcreds(getdatadir()) token = twittertoken(key, secret, twitteruser, username) if not token: raise TweepError("Can't get twitter token") twitter = twitterapi(key, secret, token) for txt in result: status = twitter.update_status(txt) logging.info("logged %s tweets for %s" % (len(result), username)) except TweepError, ex: logging.error("twitter - error: %s" % str(ex)) return len(result)
def getcreds(datadir=None): if not datadir: datadir = getdatadir() try: mod = _import_byfile( "credentials", datadir + os.sep + "config" + os.sep + "credentials.py") global go go = True except (IOError, ImportError): logging.warn( "the twitter plugin needs the credentials.py file in the %s/config dir. see %s/examples" % (datadir, datadir)) return (None, None) logging.warn("found credentials.py") return mod.CONSUMER_KEY, mod.CONSUMER_SECRET
def handle_twitter_confirm(bot, ievent): """ confirm auth with PIN. """ if not go: ievent.reply("the twitter plugin needs the credentials.py file in the %s/config dir. see .jsb/data/examples" % getdatadir()) return pin = ievent.args[0] if not pin: ievent.missing("<PIN> .. see the twitter-auth command.") return try: access_token = getauth(getdatadir()).get_access_token(pin) except (TweepError, urllib2.HTTPError), e: ievent.reply('twitter failed: %s' % (str(e),)) return
def __init__(self, id, botname=None, type="notset"): if not id: raise NoChannelSet() if not botname: Persist.__init__(self, getdatadir() + os.sep + 'channels' + os.sep + stripname(id)) else: Persist.__init__(self, getdatadir() + os.sep + 'fleet' + os.sep + botname + os.sep + 'channels' + os.sep + stripname(id)) self.id = id self.type = type self.lastmodified = time.time() self.data.id = id self.data.silentcommands = self.data.silentcommands or [] self.data.allowcommands = self.data.allowcommands or [] self.data.feeds = self.data.feeds or [] self.data.forwards = self.data.forwards or [] self.data.allowwatch = self.data.allowwatch or [] self.data.watched = self.data.watched or [] self.data.passwords = self.data.passwords or {} self.data.cc = self.data.cc or "!" self.data.nick = self.data.nick or "jsb" self.data.key = self.data.key or "" self.data.denyplug = self.data.denyplug or [] self.data.createdfrom = whichmodule() self.data.cacheindex = 0 self.data.tokens = self.data.tokens or [] self.data.webchannels = self.data.webchannels or [] logging.debug("channelbase - created channel %s" % id)
def handle_chatlogsearch(bot, event): """ arguments: <searchtxt> - search in the logs. """ if not event.rest: event.missing("<searchtxt>") ; return result = [] chatlogdir = getdatadir() + os.sep + "chatlogs" chan = event.options.channel or event.channel logs = os.listdir(chatlogdir) logs.sort() for f in logs: filename = stripname(f) if not chan[1:] in filename: continue for line in open(chatlogdir + os.sep + filename, 'r'): if event.rest in line: result.append(line) if result: event.reply("search results for %s: " % event.rest, result, dot= " || ") else: event.reply("no result found for %s" % chan)
def __init__(self): brainfile = open(os.path.join(getdatadir() + os.sep + 'plugs' + os.sep + 'myplugs.common.pr', 'brain'), 'r') self.guesser = {} currentguesser='' print "" #just a new line for line in brainfile.readlines(): if (line.startswith('!')): currentguesser=line.strip()[1:] self.guesser[currentguesser] = Bayes() if line.__contains__(":"): s=line.strip().split(':') self.guesser[currentguesser].train(s[0].strip().lower(),s[1].strip().lower()) brainfile.close()
def setloglevel(level_name="warn", colors=True, datadir=None): """ set loglevel to level_name. """ if not level_name: return global level global filehandler LOGDIR = init(getdatadir()) format_short = "\033[1m%(asctime)-8s\033[0m -=- %(levelname)-8s -=- \033[93m%(message)-75s\033[0m -=- \033[92m%(module)s.%(funcName)s.%(lineno)s\033[0m -=- \033[94m%(threadName)s\033[0m" format_short_plain = "%(asctime)-8s -=- %(levelname)-8s -=- %(message)-75s -=- %(module)s.%(funcName)s.%(lineno)s -=- %(threadName)s" datefmt = '%H:%M:%S' formatter_short = logging.Formatter(format_short, datefmt=datefmt) formatter_short_plain = logging.Formatter(format_short_plain, datefmt=datefmt) try: filehandler = logging.handlers.TimedRotatingFileHandler(LOGDIR + os.sep + "jsb.log", 'midnight') except (IOError, AttributeError), ex: logging.error("can't create file loggger %s" % str(ex)) filehandler = None
def markovlearnspider(target): logging.warn("starting spider learn on %s" % target) coll = PersistCollection(getdatadir() + os.sep + 'spider' + os.sep + "data") if target.startswith("spider://"): target = target[9:] objs = coll.search('url', target) for obj in objs: if not obj.data and obj.data.url: print "skip - no url" ; continue time.sleep(0.001) if target not in obj.data.url: continue logging.warn("url is %s" % obj.data.url) try: if obj.data and obj.data.txt: for line in obj.data.txt.split("\n"): if line.count(";") > 1: continue markovtalk_learn(striphtml(line)) except: handle_exception()
def handle_chatlogsearch(bot, event): """ arguments: <searchtxt> - search in the logs. """ if not event.rest: event.missing("<searchtxt>") ; return result = [] chatlogdir = getdatadir() + os.sep + "chatlogs" if event.options and event.options.channel: chan = event.options.channel else: chan = event.channel logs = os.listdir(chatlogdir) logs.sort() for f in logs: filename = stripname(f) if not chan[1:] in filename: continue for line in open(chatlogdir + os.sep + filename, 'r'): if event.rest in line: result.append(line) if result: event.reply("search results for %s: " % event.rest, result, dot= " || ") else: event.reply("no result found for %s" % chan)
def killmcdaemon(): if not getmainconfig().memcached: return logging.warn("killing memcached daemon") rundir = getdatadir() + os.sep + 'run' sock = os.path.abspath(rundir + os.sep + "memcached.socket") pidfile = sock[:-7] + ".pid" try: pid = int(open(pidfile, "r").read().strip()) except Exception,ex : logging.warn("can't determine pid of memcached from %s - %s" % (pidfile,str(ex))) ; return False logging.warn("pid is %s" % pid) data = isactive(sock) if not data: logging.warn("memcached is not runniing") ; return False try: curr_connections = int(data[0][1]["curr_connections"]) except Exception, ex: logging.warn("can't determine current connections of memcached .. not killing - %s" % str(ex)) ; return False if curr_connections and curr_connections != 1: logging.warn("current connections of memcached is %s .. not killing" % curr_connections) ; return False try: os.kill(pid, 15) ; logging.warn("killed memcached with pid %s" % pid) except Exception, ex: logging.warn("failed to kill memcached (%s) - %s" % (pid, str(ex))) try: os.remove(pidfile) except: pass