aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGuillermo Ramos2019-06-24 13:10:02 +0200
committerGuillermo Ramos2019-06-24 17:39:53 +0200
commit57f8e009519b305294d002784967239e7a41ef5e (patch)
tree1f76ce42d4dc96e3561dbef035631fdef008a704 /lib
parent47fb4fc02407def9d9b19a2746983b8c65937e3f (diff)
downloadtgutils-57f8e009519b305294d002784967239e7a41ef5e.tar.gz
TgLib::fetch_token
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;