Commits

Josh VanderLinden committed e074634

Worked a little bit more on the connection issues that a few folks have been experiencing.

Comments (0)

Files changed (3)

 syntax: glob
 *.pyc
 *.log
+*.orig
 build
 dist
 simplejson
 scripts/Output
 twim.wpr
 nbproject
+twibber.tmproj
     0x2019: u"'",
 }
 
+class ConnectionError(object): pass
+
 class Twibber(object):
     """
     The main program
     _queue = []
     _gui = None
     _domains = {}
+    _is_connected = False
+    _has_connected = False
 
     def __init__(self, with_gui=True):
         log.info('Starting app on %s' % sys.platform)
-        self.interval = timedelta(seconds=config.update_interval)
+        self.update_interval = timedelta(seconds=config.update_interval)
+        self.mention_interval = timedelta(seconds=config.mention_interval)
+        self.direct_interval = timedelta(seconds=config.direct_interval)
 
         # start the GUI if necessary
         if with_gui:
         """
 
         try:
-            if force:
-                self._client = None
+            if force and self.has_connected:
+                self.client.reconnectAndReauth()
+            else:
+                params = dict(server=None, proxy=None, 
+                    secure=int(config.jabber_server_secure))
+                
+                # different server?
+                if len(config.jabber_server_ip):
+                    params['server'] = (
+                        config.jabber_server_ip, 
+                        config.jabber_server_port
+                    )
+                
+                # see if we have a proxy
+                if len(config.jabber_proxy_host):
+                    params['proxy'] = dict(
+                        host=config.jabber_proxy_host,
+                        port=config.jabber_proxy_port,
+                        user=config.jabber_proxy_username,
+                        password=config.jabber_proxy_password
+                    )
 
-            # handle messages sent by the user
-            self.client.RegisterHandler('message', self.OnMessage)
-            self.client.RegisterHandler('presence', self.OnPresence)
+                if self.client.connect(**params) == '':
+                    raise ConnectionError()
+        
+                from twibber import APP_TITLE, __version__
+                auth = self.client.auth(self.jid.getNode(),
+                                        self.login_as_pass,
+                                        '%s %s' % (APP_TITLE, __version__))
+
+                # handle messages sent by the user
+                self.client.RegisterHandler('message', self.OnMessage)
+                self.client.RegisterHandler('presence', self.OnPresence)
 
             # tell people we're online
             self.client.sendInitPresence()
             self.presence = xmpp.Presence(show='chat',
                                           status='Doing thy bidding, master.')
             self.client.send(self.presence)
+            self.is_connected = True
+            self.has_connected = True
         except Exception, ex:
             log.error(ex)
-            sys.exit(ex)
 
     def GetClient(self):
         """
         Gets the client and connects to Jabber if we're not already connected
         """
-        try:
-            if not self._client:
-                # try to connect to Jabber
-                self.jid = xmpp.JID(config.login_as_user)
-                self._client = xmpp.Client(self.jid.getDomain(), debug=[])
-
-            if not self._client.isConnected():
-                conn = self._client.connect()
-
-                if not conn:
-                    raise Exception('Failed to connect')
-                auth = self._client.auth(self.jid.getNode(),
-                                              config.login_as_pass,
-                                              resource=self.jid.getResource())
-                if not auth:
-                    raise Exception('Failed to log into Jabber')
-        except Exception, ex:
-            log.error(ex)
-            self._client = None
-            time.sleep(5)
-            return self.GetClient()
-
+        if not self._client:
+            # try to connect to Jabber
+            self.jid = xmpp.JID(config.login_as_user)
+            self._client = xmpp.Client(self.jid.getDomain(), 
+                                       config.jabber_server_port,
+                                       debug=[])
         return self._client
     client = property(GetClient)
 
         """
         Shut everything down
         """
+        log.info('Terminating the application')
+        self.checker.set()
+        self.poster.set()
+        self.client.disconnected()
+
         config.persist()
 
         if self._gui and not from_gui:
             self._gui.OnExit(from_core=True)
 
-        log.info('Terminating the application')
-        self.checker.set()
-        self.poster.set()
-        self.client.disconnect()
         sys.exit(0)
 
     def OnHelpCommand(self, user, body, remaining):
                 self.TellUser('Update interval changing from %i to %i.' % (
                                 config.update_interval, interval))
                 config.update_interval = interval
-                self.interval = timedelta(seconds=interval)
+                self.update_interval = timedelta(seconds=interval)
         else:
             self.TellUser('Current Interval: %i' % config.update_interval)
 
         sender = evt.getFrom().getStripped()
         body = evt.getBody()
 
+        # don't accept messages from anyone but the user we sent tweets to
+        if sender != config.send_to_user: return
         if not body: return
         log.info('Received message from %s: %s' % (sender, body))
 
-        # don't accept messages from anyone but the user we sent tweets to
-        if sender != config.send_to_user: return
-
         # make sure this is a valid message
         valid_types = ('message', 'chat', 'normal', None)
         if evt.getType() not in valid_types: return
         try:
             now = datetime.now()
             for user in config.users.values():
-                last_update = user.last_update or now - self.interval
-                next_run = last_update + self.interval
+                last_update = user.last_update or now - self.update_interval
+                next_run = last_update + self.update_interval
                 if next_run <= now:
                     log.info('Getting updates for %s' % user.username)
                     self.GetUpdatesFor(user)
         'scheduled_tweets': '',
         'show_at_launch': (5, int),
         'max_url_length': (25, int),
+        'mention_interval': (180, int),
+        'direct_interval': (300, int),
+        'jabber_server_ip': '',
+        'jabber_server_port': (5223, int),
+        'jabber_server_secure': (True, bool),
+        'jabber_proxy_host': '',
+        'jabber_proxy_port': (5223, int),
+        'jabber_proxy_username': '',
+        'jabber_proxy_password': ''
     }
+    
     _users = None
     _scheduled = None
     _filtered = None
         self.configfile = configfile
 
         # grab these values from the configuration
-        self.to_grab = ('update_interval', 'last_tweet_id', 'login_as_user',
-                        'login_as_pass', 'send_to_user', 'display_destination',
-                        'show_at_launch', 'max_url_length')
+        omit = ('twitter_users', 'default_user', 'filtered_tags', 
+                'scheduled_tweets')
+        self.to_grab = tuple(k for k in Config.Defaults.keys() if k not in omit)
 
         log.debug('Reading configuration from: %s' % self.configfile)
         self.parser = SafeConfigParser()
         """
         log.info('Saving configuration')
         try:
-            self._set_users(self.users)
+            self.users = self.users
             self.scheduled_tweets = self.scheduled_tweets
             self.filtered_tags = self.filtered_tags
             self.default_user = self.default_user