-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSMS.pl
More file actions
executable file
·328 lines (277 loc) · 7.95 KB
/
SMS.pl
File metadata and controls
executable file
·328 lines (277 loc) · 7.95 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/perl -s
#
## Shownotes Message Service
use strict;
use warnings;
use utf8;
use Log::Log4perl qw(:easy);
use Data::Dumper;
use Config::Simple;
use File::Basename;
use DBI;
use Net::XMPP;
use JSON;
# make a new config config object
my $currentpath = dirname(__FILE__);
my $cfg = new Config::Simple("$currentpath/sms.config");
# some global variables
my $programpath = $cfg->param('directory');
my $account = '';
my $msg = "";
my $syn = 0;
my $fin = 0;
# Log config
Log::Log4perl->init("$currentpath/logging.config");
# connect to database
my $dbh = DBI->connect(
"dbi:SQLite:dbname=$programpath/data.db",
"",
"",
{ RaiseError => 1 }, #Exceptions instead of error
) or die $DBI::errstr;
$dbh->do("PRAGMA foreign_keys = ON");
# create Subscription table if not exists
$dbh->do(
"CREATE TABLE IF NOT EXISTS subscriptions(
jid TEXT NOT NULL,
slug TEXT NOT NULL,
timestamp INT NOT NULL,
UNIQUE (jid,slug)
)"
);
# create Subscribers table if not exists
$dbh->do(
"CREATE TABLE IF NOT EXISTS subscribers(
jid TEXT NOT NULL,
servicehost TEXT NOT NULL,
token TEXT DEFAULT 0,
challenge TEXT DEFAULT 0,
UNIQUE (jid,servicehost)
)"
);
# create pad info table if not exists
$dbh->do(
"CREATE TABLE IF NOT EXISTS padinfo(
jid TEXT NOT NULL PRIMARY KEY,
info INT NOT NULL DEFAULT 0
)"
);
# make a jabber client object
my $con = new Net::XMPP::Client();
# set the callback functions
$con->SetMessageCallBacks( chat => \&message );
$con->SetCallBacks( onauth => \&send_status );
$con->SetPresenceCallBacks( type => undef );
# execute jabber client
$con->Execute(
hostname => $cfg->param('server'),
port => $cfg->param('port'),
username => $cfg->param('username'),
password => $cfg->param('password'),
resource => $cfg->param('botresource')
);
# status update callback
sub send_status {
INFO("Authenticated");
print "Authenticated - Service is running";
$con->PresenceSend( show => "chat" );
}
# callback for message handle
sub message {
# get informations about the client, who is connected
my ( $sid, $Mess ) = @_;
my $body = lc $Mess->GetBody();
my $jid = $Mess->GetFrom();
# get account without ressource
$jid =~ /(.+@.+\.\w+)\/.+/;
$account = lc $1;
INFO( "Event at: " . localtime(time) );
INFO( "Message from: $account" );
INFO( "Body: $body" );
# command selection
if ( $body eq 'list' ) {
podlist();
}
elsif ( $body eq 'reglist' ) {
reglist();
}
elsif ( $body =~ /^reg ([\w|\d|-]+)$/i ) {
register($1);
}
elsif ( $body =~ /^unreg ([\*|\w|\d|-]+)$/i ) {
unregister($1);
}
elsif ( $body =~ /^notify (on|off)$/i ) {
showpadinfo($1);
}
elsif ( $body eq 'about' ) {
about();
}
elsif ( $body eq '' ) {
print "Empty Body\n";
# do nothing...
# workaround for empty message bodys
}
elsif ($body eq 'syn') {
$msg = "SYN-ACK";
$syn = 1;
print "$account found the easteregg!\n";
}
elsif ($body eq 'ack' and $syn == 1) {
$msg = "Easter egg connection established\nCongratulations!";
$syn = 0;
}
elsif ($body eq 'fin') {
$msg = "FIN-ACK";
$fin = 1;
print "$account found the easteregg!\n";
}
elsif ($body eq 'ack' and $fin == 1) {
$msg = "Easter egg connection disconnected\nByeBye!";
$fin = 0;
}
else {
printhelp();
}
# send message back to client
$con->MessageSend( to => $account, type => 'chat', body => $msg );
$msg = '';
return;
}
#showpad info
sub showpadinfo {
my $servicehost = $cfg->param('server');
$dbh->do(
"INSERT OR IGNORE INTO subscribers
VALUES('$account','$servicehost',0,0)
"
);
$dbh->do(
"INSERT OR IGNORE INTO padinfo
VALUES('$account',0)
"
);
my $infoverbal = shift;
my $info = 0;
if ( $infoverbal eq "on" ) {
$info = 1;
}
my $sth = $dbh->do(
"UPDATE padinfo SET info=$info
WHERE jid LIKE '$account'
"
);
if ( $info == 1 ) {
$msg = "Showpad notification activated";
}
else {
$msg = "Showpad notification deactivated";
}
}
# about
sub about {
$msg =
"Shownotes Message Service\n=========================\n\nSource: https://github.com/shownotes/shownotes-message-service\n\nAuthor: Martin Stoffers\nEmail: Dr4k3\@shownot.es\nJabber: Dr4k3\@fastreboot.de\n\nContributers:\n\n Simon Waldherr\n Email: Simon\@shownot.es\n Jabber: SimonWaldherr\@jabber.shownot.es\n\n Ingolf Gürges\n Jabber: ingolf\@fastreboot.de\n\n\nThis program is published under the terms of GPLv2 and comes with no warranty.\nhttp://www.gnu.org/licenses/gpl-2.0.txt";
}
# help
sub printhelp {
$msg =
"list - Get a list of podcasts\n\nreg < podcast > - Subscribe to a podcast notification\n\nreglist - Get a list of all your subscribtions\n\nunreg < podcast | * > - Unsubscribe a podcast notification\n\nnotify < ON | OFF > - Get notifications about new showpads on http://pad.shownot.es\n\nabout - About the bot";
}
# list all podcasts
sub podlist {
my $sth = $dbh->prepare(
"SELECT slug FROM podcasts
ORDER BY slug ASC
"
);
$sth->execute();
my $column;
while ( $column = $sth->fetchrow_array() ) {
$msg = $msg . "$column; ";
}
$sth->finish();
}
# lists all subscribed podcasts of a user
sub reglist {
my $sth = $dbh->prepare(
"SELECT slug FROM subscriptions
WHERE jid LIKE '$account'
"
);
$sth->execute();
my $column;
$msg = $msg . "You are subscribed to the following podcasts notifications:\n";
while ( $column = $sth->fetchrow_array() ) {
$msg = $msg . " $column\n";
}
$sth->finish();
}
# unregister a podcast
sub unregister {
my $podslug = shift;
if ( $podslug eq '*' ) {
$dbh->do(
"DELETE FROM subscriptions
WHERE jid LIKE '$account'
"
);
# set message
$msg = "Unsubscribed from all podcast notifications";
INFO("Unsubscribed $account from all podcast notifications");
}
else {
$dbh->do(
"DELETE FROM subscriptions
WHERE jid LIKE '$account'
AND slug LIKE '$podslug'
"
);
# set message
$msg = $podslug . " unsubscribed";
INFO("$podslug unsubscribed for $account");
}
}
# register a podcast
sub register {
my $podslug = shift;
my $servicehost = $cfg->param('server');
my $sth = $dbh->prepare(
"SELECT slug FROM podcasts
WHERE slug LIKE '$podslug'
"
);
$sth->execute();
if ( defined $sth->fetchrow_array() ) {
$sth->finish();
$dbh->do(
"INSERT OR IGNORE INTO subscribers
VALUES('$account','$servicehost',0,0)
"
);
$dbh->do(
"INSERT OR IGNORE INTO padinfo
VALUES('$account',0)
"
);
my $timestamp = time;
$dbh->do(
"INSERT INTO subscriptions
SELECT '$account','$podslug',$timestamp
WHERE NOT EXISTS
( SELECT jid,slug FROM subscriptions
WHERE jid LIKE '$account'
AND slug LIKE '$podslug'
)
"
);
# set message
$msg = $podslug . " registered to " . $account;
INFO("$podslug registered for $account");
}
else {
$msg = $podslug . " not in list";
INFO("Register failed for $podslug");
$sth->finish();
}
}