From 76bccec57235054292319e1002e55f187d70a265 Mon Sep 17 00:00:00 2001
From: dadav <33197631+dadav@users.noreply.github.com>
Date: Thu, 3 Oct 2019 21:09:34 +0200
Subject: [PATCH 1/2] Outsource twitter-code to plugin

---
 sdcard/rootfs/root/pwnagotchi/scripts/main.py | 25 ---------
 .../pwnagotchi/plugins/default/twitter.py     | 53 +++++++++++++++++++
 2 files changed, 53 insertions(+), 25 deletions(-)
 create mode 100644 sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/twitter.py

diff --git a/sdcard/rootfs/root/pwnagotchi/scripts/main.py b/sdcard/rootfs/root/pwnagotchi/scripts/main.py
index b4730d0..c401c8e 100755
--- a/sdcard/rootfs/root/pwnagotchi/scripts/main.py
+++ b/sdcard/rootfs/root/pwnagotchi/scripts/main.py
@@ -104,31 +104,6 @@ if args.do_manual:
         if Agent.is_connected():
             plugins.on('internet_available', config, log)
 
-            if config['twitter']['enabled'] and log.is_new() and log.handshakes > 0:
-                import tweepy
-
-                logging.info("detected a new session and internet connectivity!")
-
-                picture = '/dev/shm/pwnagotchi.png'
-
-                display.update(force=True)
-                display.image().save(picture, 'png')
-                display.set('status', 'Tweeting...')
-                display.update(force=True)
-
-                try:
-                    auth = tweepy.OAuthHandler(config['twitter']['consumer_key'], config['twitter']['consumer_secret'])
-                    auth.set_access_token(config['twitter']['access_token_key'], config['twitter']['access_token_secret'])
-                    api = tweepy.API(auth)
-
-                    tweet = Voice(lang=config['main']['lang']).on_log_tweet(log)
-                    api.update_with_media(filename=picture, status=tweet)
-                    log.save_session_id()
-
-                    logging.info("tweeted: %s" % tweet)
-                except Exception as e:
-                    logging.exception("error while tweeting")
-
     quit()
 
 agent.start_ai()
diff --git a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/twitter.py b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/twitter.py
new file mode 100644
index 0000000..6eb61a8
--- /dev/null
+++ b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/twitter.py
@@ -0,0 +1,53 @@
+__author__ = '33197631+dadav@users.noreply.github.com'
+__version__ = '1.0.0'
+__name__ = 'twitter'
+__license__ = 'GPL3'
+__description__ = 'This plugin creates tweets about the recent activity of pwnagotchi'
+__enabled__ = True
+
+import logging
+from pwnagotchi.voice import Voice
+
+UI = None
+
+
+def on_loaded():
+    logging.info("Twitter plugin loaded.")
+
+
+# called in manual mode when there's internet connectivity
+def on_internet_available(config, log):
+    if config['twitter']['enabled'] and log.is_new() and log.handshakes > 0 and UI:
+        try:
+            import tweepy
+        except ImportError:
+            logging.error("Couldn't import tweepy")
+            return
+
+        logging.info("detected a new session and internet connectivity!")
+
+        picture = '/dev/shm/pwnagotchi.png'
+
+        UI.update(force=True)
+        UI.image().save(picture, 'png')
+        UI.set('status', 'Tweeting...')
+        UI.update(force=True)
+
+        try:
+            auth = tweepy.OAuthHandler(config['twitter']['consumer_key'], config['twitter']['consumer_secret'])
+            auth.set_access_token(config['twitter']['access_token_key'], config['twitter']['access_token_secret'])
+            api = tweepy.API(auth)
+
+            tweet = Voice(lang=config['main']['lang']).on_log_tweet(log)
+            api.update_with_media(filename=picture, status=tweet)
+            log.save_session_id()
+
+            logging.info("tweeted: %s" % tweet)
+        except Exception as e:
+            logging.exception("error while tweeting")
+
+
+def on_ui_setup(ui):
+    # need that object
+    global UI
+    UI = ui

From 62350a37ea91704df83910c3c623303b81e696bf Mon Sep 17 00:00:00 2001
From: dadav <33197631+dadav@users.noreply.github.com>
Date: Thu, 3 Oct 2019 21:18:32 +0200
Subject: [PATCH 2/2] Set manual ui view

---
 .../pwnagotchi/scripts/pwnagotchi/plugins/default/twitter.py     | 1 +
 1 file changed, 1 insertion(+)

diff --git a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/twitter.py b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/twitter.py
index 6eb61a8..7aa4364 100644
--- a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/twitter.py
+++ b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/twitter.py
@@ -28,6 +28,7 @@ def on_internet_available(config, log):
 
         picture = '/dev/shm/pwnagotchi.png'
 
+        UI.on_manual_mode(log)
         UI.update(force=True)
         UI.image().save(picture, 'png')
         UI.set('status', 'Tweeting...')