Skip to content

Instantly share code, notes, and snippets.

from twisted.application import internet, service
from twisted.words.protocols import irc
from twisted.internet import protocol
class Bot(irc.IRCClient):
nickname = "SocPuppet"
def connectionMade(self):
irc.IRCClient.connectionMade(self)
print "Connected"
from twisted.words.protocols import irc
from twisted.internet import protocol, reactor
import traceback
class Bot(irc.IRCClient):
nickname = "SocPuppet"
def try_except(self, func, *args):
try:
return func(*args)
from collections import defaultdict
class Manager(object):
def __init__(self):
self.triggers = defaultdict(list) # event: [func, ]
def register(self, *events):
def register_for_event(func):
for event in events:
self.triggers[event.lower()].append(func)
return func
c = ConfigObj("socuppet.conf")
p = PluginManager()
f = BotFactory(c, p)
if "servers" in c:
for name, server in c['servers'].iteritems():
botService = internet.TCPClient(server["host"], 6667, f)
botService.startService()
reactor.addSystemEventTrigger('before', 'shutdown', botService.stopService)
class BotFactory(protocol.ReconnectingClientFactory):
protocol = Bot
def __init__(self, config, name, state):
self.config = config
self.name = name
self.sharedstate = state
self.quitted = False
def clientConnectionLost(self, connector, unused_reason):
d = defer.maybeDeferred(func, self, user, channel, msg)
d.addErrback(self.printerror)
d.addCallback(self.msg, channel)
def msg(self, user, message):
pass
# user ends up having the message, and message the target.
@EntityReborn
EntityReborn / start.py
Created May 1, 2011 20:42
bootstrap for a restartable python process.
# The only requirements are that the process ends with a specific return code (7 in this case)
import os
import sys
import subprocess
class main(object):
def __init__(self, file, cwd):
self.file = os.path.abspath(file)
self.cwd = os.path.abspath(cwd)
import hashlib
class User(object):
def __init__(self):
self.password = ""
def set_password(self, raw_password):
hash = "pass" # randomly generate this
m = hashlib.sha1(raw_password + hash)
self.password = "%s$%s" % (m.hexdigest(), hash)
import hashlib
class Klass(object):
def func(self):
"""Ohai from Klass.func"""
print self.func.__doc__
def Func():
"""Ohai from __main__.Func"""
print Func.__doc__
from collections import defaultdict
class Manager(object):
def __init__(self):
self.strings = defaultdict(list) # { stringname: [func1, func2, ...], ... }
def deco(self, string):
def decofunc(func):
self.strings[string].append(func)
return func