[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r21925: {weather} Make the new weather vaguely work the way the operative weat (weather/trunk)



Author: kaner
Date: 2010-03-12 14:13:02 +0000 (Fri, 12 Mar 2010)
New Revision: 21925

Modified:
   weather/trunk/weather.py
Log:
Make the new weather vaguely work the way the operative weather does


Modified: weather/trunk/weather.py
===================================================================
--- weather/trunk/weather.py	2010-03-12 13:13:44 UTC (rev 21924)
+++ weather/trunk/weather.py	2010-03-12 14:13:02 UTC (rev 21925)
@@ -30,7 +30,21 @@
 THANKS_OUT = """
     Thanks for using tor weather. A confirmation request has been sent to '%s'.
 """
+REPORT_MAIL = """This is a Tor Weather report.
 
+It appears that a tor node you elected to monitor,
+
+(node id: %s)
+
+has been uncontactable through the Tor network for a while.  You may wish 
+to look at it to see why.  The last error message from our code while trying to
+contact it is included below.  You may or may not find it helpful!
+
+(You can unsubscribe from these reports at any time by visiting the 
+following url:
+
+%s )"""
+
 CONFIRMATION_MAIL = """
     Dear human, this is the Tor Weather Report system.  
 
@@ -293,12 +307,15 @@
         q.addErrback(self._errBack)
 
     def _checkRet(self, resultList):
-        print "Checkret"
+        # Loop through result list and check each node
         for result in resultList:
             print "Result: ", result
             checkHost = result[2]
             if not self._checkHost(checkHost):
                 print "Server %s seems to be offline" % checkHost
+                self._handleOfflineNode(result)
+            else:
+                print "Server %s is ok" % checkHost
     
     def _errBack(self, failure):
         print "Error: ", failure.getErrorMessage()
@@ -308,6 +325,34 @@
         torPing = TorPing()
         return torPing.ping(hostID)
 
+    def _handleOfflineNode(self, dbRow):
+        # Log, mail
+        if self._decideNotice(dbRow):
+            self._sendNotice(dbRow)
+
+    def _decideNotice(self, dbRow):
+        # This is just a placeholder for now. We'll decide later what 
+        # conditions we want to check
+        return True
+
+    def _sendNotice(self, dbRow):
+        nodeId = dbRow[2]
+        unsubsURL =  URLbase + "/unsubscribe?auth=" + str(dbRow[4])
+        message = MIMEMultipart()
+        message['Subject'] = "Tor Weather Subscription Request"
+        message['To'] = dbRow[1]
+        message['From'] = mailFrom
+
+        messageText = REPORT_MAIL % (nodeId, unsubsURL)
+        text = MIMEText(messageText, _subtype="plain", _charset="ascii")
+        # Add text part
+        message.attach(text)
+
+        # Try to send
+        smtp = smtplib.SMTP("localhost:25")
+        smtp.sendmail(mailFrom, dbRow[1], message.as_string())
+        smtp.quit()
+
 class TorPing:
   "Check to see if various tor nodes respond to SSL hanshakes"
   def __init__(self, control_host = "127.0.0.1", control_port = 9051):
@@ -334,25 +379,19 @@
     del self.control
     self.control = None
 
-  def ping(self, node_id):
+  def ping(self, nodeId):
     "Let's see if this tor node is up by only asking Tor."
-    string = "ns/id/" + node_id
     try:
-       info = self.control.get_info(string)
+       info = self.control.get_info(str("ns/id/" + nodeId))
     except TorCtl.ErrorReply:
         # If we're getting here, we're likely seeing:
         # ErrorReply: 552 Unrecognized key "ns/id/46D9..."
         # This means that the node isn't recognized by 
-       x = traceback.format_exc()
-       print x
-       info = None
-       return False
+        # XXX: Log
+        return False
 
     except:
-        # Remove this, it's a hack to debug this specific bug
-        x = traceback.format_exc()
-        print x
-        info = None
+        # XXX: Log
         return False
 
     # If we're here, we were able to fetch information about the router
@@ -370,7 +409,5 @@
     reactor.listenTCP(8000, weatherSite)
     reactor.run()
 
-
-
 if __name__ == "__main__":
     main()