<s> from mk2 . plugins import Plugin <EOL> from mk2 . events import ServerEvent , EventPriority <EOL> from twisted . internet import reactor <EOL> from twisted . internet . defer import Deferred , DeferredList <EOL> from twisted . mail import smtp , relaymanager <EOL> from twisted . web . client import getPage <EOL> from cStringIO import StringIO <EOL> from email . mime . text import MIMEText <EOL> from urllib import urlencode <EOL> import re <EOL> _endpoint = { } <EOL> _plugin = None <EOL> def endpoint ( s ) : <EOL> def _wrapper ( cls ) : <EOL> _endpoint [ s ] = cls <EOL> cls . scheme = s <EOL> return cls <EOL> return _wrapper <EOL> class Endpoint ( object ) : <EOL> causes = "<STR_LIT:*>" <EOL> priority = "<STR_LIT:*>" <EOL> def __init__ ( self , plugin , uri ) : <EOL> pass <EOL> def push ( self , event ) : <EOL> pass <EOL> def filter ( self , event ) : <EOL> if self . priority != "<STR_LIT:*>" : <EOL> if int ( self . priority ) > event . priority : <EOL> return False <EOL> if self . causes != "<STR_LIT:*>" : <EOL> for cause in self . causes . split ( "<STR_LIT:U+002C>" ) : <EOL> if cause == event . cause : <EOL> return True <EOL> if cause . endswith ( "<STR_LIT:/>" ) and event . cause . startswith ( cause ) : <EOL> return True <EOL> return False <EOL> return True <EOL> def wait ( self , defer ) : <EOL> def done_waiting ( a ) : <EOL> _plugin . pending . remove ( defer ) <EOL> return a <EOL> _plugin . pending . add ( defer ) <EOL> defer . addBoth ( done_waiting ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . __class__ . __name__ , <EOL> self . url , <EOL> self . causes , self . priority ) <EOL> class HTTPEndpoint ( Endpoint ) : <EOL> method = "<STR_LIT:POST>" <EOL> postdata = { } <EOL> def push ( self , event ) : <EOL> self . setup ( event ) <EOL> defer = getPage ( self . endpoint , <EOL> method = self . method , <EOL> postdata = urlencode ( self . postdata ) , <EOL> headers = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . wait ( defer ) <EOL> @ endpoint ( "<STR_LIT>" ) <EOL> class NMAEndpoint ( HTTPEndpoint ) : <EOL> endpoint = "<STR_LIT>" <EOL> method = "<STR_LIT:POST>" <EOL> def __init__ ( self , plugin , url ) : <EOL> self . postdata = { <EOL> "<STR_LIT>" : url , <EOL> "<STR_LIT>" : "<STR_LIT>" . format ( plugin . parent . server_name ) , <EOL> } <EOL> def setup ( self , event ) : <EOL> self . postdata . update ( priority = event . priority , <EOL> event = event . friendly , <EOL> description = event . data ) <EOL> @ endpoint ( "<STR_LIT>" ) <EOL> class ProwlEndpoint ( HTTPEndpoint ) : <EOL> endpoint = "<STR_LIT>" <EOL> method = "<STR_LIT:POST>" <EOL> def __init__ ( self , plugin , url ) : <EOL> self . postdata = { <EOL> "<STR_LIT>" : url , <EOL> "<STR_LIT>" : "<STR_LIT>" . format ( plugin . parent . server_name ) , <EOL> } <EOL> def setup ( self , event ) : <EOL> self . postdata . update ( priority = event . priority , <EOL> event = event . friendly , <EOL> description = event . data ) <EOL> @ endpoint ( "<STR_LIT>" ) <EOL> class PushoverEndpoint ( HTTPEndpoint ) : <EOL> endpoint = "<STR_LIT>" <EOL> method = "<STR_LIT:POST>" <EOL> device = None <EOL> def __init__ ( self , plugin , url ) : <EOL> if not plugin . pushover_token : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . postdata = { <EOL> "<STR_LIT:user>" : url , <EOL> "<STR_LIT>" : plugin . pushover_token , <EOL> } <EOL> def setup ( self , event ) : <EOL> self . postdata . update ( priority = max ( - <NUM_LIT:1> , event . priority ) , <EOL> title = event . friendly , <EOL> message = event . data ) <EOL> if self . device : <EOL> self . postdata [ "<STR_LIT>" ] = self . device <EOL> @ endpoint ( "<STR_LIT>" ) <EOL> class SMTPEndpoint ( Endpoint ) : <EOL> def __init__ ( self , plugin , url ) : <EOL> self . smtp_host , self . smtp_user , self . smtp_password = plugin . email_smtp_server , plugin . email_smtp_user , plugin . email_smtp_password <EOL> self . smtp_security = plugin . email_smtp_security <EOL> if '<STR_LIT::>' in self . smtp_host : <EOL> host = self . smtp_host . split ( '<STR_LIT::>' ) <EOL> self . smtp_host , self . smtp_port = host [ <NUM_LIT:0> ] , int ( host [ <NUM_LIT:1> ] ) <EOL> else : <EOL> self . smtp_port = <NUM_LIT> <EOL> self . from_addr = plugin . email_address <EOL> self . from_name = "<STR_LIT>" . format ( plugin . parent . server_name ) <EOL> self . to_addr = url <EOL> def getMailExchange ( self , host ) : <EOL> mxc = relaymanager . MXCalculator ( ) <EOL> def cbMX ( mxRecord ) : <EOL> return str ( mxRecord . name ) <EOL> return mxc . getMX ( host ) . addCallback ( cbMX ) <EOL> def sendEmail ( self , from_ , from_name , to , msg_ , subject = "<STR_LIT>" ) : <EOL> def send ( host , user = None , pw = None , require_security = False ) : <EOL> msg = MIMEText ( msg_ ) <EOL> msg [ '<STR_LIT>' ] = "<STR_LIT>" . format ( from_name , from_ ) <EOL> msg [ '<STR_LIT>' ] = to <EOL> msg [ '<STR_LIT>' ] = subject <EOL> msgfile = StringIO ( msg . as_string ( ) ) <EOL> d = Deferred ( ) <EOL> factory = smtp . ESMTPSenderFactory ( user , pw , from_ , to , msgfile , d , <EOL> requireAuthentication = ( user is not None ) , <EOL> requireTransportSecurity = require_security ) <EOL> reactor . connectTCP ( host , self . smtp_port , factory ) <EOL> self . wait ( d ) <EOL> return d <EOL> if self . smtp_host : <EOL> return send ( self . smtp_host , self . smtp_user , self . smtp_password , self . smtp_security ) <EOL> else : <EOL> return self . getMailExchange ( to . split ( "<STR_LIT:@>" ) [ <NUM_LIT:1> ] ) . addCallback ( send ) <EOL> def push ( self , event ) : <EOL> defer = self . sendEmail ( self . from_addr , self . from_name , self . to_addr , event . data , event . friendly ) <EOL> self . wait ( defer ) <EOL> class Push ( Plugin ) : <EOL> endpoints = Plugin . Property ( default = "<STR_LIT>" ) <EOL> email_address = Plugin . Property ( default = "<STR_LIT>" ) <EOL> email_smtp_server = Plugin . Property ( default = "<STR_LIT>" ) <EOL> email_smtp_user = Plugin . Property ( default = "<STR_LIT>" ) <EOL> email_smtp_password = Plugin . Property ( default = "<STR_LIT>" ) <EOL> email_smtp_security = Plugin . Property ( default = False ) <EOL> pushover_token = Plugin . Property ( default = "<STR_LIT>" ) <EOL> def setup ( self ) : <EOL> global _plugin <EOL> _plugin = self <EOL> self . pending = set ( ) <EOL> self . configure_endpoints ( ) <EOL> self . register ( self . send_alert , ServerEvent , priority = EventPriority . MONITOR ) <EOL> self . eventid = reactor . addSystemEventTrigger ( '<STR_LIT>' , '<STR_LIT>' , self . finish ) <EOL> def teardown ( self ) : <EOL> reactor . removeSystemEventTrigger ( self . eventid ) <EOL> def finish ( self ) : <EOL> return DeferredList ( list ( self . pending ) ) <EOL> def configure_endpoints ( self ) : <EOL> eps = self . endpoints . split ( "<STR_LIT:\n>" ) <EOL> self . _endpoints = [ ] <EOL> for ep in eps : <EOL> if not ep . strip ( ) : <EOL> continue <EOL> try : <EOL> bits = re . split ( "<STR_LIT>" , ep ) <EOL> url , md = bits [ <NUM_LIT:0> ] , bits [ <NUM_LIT:1> : ] <EOL> scheme , ee = re . split ( "<STR_LIT>" , url ) <EOL> if scheme not in _endpoint : <EOL> self . console ( "<STR_LIT>" . format ( url ) ) <EOL> continue <EOL> cls = _endpoint [ scheme ] <EOL> inst = cls ( self , ee ) <EOL> inst . url = url <EOL> for k , v in [ d . split ( "<STR_LIT:=>" ) for d in md ] : <EOL> setattr ( inst , k , v ) <EOL> self . _endpoints . append ( inst ) <EOL> except Exception as e : <EOL> self . console ( "<STR_LIT>" . format ( e , ep ) ) <EOL> def send_alert ( self , event ) : <EOL> for ep in self . _endpoints : <EOL> if ep . filter ( event ) : <EOL> ep . push ( event ) </s>
