Back to index

lightning-sunbird  0.9+nobinonly
icalmessage.c
Go to the documentation of this file.
00001 /* -*- Mode: C -*-
00002     ======================================================================
00003     FILE: icalmessage.c
00004     CREATOR: ebusboom 07 Nov 2000
00005   
00006     $Id: icalmessage.c,v 1.7 2005/01/24 14:02:20 acampi Exp $
00007     $Locker:  $
00008     
00009     (C) COPYRIGHT 2000, Eric Busboom, http://www.softwarestudio.org
00010     
00011     This program is free software; you can redistribute it and/or modify
00012     it under the terms of either: 
00013     
00014     The LGPL as published by the Free Software Foundation, version
00015     2.1, available at: http://www.fsf.org/copyleft/lesser.html
00016     
00017     Or:
00018     
00019     The Mozilla Public License Version 1.0. You may obtain a copy of
00020     the License at http://www.mozilla.org/MPL/
00021 
00022 
00023     ======================================================================*/
00024 
00025 #ifdef HAVE_CONFIG_H
00026 #include "config.h"
00027 #endif
00028 
00029 #include "icalmessage.h"
00030 #include "icalenums.h"
00031 #include <ctype.h>  /* for tolower()*/
00032 #include <string.h> /* for strstr */
00033 #include <stdlib.h> /* for free(), malloc() */
00034 icalcomponent* icalmessage_get_inner(icalcomponent* comp)
00035 {
00036     if (icalcomponent_isa(comp) == ICAL_VCALENDAR_COMPONENT){
00037        return icalcomponent_get_first_real_component(comp);
00038     } else {
00039        return comp;
00040     }
00041 }
00042 
00043 static char* lowercase(const char* str)
00044 {
00045     char* p = 0;
00046     char* n = icalmemory_strdup(str);
00047 
00048     if(str ==0){
00049        return 0;
00050     }
00051 
00052     for(p = n; *p!=0; p++){
00053        *p = tolower(*p);
00054     }
00055 
00056     return n;
00057 }
00058 
00059 icalproperty* icalmessage_find_attendee(icalcomponent* comp, const char* user)
00060 {
00061     icalcomponent *inner = icalmessage_get_inner(comp);
00062     icalproperty *p,*attendee = 0;
00063     char* luser = lowercase(user);
00064 
00065     for(p = icalcomponent_get_first_property(inner, ICAL_ATTENDEE_PROPERTY);
00066        p != 0;
00067        p = icalcomponent_get_next_property(inner, ICAL_ATTENDEE_PROPERTY)
00068        ){
00069 
00070        char* lattendee;
00071 
00072        lattendee = lowercase(icalproperty_get_attendee(p));
00073 
00074        if (strstr(lattendee,user) != 0){
00075            attendee = p;
00076            break;
00077        }
00078 
00079        free(lattendee);
00080 
00081     }
00082     
00083     free(luser);
00084 
00085     return attendee;
00086 
00087 }
00088 
00089 void icalmessage_copy_properties(icalcomponent* to, icalcomponent* from, 
00090               icalproperty_kind kind)
00091 {
00092     icalcomponent *to_inner = icalmessage_get_inner(to);
00093     icalcomponent *from_inner = icalmessage_get_inner(from);
00094 
00095     if (to_inner == 0 && from_inner == 0){
00096        icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
00097        return;
00098     }
00099 
00100     if(!icalcomponent_get_first_property(from_inner,kind)){
00101        return;
00102     }
00103 
00104     icalcomponent_add_property(to_inner,
00105               icalproperty_new_clone(
00106                  icalcomponent_get_first_property(
00107                      from_inner,
00108                      kind)
00109                  )
00110        );
00111 }
00112 
00113 icalcomponent *icalmessage_new_reply_base(icalcomponent* c,
00114                                        const char* user,
00115                                        const char* msg)
00116 {
00117     icalproperty *attendee;
00118     char tmp[1024];
00119 
00120     icalcomponent *reply = icalcomponent_vanew(
00121        ICAL_VCALENDAR_COMPONENT,
00122        icalproperty_new_method(ICAL_METHOD_REPLY),
00123        icalcomponent_vanew(
00124            ICAL_VEVENT_COMPONENT,
00125            icalproperty_new_dtstamp(icaltime_from_timet(time(0),0)),
00126            0),
00127        0);
00128 
00129     icalcomponent *inner = icalmessage_get_inner(reply);
00130 
00131     icalerror_check_arg_rz(c,"c");
00132 
00133     icalmessage_copy_properties(reply,c,ICAL_UID_PROPERTY);
00134     icalmessage_copy_properties(reply,c,ICAL_ORGANIZER_PROPERTY);
00135     icalmessage_copy_properties(reply,c,ICAL_RECURRENCEID_PROPERTY);
00136     icalmessage_copy_properties(reply,c,ICAL_SUMMARY_PROPERTY);
00137     icalmessage_copy_properties(reply,c,ICAL_SEQUENCE_PROPERTY);
00138 
00139     icalcomponent_set_dtstamp(reply,icaltime_from_timet(time(0),0));
00140 
00141     if(msg != 0){
00142        icalcomponent_add_property(inner,icalproperty_new_comment(msg));
00143     }
00144 
00145     /* Copy this user's attendee property */
00146 
00147     attendee = icalmessage_find_attendee(c,user);
00148 
00149     if (attendee == 0){
00150        icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
00151        icalcomponent_free(reply);
00152        return 0;
00153     }
00154 
00155     icalcomponent_add_property(inner,icalproperty_new_clone(attendee));
00156 
00157     /* Add PRODID and VERSION */
00158 
00159     icalcomponent_add_property(reply,icalproperty_new_version("2.0"));
00160     
00161 #ifndef WIN32    
00162     snprintf(tmp,sizeof(tmp),
00163            "-//SoftwareStudio//NONSGML %s %s //EN",PACKAGE,VERSION);
00164 #else
00165     snprintf(tmp,sizeof(tmp),
00166            "-//SoftwareStudio//NONSGML %s %s //EN",ICAL_PACKAGE,ICAL_VERSION);
00167 #endif
00168     icalcomponent_add_property(reply,icalproperty_new_prodid(tmp));
00169 
00170     return reply;
00171 
00172 }
00173 
00174 icalcomponent* icalmessage_new_accept_reply(icalcomponent* c,
00175                                        const char* user,
00176                                        const char* msg)
00177 {
00178 
00179     icalcomponent *reply;
00180     icalproperty *attendee;
00181     icalcomponent *inner;
00182 
00183     icalerror_check_arg_rz(c,"c");
00184     
00185     reply = icalmessage_new_reply_base(c,user,msg);
00186 
00187     if(reply == 0){
00188        return 0;
00189     }
00190 
00191     inner  = icalmessage_get_inner(reply);
00192 
00193     attendee = icalcomponent_get_first_property(inner,
00194                                           ICAL_ATTENDEE_PROPERTY);
00195 
00196     icalproperty_set_parameter(attendee,
00197               icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED));
00198 
00199     return reply;
00200 }
00201 
00202 icalcomponent* icalmessage_new_decline_reply(icalcomponent* c,
00203                                        const char* user,
00204                                        const char* msg)
00205 {
00206     icalcomponent *reply;
00207     icalproperty *attendee;
00208     icalcomponent *inner;
00209 
00210     icalerror_check_arg_rz(c,"c");
00211     
00212     reply = icalmessage_new_reply_base(c,user,msg);
00213     inner = icalmessage_get_inner(reply);
00214     if(reply == 0){
00215        return 0;
00216     }
00217 
00218     attendee = icalcomponent_get_first_property(inner,
00219                                           ICAL_ATTENDEE_PROPERTY);
00220 
00221     icalproperty_set_parameter(attendee,
00222               icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED));
00223 
00224     return reply;
00225 }
00226 
00227 /* New is modified version of old */
00228 icalcomponent* icalmessage_new_counterpropose_reply(icalcomponent* oldc,
00229                                               icalcomponent* newc,
00230                                               const char* user,
00231                                               const char* msg)
00232 {
00233     icalcomponent *reply;
00234 
00235     icalerror_check_arg_rz(oldc,"oldc");
00236     icalerror_check_arg_rz(newc,"newc");
00237     
00238     reply = icalmessage_new_reply_base(newc,user,msg);
00239 
00240     icalcomponent_set_method(reply,ICAL_METHOD_COUNTER);
00241 
00242     return reply;
00243 
00244 }
00245 
00246 
00247 icalcomponent* icalmessage_new_delegate_reply(icalcomponent* c,
00248                                          const char* user,
00249                                          const char* delegatee,
00250                                          const char* msg)
00251 {
00252 
00253     icalcomponent *reply;
00254     icalproperty *attendee;
00255     icalcomponent *inner;
00256 
00257     icalerror_check_arg_rz(c,"c");
00258 
00259     reply =  icalmessage_new_reply_base(c,user,msg);
00260     inner = icalmessage_get_inner(reply);
00261     if(reply == 0){
00262        return 0;
00263     }
00264 
00265     attendee = icalcomponent_get_first_property(inner,
00266                                           ICAL_ATTENDEE_PROPERTY);
00267 
00268     icalproperty_set_parameter(attendee,
00269               icalparameter_new_partstat(ICAL_PARTSTAT_DELEGATED));
00270 
00271     icalproperty_set_parameter(attendee,
00272               icalparameter_new_delegatedto(delegatee));
00273 
00274     return reply;
00275 
00276 }
00277 
00278 icalcomponent* icalmessage_new_delegate_request(icalcomponent* c,
00279                                          const char* user,
00280                                          const char* delegatee,
00281                                          const char* msg)
00282 {
00283 
00284     icalcomponent *reply;
00285     icalproperty *attendee;
00286     icalcomponent *inner;
00287 
00288     icalerror_check_arg_rz(c,"c");
00289 
00290     reply =  icalmessage_new_reply_base(c,user,msg);
00291     inner = icalmessage_get_inner(reply);
00292 
00293     if(reply == 0){
00294        return 0;
00295     }
00296 
00297     icalcomponent_set_method(reply,ICAL_METHOD_REQUEST);
00298 
00299     attendee = icalcomponent_get_first_property(inner,
00300                                           ICAL_ATTENDEE_PROPERTY);
00301 
00302     icalproperty_set_parameter(attendee,
00303               icalparameter_new_partstat(ICAL_PARTSTAT_DELEGATED));
00304 
00305     icalproperty_set_parameter(attendee,
00306               icalparameter_new_delegatedto(delegatee));
00307 
00308     icalcomponent_add_property(
00309        inner,
00310        icalproperty_vanew_attendee(
00311            delegatee,
00312            icalparameter_new_delegatedfrom(
00313               icalproperty_get_attendee(attendee)
00314               ),
00315            0
00316            )
00317        );
00318             
00319 
00320     return reply;
00321 
00322 }
00323 
00324 
00325 icalcomponent* icalmessage_new_cancel_event(icalcomponent* c,
00326                                        const char* user,
00327                                        const char* msg);
00328 icalcomponent* icalmessage_new_cancel_instance(icalcomponent* c,
00329                                        const char* user,
00330                                        const char* msg);
00331 icalcomponent* icalmessage_new_cancel_all(icalcomponent* c,
00332                                        const char* user,
00333                                        const char* msg);
00334 
00335 
00336 
00337 icalcomponent* icalmessage_new_error_reply(icalcomponent* c,
00338                                       const char* user,
00339                                       const char* msg,
00340                                       const char* debug,
00341                                       icalrequeststatus code)
00342 {
00343     icalcomponent *reply;
00344     icalcomponent *inner, *cinner;
00345     struct icalreqstattype rs;
00346 
00347     icalerror_check_arg_rz(c,"c");
00348     
00349     reply = icalmessage_new_reply_base(c,user,msg);
00350     inner = icalmessage_get_inner(reply);
00351     cinner = icalmessage_get_inner(c);
00352     if(reply == 0){
00353        return 0;
00354     }
00355 
00356     if( code != ICAL_UNKNOWN_STATUS){
00357        rs.code = code;
00358        rs.debug = debug;
00359        
00360        icalcomponent_add_property(inner, 
00361                                icalproperty_new_requeststatus(rs));
00362     } else { /*  code == ICAL_UNKNOWN_STATUS */ 
00363 
00364        /* Copy all of the request status properties */
00365        icalproperty *p;
00366        for(p = icalcomponent_get_first_property(cinner,
00367                                            ICAL_REQUESTSTATUS_PROPERTY);
00368            p != 0;
00369            p = icalcomponent_get_next_property(cinner,
00370                                           ICAL_REQUESTSTATUS_PROPERTY)){
00371 
00372            
00373            icalcomponent_add_property(inner,icalproperty_new_clone(p));
00374        }
00375     }
00376 
00377     return reply;
00378 }