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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
|
/*
* xmltv2vdr.cpp: A plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
*/
#include <vdr/plugin.h>
#include <vdr/videodir.h>
#include <unistd.h>
#include <getopt.h>
#include <stdarg.h>
#include <locale.h>
#include <langinfo.h>
#include <sqlite3.h>
#include <time.h>
#include <sys/types.h>
#include <pwd.h>
#include <netdb.h>
#include "setup.h"
#include "xmltv2vdr.h"
#include "debug.h"
int ioprio_set(int which, int who, int ioprio)
{
#if defined(__i386__)
#define __NR_ioprio_set 289
#elif defined(__ppc__)
#define __NR_ioprio_set 273
#elif defined(__x86_64__)
#define __NR_ioprio_set 251
#elif defined(__ia64__)
#define __NR_ioprio_set 1274
#else
#define __NR_ioprio_set 0
#endif
if (__NR_ioprio_set)
{
return syscall(__NR_ioprio_set, which, who, ioprio);
}
else
{
return 0; // just do nothing
}
}
char *logfile=NULL;
void logger(cEPGSource *source, char logtype, const char* format, ...)
{
va_list ap;
char fmt[255];
if (source && logtype!='T')
{
if (logtype=='E')
{
if (snprintf(fmt,sizeof(fmt),"xmltv2vdr: '%s' ERROR %s",source->Name(),format)==-1) return;
}
else
{
if (snprintf(fmt,sizeof(fmt),"xmltv2vdr: '%s' %s",source->Name(),format)==-1) return;
}
}
else
{
if (logtype=='E')
{
snprintf(fmt,sizeof(fmt),"xmltv2vdr: ERROR %s",format);
}
else
{
snprintf(fmt,sizeof(fmt),"xmltv2vdr: %s",format);
}
}
va_start(ap, format);
char *ptr;
if (vasprintf(&ptr,fmt,ap)==-1) return;
va_end(ap);
struct tm tm;
if (logfile || source)
{
time_t now=time(NULL);
localtime_r(&now,&tm);
}
char *crlf=strchr(ptr,'\n');
if (crlf) *crlf=0;
crlf=strchr(ptr,'\r');
if (crlf) *crlf=0;
if (source && logtype!='T')
{
source->Add2Log(&tm,logtype,ptr);
}
if (logfile)
{
char dt[30];
strftime(dt,sizeof(dt)-1,"%b %d %H:%M:%S",&tm);
FILE *l=fopen(logfile,"a+");
if (l)
{
fprintf(l,"%s [%i] %s\n",dt,cThread::ThreadId(),ptr);
fclose(l);
}
}
switch (logtype)
{
case 'E':
if (SysLogLevel>0) syslog_with_tid(LOG_ERR,"%s",ptr);
break;
case 'I':
if (SysLogLevel>1) syslog_with_tid(LOG_ERR,"%s",ptr);
break;
case 'D':
if (SysLogLevel>2) syslog_with_tid(LOG_ERR,"%s",ptr);
break;
default:
break;
}
free(ptr);
}
// -------------------------------------------------------------
bool cSVDRPMsg::readreply(int fd)
{
usleep(400000);
char c=' ';
do
{
struct pollfd fds;
fds.fd=fd;
fds.events=POLLIN;
fds.revents=0;
int ret=poll(&fds,1,600);
if (ret<=0) return false;
if (fds.revents!=POLLIN) return false;
if (read(fd,&c,1)<0) return false;
}
while (c!='\n');
return true;
}
bool cSVDRPMsg::Send(const char *format, ...)
{
char *msg;
va_list ap;
va_start(ap, format);
if (vasprintf(&msg,format,ap)==-1) return false;
va_end(ap);
int port;
struct servent *serv=getservbyname("svdrp","tcp");
if (serv)
{
port=htons(serv->s_port);
}
else
{
#if VDRVERSNUM < 10715
port=2001;
#else
port=6419;
#endif
}
struct sockaddr_in name;
name.sin_family = AF_INET;
name.sin_port = htons(port);
name.sin_addr.s_addr=inet_addr("127.0.0.1");
uint size = sizeof(name);
int sock;
sock=socket(PF_INET, SOCK_STREAM, 0);
if (sock<0) return false;
if (connect(sock, (struct sockaddr *)&name,size)!=0)
{
close(sock);
free(msg);
return false;
}
if (!readreply(sock))
{
close(sock);
free(msg);
return false;
}
ssize_t ret;
ret=write(sock,"PLUG epgsearch ",15);
if (ret!=(ssize_t)-1) ret=write(sock,msg,strlen(msg));
if (ret!=(ssize_t)-1) ret=write(sock,"\r\n",2);
if (!readreply(sock) || (ret==(ssize_t)-1))
{
close(sock);
free(msg);
return false;
}
ret=write(sock,"QUIT\r\n",6);
if (ret!=(ssize_t)-1) readreply(sock);
close(sock);
free(msg);
return true;
}
// -------------------------------------------------------------
cEPGSearch_Client::cEPGSearch_Client()
{
plugin=cPluginManager::GetPlugin("epgsearch");
}
bool cEPGSearch_Client::EnableSearchTimer()
{
if (!plugin) return false;
Epgsearch_enablesearchtimers_v1_0 serviceData;
serviceData.enable = true;
if (!plugin->Service("Epgsearch-enablesearchtimers-v1.0", (void*) &serviceData))
{
// SVDRP fallback
cSVDRPMsg msg;
if (!msg.Send("SETS ON")) return false;
}
return true;
}
bool cEPGSearch_Client::DisableSearchTimer()
{
if (!plugin) return false;
Epgsearch_enablesearchtimers_v1_0 serviceData;
serviceData.enable = false;
if (!plugin->Service("Epgsearch-enablesearchtimers-v1.0", (void*) &serviceData))
{
// SVDRP fallback
cSVDRPMsg msg;
if (!msg.Send("SETS OFF")) return false;
}
return true;
}
// -------------------------------------------------------------
cGlobals::cGlobals()
{
confdir=NULL;
epgfile=NULL;
epdir=NULL;
epcodeset=NULL;
imgdir=NULL;
codeset=NULL;
srcorder=NULL;
wakeup=false;
epghandler=NULL;
epgtimer=NULL;
epgseasonepisode=NULL;
epall=0;
order=strdup(GetDefaultOrder());
imgdelafter=30;
if (asprintf(&epgfile,"%s/epg.db",VideoDirectory)==-1) {};
if (asprintf(&imgdir,"%s","/var/cache/vdr/epgimages")==-1) {};
if (access(imgdir,R_OK|W_OK)==-1)
{
free(imgdir);
imgdir=NULL;
}
if (setlocale(LC_CTYPE,""))
codeset=strdup(nl_langinfo(CODESET));
else
{
char *LangEnv=getenv("LANG");
if (LangEnv)
{
codeset=strdup(strchr(LangEnv,'.'));
if (codeset)
codeset++; // skip dot
}
}
if (!codeset)
{
codeset=strdup("ASCII//TRANSLIT");
}
struct passwd pwd,*pwdbuf;
char buf[1024];
getpwuid_r(getuid(),&pwd,buf,sizeof(buf),&pwdbuf);
if (pwdbuf)
{
if (asprintf(&epdir,"%s/.eplists/lists",pwdbuf->pw_dir)!=-1)
{
if (access(epdir,R_OK))
{
free(epdir);
epdir=NULL;
}
else
{
epcodeset=codeset;
}
}
}
}
cGlobals::~cGlobals()
{
free(confdir);
free(epgfile);
free(epdir);
free(imgdir);
free(codeset);
free(order);
free(srcorder);
if (epgtimer)
{
epgtimer->Stop();
delete epgtimer;
}
if (epgseasonepisode)
{
epgseasonepisode->Stop();
delete epgseasonepisode;
}
epgsources.Remove();
epgmappings.Remove();
textmappings.Remove();
}
char *cGlobals::GetDefaultOrder()
{
return (char *) "LOT,CRS,CAD,ORT,CAT,VID,AUD,SEE,RAT,STR,REV";
}
void cGlobals::SetImgDir(const char* ImgDir)
{
if (!ImgDir) return;
if (access(ImgDir,R_OK|W_OK)==-1)
{
esyslog("cannot access %s",ImgDir);
return;
}
free(imgdir);
imgdir=strdup(ImgDir);
}
void cGlobals::SetEPDir(const char* EPDir)
{
if (!EPDir) return;
if (access(EPDir,R_OK)==-1)
{
esyslog("cannot access %s",EPDir);
return;
}
free(epdir);
epcodeset=codeset;
epdir=strdup(EPDir);
if (!epdir) return;
epcodeset=strchr((char *) epdir,',');
if (epcodeset)
{
*epcodeset=0;
}
else
{
epcodeset=(char *) codeset;
}
}
bool cGlobals::DBExists()
{
if (!epgfile) return true; // is this safe?
struct stat statbuf;
if (stat(epgfile,&statbuf)==-1) return false; // no database
if (!statbuf.st_size) return false; // no database
return true;
}
// -------------------------------------------------------------
cEPGHandler::cEPGHandler(cGlobals* Global): import(Global)
{
epall=0;
maps=Global->EPGMappings();
sources=Global->EPGSources();
db=NULL;
now=0;
}
bool cEPGHandler::IgnoreChannel(const cChannel* Channel)
{
now=time(NULL);
if (!maps) return false;
if (!Channel) return false;
return maps->IgnoreChannel(Channel);
}
bool cEPGHandler::check4proc(cEvent *event, bool &spth, cEPGMapping **map)
{
if (map) *map=NULL;
spth=false;
if (!event) return false;
if (now>(event->StartTime()+event->Duration())) return false; // event in the past?
if (!maps) return false;
if (!import.DBExists()) return false;
cEPGMapping *t_map=maps->GetMap(event->ChannelID());
if (!t_map)
{
if (!epall) return false;
if (!event->HasTimer()) return false;
if (!event->ShortText()) return false;
spth=true;
}
if (map) *map=t_map;
return true;
}
bool cEPGHandler::SetTitle(cEvent* Event, const char* UNUSED(Title))
{
if (!Event->Title()) return false; // no title? new event! let VDR handle this..
return true;
}
bool cEPGHandler::SetShortText(cEvent* Event, const char* ShortText)
{
// prevent setting empty shorttext
if (!ShortText) return true;
// prevent setting empty shorttext
if (!strlen(ShortText)) return true;
// prevent setting shorttext equal to title
if (Event->Title() && !strcasecmp(Event->Title(),ShortText)) return true;
if (!Event->ShortText())
{
#if VDRDEBUG
tsyslog("{%5i} setting stext (%s) of '%s'",Event->EventID(),
ShortText,Event->Title());
#endif
return false; // no shorttext? new event! let VDR handle this..
}
return true;
}
bool cEPGHandler::SetDescription(cEvent* Event, const char* Description)
{
bool seth;
if (!check4proc(Event,seth,NULL)) return false;
if (import.WasChanged(Event))
{
// ok we already changed this event!
if (!Description) return true; // prevent setting nothing to description
int len=strlen(Description);
if (!len) return true; // prevent setting nothing to description
if (!strcasestr(Event->Description(),Description))
{
// eit description changed -> set it
tsyslog("{%5i} %schanging descr of '%s'",Event->EventID(),seth ? "*" : "",
Event->Title());
return false;
}
#ifdef VDRDEBUG
tsyslog("{%5i} %salready seen descr '%s'",Event->EventID(),seth ? "*" : "",
Event->Title());
#endif
return true;
}
tsyslog("{%5i} %ssetting descr of '%s'",Event->EventID(),seth ? "*" : "",
Event->Title());
return false;
}
bool cEPGHandler::HandleEvent(cEvent* Event)
{
bool special_epall_timer_handling;
cEPGMapping *map;
if (!check4proc(Event,special_epall_timer_handling,&map)) return false;
int Flags=0;
const char *ChannelID=strdup(*Event->ChannelID().ToString());
if (!ChannelID) return false;
if (special_epall_timer_handling)
{
Flags=USE_SEASON;
}
else
{
// map is always set if seth==false
Flags=map->Flags();
}
if (ioprio_set(1,getpid(),7 | 3 << 13)==-1)
{
tsyslog("failed to set ioprio to 3,7");
}
cEPGSource *source=NULL;
cXMLTVEvent *xevent=import.SearchXMLTVEvent(&db,ChannelID,Event);
if (!xevent)
{
if (!epall)
{
free((void*)ChannelID);
return false;
}
source=sources->GetSource(EITSOURCE);
if (!source) tsyslog("no source for %s",EITSOURCE);
bool useeptext=((epall & EPLIST_USE_STEXTITLE)==EPLIST_USE_STEXTITLE);
if (useeptext) Flags|=(USE_SHORTTEXT|OPT_SEASON_STEXTITLE);
xevent=import.AddXMLTVEvent(source,db,ChannelID,Event,Event->Description(),useeptext);
if (!xevent)
{
free((void*)ChannelID);
return false;
}
}
else
{
source=sources->GetSource(xevent->Source());
}
free((void*)ChannelID);
if (!source)
{
tsyslog("no source for %s",xevent->Source());
delete xevent;
return false;
}
import.PutEvent(source,db,NULL,Event,xevent,Flags);
delete xevent;
return false; // let other handlers change this event
}
bool cEPGHandler::SortSchedule(cSchedule* UNUSED(Schedule))
{
if (db)
{
import.Commit(NULL,db);
sqlite3_close(db);
db=NULL;
}
return false; // we dont sort!
}
// -------------------------------------------------------------
cEPGTimer::cEPGTimer(cGlobals *Global) :
cThread("xmltv2vdr timer"),import(Global)
{
sources=Global->EPGSources();
maps=Global->EPGMappings();
epall=0;
}
void cEPGTimer::Action()
{
if (!import.DBExists()) return; // no database? -> exit immediately
SetPriority(19);
if (ioprio_set(1,getpid(),7 | 3 << 13)==-1)
{
dsyslog("failed to set ioprio to 3,7");
}
cSchedulesLock schedulesLock(true,10); // wait 10ms for lock!
const cSchedules *schedules = cSchedules::Schedules(schedulesLock);
if (!schedules) return;
if (Timers.BeingEdited()) return;
Timers.IncBeingEdited();
sqlite3 *db=NULL;
cEPGSource *source=sources->GetSource(EITSOURCE);
bool useeptext=((epall & EPLIST_USE_STEXTITLE)==EPLIST_USE_STEXTITLE);
int Flags=USE_SEASON;
if (useeptext) Flags|=(USE_SHORTTEXT|OPT_SEASON_STEXTITLE);
for (cTimer *Timer = Timers.First(); Timer; Timer = Timers.Next(Timer))
{
if (Timer->Recording()) continue; // to late ;)
cEvent *event=(cEvent *) Timer->Event();
if (!event) continue;
if (!useeptext)
{
if (!event->ShortText() && !event->Description()) continue; // no text -> no episode
if (event->ShortText() && event->Description())
{
if ((strlen(event->ShortText())+strlen(event->Description()))==0) continue; // no text -> no episode
}
}
if (maps->ProcessChannel(event->ChannelID()) && event->ShortText()) continue; // already processed by xmltv2vdr
const char *ChannelID=strdup(*event->ChannelID().ToString());
cXMLTVEvent *xevent=import.SearchXMLTVEvent(&db,ChannelID,event);
if (!xevent)
{
xevent=import.AddXMLTVEvent(source,db,ChannelID,event,event->Description(),useeptext);
if (!xevent)
{
free((void*)ChannelID);
continue;
}
}
else
{
if (!event->ShortText() && event->Description())
{
import.AddShortTextFromEITDescription(xevent,event->Description());
}
}
free((void*)ChannelID);
import.PutEvent(source,db,NULL,event,xevent,Flags);
delete xevent;
}
if (db)
{
import.Commit(source,db);
sqlite3_close(db);
}
Timers.DecBeingEdited();
}
// -------------------------------------------------------------
cHouseKeeping::cHouseKeeping(cGlobals *Global): cThread("xmltv2vdr housekeeping")
{
global=Global;
}
void cHouseKeeping::checkdir(const char* imgdir, int age, int &cnt, int &lcnt)
{
if (age<=0) return;
DIR *dir=opendir(imgdir);
if (!dir) return;
time_t tmin=time(NULL);
tmin-=(age*86400);
struct dirent dirent_buf,*dirent;
while (readdir_r(dir,&dirent_buf,&dirent)==0)
{
if (!dirent) break;
if (dirent->d_name[0]=='.') continue;
if ((dirent->d_type==DT_LNK) || (dirent->d_type==DT_REG))
{
struct stat statbuf;
char *fpath;
if (asprintf(&fpath,"%s/%s",imgdir,dirent->d_name)!=-1)
{
if (stat(fpath,&statbuf)!=-1)
{
if (statbuf.st_mtime<tmin)
{
if (unlink(fpath)!=-1)
{
if (dirent->d_type==DT_LNK) lcnt++;
if (dirent->d_type==DT_REG) cnt++;
}
}
}
free(fpath);
}
}
}
closedir(dir);
return;
}
int sd_select(const dirent* dirent)
{
if (!dirent) return 0;
if (strstr(dirent->d_name,"-img"))
{
if (dirent->d_type==DT_DIR) return 1;
}
return 0;
}
void cHouseKeeping::Action()
{
if (global->ImgDelAfter() && global->ImgDir())
{
int cnt=0,lcnt=0;
checkdir(global->ImgDir(),global->ImgDelAfter(),cnt,lcnt);
struct dirent **names;
int ret=scandir("/var/lib/epgsources",&names,sd_select,alphasort);
if (ret>0)
{
for (int i=0; i<ret; i++)
{
char *newdir;
if (asprintf(&newdir,"/var/lib/epgsources/%s",names[i]->d_name)!=-1)
{
checkdir(newdir,global->ImgDelAfter(),cnt,lcnt);
free(newdir);
}
}
free(names);
}
if (lcnt)
{
isyslog("removed %i links",lcnt);
}
if (cnt)
{
isyslog("removed %i pics",cnt);
}
}
if (!global->DBExists()) return;
cSchedulesLock schedulesLock(true,10); // wait 10ms for lock!
const cSchedules *schedules = cSchedules::Schedules(schedulesLock);
if (!schedules) return;
sqlite3 *db=NULL;
if (sqlite3_open_v2(global->EPGFile(),&db,SQLITE_OPEN_READWRITE,NULL)==SQLITE_OK)
{
char *sql;
if (asprintf(&sql,"delete from epg where ((starttime+duration) < %li)",time(NULL))!=-1)
{
char *errmsg;
if (sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
esyslog("%s",errmsg);
sqlite3_free(errmsg);
}
else
{
int changes=sqlite3_changes(db);
if (changes)
{
isyslog("removed %i old entries from db",changes);
sqlite3_exec(db,"VACCUM;",NULL,NULL,NULL);
}
}
free(sql);
}
}
sqlite3_close(db);
}
// -------------------------------------------------------------
cEPGSeasonEpisode::cEPGSeasonEpisode(cGlobals *Global): cThread("xmltv2vdr seasonepisode")
{
epgfile=Global->EPGFile();
}
void cEPGSeasonEpisode::Action()
{
// TODO: update season, episode, episodeoverall fields in db
}
// -------------------------------------------------------------
cPluginXmltv2vdr::cPluginXmltv2vdr(void) : housekeeping(&g),epgexecutor(g.EPGSources())
{
// Initialize any member variables here.
// DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
// VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
logfile=NULL;
last_maintime_t=0;
last_timer_t=last_epcheck_t=last_housetime_t=time(NULL); // start this threads later!
g.SetEPAll(0);
g.TEXTMappings()->Add(new cTEXTMapping("country",tr("country")));
g.TEXTMappings()->Add(new cTEXTMapping("year",tr("year")));
g.TEXTMappings()->Add(new cTEXTMapping("originaltitle",tr("originaltitle")));
g.TEXTMappings()->Add(new cTEXTMapping("category",tr("category")));
g.TEXTMappings()->Add(new cTEXTMapping("actor",tr("actor")));
g.TEXTMappings()->Add(new cTEXTMapping("adapter",tr("adapter")));
g.TEXTMappings()->Add(new cTEXTMapping("commentator",tr("commentator")));
g.TEXTMappings()->Add(new cTEXTMapping("composer",tr("composer")));
g.TEXTMappings()->Add(new cTEXTMapping("director",tr("director")));
g.TEXTMappings()->Add(new cTEXTMapping("editor",tr("editor")));
g.TEXTMappings()->Add(new cTEXTMapping("guest",tr("guest")));
g.TEXTMappings()->Add(new cTEXTMapping("presenter",tr("presenter")));
g.TEXTMappings()->Add(new cTEXTMapping("producer",tr("producer")));
g.TEXTMappings()->Add(new cTEXTMapping("writer",tr("writer")));
g.TEXTMappings()->Add(new cTEXTMapping("video",tr("video")));
g.TEXTMappings()->Add(new cTEXTMapping("blacknwhite",tr("blacknwhite")));
g.TEXTMappings()->Add(new cTEXTMapping("audio",tr("audio")));
g.TEXTMappings()->Add(new cTEXTMapping("dolby",tr("dolby")));
g.TEXTMappings()->Add(new cTEXTMapping("dolbydigital",tr("dolbydigital")));
g.TEXTMappings()->Add(new cTEXTMapping("bilingual",tr("bilingual")));
g.TEXTMappings()->Add(new cTEXTMapping("review",tr("review")));
g.TEXTMappings()->Add(new cTEXTMapping("starrating",tr("starrating")));
g.TEXTMappings()->Add(new cTEXTMapping("season",tr("season")));
g.TEXTMappings()->Add(new cTEXTMapping("episode",tr("episode")));
g.TEXTMappings()->Add(new cTEXTMapping("episodeoverall",tr("episodeoverall")));
}
cPluginXmltv2vdr::~cPluginXmltv2vdr()
{
// Clean up after yourself!
#if VDRVERSNUM < 10726 && (!EPGHANDLER)
delete g.epghandler;
#endif
}
int cPluginXmltv2vdr::GetLastImportSource()
{
sqlite3 *db=NULL;
if (sqlite3_open_v2(g.EPGFile(),&db,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) return -1;
char sql[]="select srcidx from epg where srcidx<>99 order by starttime desc limit 1";
sqlite3_stmt *stmt;
int ret=sqlite3_prepare_v2(db,sql,strlen(sql),&stmt,NULL);
if (ret!=SQLITE_OK)
{
esyslog("%i %s (glis)",ret,sqlite3_errmsg(db));
sqlite3_close(db);
return -1;
}
int idx=-1;
if (sqlite3_step(stmt)==SQLITE_ROW)
{
idx=sqlite3_column_int(stmt,0);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
tsyslog("lastimportsource=%i",idx);
return idx;
}
const char *cPluginXmltv2vdr::CommandLineHelp(void)
{
// Return a string that describes all known command line options.
return " -e DIR, --episodes=DIR location of episode files: VDRSeriesTimer .episodes\n"
" or TheTVDB .xml (default is ~/.eplists/lists)\n"
" Add UTF-8 or ISO8859-15 to the path to specify the\n"
" charset used in the files, e.g. /vdr/myepisodes,UTF8\n"
" -E FILE, --epgfile=FILE write the EPG data into the given FILE (default is\n"
" 'epg.db' in the video directory) - best performance\n"
" if located on a ramdisk\n"
" -i DIR --images=DIR location of epgimages\n"
" (default is /var/cache/vdr/epgimages)\n"
" -l FILE --logfile=FILE write trace logs into the given FILE (default is\n"
" no trace log\n";
}
bool cPluginXmltv2vdr::ProcessArgs(int argc, char *argv[])
{
// Command line argument processing
static struct option long_options[] =
{
{ "episodes", required_argument, NULL, 'e'},
{ "epgfile", required_argument, NULL, 'E'},
{ "images", required_argument, NULL, 'i'},
{ "logfile", required_argument, NULL, 'l'},
{ 0,0,0,0 }
};
int c;
while ((c = getopt_long(argc, argv, "l:e:E:i:", long_options, NULL)) != -1)
{
switch (c)
{
case 'e':
g.SetEPDir(optarg);
break;
case 'E':
g.SetEPGFile(optarg);
break;
case 'i':
g.SetImgDir(optarg);
case 'l':
if (logfile) free(logfile);
logfile=strdup(optarg);
break;
default:
return false;
}
}
return true;
}
bool cPluginXmltv2vdr::Initialize(void)
{
// Initialize any background activities the plugin shall perform.
return true;
}
bool cPluginXmltv2vdr::Start(void)
{
// Start any background activities the plugin shall perform.
g.SetConfDir(ConfigDirectory(PLUGIN_NAME_I18N));
isyslog("using codeset '%s'",g.Codeset());
isyslog("using file '%s' for epg database",g.EPGFile());
if (g.EPDir())
{
isyslog("using dir '%s' (%s) for episodes",g.EPDir(),g.EPCodeset());
g.AllocateEPGSeasonThread();
}
if (g.EPAll())
{
g.AllocateEPGTimerThread();
}
if (g.ImgDir()) isyslog("using dir '%s' for epgimages (%i)",g.ImgDir(),g.ImgDelAfter());
g.EPGSources()->ReadIn(&g);
g.epghandler = new cEPGHandler(&g);
g.SetEPAll(g.EPAll());
isyslog("using sqlite v%s",sqlite3_libversion());
if (sqlite3_threadsafe()==0) esyslog("sqlite3 not threadsafe!");
cParse::InitLibXML();
return true;
}
void cPluginXmltv2vdr::Stop(void)
{
// Stop any background activities the plugin is performing.
epgexecutor.Stop();
housekeeping.Stop();
cParse::CleanupLibXML();
if (logfile)
{
free(logfile);
logfile=NULL;
}
}
void cPluginXmltv2vdr::Housekeeping(void)
{
// Perform any cleanup or other regular tasks.
time_t now=time(NULL);
if (now>(last_housetime_t+3600))
{
if (!housekeeping.Active())
{
housekeeping.Start();
}
last_housetime_t=(now / 3600)*3600;
}
}
void cPluginXmltv2vdr::MainThreadHook(void)
{
// Perform actions in the context of the main program thread.
// WARNING: Use with great care - see PLUGINS.html!
time_t now=time(NULL);
if (now>=(last_maintime_t+60))
{
if (!epgexecutor.Active())
{
if (g.EPGSources()->RunItNow()) epgexecutor.Start();
}
last_maintime_t=(now/60)*60;
}
if (g.EPDir())
{
/*
if (now>=(last_epcheck_t+900))
{
if (g.EPGSeasonEpisode()) g.EPGSeasonEpisode()->Start();
last_epcheck_t=(now/900)*900;
}
*/
if (g.EPAll())
{
if (now>=(last_timer_t+600))
{
if (g.EPGTimer()) g.EPGTimer()->Start();
last_timer_t=(now/600)*600;
}
}
}
}
cString cPluginXmltv2vdr::Active(void)
{
// Return a message string if shutdown should be postponed
if (epgexecutor.Active())
{
return tr("xmltv2vdr plugin still working");
}
return NULL;
}
time_t cPluginXmltv2vdr::WakeupTime(void)
{
// Return custom wakeup time for shutdown script
if (!g.WakeUp()) return (time_t) 0;
time_t nextruntime=g.EPGSources()->NextRunTime();
if (nextruntime) nextruntime-=(time_t) 180;
#ifdef VDRDBG
tsyslog("reporting wakeuptime %s",ctime(&nextruntime));
#endif
return nextruntime;
}
const char *cPluginXmltv2vdr::MainMenuEntry(void)
{
// Return a main menu entry
return NULL;
}
cOsdObject *cPluginXmltv2vdr::MainMenuAction(void)
{
// Perform the action when selected from the main VDR menu.
return NULL;
}
cMenuSetupPage *cPluginXmltv2vdr::SetupMenu(void)
{
// Return a setup menu in case the plugin supports one.
return new cMenuSetupXmltv2vdr(&g);
}
bool cPluginXmltv2vdr::SetupParse(const char *Name, const char *Value)
{
// Parse your own setup parameters and store their values.
if (!strncasecmp(Name,"channel",7))
{
if (strlen(Name)<10) return false;
g.EPGMappings()->Add(new cEPGMapping(&Name[8],Value));
}
else if (!strncasecmp(Name,"textmap",7))
{
if (strlen(Name)<10) return false;
cTEXTMapping *textmap=g.TEXTMappings()->GetMap(&Name[8]);
if (textmap)
{
textmap->ChangeValue(Value);
}
else
{
g.TEXTMappings()->Add(new cTEXTMapping(&Name[8],Value));
}
}
else if (!strcasecmp(Name,"options.epall"))
{
g.SetEPAll(atoi(Value));
}
else if (!strcasecmp(Name,"options.wakeup"))
{
g.SetWakeUp((bool) atoi(Value));
}
else if (!strcasecmp(Name,"options.imgdelafter"))
{
g.SetImgDelAfter(atoi(Value));
}
else if (!strcasecmp(Name,"options.order"))
{
g.SetOrder(Value);
}
else if (!strcasecmp(Name,"source.order"))
{
g.SetSrcOrder(Value);
}
else return false;
return true;
}
bool cPluginXmltv2vdr::Service(const char *UNUSED(Id), void *UNUSED(Data))
{
// Handle custom service requests from other plugins
return false;
}
const char **cPluginXmltv2vdr::SVDRPHelpPages(void)
{
// Returns help text
static const char *HelpPages[]=
{
"UPDT [force]\n"
" Start epg update from db, with force download data before\n",
"DELD\n"
" Delete xmltv2vdr epg database (triggers update)\n",
"HOUS\n"
" Start housekeeping manually\n",
"TIMR\n"
" Start timerthread manually\n",
NULL
};
return HelpPages;
}
cString cPluginXmltv2vdr::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
{
// Process SVDRP commands
cString output=NULL;
if (!strcasecmp(Command,"UPDT"))
{
if (!g.EPGSources()->Count())
{
ReplyCode=550;
output="no epg sources installed\n";
}
else
{
if (epgexecutor.Active())
{
ReplyCode=550;
output="update already running\n";
}
else
{
if (Option && strstr(Option,"force"))
{
epgexecutor.SetForceDownload();
}
else
{
epgexecutor.SetForceImport(GetLastImportSource());
}
if (epgexecutor.Start())
{
ReplyCode=250;
output="update started\n";
}
else
{
ReplyCode=550;
output="failed to start update\n";
}
}
}
}
if (!strcasecmp(Command,"DELD"))
{
if (g.EPGFile())
{
if (unlink(g.EPGFile())==-1)
{
ReplyCode=550;
output="failed to delete database\n";
}
else
{
ReplyCode=250;
output="database deleted\n";
}
}
else
{
ReplyCode=550;
output="epgfile parameter not set\n";
}
}
if (!strcasecmp(Command,"TIMR"))
{
if (!epgexecutor.Active() && g.EPGTimer() && !g.EPGTimer()->Active())
{
g.EPGTimer()->Start();
last_timer_t=(time(NULL)/600)*600;
ReplyCode=250;
output="timerthread started\n";
}
else
{
if (g.EPGTimer())
{
ReplyCode=550;
output="system busy\n";
}
else
{
ReplyCode=550;
output="no timerthread\n";
}
}
}
if (!strcasecmp(Command,"HOUS"))
{
if (!epgexecutor.Active() && !housekeeping.Active())
{
housekeeping.Start();
last_housetime_t=(time(NULL)/3600)*3600;
ReplyCode=250;
output="housekeeping started\n";
}
else
{
ReplyCode=550;
output="system busy\n";
}
}
return output;
}
VDRPLUGINCREATOR(cPluginXmltv2vdr) // Don't touch this!
|