File: rss2ical.c

package info (click to toggle)
redland 1.0.17-1.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 8,780 kB
  • ctags: 4,263
  • sloc: ansic: 37,638; sh: 12,115; perl: 2,590; xml: 807; makefile: 587
file content (451 lines) | stat: -rw-r--r-- 11,147 bytes parent folder | download | duplicates (4)
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
/* rss2ical.c: Turn RSS into ical 
 *
 * Copyright (C) 2008, David Beckett http://www.dajobe.org/
 *
 * This package is Free Software and part of Redland http://librdf.org/
 * 
 * It is licensed under the following three licenses as alternatives:
 *   1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
 *   2. GNU General Public License (GPL) V2 or any newer version
 *   3. Apache License, V2.0 or any newer version
 * 
 * You may not use this file except in compliance with at least one of
 * the above three licenses.
 * 
 * See LICENSE.html or LICENSE.txt at the top of this package for the
 * complete terms and further detail along with the license texts for
 * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
 * 
 *
 * USAGE: rss2ical [URI of RSS/Atom Feed] [Calendar Title] > result.ics
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <redland.h>


#undef RSS2ICAL_DEBUG

#define DO_ESCAPE_NL 0

static const unsigned char* get_items_query=(const unsigned char*)
"PREFIX rss: <http://purl.org/rss/1.0/>\n\
PREFIX dc: <http://purl.org/dc/elements/1.1/>\n\
PREFIX content: <http://web.resource.org/rss/1.0/modules/content/>\n\
SELECT ?item ?date ?title ?description ?creator\n\
WHERE {\n\
 ?item a rss:item;\n\
       dc:date ?date;\n\
       rss:title ?title;\n\
       rss:description ?description .\n\
 OPTIONAL { ?item dc:creator ?creator . }\n\
}";

/*
  Removed for now as OPTIONAL in Rasqal is fragile/broken:
  OPTIONAL { ?item  dc:source ?source } \n\
  OPTIONAL { ?item  content:encoded ?htmldesc } \n      \
*/

/* %s-prod id %s-cal name id %s-rel cal id %s-tzone %s-tzone */
static const char *ical_header_format="\
BEGIN:VCALENDAR\r\n\
VERSION:2.0\r\n\
PRODID:%s\r\n\
X-WR-CALNAME:%s\r\n\
X-WR-RELCALID:%s\r\n\
X-WR-TIMEZONE:%s\r\n\
CALSCALE:GREGORIAN\r\n\
METHOD:PUBLISH\r\n\
BEGIN:VTIMEZONE\r\n\
TZID:%s\r\n\
BEGIN:DAYLIGHT\r\n\
DTSTART:20060326T020000\r\n\
TZOFFSETTO:+0100\r\n\
TZOFFSETFROM:+0000\r\n\
TZNAME:BST\r\n\
END:DAYLIGHT\r\n\
BEGIN:STANDARD\r\n\
DTSTART:20061029T020000\r\n\
TZOFFSETTO:+0000\r\n\
TZOFFSETFROM:+0100\r\n\
TZNAME:GMT\r\n\
END:STANDARD\r\n\
END:VTIMEZONE\r\n\
";

static const char *ical_footer_format="\
END:VCALENDAR\r\n\
";

static const char *tzone="Europe/London";

static  char *program=NULL;


static void
ical_print(FILE *fh, const char *line)  
{
  fputs(line, fh);
  fwrite("\r\n", 1, 2, fh);
}


static void
ical_format(FILE *fh, const char *key, const char *attr,
            const char *escapes, const unsigned char *value) 
{
  int col=0;
  int i=0;
  size_t len;
  int c;
  int lineno=0;
  
  len=strlen(key);
  fwrite(key, 1, len, fh);
  col += len;

  if(attr) {
    fputc(';', fh);
    col++;
    len=strlen(attr);
    fwrite(attr, 1, len, fh);
    col += len;
  }
  
  fputc(':', fh);
  col++;

  for(i=0; (c=value[i]); i++)  {
    if(col == 75) {
      fwrite("\r\n ", 1, 3, fh);
      col=0;
      lineno++;
    }
    if(c == '\\' || 
       (escapes && (strchr(escapes, c) != NULL))) {
      fputc('\\', fh);
      col++;
    }
    if(c == '\n') {
#ifdef DO_ESCAPE_NL
      fputc('\\', fh);
      col++;
      c='n';
#else
      c=' ';
#endif
    }
    fputc(c, fh);
    col++;
  }
  fwrite("\r\n", 1, 2, fh);
}


static unsigned char*
iso2vcaldate(const unsigned char* iso_date) 
{
  unsigned char* vcaldate;
  unsigned char c;
  int i, j;
  
  /* YYYY-MM-DDTHH:MM:SSZ }
   * YYYY-MM-DDTHH:MMZ    } to YYYYMMDDTHHMMSSZ   
   * ...                  }
   */
  vcaldate=(unsigned char*)malloc(17);
  strncpy((char*)vcaldate, "00000000T000000Z", 17);
  for(i=0, j=0; (c=iso_date[i]); i++) {
    if(c == 'Z')
      break;
    if(c != ':' && c != '-')
      vcaldate[j++]=iso_date[i];
  }

  return vcaldate;
}



static unsigned char*
remove_html_entities(unsigned char* html_desc, size_t len)
{
  int i, j;
  unsigned char* description;
  unsigned char c;
  
  description=malloc(len+1);

  /* Trim leading white space */
  for(i=0, j=0; (c=html_desc[i]) && (c == ' ' || c == '\n'); i++)
    ;

  for(; (c=html_desc[i]); i++) {
    if(c == '\n')
      c=' ';
    else if(c == '&') {
      c=html_desc[++i];
      
      /* Expand &#123; to UTF-8 for codepoint decimal 123 */
      if(c == '#') {
        unsigned char *orig_p=&html_desc[i];
        unsigned long d=0;
        int ulen;
        
        i++;
        while(c) { 
          c=html_desc[i++];
          if(c<'0' || c>'9')
            break;
          d=d*10;
          d+= (c - '0');
        }
        if(c != ';') {
          fprintf(stderr, "%s: Expected ';' after &#NNN in '%s'\n",
                  program, orig_p);
          abort();
        }

#ifdef RSS2ICAL_DEBUG
        fprintf(stderr, "%s: Encoding char %d\n", program, d);
#endif
        ulen=raptor_unicode_char_to_utf8(d, &description[j]);
#ifdef RSS2ICAL_DEBUG
        fprintf(stderr, "%s: UTF-8 len was %d\n", program, ulen);
#endif
        j+= ulen;
        
      } else {
        const char* here=(const char*)&html_desc[i];

        if(!strncmp(here, "amp;", 4)) {
          i+= 4;
          c='&';
        } else if(!strncmp(here, "lt;", 3)) {
          i+= 3;
          c='<';
        } else if(!strncmp(here, "gt;", 3)) {
          i+= 3;
          c='>';
        }
          
        description[j++]=c;
      }
      continue;
    }

    description[j++]=c;
  }
  description[j]='\0';
  return description;
}


static char*
uri_to_calid(librdf_uri* uri) 
{
  size_t len;
  unsigned char* uri_string;
  char* calid;
  unsigned char c;
  int i, j;
  
  uri_string=librdf_uri_as_counted_string(uri, &len);

  calid=(char*)malloc(len+1);
  for(i=0, j=0; (c=uri_string[i]); i++) {
    if(c <= 0x20 || c >= 0x7f)
      continue;
    
    if(c == '\\' || c == ';' || c == ':' || c == '\"' || c == ',' ||
       c == '/')
      calid[j++]='-';
    else
      calid[j++]=c;
  }
 
  return calid;
}


int
main(int argc, char *argv[])
{
  librdf_world* world;
  librdf_storage* storage;
  librdf_model* model;
  librdf_parser* parser;
  librdf_query* query;
  librdf_query_results* results;
  librdf_uri *uri;
  char *p;
  char* calendar_name;
  char* calendar_id;
  
  program=argv[0];
  if((p=strrchr(program, '/')))
    program=p+1;
  else if((p=strrchr(program, '\\')))
    program=p+1;
  argv[0]=program;

  if(argc != 3) {
    fprintf(stderr, "USAGE: %s RSS-URI CALENDAR-NAME\n", program);
    return 1; 
  }
  
  world=librdf_new_world();
  librdf_world_open(world);

  storage=librdf_new_storage(world, "memory", NULL, NULL);
  model=librdf_new_model(world, storage, NULL);

  if(!model || !storage) {
    fprintf(stderr, "%s: Failed to make model or storage\n", program);
    return 1;
  }

  uri=librdf_new_uri(world, (unsigned char*)argv[1]);

  calendar_name=argv[2];

  fprintf(stderr, "%s: Reading RSS from %s\n", program,
          librdf_uri_as_string(uri));
  
  parser=librdf_new_parser(world, "rss-tag-soup", NULL, NULL);
  librdf_parser_parse_into_model(parser, uri, NULL, model);
  librdf_free_parser(parser);

  fprintf(stderr, "%s: Querying model for RSS items\n", program);
  
  query=librdf_new_query(world, "sparql", NULL, get_items_query, uri);
  
  results=librdf_model_query_execute(model, query);
  if(!results) {
    fprintf(stderr, "%s: Query of model with SPARQL query '%s' failed\n", 
            program, get_items_query);
    return 1;
  }

  fprintf(stderr, "%s: Processing results\n", program);

  calendar_id=uri_to_calid(uri);
  
  fprintf(stdout, ical_header_format,
          "-//librdf/rss2ical Version 1.0//EN",
          calendar_name, 
          calendar_id,
          tzone,
          tzone);

  free(calendar_id);

  while(!librdf_query_results_finished(results)) {
    unsigned char *uid=NULL;
    unsigned char *summary=NULL;
    unsigned char *dtstart=NULL;
    unsigned char *location=NULL;
    unsigned char *html_desc=NULL;
    size_t html_desc_len;
    unsigned char *description=NULL;
    unsigned char *url=NULL;
    librdf_node* node;
    char *creator=NULL;

    node=librdf_query_results_get_binding_value_by_name(results, "item");
    if(!librdf_node_is_resource(node))
      goto nextresult;

    url=librdf_uri_as_string(librdf_node_get_uri(node));

    /* uid is a new string */
    uid=(unsigned char*)uri_to_calid(librdf_node_get_uri(node));

    node=librdf_query_results_get_binding_value_by_name(results, "date");
    if(!librdf_node_is_literal(node)) {
      fprintf(stderr, "%s: Date in item %s is not a literal\n", program, url);
      goto nextresult;
    }
    dtstart=librdf_node_get_literal_value(node);
    dtstart=iso2vcaldate(dtstart);
    
    node=librdf_query_results_get_binding_value_by_name(results, "title");
    if(!librdf_node_is_literal(node))
      summary=(unsigned char*)"(No Title)";
    else
      summary=librdf_node_get_literal_value(node);

    node=librdf_query_results_get_binding_value_by_name(results, "htmldesc");
    if(node && librdf_node_is_literal(node))
        html_desc=librdf_node_get_literal_value_as_counted_string(node, 
                                                                  &html_desc_len);
    
    if(!description) {
      node=librdf_query_results_get_binding_value_by_name(results, "description");
      if(node && librdf_node_is_literal(node))
        html_desc=librdf_node_get_literal_value_as_counted_string(node, 
                                                                  &html_desc_len);
    }
    if(html_desc) {
      description=remove_html_entities(html_desc, html_desc_len);
    }
    
    node=librdf_query_results_get_binding_value_by_name(results, "source");
    if(node && librdf_node_is_literal(node))
      location=librdf_node_get_literal_value(node);

    node=librdf_query_results_get_binding_value_by_name(results, "creator");
    if(node && librdf_node_is_literal(node)) {
      unsigned char *value=librdf_node_get_literal_value(node);
      creator=malloc(strlen((const char*)value)+6);
      sprintf(creator, "CN=\"%s\"", value);
    }
    

    ical_print(stdout, "BEGIN:VEVENT");
    ical_format(stdout, "UID", NULL, NULL, uid);
    ical_format(stdout, "SUMMARY", NULL, NULL, summary);
    if(location)
      ical_format(stdout, "LOCATION", NULL, NULL, location);
    if(creator) {
      ical_format(stdout, "ATTENDEE", creator, NULL, 
                  (const unsigned char*)"invalid:nomail");
      free(creator);
    }
    ical_format(stdout, "DTSTART", NULL, NULL, dtstart);
    ical_format(stdout, "DTSTAMP", NULL, NULL, dtstart);
    ical_format(stdout, "LAST-MODIFIED", NULL, NULL, dtstart);
    ical_format(stdout, "DESCRIPTION", NULL, ";,\"", description);
    if(url)
      ical_format(stdout, "URL", "VALUE=URI", ";", url);
    ical_format(stdout, "CLASS", NULL, NULL, (const unsigned char*)"PUBLIC");

    ical_print(stdout, "END:VEVENT");

    free(description);
    free(uid);
    
nextresult:
    librdf_query_results_next(results);
  }

  fputs(ical_footer_format, stdout);

  librdf_free_query_results(results);
  librdf_free_query(query);

  librdf_free_uri(uri);

  librdf_free_model(model);
  librdf_free_storage(storage);

  librdf_free_world(world);

  /* keep gcc -Wall happy */
  return(0);
}