aboutsummaryrefslogtreecommitdiffstats
path: root/libical/src/test/stow.c
blob: f742b417a591ec437eac783aa2df3a044fe72bb3 (plain) (blame)
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
/* -*- Mode: C -*-
  ======================================================================
  FILE: stow.c
  CREATOR: eric 29 April 2000
  
  $Id$
  $Locker$
    
 (C) COPYRIGHT 2000 Eric Busboom
 http://www.softwarestudio.org

 The contents of this file are subject to the Mozilla Public License
 Version 1.0 (the "License"); you may not use this file except in
 compliance with the License. You may obtain a copy of the License at
 http://www.mozilla.org/MPL/
 
 Software distributed under the License is distributed on an "AS IS"
 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 the License for the specific language governing rights and
 limitations under the License.
 
 The Initial Developer of the Original Code is Eric Busboom

 ======================================================================*/


#include <stdio.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <limits.h> /* for PATH_MAX */
#include <assert.h>
#include <stdlib.h>
#include <sys/utsname.h> /* for uname */
#include <sys/stat.h> /* for stat */
#include <unistd.h> /* for stat, getpid, getopt */
#include <pwd.h> /* For getpwent */
#include <sys/types.h> /* For getpwent */
#include <ctype.h> /* for tolower */

#include "ical.h"
#include "icalcalendar.h"
#include "icalfileset.h"
#include "icalmime.h"

char* program_name;
#define TMPSIZE 2048
#define SENDMAIL "/usr/lib/sendmail -t"

enum options {
    STORE_IN_FILE,
    STORE_IN_DB,
    INPUT_IS_MIME,
    INPUT_IS_ICAL,
    INPUT_FROM_STDIN,
    INPUT_FROM_FILE,
    ERRORS_TO_STDOUT,
    ERRORS_TO_ORGANIZER
};

struct options_struct
{
    enum options storage;
    enum options input_type;
    enum options input_source;
    enum options errors;
    char* input_file;
    char* calid;
    char* output_file;
};


enum file_type
{
    ERROR,
    NO_FILE,
    DIRECTORY,
    REGULAR,
    OTHER
};

enum file_type test_file(char *path)
{
    struct stat sbuf;
    enum file_type type;
    
    errno = 0;

    /* Check if the path already exists and if it is a directory*/
    if (stat(path,&sbuf) != 0){
    
    /* A file by the given name does not exist, or there was
           another error */
    if(errno == ENOENT)
    {
        type = NO_FILE;
    } else {
        type = ERROR;
    }

    } else {
    /* A file by the given name exists, but is it a directory? */
    
    if (S_ISDIR(sbuf.st_mode)){ 
        type = DIRECTORY;
    } else if(S_ISREG(sbuf.st_mode)){ 
        type = REGULAR;
    } else {
        type = OTHER;
    }
    }

    return type;
}

char* lowercase(char* str)
{
    char* p = 0;
    char* new = strdup(str);

    if(str ==0){
    return 0;
    }

    for(p = new; *p!=0; p++){
    *p = tolower(*p);
    }

    return new;
}

#if 0
char* get_local_attendee(struct options_struct *opt)
{
    char attendee[PATH_MAX];

    if(opt->calid){

    strncpy(attendee,opt->calid,PATH_MAX);

    } else {
    
    char* user = getenv("USER");
    struct utsname uts;
    uname(&utget_option);
    /* HACK nodename may not be a fully qualified domain name */
    snprintf(attendee,PATH_MAX,"%s@%s",user,uts.nodename);
   
    }

    return lowercase(attendee);
}
#endif

void usage(char *message)
{
    fprintf(stderr,"Usage: %s [-emdcn] [-i inputfile] [-o outputfile] [-u calid]\n",program_name);

}

icalcomponent* get_first_real_component(icalcomponent *comp)
{
    icalcomponent *c;

    for(c = icalcomponent_get_first_component(comp,ICAL_ANY_COMPONENT);
    c != 0;
    c = icalcomponent_get_next_component(comp,ICAL_ANY_COMPONENT)){
    if (icalcomponent_isa(c) == ICAL_VEVENT_COMPONENT ||
        icalcomponent_isa(c) == ICAL_VTODO_COMPONENT ||
        icalcomponent_isa(c) == ICAL_VJOURNAL_COMPONENT )
    {
        return c;
    }
    }

    return 0;
}



char* make_mime(char* to, char* from, char* subject, 
        char* text_message, char* method, char* ical_message)
{
    size_t size = strlen(to)+strlen(from)+strlen(subject)+
    strlen(text_message)+ strlen(ical_message)+TMPSIZE;

    char mime_part_1[TMPSIZE];
    char mime_part_2[TMPSIZE];
    char content_id[TMPSIZE];
    char boundary[TMPSIZE];
    struct utsname uts;
    char* m;


    if ((m = malloc(sizeof(char)*size)) == 0){
    fprintf(stderr,"%s: Can't allocate memory: %s\n",program_name,strerror(errno));
    exit(1);
    }

    uname(&uts);

    srand(time(0)<<getpid());
    sprintf(content_id,"%d-%d@%s",(int)time(0),rand(),uts.nodename);
    sprintf(boundary,"%d-%d-%s",(int)time(0),rand(),uts.nodename);

    sprintf(mime_part_1,"Content-ID: %s\n\
Content-type: text/plain\n\
Content-Description: Text description of error message\n\n\
%s\n\n--%s",
        content_id,text_message,boundary);

    if(ical_message != 0 && method != 0){
    sprintf(mime_part_2,"Content-ID: %s\n\
Content-type: text/calendar; method=%s\n\
Content-Description: iCal component reply\n\n\
%s\n\n--%s--",
        content_id,method,ical_message,boundary);
    }

    sprintf(m,"To: %s\n\
From: %s\n\
Subject: %s\n\
MIME-Version: 1.0\n\
Content-ID: %s\n\
Content-Type:  multipart/mixed; boundary=\"%s\"\n\
\n\
 This is a multimedia message in MIME format\n\
\n\
--%s
%s
",
        to,from,subject,content_id,boundary,boundary,
        mime_part_1);

    if(ical_message != 0 && method != 0){
    strcat(m, mime_part_2);
    } else {
    strcat(m,"--\n");
    }

    return m;
}

/* The incoming component had fatal errors */
void return_failure(icalcomponent* comp,  char* message, 
            struct options_struct *opt)
{
    char* local_attendee = opt->calid;
    FILE* p;

    icalcomponent  *inner = get_first_real_component(comp);

    icalproperty *organizer_prop = icalcomponent_get_first_property(inner,ICAL_ORGANIZER_PROPERTY);
    char *organizer = icalproperty_get_organizer(organizer_prop);

    organizer += 7;

    if (opt->errors == ERRORS_TO_ORGANIZER){
    p = popen(SENDMAIL,"w");
    } else {
    p = stdout;
    }

    if(p == 0){
    fprintf(stderr,
        "%s: fatal. Could not open pipe to sendmail (\"%s\") \n",
        program_name,SENDMAIL);
    exit(1);
     }
   
    fputs(make_mime(organizer, local_attendee, "iMIP error", 
            message, "reply",
            icalcomponent_as_ical_string(comp)),p);
    
    if (opt->errors == ERRORS_TO_ORGANIZER){ 
    pclose(p);
    }   
}

/* The program had a fatal error and could not process the incoming component*/
void return_error(icalcomponent* comp,  char* message, struct options_struct *opt)
{
    

    fputs(make_mime("Dest", "Source", "iMIP system failure", 
            message, 0,0),stdout);

}

icalcomponent* make_reply(icalcomponent *comp, icalproperty *return_status, 
             struct options_struct *opt)

{
    icalcomponent *reply, *rinner;
    icalcomponent  *inner = get_first_real_component(comp);
    icalproperty *p=0;
    char* local_attendee = opt->calid;
    char attendee[TMPSIZE];

    char prodid[TMPSIZE];

    snprintf(attendee,TMPSIZE,"mailto:%s",local_attendee);

    snprintf(prodid,TMPSIZE,"-//Softwarestudio.org//%s version %s//EN",ICAL_PACKAGE,ICAL_VERSION);

    /* Create the base component */
    reply = icalcomponent_vanew( 
    ICAL_VCALENDAR_COMPONENT,
    icalproperty_new_version(strdup("2.0")),
    icalproperty_new_prodid(strdup(prodid)),
    icalproperty_new_method(ICAL_METHOD_REPLY),
    icalcomponent_vanew(
        ICAL_VEVENT_COMPONENT,
        icalproperty_new_clone(
        icalcomponent_get_first_property(inner,ICAL_DTSTAMP_PROPERTY)),
        icalproperty_new_clone(
        icalcomponent_get_first_property(inner,ICAL_ORGANIZER_PROPERTY)),
        icalproperty_new_clone(
        icalcomponent_get_first_property(inner,ICAL_UID_PROPERTY)),
        icalproperty_new_attendee(attendee),
        0),
    0);

    
    /* Convert errors into request-status properties and transfers
       them to the reply component */

    icalcomponent_convert_errors(comp);

    rinner = get_first_real_component(reply);

    for(p = icalcomponent_get_first_property(inner,
                         ICAL_REQUESTSTATUS_PROPERTY);
    p != 0;
    p = icalcomponent_get_next_property(inner,
                        ICAL_REQUESTSTATUS_PROPERTY)){
    
    icalcomponent_add_property(rinner,icalproperty_new_clone(p));
    }
        
    if(return_status != 0){
    icalcomponent_add_property(rinner, return_status);
    }
        
    return reply;
        
}

int check_attendee(icalproperty *p,  struct options_struct *opt){  
    char* s = icalproperty_get_attendee(p);
    char* lower_attendee = lowercase(s);
    char* local_attendee = opt->calid;
    
    /* Check that attendee begins with "mailto:" */
    if (strncmp(lower_attendee,"mailto:",7) == 0){
    /* skip over the mailto: part */
    lower_attendee += 7;
    
    if(strcmp(lower_attendee,local_attendee) == 0){
        return 1;
    }
    
    lower_attendee -= 7;
    
    free(lower_attendee);
    } 

    return 0;
}

char static_component_error_str[PATH_MAX];
char* check_component(icalcomponent* comp,  icalproperty **return_status,
              struct options_struct *opt)
{
    char* component_error_str=0;
    icalcomponent* inner;
    int errors = 0;
    icalproperty *p;
    int found_attendee = 0;

    *return_status = 0;

    /* This do/while loop only executes once because it is being used
       to fake exceptions */

    do {

    /* Check that we actually got a component */
    if(comp == 0){
        strcpy(static_component_error_str,
           "Did not find a component");
        component_error_str = static_component_error_str;
        break;
    }

    /* Check that the root component is a VCALENDAR */
    if(icalcomponent_isa(comp) != ICAL_VCALENDAR_COMPONENT){
        strcpy(static_component_error_str,
           "Root component is not a VCALENDAR");
        component_error_str = static_component_error_str;
        break;
    }


    /* Check that the component has a METHOD */

    if (icalcomponent_get_first_property(comp,ICAL_METHOD_PROPERTY) == 0)
    {
        strcpy(static_component_error_str,
           "Component does not have a METHOD property");
        component_error_str = static_component_error_str;
        break;
    }
    
    inner = get_first_real_component(comp);


    /* Check that the compopnent has an organizer */
    if(icalcomponent_get_first_property(inner,ICAL_ORGANIZER_PROPERTY) == 0){
        fprintf(stderr,"%s: fatal. Component does not have an ORGANIZER property\n",program_name);

        exit(1);
    }


    /* Check for this user as an attendee or organizer */

    for(p = icalcomponent_get_first_property(inner,ICAL_ATTENDEE_PROPERTY);
        p != 0;
        p = icalcomponent_get_next_property(inner,ICAL_ATTENDEE_PROPERTY)){
      
        found_attendee += check_attendee(p,opt);
    }

    for(p = icalcomponent_get_first_property(inner,ICAL_ORGANIZER_PROPERTY);
        p != 0;
        p = icalcomponent_get_next_property(inner,ICAL_ORGANIZER_PROPERTY)){
      
        found_attendee += check_attendee(p,opt);
    }
        
    if (found_attendee == 0){
        struct icalreqstattype rs;
        char* rs_string;
        memset(static_component_error_str,0,PATH_MAX);

        snprintf(static_component_error_str,PATH_MAX,
           "This target user (%s) is not listed as an attendee or organizer",
            opt->calid );
        component_error_str = static_component_error_str;

        rs.code = ICAL_3_7_INVCU_STATUS;
        rs.desc = 0;
        rs.debug  = component_error_str;    
        rs_string = icalreqstattype_as_string(rs);

        *return_status = icalproperty_new_requeststatus(rs_string);

        break;
    }



    /* Check that the component passes iTIP restrictions */
    
    errors = icalcomponent_count_errors(comp);
    icalrestriction_check(comp);
    
    if(errors != icalcomponent_count_errors(comp)){
        snprintf(static_component_error_str,PATH_MAX,
           "The component does not conform to iTIP restrictions.\n Here is the original component; look at the X-LIC-ERROR properties\nfor details\n\n%s",icalcomponent_as_ical_string(comp));
        component_error_str = static_component_error_str;
        break;
    }



    } while(0);

    return component_error_str;
}


void get_options(int argc, char* argv[], struct options_struct *opt)
{
    int c;
    extern char *optarg;
    extern int optind, optopt;
    int errflg=0;

    opt->storage = STORE_IN_FILE;
    opt->input_source = INPUT_FROM_STDIN;
    opt->input_type = INPUT_IS_ICAL;
    opt->input_file = 0;
    opt->errors = ERRORS_TO_ORGANIZER;
    opt->calid = 0;
    opt->output_file = 0;


    while ((c = getopt(argc, argv, "nemu:o:d:b:c:i:")) != -1) {
             switch (c) {
         case 'e': { /* Input data is MIME encapsulated */
             opt->input_type = INPUT_IS_MIME;
             break;
         }
         case 'm': { /* Input is iCal. Default*/
             opt->input_type = INPUT_IS_ICAL;
             break;
         }
         case 'i': { /* Input comes from named file */
             opt->input_source = INPUT_FROM_FILE;
             opt->input_file = strdup(optarg);
             break;
         }
         case 'o': { /* Output goes to named file. Default*/
             opt->output_file = strdup(optarg);
             opt->storage = STORE_IN_FILE;
             break;
         }
         case 'd': { /* Output goes to database */
             fprintf(stderr,"%s: option -d is unimplmented\n",program_name);
             opt->storage = STORE_IN_DB;
             errflg++;
             break;
         }
         case 'c': {

             break;
         }
         case 'u': { /* Set the calid for the output database or
                file. Default is user name of user running
                program */
             opt->calid = strdup(optarg);
             break;
         }

         case 'n': { /* Dump error to stdout. Default is to
                        send error to the organizer specified
                        in the iCal data */
             opt->errors = ERRORS_TO_STDOUT;
             break;
         }

         case ':': {/* Option given without an operand */
             fprintf(stderr,
                 "%s: Option -%c requires an operand\n", 
                 program_name,optopt);
             errflg++;
             break;
         }
         case '?': {
             errflg++;
         }

             }

         if (errflg >0){
         usage("");
         exit(1);
         }
    } 

    if(opt->calid == 0){
    /* If no calid specified, use username */
    char attendee[PATH_MAX];
    char* user = getenv("USER");
    struct utsname uts;
    uname(&uts);
    /* HACK nodename may not be a fully qualified domain name */
    snprintf(attendee,PATH_MAX,"%s@%s",user,uts.nodename);

    opt->calid = lowercase(attendee);
    }

    if(opt->storage == STORE_IN_FILE &&
    opt->output_file ==0){
    char file[PATH_MAX];
    char* user = getenv("USER");
    struct passwd *pw;

    if(!user){
        fprintf(stderr,"%s: Can't get username. Try explicitly specifing the output file with -o", program_name);
        exit(1);
    }
    
    /* Find password entry for user */
    while( (pw = getpwent())!=0){
        if(strcmp(user,pw->pw_name)==0){
        break;
        }
    }
        
    if(pw==0){
        fprintf(stderr,"%s: Can't get get password entry for user \"%s\" Try explicitly specifing the output file with -o", 
            program_name,user);
        exit(1);
    }

    if(pw->pw_dir==0){
        fprintf(stderr,"%s: User \"%s\" has no home directory. Try explicitly specifing the output file with -o", 
            program_name, user);
        exit(1);
    }

    snprintf(file,PATH_MAX,"%s/.facs/%s",pw->pw_dir,opt->calid);

    opt->output_file = strdup(file);
    }


    /* Now try to create the calendar directory if it does
       not exist */

     if(opt->storage == STORE_IN_FILE ) {
    char * p;
    char* facspath = strdup(opt->output_file);
    enum file_type type;

    /* Cut off the last slash to make it just a directoy */

    p = strrchr(facspath,'/');

    if (p == 0){
       fprintf(stderr,"%s: Invalid calendar filename \"%s\"", 
            program_name,facspath);
       exit(1);
    }   
    
    *p='\0';

    type = test_file(facspath);

    errno = 0;
    if (type == NO_FILE){
        
        if(mkdir(facspath,0775) != 0){
        fprintf(stderr,
            "%s: Failed to create calendar directory %s: %s\n",
            program_name,facspath, strerror(errno));
        exit(1);
        } else {
        fprintf(stderr,"%s: Creating calendar directory %s\n",
            program_name,facspath);
        }
        
    } else if(type==REGULAR || type == ERROR){
        fprintf(stderr,"%s: Cannot create calendar directory %s\n",
            program_name,facspath);
        exit(1);
    }           
    }
}

char* check_options(struct options_struct *opt)
{
    return 0;
}

void store_component(icalcomponent *comp, struct options_struct *opt)
{
    icalerrorenum error; 


    if(opt->storage == STORE_IN_FILE){
    icalfileset *fs = icalfileset_new(opt->output_file);

    if (fs == 0){
        fprintf(stderr,
            "%s: Failed to get incoming component directory: %s\n",
            program_name, icalerror_strerror(icalerrno));
        exit(1);
    }


    error = icalfileset_add_component(fs,comp);
    
    if (error != ICAL_NO_ERROR){
        fprintf(stderr,"%s: Failed to write incoming component: %s\n",
            program_name, icalerror_strerror(icalerrno));
        exit(1);
    }
    
    error = icalfileset_commit(fs);
    
    if (error != ICAL_NO_ERROR){
        fprintf(stderr,"%s: Failed to commit incoming cluster: %s\n",
            program_name, icalerror_strerror(icalerrno));
        exit(1);
    }
    
    icalfileset_free(fs);

    return;
    } else {
    assert(0);
    }
}

char* read_stream(char *s, size_t size, void *d)
{
  char *c = fgets(s,size, (FILE*)d);

  return c;
}

icalcomponent* read_nonmime_component(struct options_struct *opt)
{
    FILE *stream;
    icalcomponent *comp;
    icalparser* parser = icalparser_new();
    char* line;

    if(opt->input_source == INPUT_FROM_FILE){
    stream = fopen(opt->input_file,"r");    

    if (stream == 0){
        perror("Can't open input file");
        exit(1);
    }

    } else {
    stream = stdin;         
    }

    assert(stream != 0);
    icalparser_set_gen_data(parser,stream);
   
    do {    
    line = icalparser_get_line(parser,read_stream);
    
    comp = icalparser_add_line(parser,line);
    
    if (comp != 0){
        icalparser_claim(parser);
        return comp;
    }
    
    } while ( line != 0);

    if(opt->input_source == INPUT_FROM_FILE){
    fclose(stream);
    }


    return comp;
 }

icalcomponent* find_vcalendar(icalcomponent* comp)
{
    icalcomponent *c,*rtrn;

    for(c = icalcomponent_get_first_component(comp,ICAL_ANY_COMPONENT);
    c != 0;
    c = icalcomponent_get_next_component(comp,ICAL_ANY_COMPONENT)){

    if(icalcomponent_isa(c) == ICAL_VCALENDAR_COMPONENT){
        icalcomponent_remove_component(comp,c);
        return c;
    }

    if((rtrn=find_vcalendar(c)) != 0){
        return rtrn;
    }
    }

    return 0;
}

icalcomponent* read_mime_component(struct options_struct *opt)
{
    icalcomponent *comp,*mimecomp;
    FILE* stream;
    
    if(opt->input_source == INPUT_FROM_FILE){
    stream = fopen(opt->input_file,"r");    
    
    if (stream == 0){
        perror("Can't open input file");
        exit(1);
    }
    
    } else {
    stream = stdin;         
    }
    
    assert(stream != 0);
    
    mimecomp = icalmime_parse(read_stream,(void*)stream);

    /* now find the iCal component embedded within the mime component */
    comp = find_vcalendar(mimecomp);


    if(comp == 0){
    return 0;
    }

    return comp;    
}

icalcomponent* read_component(struct options_struct *opt)
{
    if(opt->input_type == INPUT_IS_MIME){
    return read_mime_component(opt);
    } else if (opt->input_type == INPUT_IS_ICAL){
    return read_nonmime_component(opt);
    } else {
    fprintf(stderr,"%s: Internal Error; unknown option for input_type\n",
        program_name);
    exit(1);
    }
}

int main(int argc, char* argv[] )
{
    char* options_error_str;
    char* component_error_str;
    icalcomponent* comp, *reply;
    struct options_struct opt;
    icalproperty *return_status;

    program_name = strrchr(argv[0],'/');

    get_options(argc, argv, &opt);

    if ( (options_error_str = check_options(&opt)) != 0 ){
    usage(options_error_str);
    exit(1);
    }    

    comp = read_component(&opt);

    if ( (component_error_str = 
      check_component(comp,&return_status,&opt)) != 0){
    reply = make_reply(comp,return_status,&opt);
    return_failure(reply, component_error_str, &opt);
    icalcomponent_free(reply);
    exit(0);
    }

    store_component(comp,&opt);


    /* Don't free the component comp, since it is now part of the
       store, and will be freed there */

    exit(0);
}