statdict = Statdict() s = create_session() whokarma = s.query(WhoKarma).filter(WhoKarma.nick==nick).filter(WhoKarma.updown=='down') if not whokarma: return [] for i in whokarma: statdict.upitem(i.item) return statdict.top() karma = KarmaDb() Base.metadata.create_all() assert(karma) ratelimited = [] limiterlock = thread.allocate_lock() limlock = lockdec(limiterlock) nolimiter = False def size(): """ return number of kamra items """ return karma.size() def search(what, queue): rlog(10, 'karma', 'searched for %s' % what) result = karma.search(what) for i in result: queue.put_nowait("%s has karma %s" % (i[0], i[1])) @limlock def ratelimit(bot, ievent): """ karma rate limiter """
# gozerbot/pdod.py # # """ pickled dicts of dicts """ __copyright__ = 'this file is in the public domain' from gozerbot.generic import lockdec from persist import Persist import thread pdodlock = thread.allocate_lock() locked = lockdec(pdodlock) class Pdod(Persist): """ pickled dicts of dicts """ def __init__(self, filename): Persist.__init__(self, filename) if not self.data: self.data = {} def __getitem__(self, name): """ return item with name """ if self.data.has_key(name): return self.data[name] @locked def save(self):
# """ database interface """ __copyright__ = 'this file is in the public domain' # gozerbot imports from gozerbot.config import config from gozerbot.generic import rlog, lockdec, tolatin1, handle_exception from gozerbot.datadir import datadir # basic imports import thread, os, time, types # locks dblock = thread.allocate_lock() dblocked = lockdec(dblock) class Db(object): """ this class implements a database connection. it connects to the database on initialisation. """ def __init__(self, doconnect=True, dbtype=None, ddir=None, config=config): self.datadir = ddir or datadir if not os.path.isdir(self.datadir + os.sep + 'db/'): os.mkdir(self.datadir + os.sep + 'db/') self.config = config self.dbname = self.config['dbname'] or "" self.dbhost = self.config['dbhost'] or "" self.dbuser = self.config['dbuser'] or "" self.dbpasswd = self.config['dbpasswd'] or ""
from gozerbot.persist.pdod import Pdod from gozerbot.datadir import datadir from gozerbot.fleet import fleet from gozerbot.botbase import BotBase from gozerbot.plugins import plugins from gozerbot.threads.thr import start_new_thread, threaded from gozerbot.periodical import periodical from gozerbot.morphs import inputmorphs, outputmorphs from ircevent import Ircevent # basic imports import time, thread, socket, threading, os, Queue, random # locks outlock = thread.allocate_lock() outlocked = lockdec(outlock) # exceptions class AlreadyConnected(Exception): """ already connected exception """ pass class AlreadyConnecting(Exception): """ bot is already connecting exception """ pass
from gozerbot.commands import cmnds from gozerbot.callbacks import callbacks, jcallbacks from gozerbot.persist.persistconfig import PersistConfig from gozerbot.generic import hourmin, rlog, lockdec from gozerbot.monitor import outmonitor, jabbermonitor from gozerbot.plughelp import plughelp from gozerbot.examples import examples from gozerbot.irc.ircevent import Ircevent from gozerbot.fleet import fleet import time, os, thread plughelp.add('supylog', 'log irc channels in supybot channellogger format') outlock = thread.allocate_lock() outlocked = lockdec(outlock) cfg = PersistConfig() cfg.define('channels', []) cfg.define('logtimestamp', '%Y-%m-%dT%H:%M:%S') cfg.define('filenametimestamp', '%d-%a-%Y') cfg.define('nologprefix', '[nolog]') cfg.define('nologmsg', '-= THIS MESSAGE NOT LOGGED =-') logfiles = {} stopped = False nonchanevents = ['NICK','NOTICE','QUIT'] if not os.path.isdir('logs'): os.mkdir('logs')
whokarma = s.query(WhoKarma).filter(WhoKarma.nick == nick).filter( WhoKarma.updown == 'down') if not whokarma: return [] for i in whokarma: statdict.upitem(i.item) return statdict.top() karma = KarmaDb() Base.metadata.create_all() assert (karma) ratelimited = [] limiterlock = thread.allocate_lock() limlock = lockdec(limiterlock) nolimiter = False def size(): """ return number of kamra items """ return karma.size() def search(what, queue): rlog(10, 'karma', 'searched for %s' % what) result = karma.search(what) for i in result: queue.put_nowait("%s has karma %s" % (i[0], i[1]))
from gozerbot.examples import examples from gozerbot.callbacks import callbacks, jcallbacks from gozerbot.plughelp import plughelp from gozerbot.monitor import saymonitor, jabbermonitor from gozerbot.aliases import aliases, aliasdel from gozerbot.users import users import glob, re, thread, pickle, os, mmap, time plughelp.add('log', 'logs related commands') # check if logs dir exists if not create it if not os.path.isdir('logs'): os.mkdir('logs') loglock = thread.allocate_lock() locked = lockdec(loglock) class Logs(object): """ hold handles to log files (per channel) """ def __init__(self, logdir): self.logdir = logdir self.maps = {} self.files = {} self.filenames = {} self.lock = thread.allocate_lock() self.loglist = [] try: loglistfile = open(self.logdir + os.sep + 'loglist', 'r') self.loglist = pickle.load(loglistfile)
## UPGRADE PART def upgrade(): try: convertpickle(datadir + os.sep + 'remind', datadir + os.sep + 'plugs' + \ os.sep + 'remind' + os.sep + 'remind') except: pass ## END UPGRADE PART remindlock = thread.allocate_lock() rlocked = lockdec(remindlock) class Remind(Pdol): """ remind object """ def __init__(self, name): Pdol.__init__(self, name) @rlocked def add(self, who, data): """ add a remind txt """ self[who] = data self.save() def wouldremind(self, userhost): """ check if there is a remind for userhost """
# gozerbot/rest/client.py # # """ Rest Client class """ from gozerbot.generic import geturl3, geturl4, posturl, deleteurl, rlog, \ handle_exception, useragent, lockdec, exceptionmsg from simplejson import loads from urllib2 import HTTPError, URLError from httplib import InvalidURL from urlparse import urlparse import socket, asynchat, urllib, sys, thread, re, asyncore restlock = thread.allocate_lock() locked = lockdec(restlock) class RestResult(object): def __init__(self, url="", name=""): self.url = url self.name = name self.data = None self.error = None self.status = None self.reason = "" class RestClient(object): def __init__(self, url, keyfile=None, certfile=None, port=None): if not url.endswith('/'):
""" jabber message definition .. types can be normal, chat, groupchat, headline or error """ __copyright__ = 'this file is in the public domain' # gozerbot imports from gozerbot.generic import rlog, toenc, fromenc, jabberstrip, makeargrest, lockdec from gozerbot.eventbase import EventBase # basic imports import xmpp, types, time, thread # locks replylock = thread.allocate_lock() replylocked = lockdec(replylock) class Jabbermsg(xmpp.Message, EventBase): """ jabber message object. """ def __init__(self, msg=None): if msg: xmpp.Message.__init__(self, node=str(msg)) else: xmpp.Message.__init__(self) EventBase.__init__(self) if msg:
from jabbermsg import Jabbermsg from jabberpresence import Jabberpresence from jabberiq import Jabberiq # xmpp imports from xmpp.simplexml import Node import xmpp # basic imports import time, Queue, os, threading, thread, types, xml, re # locks jabberoutlock = thread.allocate_lock() jabberinlock = thread.allocate_lock() connectlock = thread.allocate_lock() outlocked = lockdec(jabberoutlock) inlocked = lockdec(jabberinlock) connectlocked = lockdec(connectlock) class Jabberbot(BotBase): """ jabber bot class. """ def __init__(self, cfg): BotBase.__init__(self, cfg) if not self.port: self.port = 5222 self.type = 'jabber' self.outqueue = Queue.Queue() self.sock = None self.me = None self.lastin = None
def __init__(self): self.data = {} def add(self, plugin, version): """ add plugin version """ self.data[plugin] = version #self.save() def list(self): """ list plugin versions """ return self.data cfg = Cfg() installlock = thread.allocate_lock() locked = lockdec(installlock) plughelp.add('install', 'install plugin from remote site') installsites = ['http://gozerplugs.blinkenlights.nl'] class InstallerException(Exception): pass class Installer(object): ''' We're taking care of installing and verifying plugins. '''
from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.ext.orderinglist import ordering_list from sqlalchemy import Text, Integer, Sequence, ForeignKey, DateTime from sqlalchemy import create_engine, Column, String, Table from sqlalchemy.orm import scoped_session, sessionmaker, relation, create_session # basic imports import sqlalchemy, thread, os, time, logging # debug settings if config['debug']: logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) # locks alchemylock = thread.allocate_lock() dblocked = lockdec(alchemylock) # vars Base = declarative_base() Session = None engine = None def geturi(ddir=None, mainconfig=None): """ determine database URI from config file """ d = ddir or datadir # set config file if mainconfig: config = mainconfig
""" jabber message definition .. types can be normal, chat, groupchat, headline or error """ __copyright__ = 'this file is in the public domain' # gozerbot imports from gozerbot.generic import rlog, toenc, fromenc, jabberstrip, makeargrest, lockdec from gozerbot.eventbase import EventBase # basic imports import xmpp, types, time, thread # locks replylock = thread.allocate_lock() replylocked = lockdec(replylock) class Jabbermsg(xmpp.Message, EventBase): """ jabber message object. """ def __init__(self, msg=None): if msg: xmpp.Message.__init__(self, node=str(msg)) else: xmpp.Message.__init__(self) EventBase.__init__(self) if msg: self.orig = msg
plughelp.add('remind', 'check if user says something if so do /msg') ## UPGRADE PART def upgrade(): try: convertpickle(datadir + os.sep + 'remind', datadir + os.sep + 'plugs' + \ os.sep + 'remind' + os.sep + 'remind') except: pass ## END UPGRADE PART remindlock = thread.allocate_lock() rlocked = lockdec(remindlock) class Remind(Pdol): """ remind object """ def __init__(self, name): Pdol.__init__(self, name) @rlocked def add(self, who, data): """ add a remind txt """ self[who] = data self.save() def wouldremind(self, userhost):
except IOError, ex: if 'No such file' in str(ex): rlog(10, 'todo', 'nothing to upgrade') except Exception, ex: rlog(10, 'todo', "can't upgrade .. reason: %s" % str(ex)) handle_exception() else: rlog(10, 'quote', "upgraded %s items" % teller) ## END UPGRADE PART plughelp.add('todo', 'todo lists') todolock = thread.allocate_lock() locked = lockdec(todolock) class TodoDb(object): """ database todo interface """ @trans def reset(self, session, name): item = session.query(Todo).filter(Todo.name == name.lower).first() session.delete(item) return 1 def size(self): """ return number of todo's """ s = create_session() count = s.query(sa.func.count(Todo.indx)).first()[0] return count
result.append(j) def checkfordate(data, date): for item in data: try: d = item["updated"] except KeyError: continue if date == d: return True return False rsslock = thread.allocate_lock() locked = lockdec(rsslock) class RssException(Exception): pass class Rss301(RssException): pass class RssStatus(RssException): pass class RssBozoException(RssException):
teller += 1 except IOError, ex: if 'No such file' in str(ex): rlog(10, 'todo', 'nothing to upgrade') except Exception, ex: rlog(10, 'todo', "can't upgrade .. reason: %s" % str(ex)) handle_exception() else: rlog(10, 'quote', "upgraded %s items" % teller) ## END UPGRADE PART plughelp.add('todo', 'todo lists') todolock = thread.allocate_lock() locked = lockdec(todolock) class TodoDb(object): """ database todo interface """ @trans def reset(self, session, name): item = session.query(Todo).filter(Todo.name==name.lower).first() session.delete(item) return 1 def size(self): """ return number of todo's """ s = create_session() count = s.query(sa.func.count(Todo.indx)).first()[0]
__copyright__ = 'this file is in the public domain' from gozerbot.stats import stats from gozerbot.generic import handle_exception, rlog, lockdec import threading, re, time, thread dontshowthreads = [ 'Periodical.runjob', ] # regular expression to determine thread name methodre = re.compile('method\s+(\S+)', re.I) funcre = re.compile('function\s+(\S+)', re.I) threadlock = thread.allocate_lock() locked = lockdec(threadlock) class Botcommand(threading.Thread): """ thread for running bot commands .. give feedback of exceptions to ircevent argument (second after bot) """ def __init__(self, group, target, name, args, kwargs): threading.Thread.__init__(self, None, target, name, args, kwargs) self.name = name self.ievent = args[1] self.setDaemon(True) def run(self): """ run the bot command """ try: rlog(10, 'thr', 'running bot command thread %s' % self.name)
from gozerbot.examples import examples from gozerbot.callbacks import callbacks, jcallbacks from gozerbot.plughelp import plughelp from gozerbot.monitor import saymonitor, jabbermonitor from gozerbot.aliases import aliases, aliasdel from gozerbot.users import users import glob, re, thread, pickle, os, mmap, time plughelp.add('log', 'logs related commands') # check if logs dir exists if not create it if not os.path.isdir('logs'): os.mkdir('logs') loglock = thread.allocate_lock() locked = lockdec(loglock) class Logs(object): """ hold handles to log files (per channel) """ def __init__(self, logdir): self.logdir = logdir self.maps = {} self.files = {} self.filenames = {} self.lock = thread.allocate_lock() self.loglist = [] try: loglistfile = open(self.logdir + os.sep + 'loglist', 'r') self.loglist = pickle.load(loglistfile) except:
result.append(j) def checkfordate(data, date): for item in data: try: d = item['updated'] except KeyError: continue if date == d: return True return False rsslock = thread.allocate_lock() locked = lockdec(rsslock) class RssException(Exception): pass class Rss301(RssException): pass class RssStatus(RssException): pass class RssBozoException(RssException):
""" own threading wrapper """ __copyright__ = 'this file is in the public domain' from gozerbot.stats import stats from gozerbot.generic import handle_exception, rlog, lockdec import threading, re, time, thread dontshowthreads = ['Periodical.runjob', ] # regular expression to determine thread name methodre = re.compile('method\s+(\S+)', re.I) funcre = re.compile('function\s+(\S+)', re.I) threadlock = thread.allocate_lock() locked = lockdec(threadlock) class Botcommand(threading.Thread): """ thread for running bot commands .. give feedback of exceptions to ircevent argument (second after bot) """ def __init__(self, group, target, name, args, kwargs): threading.Thread.__init__(self, None, target, name, args, kwargs) self.name = name self.ievent = args[1] self.setDaemon(True) def run(self): """ run the bot command """ try:
rlog(10, 'infoitem', 'nothing to upgrade') except Exception, ex: rlog(10, 'infoitem', "can't upgrade: %s" % str(ex)) return rlog(10, 'infoitem', 'upgraded %s infoitems' % str(teller)) return teller import thread, os, time plughelp.add('infoitem', 'also known as factoids .. info can be retrieved \ by keyword or searched') infolock = thread.allocate_lock() # create lock descriptor locked = lockdec(infolock) class InfoitemsDb(object): """ information items """ @trans def add(self, session, item, description, userhost, ttime): """ add an item """ item = item.lower() result = 0 try: newitem = InfoItems(item, description, userhost, datetime.fromtimestamp(ttime)) session.add(newitem) result = 1 except Exception, ex:
from gozerbot.fleet import fleet from gozerbot.examples import examples from gozerbot.threads.thr import start_new_thread from gozerbot.plughelp import plughelp from gozerbot.persist.persistconfig import PersistConfig import os, time, thread plughelp.add('shoutcast', 'query a shoutcast server or periodically watch \ them') cfg = PersistConfig() cfg.define('scwatch', 0) cfg.define('nodes', []) sclock = thread.allocate_lock() locked = lockdec(sclock) class SCwatcher(object): def __init__(self): self.stop = False self.songsplayed = [] def run(self): self.starttime = int(time.time()) res = "" while cfg.get('scwatch') and not self.stop: time.sleep(1) if self.stop: break godo = []
""" database interface """ __copyright__ = "this file is in the public domain" # gozerbot imports from gozerbot.config import config from gozerbot.generic import rlog, lockdec, tolatin1, handle_exception from gozerbot.datadir import datadir # basic imports import thread, os, time, types # locks dblock = thread.allocate_lock() dblocked = lockdec(dblock) class Db(object): """ this class implements a database connection. it connects to the database on initialisation. """ def __init__(self, doconnect=True, dbtype=None, ddir=None, config=config): self.datadir = ddir or datadir if not os.path.isdir(self.datadir + os.sep + "db/"): os.mkdir(self.datadir + os.sep + "db/") self.config = config self.dbname = self.config["dbname"] or "" self.dbhost = self.config["dbhost"] or ""
from jabbermsg import Jabbermsg from jabberpresence import Jabberpresence from jabberiq import Jabberiq # xmpp imports from xmpp.simplexml import Node import xmpp # basic imports import time, Queue, os, threading, thread, types, xml, re # locks jabberoutlock = thread.allocate_lock() jabberinlock = thread.allocate_lock() connectlock = thread.allocate_lock() outlocked = lockdec(jabberoutlock) inlocked = lockdec(jabberinlock) connectlocked = lockdec(connectlock) class Jabberbot(BotBase): """ jabber bot class. """ def __init__(self, cfg): BotBase.__init__(self, cfg) if not self.port: self.port = 5222 self.type = 'jabber' self.outqueue = Queue.Queue() self.sock = None self.me = None