summaryrefslogtreecommitdiff
path: root/update.c
blob: a8cbd21e2832a1d17711a0cc38a4ba5a5929630d (plain)
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
/*
 * update.c: TVTV plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include "update.h"


cUpdate::cUpdate()
{
    active = false;
    data.memory = NULL;
    data.size = 0;
    chanmap = new cChannelMap();
}

cUpdate::~cUpdate()
{
    if (active) {
	active = false;
	Cancel(3);
    }

    if (data.memory) {
        free(data.memory);
        data.memory = NULL;
        data.size = 0;
    }
}

void cUpdate::StartUpdate()
{
    if (!active)
	Start();
    else {
	active = false;
	Cancel(3);
	usleep(250);
	Start();
    }
}

int cUpdate::ReloadChannelMap() {
  return chanmap->ReloadChannelMap();
}

void cUpdate::Action(void)
{
    dsyslog("TVTV: Timer Thread started (pid=%d)", getpid());
    active = true;
    do {
	MakeTimerUpdate();
	if (TVTVConfig.autoupdate && TVTVConfig.updatetime > 0)
	    for (long i = 0; active && i < TVTVConfig.updatetime * 60 * 2; i++)
		usleep(500000);
    }
    while (active && TVTVConfig.autoupdate && TVTVConfig.updatetime > 0);
    
    active = false;
    dsyslog("TVTV: Timer Thread ended (pid=%d)", getpid());
    Cancel(0);
}


void cUpdate::Get_Packed_String(char *sOut)
{
  char *s;
  unsigned char signature[17];

  asprintf(&s, "EPGSync%s%s", TVTVConfig.username, TVTVConfig.password);

  MD5_CTX ctx;
  MD5Init(&ctx);
  MD5Update(&ctx, (unsigned char *)s, strlen(s));
  MD5Final(signature, &ctx);
  signature[16] = 0;

  char aStr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

  char aOut[30];
  int j = 0;

  aOut[16] = '\0';

  for (int i = 0; i < 16; i+= 3) {
      uint8_t x, y;

      x = signature[i];
      x >>= 2;
      x = x & 0x3f;
      aOut[j++] = aStr[x];

      y = signature[i];
      y = y & 0x3;
      y <<= 4;

      x = signature[i + 1];
      x >>= 4;
      x = x & 0x0f;
      x = x ^ y;
      aOut[j++] = aStr[x];

      if (i + 1 < 16) {
	  y = signature[i + 1];
	  y = y & 0x0f;
	  y <<= 2;

	  // 3
	  x = signature[i + 2];
	  x = x & 0xff;
	  x >>= 6;
	  x = x ^ y;
	  aOut[j++] = aStr[x];

	  // 4
	  x = signature[i + 2];
	  x &= 0x3f;
	  aOut[j++] = aStr[x];
        }
      else {
	  aOut[j++] = (char) 0x3d;
	  aOut[j++] = (char) 0x3d;
        }
      aOut[j] = '\0';
    }

  strcpy(sOut, aOut);
  free(s);
}


static size_t WriteMemoryCallback( void *ptr, size_t size, size_t nmemb, void *data) {
    size_t realsize = size * nmemb;
    struct MemoryStruct *mem = (struct MemoryStruct *)data;

    if (mem->memory) 
        mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
    else
	mem->memory = (char *)malloc(mem->size + realsize + 1);
    
    if (mem->memory) {
        memcpy(&(mem->memory[mem->size]), ptr, realsize);
        mem->size += realsize;
        mem->memory[mem->size] = 0;
    }
    return realsize;
}


int cUpdate::DownloadCSVData(const char *url) {
    CURL *curl_handle;

    // Initialize 'data' struct
    if (data.memory) free(data.memory);
    data.memory = NULL;
    data.size = 0;
    
    if (curl_global_init(CURL_GLOBAL_ALL) != 0) {
        esyslog("TVTV: Something went wrong with curl_global_init()");
	return -1;
    }
    curl_handle = curl_easy_init();
    if (curl_handle == NULL) {
        esyslog("TVTV: Unable to get handle from curl_easy_init()");
	return -1;
    }
    curl_easy_setopt(curl_handle, CURLOPT_URL, url);  // Specify URL to get
    
    if (TVTVConfig.useproxy) {
        curl_easy_setopt(curl_handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
	curl_easy_setopt(curl_handle, CURLOPT_PROXY, TVTVConfig.httpproxy);  // Specify HTTP proxy
    }

    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);  // Send all data to this function
    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&data);  // Pass our 'data' struct to the callback function
    curl_easy_setopt(curl_handle, CURLOPT_MAXFILESIZE, 1048576);  // Set maximum file size to get (bytes)
    curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);  // No progress meter
    curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);  // No signaling
    curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);  // Set timeout to 30 seconds
    curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, TVTV_USERAGENT);  // Some servers don't like requests that are made without a user-agent field
    
    if (curl_easy_perform(curl_handle) != 0) {
	curl_easy_cleanup(curl_handle);  // Cleanup curl stuff
	if (data.memory) {
	    free(data.memory);
	    data.memory = NULL;
	    data.size = 0;
	}
	esyslog("TVTV: Couldn't download the stream");
	return -1;
    }

    curl_easy_cleanup(curl_handle);  // Cleanup curl stuff
    return data.size;
}


uint8_t cUpdate::toHex(const uint8_t &x)
{
    return x > 9 ? x + 55: x + 48;
}
    
void cUpdate::URLEncode(const char *sIn, char *sOut)
{
    register char *pOutTmp = NULL;
    char *pOutBuf = NULL;
    register char *pInTmp = NULL;
    char *pInBuf = (char *)sIn;
    
    //alloc out buffer
    pOutBuf = sOut;
	
    if(pOutBuf) {
        pInTmp	= pInBuf;
        pOutTmp = pOutBuf;
				    
	// do encoding
	while (*pInTmp) {
	    if(isalnum(*pInTmp))
	        *pOutTmp++ = *pInTmp;
	    else
	        if(isspace(*pInTmp))
		    *pOutTmp++ = '+';
	        else {
	            *pOutTmp++ = '%';
		    *pOutTmp++ = toHex(*pInTmp>>4);
	            *pOutTmp++ = toHex(*pInTmp%16);
		}
	    pInTmp++;
	}
	*pOutTmp = '\0';
    }
}


bool cUpdate::MakeTimerUpdate(void)
{
    char *sEnCryptS = NULL;
    char *sPackedString = NULL;
    char *url = NULL;
    
    sPackedString = (char *)calloc(30, sizeof (char));
    sEnCryptS = (char *)calloc(100, sizeof (char));

    Get_Packed_String(sPackedString);
    dsyslog("TVTV: Packed String: %s", sPackedString);
    URLEncode(sPackedString, sEnCryptS);    

    asprintf(&url, "http://%s/cgi-bin/WebObjects/TVSync.woa/wa/getjobs?&serial=0&account=%s&product=35&target=%s&doctype=csv&access=%s",
             TVTV_SERVERS[TVTVConfig.tvtv_server], TVTVConfig.username, TVTV_SERVERS[TVTVConfig.tvtv_server], sEnCryptS);

    isyslog("TVTV: Timer Update started");
    
    int iFileSize = DownloadCSVData(url);
    
    if (iFileSize > 0) {
	dsyslog("TVTV: Received %d Bytes", iFileSize);
	/* process CSV file */
	ProcessImportedFile(data.memory);
	isyslog("TVTV: Timer File processed");
    }
    else {
	esyslog("TVTV: Download Error");
    }

    // Clean up
    if (data.memory) {
	free(data.memory);
	data.memory = NULL;
	data.size = 0;
    }
    if (url) free(url);
    if (sEnCryptS) free(sEnCryptS);
    if (sPackedString) free(sPackedString);
    
    return true;
}


int cUpdate::calc_field_cnt(string *s) {
  int n=0;
  size_t p = 0;

  if ((s == NULL) || (s->empty())) return 0;

  p = s->find(',', 0);
  while (p != string::npos) {
    n++;
    p = s->find(',', p+1);
  }
  n++;
  return n;
}

char *cUpdate::strip_str(char *s) {
  char *m=NULL;
  unsigned int i=0;

  if (s==NULL) return NULL;

  m=(char *)calloc(strlen(s)+1, sizeof(char));

  for (i=0; i<strlen(s); i++) {
    if (s[i] == '"') {
      if (s[i+1] == '"') {
	strncat(m, s+i, 1);
	i++;
      }
    } else if ((s[i] == '\\')&&(s[i+1] == 'n')) {
      strncat(m, "\n", 1);
      i++;
    } else {
      strncat(m, s+i, 1);
    }
  }
  return m;
}


char **cUpdate::split_csv(const char *job_line, int field_cnt) {

  char **arr=NULL;
  char *sp=NULL, *ep=NULL;
  char *s=NULL;
  int i=0;
  int n=0;
  int fini=0;

  if ((job_line == NULL)||(field_cnt == 0)) { return NULL; }

  arr=(char **)calloc(field_cnt, sizeof(char *));
  sp=(char *)job_line;
  
  while ((n < field_cnt)&&(fini == 0)) {
    if (*sp == '"') {
      sp++;
      ep=index(sp, '"');
      if (ep != NULL) {
        while ( *(ep+1) == '"' ) { ep=index(ep+2, '"'); }
        ep++;
      } else {
 	ep=(char *)job_line + strlen(job_line);
	fini=1;
      }
    } else {
      ep=index(sp, ',');
      if (ep == NULL) {
	ep=(char *)job_line + strlen(job_line);
	fini=1;
      }
    }
    s=(char *)calloc(ep-sp+1, sizeof(char));
    strncpy(s, sp, ep-sp);
    arr[n]=strip_str(s);
    free(s);
    
    sp=ep+1;
    n++;
    
    if ((n < field_cnt)&&(fini==1)) {
      for(i=0; i<n; i++) free(arr[i]);
      free(arr);
      arr=NULL;
    }
    
  }
  return arr;
}


string *cUpdate::read_line_from_buffer(const char *buf, unsigned int *idx) {
  string s;

  if (buf == NULL) return NULL;

  while ((buf[*idx] != 0)&&(buf[*idx] != '\n')) {
    if (!iscntrl(buf[*idx])) s.append(1, buf[*idx]);
    (*idx)++;
  }
  while ( (buf[*idx] != 0) && (iscntrl(buf[*idx]) )) (*idx)++;
  return new string(s);
}

void cUpdate::ProcessImportedFile(const char *sBuffer)
{
    char **fields=NULL;
    char **tvtvjob=NULL;
    int field_cnt=0;
    unsigned int p=0;
    size_t rp=0;
    bool vps = false;
    tChannelID vdrch, channelID;
    bool timer_update=false;
#if VDRVERSNUM < 10336
    unsigned int max_desc_len=0;
#endif

    time_t tStartTime, tVpsTime, tEndTime, tCurrentTime;
    struct tm tStart, tVps, tEnd;
    int StartTZ, VpsTZ, EndTZ;

    int tvtv_bugfix_secs = 0; /* seconds of TVTV bugfix (sends sometimes UTC instead of local time) */
    struct tm *timelocal;
    time_t tloc;
		
    cTimer *oTimer = NULL;
    cTimer *ti = NULL;
    cChannel *oTVTVChannel = NULL;

    map<string, string> tvtv_timer;
    string *sLine=NULL;
    string s;

    const string sTokenTvtvUid = "TVTV-UID: ";
    
    dsyslog("TVTV: Processing CSV data");

    // skip header
    do {
      sLine = read_line_from_buffer(sBuffer, &p);
      field_cnt = calc_field_cnt(sLine);
    } while (( field_cnt < 10 ) && ( !sLine->empty() ));
    fields=split_csv(sLine->c_str(), field_cnt);
    dsyslog("TVTV: Found %d fields in CSV data", field_cnt);

    // read timer jobs and create timers
    dsyslog("TVTV: Start reading timer jobs");
    sLine = read_line_from_buffer(sBuffer, &p);
    while (!sLine->empty()) {
      dsyslog("TVTV: Received '%s...'", sLine->substr(0,130).c_str());
      tvtvjob=split_csv(sLine->c_str(), field_cnt);
      if (tvtvjob != NULL) {
        for (int i=0; i<field_cnt; i++) {
	  tvtv_timer[fields[i]] = tvtvjob[i];
	  free(tvtvjob[i]);
        }
        free(tvtvjob);
      }

      // replace newline characters by '|'
      rp = tvtv_timer[DEF_TVTV_SCHEDULE_DESC].find('\n',0 );
      while (rp != string::npos) { tvtv_timer[DEF_TVTV_SCHEDULE_DESC].replace(rp, 1, 1, '|'); rp = tvtv_timer[DEF_TVTV_SCHEDULE_DESC].find('\n',0 ); }
      rp = tvtv_timer[DEF_TVTV_SCHEDULE_PERS].find('\n',0 );
      while (rp != string::npos) { tvtv_timer[DEF_TVTV_SCHEDULE_PERS].replace(rp, 1, 1, '|'); rp = tvtv_timer[DEF_TVTV_SCHEDULE_PERS].find('\n',0 ); }

      // Get ChannelID from channel map
      vdrch=chanmap->GetChanID(atoi(tvtv_timer[DEF_TVTV_SCHEDULE_CHID].c_str()));
      if (!(vdrch == tChannelID::InvalidID)) {

#if VDRVERSNUM >= 10305
	oTVTVChannel = Channels.GetByChannelID(vdrch, true, true);
#else
        oTVTVChannel = Channels.GetByChannelID(vdrch, true);
#endif
	if (oTVTVChannel) {

	  oTimer = new cTimer;
			
	  // Calc Start and Stop Time
	  sscanf(tvtv_timer[DEF_TVTV_SCHEDULE_STM].c_str(), "%04d-%02d-%02d %02d:%02d:%02d %d",
		 &tStart.tm_year, &tStart.tm_mon, &tStart.tm_mday,
		 &tStart.tm_hour, &tStart.tm_min, &tStart.tm_sec, &StartTZ);
	      
	  sscanf(tvtv_timer[DEF_TVTV_SCHEDULE_VTM].c_str(), "%04d-%02d-%02d %02d:%02d:%02d %d",
		 &tVps.tm_year, &tVps.tm_mon, &tVps.tm_mday,
		 &tVps.tm_hour, &tVps.tm_min, &tVps.tm_sec, &VpsTZ);

	  sscanf(tvtv_timer[DEF_TVTV_SCHEDULE_ETM].c_str(), "%04d-%02d-%02d %02d:%02d:%02d %d",
		 &tEnd.tm_year, &tEnd.tm_mon, &tEnd.tm_mday,
		 &tEnd.tm_hour, &tEnd.tm_min, &tEnd.tm_sec, &EndTZ);
		  
	  tStart.tm_year -= 1900; tStart.tm_mon  -= 1; tStart.tm_isdst = -1;
	  tVps.tm_year   -= 1900; tVps.tm_mon    -= 1; tVps.tm_isdst   = -1;
	  tEnd.tm_year   -= 1900; tEnd.tm_mon    -= 1; tEnd.tm_isdst   = -1;		

	  tStartTime = timegm(&tStart);
	  tVpsTime   = timegm(&tVps);
	  tEndTime   = timegm(&tEnd);
	  time(&tCurrentTime); /* current time */

// VPS was introduced with VDR 1.3.5
#if VDRVERSNUM >= 10305
	  vps = Setup.UseVps && (tVpsTime != -1) && TVTVConfig.usevps;
#else
	  vps = false;
#endif	        

	  // fix buggy received timezones
	  timelocal = localtime(&tStartTime);
	  if (timelocal->tm_gmtoff / 36 != StartTZ) {
            dsyslog("TVTV: buggy timezone in tSTartTime detected: %+05d, fix to: %+05d", StartTZ, (int) (timelocal->tm_gmtoff / 36));
	    StartTZ = timelocal->tm_gmtoff / 36;
 	  };

	  timelocal = localtime(&tEndTime);
	  if (timelocal->tm_gmtoff / 36 != EndTZ) {
            dsyslog("TVTV: buggy timezone in tEndTime detected: %+05d, fix to: %+05d", EndTZ, (int) (timelocal->tm_gmtoff / 36));
	    EndTZ = timelocal->tm_gmtoff / 36;
 	  };

          tStartTime -= StartTZ*36;
	  tEndTime -= EndTZ*36;
			
	  if (vps) {
  	    timelocal = localtime(&tVpsTime);
	    if (timelocal->tm_gmtoff / 36 != VpsTZ) {
              dsyslog("TVTV: buggy timezone in tVpsTime detected: %+05d, fix to: %+05d", VpsTZ, (int) (timelocal->tm_gmtoff / 36));
	      VpsTZ = timelocal->tm_gmtoff / 36;
 	    };
	    tVpsTime -= VpsTZ*36;
	  };

	  tStartTime -= Setup.MarginStart * 60;
	  if (!vps) tEndTime += Setup.MarginStop * 60;
	  localtime_r(&tStartTime, &tStart);
	  localtime_r(&tVpsTime, &tVps);
	  localtime_r(&tEndTime, &tEnd);

	  s = ""; // start

	  // Adding optional Station Name to File Name to make it more unique
	  if (TVTVConfig.usestation) 
#if VDRVERSNUM >= 10315
            s = s + oTVTVChannel->ShortName(true) + "~";
#else
            s = s + oTVTVChannel->Name() + "~";
#endif

	  // Format as 1st
	  if (((TVTVConfig.FormatRecordName == eRecordName_FormatNatureTitle) || (TVTVConfig.FormatRecordName == eRecordName_FormatTitleNature)) && ! tvtv_timer[DEF_TVTV_SCHEDULE_FRM].empty()) {
	    s = tvtv_timer[DEF_TVTV_SCHEDULE_FRM] + "~";
	  };

	  // Nature as 1st or 2nd before title
	  if (((TVTVConfig.FormatRecordName == eRecordName_NatureTitle) || (TVTVConfig.FormatRecordName == eRecordName_FormatNatureTitle)) && ! tvtv_timer[DEF_TVTV_SCHEDULE_NAT].empty()) {
	    s = tvtv_timer[DEF_TVTV_SCHEDULE_NAT] + "~";
	  };

	  // Add Title to File Name
	  s += tvtv_timer[DEF_TVTV_SCHEDULE_TIT];

	  // Nature after title
	  if (((TVTVConfig.FormatRecordName == eRecordName_TitleNature) || (TVTVConfig.FormatRecordName == eRecordName_FormatTitleNature)) && ! tvtv_timer[DEF_TVTV_SCHEDULE_NAT].empty()) {
	    s = "~" + tvtv_timer[DEF_TVTV_SCHEDULE_NAT];
	  };

	  // Colons should be replaced by '|' in title
	  rp = s.find(':',0 ); 
	  while (rp != string::npos) { s.replace(rp, 1, 1, '|'); rp = s.find(':',0 ); }
	  
	  // create timer string
	  ostringstream oss(ostringstream::out);
	  oss.fill('0');
#if VDRVERSNUM >= 10305
	  oss << (tfActive|(vps?tfVps:tfNone)) << ":";
#else
          oss << "1:";
#endif
	  channelID = oTVTVChannel->GetChannelID();
	  oss << *channelID.ToString();
	  oss << ":";

// Starting with VDR 1.3.23 timer entries support a full date in ISO notation. 
#if VDRVERSNUM >= 10323
          oss.width(4); oss << ((vps?(tVps.tm_year):(tStart.tm_year))+1900) << "-";
	  oss.width(2); oss << ((vps?(tVps.tm_mon):(tStart.tm_mon))+1) << "-";
#endif
          oss.width(2); oss << (vps?(tVps.tm_mday):(tStart.tm_mday)) << ":";
	  oss.width(4); oss << (vps?(tVps.tm_hour * 100 + tVps.tm_min):(tStart.tm_hour * 100 + tStart.tm_min)) << ":";
	  oss.width(4); oss << tEnd.tm_hour * 100 + tEnd.tm_min << ":";
	  oss << Setup.DefaultPriority << ":";
	  oss << Setup.DefaultLifetime << ":";
	  oss << s.c_str() << ":";
	  
	  dsyslog("TVTV: Timer entry '%s'", oss.str().c_str());
	  s=oss.str();

// Starting with VDR 1.3.44 the description within info.vdr is taken
// just from EPG. A description coming with a timer is added as a comment
// to info.vdr. This setup parameter decides whether to add this description
// as a comment or not.
#if VDRVERSNUM >= 10344
	  if (TVTVConfig.usetvtvdescr) {
#endif
	  
// Recording summary changed in VDR 1.3.25
#if VDRVERSNUM < 10325
	    s += tvtv_timer[DEF_TVTV_SCHEDULE_TIT];
	    if ((tvtv_timer[DEF_TVTV_SCHEDULE_PERS].length() > 0)||(tvtv_timer[DEF_TVTV_SCHEDULE_DESC].length() > 0)) s += "||";
#endif

	    if (tvtv_timer[DEF_TVTV_SCHEDULE_PERS].length() > 0) {
	      s += "Darsteller: " + tvtv_timer[DEF_TVTV_SCHEDULE_PERS];
  	      if (tvtv_timer[DEF_TVTV_SCHEDULE_DESC].length() > 0) s += "||" + tvtv_timer[DEF_TVTV_SCHEDULE_DESC];
	    } else {
	      if (tvtv_timer[DEF_TVTV_SCHEDULE_DESC].length() > 0) s += tvtv_timer[DEF_TVTV_SCHEDULE_DESC];
	    }

// Length of timer entry is limited to less than 10240 characters. Keeping off another
// 15 to avoid problems with additional characters (like newline). 
// This is not needed for VDR 1.3.36+ anymore!
#if VDRVERSNUM < 10336
	    max_desc_len = MAXPARSEBUFFER - sTokenTvtvUid.length() - tvtv_timer[DEF_TVTV_SCHEDULE_UID].length() - 15;
	    if (s.length() > max_desc_len) {
	      dsyslog("TVTV: received description too long, cutted (%d -> %d)", s.length(), max_desc_len);
	      s.erase(max_desc_len);
	    }
#endif

#if VDRVERSNUM >= 10344
	  }
#endif
	  // Add Timer's UID to description to identify it later on
	  if (tvtv_timer[DEF_TVTV_SCHEDULE_UID].length() > 0) s += "||" + sTokenTvtvUid + tvtv_timer[DEF_TVTV_SCHEDULE_UID];

	  
          if (oTimer->Parse(s.c_str())) {
	    cTimer *t = Timers.GetTimer(oTimer);
	    if (t) { // Timer exists
	      if (tvtv_timer[DEF_TVTV_SCHEDULE_ACT] == "delete") {
		if (t->Recording()) {
		  isyslog("TVTV: timer %d recording, will be deleted after recording has ended (%s)", t->Index() + 1, t->File());
		} else {
		  isyslog("TVTV: timer %d deleted [%s/%s/%d/%04d-%04d/%s]", oTimer->Index() + 1, 
		                                                         tvtv_timer[DEF_TVTV_SCHEDULE_UID].c_str(), 
		                                                         tvtv_timer[DEF_TVTV_SCHEDULE_CHN].c_str(), 
									 vps ? (tVps.tm_mday):(tStart.tm_mday), 
									 vps ? (tVps.tm_hour * 100 + tVps.tm_min):(tStart.tm_hour * 100 + tStart.tm_min), 
									 tEnd.tm_hour * 100 + tEnd.tm_min,
									 vps ? "VPS":"-");
		  Timers.Del(t);
		}
	      } else {
		isyslog("TVTV: timer %d exist (%s)", t->Index() + 1, t->File());
	      }
	      delete oTimer;
	    } else { // if (t)

	      // try to figure out if timer needs to be updated
	      timer_update=false;
	      ti = Timers.First(); 
	      while (ti != NULL) { 
#if VDRVERSNUM >= 10344
		if (ti->Aux() != NULL) {
		  s=ti->Aux();
#else
                if (ti->Summary() != NULL) {
	          s=ti->Summary();
#endif
		  if (s.find(sTokenTvtvUid + tvtv_timer[DEF_TVTV_SCHEDULE_UID], 0) != string::npos) {
		    timer_update=true;
		    break;
		  } else {
		    ti = Timers.Next(ti); 
		  }
		} else {
		  ti = Timers.Next(ti);
		}
	      }

	      if (timer_update) { // Timer exists
		if (tvtv_timer[DEF_TVTV_SCHEDULE_ACT] == "rec") {
		  // avoid Timer updates if Timer is shifted (TVTV UTC Problem)
		  if (TVTVConfig.tvtv_bugfix == eTimeShiftBugfixManual) {
		  	  // exactly configured hours
		          dsyslog("TVTV: manual configured timezone shift: %d hrs", TVTVConfig.tvtv_bugfix_hrs);
			  tvtv_bugfix_secs = TVTVConfig.tvtv_bugfix_hrs * 3600;
		  } else if (TVTVConfig.tvtv_bugfix == eTimeShiftBugfixAuto) {
			  // autodetect time zone distance
			  time(&tloc);
			  timelocal = localtime(&tloc);
		          dsyslog("TVTV: autodetected timezone: %s, shift: %d hrs", timelocal->tm_zone, (int) (timelocal->tm_gmtoff / 3600));
			  tvtv_bugfix_secs = timelocal->tm_gmtoff;
		  };

		  if ((TVTVConfig.tvtv_bugfix != eTimeShiftBugfixOff) && ((((ti->StartTime() - tStartTime) == tvtv_bugfix_secs) && 
		                                  ((ti->StopTime() - tEndTime) == tvtv_bugfix_secs)) || 
		                                 (vps && ((ti->StopTime() - tEndTime) == tvtv_bugfix_secs)) )) {
		    isyslog("TVTV: timer %d update rejected (%s) [%s/%s/%d/%04d-%04d/%s]", ti->Index() + 1, 
		                                                                        ti->File(), 
											tvtv_timer[DEF_TVTV_SCHEDULE_UID].c_str(), 
											tvtv_timer[DEF_TVTV_SCHEDULE_CHN].c_str(), 
											vps ? (tVps.tm_mday):(tStart.tm_mday), 
											vps ? (tVps.tm_hour * 100 + tVps.tm_min):(tStart.tm_hour * 100 + tStart.tm_min), 
											tEnd.tm_hour * 100 + tEnd.tm_min,
											vps ? "VPS":"-"
											);
		  } else {
		    if (ti->Recording()) {
		      isyslog("TVTV: timer %d recording, will be deleted after recording has ended (%s)", ti->Index() + 1, ti->File());
		    } else {
		      Timers.Del(ti);
		    }
		    Timers.Add(oTimer);
		    isyslog("TVTV: timer %d updated (%s) [%s/%s/%d/%04d-%04d/%s]", oTimer->Index() + 1, 
		                                                                oTimer->File(), 
										tvtv_timer[DEF_TVTV_SCHEDULE_UID].c_str(), 
										tvtv_timer[DEF_TVTV_SCHEDULE_CHN].c_str(), 
										vps ? (tVps.tm_mday):(tStart.tm_mday), 
										vps ? (tVps.tm_hour * 100 + tVps.tm_min):(tStart.tm_hour * 100 + tStart.tm_min), 
										tEnd.tm_hour * 100 + tEnd.tm_min,
										vps ? "VPS":"-"
										);
		  }
		} else if (tvtv_timer[DEF_TVTV_SCHEDULE_ACT] == "delete") {
		  if (ti->Recording()) {
		    isyslog("TVTV: timer %d recording, will be deleted after recording has ended (%s)", ti->Index() + 1, ti->File());
		  } else {
		    isyslog("TVTV: timer %d deleted [%s/%s/%d/%04d-%04d/%s]", oTimer->Index() + 1, 
		                                                           tvtv_timer[DEF_TVTV_SCHEDULE_UID].c_str(), 
									   tvtv_timer[DEF_TVTV_SCHEDULE_CHN].c_str(), 
									   vps ? (tVps.tm_mday):(tStart.tm_mday), 
									   vps ? (tVps.tm_hour * 100 + tVps.tm_min):(tStart.tm_hour * 100 + tStart.tm_min), 
									   tEnd.tm_hour * 100 + tEnd.tm_min,
									   vps ? "VPS":"-");
		    Timers.Del(ti);
		  }
		} 
	      } else { // if (timer_update)
		if (tvtv_timer[DEF_TVTV_SCHEDULE_ACT] == "rec") {

		  if ((tEndTime < tCurrentTime) && (vps == false)) {
		    // Do not add timer entry in the past, if vps is not active
		    isyslog("TVTV: timer NOT added (EndTime in the past) (%s) [%s/%s/%d/%04d-%04d/%s]", 
		                                                            oTimer->File(), 
									    tvtv_timer[DEF_TVTV_SCHEDULE_UID].c_str(), 
									    tvtv_timer[DEF_TVTV_SCHEDULE_CHN].c_str(), 
									    vps ? (tVps.tm_mday):(tStart.tm_mday), 
									    vps ? (tVps.tm_hour * 100 + tVps.tm_min):(tStart.tm_hour * 100 + tStart.tm_min), 
									    tEnd.tm_hour * 100 + tEnd.tm_min,
									    vps ? "VPS":"-");
		  } else { // if (tEndTime < tCurrentTime)

		    if ((tStartTime < tCurrentTime) && (vps == false) && (TVTVConfig.AddOngoingNonVpsTimers == 0)) {
  		      // Do not add timer entry with start time in the past, if vps is not active
		      isyslog("TVTV: timer NOT added (StartTime in the past & AddOngoingNonVpsTimers=off) (%s) [%s/%s/%d/%04d-%04d/%s]", 
		                                                            oTimer->File(), 
									    tvtv_timer[DEF_TVTV_SCHEDULE_UID].c_str(), 
									    tvtv_timer[DEF_TVTV_SCHEDULE_CHN].c_str(), 
									    vps ? (tVps.tm_mday):(tStart.tm_mday), 
									    vps ? (tVps.tm_hour * 100 + tVps.tm_min):(tStart.tm_hour * 100 + tStart.tm_min), 
									    tEnd.tm_hour * 100 + tEnd.tm_min,
									    vps ? "VPS":"-");
                    } else {
		      Timers.Add(oTimer);

		      isyslog("TVTV: timer %d added (%s) [%s/%s/%d/%04d-%04d/%s]", oTimer->Index() + 1, 
		                                                            oTimer->File(), 
									    tvtv_timer[DEF_TVTV_SCHEDULE_UID].c_str(), 
									    tvtv_timer[DEF_TVTV_SCHEDULE_CHN].c_str(), 
									    vps ? (tVps.tm_mday):(tStart.tm_mday), 
									    vps ? (tVps.tm_hour * 100 + tVps.tm_min):(tStart.tm_hour * 100 + tStart.tm_min), 
									    tEnd.tm_hour * 100 + tEnd.tm_min,
									    vps ? "VPS":"-");
		      if ((tEndTime < tCurrentTime) && (vps == false)) {
		        isyslog("TVTV: timer %d notice: StartTime is behind CurrentTime (AddOngoingNonVpsTimers=on)", oTimer->Index() + 1);
                      };

		    }
		  } // if (tEndTime < tCurrentTime)
		}
	      }
		  
	    }
	  } else {
	    isyslog("TVTV: Timer Error: %s", s.c_str());
	  }

	} else { // if (oTVTVChannel)
	  isyslog("TVTV: ChannelID <%s> for Channel <%s> was not found in channels.conf!", (const char *)vdrch.ToString(), tvtv_timer[DEF_TVTV_SCHEDULE_CHN].c_str());
	}

      } else { // if (!(vdrch == tChannelID::InvalidID))
	isyslog("TVTV: Channel <%s (ID: %d)> is not found!", tvtv_timer[DEF_TVTV_SCHEDULE_CHN].c_str(), atoi(tvtv_timer[DEF_TVTV_SCHEDULE_CHID].c_str()));
      }

      // read next line
      sLine = read_line_from_buffer(sBuffer, &p);
      tvtv_timer.clear();

    } // while (!sLine->empty())

    Timers.Save();
}