/
pb
executable file
·134 lines (109 loc) · 2.75 KB
/
pb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/perl
use strict;
use IO::Socket::INET;
use WWW::PushBullet;
use File::Slurp;
use Sys::Hostname;
use File::Basename;
use IO::File;
use Env qw(HOME);
# auto-flush on socket
$| = 1;
# hostname
my $host = hostname;
# apikey
if ( !-e $HOME . "/.pb.key" ) {
print "Store PushBullet API key in ~/.pb.key\n";
exit;
}
my $apikey = read_file( $HOME . '/.pb.key' );
chomp($apikey);
my $pb = WWW::PushBullet->new( { apikey => $apikey } );
# only one file
if ( $ARGV[1] ) { print "One file or STDIN\n"; exit; }
my $directurl;
my $data;
# stdin or file ?
if ( !$ARGV[0] ) {
# read data from stdin
while (<STDIN>) {
$data .= $_;
}
$directurl = $data if $data =~ /^http/;
}
else {
# does file exists ?
if ( !-e $ARGV[0] ) {
print "No such file $ARGV[0]\n";
$directurl = $ARGV[0] if $ARGV[0] =~ /^http/;
if ( !defined $directurl ) {
$pb->push_note(
{
title => $host,
body => $ARGV[0],
}
);
print "Sending note instead: " . $ARGV[0] . "\n";
exit;
}
}
else {
if ( -B $ARGV[0] ) {
print "This is a binary, uploading to weepee transfer with curl: \n";
my $toexec = "curl --progress-bar --upload-file \""
. $ARGV[0]
. "\" \"https://transfer.weepee.io/"
. basename( $ARGV[0] )
. "\" | pb\n";
my $exec = "tmux send '" . $toexec . "'";
`$exec`;
exit;
}
else {
$data = read_file( $ARGV[0] );
}
}
}
if ( defined $directurl ) {
$directurl =~ s/\s+$//;
# send to pushbullet
$pb->push_link(
{
title => $host,
url => $directurl
}
);
print "send url: " . $directurl . "\n";
}
else {
my $fd = new IO::File ">/tmp/$$.pb";
# create a connecting socket
my $socket = new IO::Socket::INET(
PeerAddr => 'pb.weepee.io',
PeerPort => '9999',
Proto => 'tcp',
);
die "cannot connect to pb.weepee.io$!\n" unless $socket;
# data to send to a server
my $size = $socket->send($data);
$fd->print($data);
$fd->close;
# notify server that request has been sent
shutdown( $socket, 1 );
# receive a response of up to 1024 characters from server
my $response = "";
$socket->recv( $response, 1024 );
$socket->close();
$response =~ s/\s+$//;
my $toexec = "cat /tmp/$$.pb | pbcopy; rm -f /tmp/$$.pb\n";
my $exec = "tmux send '" . $toexec . "'";
`$exec`;
# send to pushbullet
$pb->push_link(
{
title => $host,
url => $response
}
);
print $response;
}