aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/TgLib.pm16
-rw-r--r--lib/TgLib/Env.pm3
2 files changed, 17 insertions, 2 deletions
diff --git a/lib/TgLib.pm b/lib/TgLib.pm
index 9be77d7..192d28a 100644
--- a/lib/TgLib.pm
+++ b/lib/TgLib.pm
@@ -1,6 +1,20 @@
package TgLib;
+use TgLib::Env qw<$CONFIG_HOME>;
+
use parent qw<Exporter>;
-our @EXPORT = qw<>;
+our @EXPORT = qw<fetch_token>;
+
+sub fetch_token {
+ my $token = $ENV{'TGUTILS_TOKEN'};
+ unless ($token) {
+ open(my $cfg, "<", "$CONFIG_HOME/tgutils_token") or return;
+ $token = <$cfg>;
+ chomp $token;
+ close $cfg;
+ }
+ $token =~ /^[0-9]+:[a-zA-Z0-9]+$/ or die "Invalid bot token ($token)";
+ return $token;
+}
1;
diff --git a/lib/TgLib/Env.pm b/lib/TgLib/Env.pm
index b1e94aa..c019a2c 100644
--- a/lib/TgLib/Env.pm
+++ b/lib/TgLib/Env.pm
@@ -1,10 +1,11 @@
package TgLib::Env;
use parent qw<Exporter>;
-our @EXPORT = qw<$HOME $CONFIG_HOME $CACHE_HOME>;
+our @EXPORT = qw<$HOME $CONFIG_HOME $CACHE_HOME $TOKEN>;
our $HOME = $ENV{'HOME'};
our $CONFIG_HOME = $ENV{'XDG_CONFIG_HOME'} || "$HOME/.config";
our $CACHE_HOME = $ENV{'XDG_CACHE_HOME'} || "$HOME/.cache";
+our $TOKEN = $ENV{'TGUTILS_TOKEN'};
1;