#!/usr/bin/python2.4 from ConfigParser import ConfigParser routes = ConfigParser() routes.read('/home/rubys/.xnotify') private_only = False from glob import glob import os, sys, time, xml.dom, re from xml.sax.saxutils import escape sys.path.insert(0,'/home/rubys/pydns-2.3.1') sys.path.insert(0,'/home/rubys/xmpppy-0.4.0') import xmpp def send(file): import entry, re from config import channel, directory # parse file into (title, link, byline, originator) base=file.split('-')[0].split('.')[0] link=channel.link + entry.post(base).link() title,body = open(directory.data+file).read().split('\n',1) originator = None if file.endswith('.txt'): byline = 'Posted by %s' % (channel.link,channel.title) originator = channel.link else: link='%s#c%d' % (link, os.stat(directory.data+file).st_mtime) byline = re.sub('^(<.*?>)*', '', body.strip().split('\n')[-1]) for poster in re.findall('', '', '%s <%s>' % (message, link)) def x2c(match,base): return unichr(int(match.group(1),base)).encode('utf-8') text = re.sub('&#x([0-9a-zA-Z]+);', lambda match: x2c(match,16), text) text = re.sub('&#(\d+);', lambda match: x2c(match,10), text) special={'lt':'<', 'gt':'>', 'amp':'&', 'quot':'"', 'apos':"'"} text = re.sub("&(%s);" % '|'.join(special.keys()), lambda match: special[match.group(1)], text) send_message(subs, text, message) def send_message(destinations, plaintext, xhtml='', register=False): # determine which host to use to route to this particular destination def route(dest): host = dest.split('@',1)[-1].split('/')[0] if not routes.has_section(host): host = 'DEFAULT' return host # sort messages by destination hosts = {} for dest in destinations: hosts.setdefault(route(dest),[]).append(dest) clients = [] for host, ids in hosts.items(): if private_only: # useful for testing, see __main__ below if not routes.has_option(host,'class'): continue if not routes.get(host,'class') == 'private': continue # establish a connection to this host try: jid=xmpp.protocol.JID(routes.get(host, 'user')) client=xmpp.Client(jid.getDomain(), debug=[]) con=client.connect((routes.get(host, 'address'), routes.getint(host, 'port'))) if not con: continue auth=client.auth(jid.getNode(), routes.get(host, 'password'), resource=jid.getResource()) if not auth: continue # the following triggers a subscription request if register: client.sendInitPresence(requestRoster=True) except: continue # send each message destined to this particular host for id in ids: message=xmpp.protocol.Message(id, plaintext) if xhtml: try: payload=xmpp.simplexml.XML2Node('%s' % (xml.dom.XHTML_NAMESPACE, xhtml)) message.addChild('html', {}, [payload], xmpp.NS_XHTML_IM) except: pass try: client.send(message) except: pass # schedule clean disconnect clients.append(client) # give old servers a chance to process the request if clients: time.sleep(1) # disconnect for client in clients: client.disconnect() if __name__ == '__main__': import config private_only=True for file in sys.argv[1:]: send(os.path.basename(file))