Back to index

lightning-sunbird  0.9+nobinonly
process.c
Go to the documentation of this file.
00001 /* -*- Mode: C -*-
00002   ======================================================================
00003   FILE: process.c
00004   CREATOR: eric 11 February 2000
00005   
00006   $Id: process.c,v 1.7 2002/06/28 10:45:12 acampi Exp $
00007   $Locker:  $
00008     
00009  (C) COPYRIGHT 2000 Eric Busboom
00010  http://www.softwarestudio.org
00011 
00012  The contents of this file are subject to the Mozilla Public License
00013  Version 1.0 (the "License"); you may not use this file except in
00014  compliance with the License. You may obtain a copy of the License at
00015  http://www.mozilla.org/MPL/
00016  
00017  Software distributed under the License is distributed on an "AS IS"
00018  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00019  the License for the specific language governing rights and
00020  limitations under the License.
00021  
00022  ======================================================================*/
00023 
00024 #include <stdio.h> /* for printf */
00025 #include <errno.h>
00026 #include <string.h> /* For strerror */
00027 #include <stdlib.h> /* for free */
00028 
00029 #include "ical.h"
00030 #include "icalss.h"
00031 
00032 void send_message(icalcomponent *reply,const char* this_user)
00033 {
00034     printf("From: %s\n\n%s\n",this_user,icalcomponent_as_ical_string(reply));
00035     
00036 
00037 }
00038 
00039 
00040 int main(int argc, char* argv[])
00041 {
00042     icalcomponent *c, *next_c;
00043     int i=0;
00044     const char *class_string;
00045     int dont_remove;
00046     
00047     icalset* f = icalset_new_file("../../test-data/process-incoming.ics");
00048     icalset* trash = icalset_new_file("trash.ics");
00049     icalset* cal = icalset_new_file("../../test-data/process-calendar.ics");
00050     icalset* out = icalset_new_file("outgoing.ics");
00051 
00052     const char* this_user = "alice@cal.softwarestudio.org";
00053 
00054     assert(f!= 0);
00055     assert(cal!=0);
00056     assert(trash!=0);
00057     assert(out!=0);
00058 
00059 
00060     /* Foreach incoming message */
00061     for(c=icalset_get_first_component(f);c!=0;c = next_c){
00062        
00063        icalproperty_xlicclass class;
00064        icalcomponent *match;
00065        icalcomponent *inner; 
00066        icalcomponent *reply = 0;
00067 
00068        assert(c!=0);
00069 
00070        inner = icalcomponent_get_first_real_component(c);
00071 
00072        i++;
00073        reply = 0;
00074        dont_remove = 0;
00075 
00076        if(inner == 0){
00077            printf("Bad component, no inner\n %s\n",
00078                  icalcomponent_as_ical_string(c));
00079            continue;
00080        }
00081 
00082        /* Find a booked component that is matched to the incoming
00083           message, based on the incoming component's UID, SEQUENCE
00084           and RECURRENCE-ID*/
00085 
00086        match = icalset_fetch_match(cal,c);
00087 
00088        class = icalclassify(c,match,this_user);
00089 
00090        class_string = icalproperty_enum_to_string(class);
00091 
00092        /* Print out the notes associated with the incoming component
00093            and the matched component in the */
00094        {
00095            const char *c_note=0;
00096            const char *m_note=0;
00097            icalproperty *p;
00098 
00099            for(p = icalcomponent_get_first_property(c,ICAL_X_PROPERTY);
00100               p!= 0;
00101               p = icalcomponent_get_next_property(c,ICAL_X_PROPERTY)){
00102 
00103               if(strcmp(icalproperty_get_x_name(p),"X-LIC-NOTE")==0){
00104                   c_note = icalproperty_get_x(p);
00105               }
00106            }
00107           
00108            if (match != 0){
00109               for(p = icalcomponent_get_first_property(match,
00110                                                   ICAL_X_PROPERTY);
00111                   p!= 0;
00112                   p = icalcomponent_get_next_property(match,
00113                                                  ICAL_X_PROPERTY)){
00114                   if(strcmp(icalproperty_get_x_name(p),"X-LIC-NOTE")==0){
00115                      m_note = icalproperty_get_x(p);
00116                   }
00117               }
00118            }
00119               
00120            if(c_note != 0){
00121               printf("Incoming: %s\n",c_note);
00122            }
00123            if(m_note != 0){
00124               printf("Match   : %s\n",m_note);
00125            }         
00126        }
00127 
00128        /* Main processing structure */
00129 
00130        switch (class){
00131            case ICAL_XLICCLASS_NONE: { 
00132               char temp[1024];
00133               /* Huh? Return an error to sender */
00134               icalrestriction_check(c);
00135               icalcomponent_convert_errors(c);
00136 
00137               snprintf(temp,1024,"I can't understand the component you sent. \n Here is the component you sent, possibly with error messages:\n %s",icalcomponent_as_ical_string(c));
00138 
00139               reply = icalmessage_new_error_reply(
00140                   c,
00141                   this_user,
00142                   temp,
00143                   "",
00144                   ICAL_UNKNOWN_STATUS
00145                   );
00146                   
00147                         
00148 
00149               break; 
00150            }
00151            case ICAL_XLICCLASS_PUBLISHNEW: { 
00152               
00153               /* Don't accept published events from anyone but
00154                  self. If self, fall through to ICAL_XLICCLASS_REQUESTNEW */
00155 
00156               
00157 
00158            }
00159            case ICAL_XLICCLASS_REQUESTNEW: { 
00160               
00161               /* Book the new component if it does not overlap
00162                  anything. If the time is busy and the start time is
00163                  an even modulo 4, delegate to
00164                  bob@cal.softwarestudio.org. If the time is busy and
00165                  is 1 modulo 4, counterpropose for the first
00166                  available free time. Otherwise, deline the meeting */
00167 
00168               icalcomponent *overlaps;
00169               overlaps = icalclassify_find_overlaps(cal,c);
00170 
00171               if(overlaps == 0){
00172                   /* No overlaps, book the meeting */
00173 /*                icalset_add_component(cal,icalcomponent_new_clone(c));*/
00174 
00175                   /* Return a reply */
00176                   reply = icalmessage_new_accept_reply(c,this_user,
00177                                     "I can make it to this meeting");
00178 
00179                   icalset_add_component(out,reply);
00180                 
00181               } else {
00182                   /* There was a conflict, so delegate, counterpropose
00183                      or decline it */
00184                   struct icaltimetype dtstart 
00185                      = icalcomponent_get_dtstart(c);
00186                 
00187                   if(dtstart.hour%4 == 0){  
00188                      /* Delegate the meeting */
00189                      reply = icalmessage_new_delegate_reply(c, 
00190                        this_user,
00191                        "bob@cal.softwarestudio.org",
00192                        "Unfortunately, I have another commitment that \
00193 conflicts with this meeting. I am delegating my attendance to Bob. ");
00194 
00195                      icalset_add_component(out,reply);
00196                     
00197                   } else if (dtstart.hour%4 == 1) {
00198                      /* Counter propose to next available time */
00199                      icalcomponent *newc;
00200                      struct icalperiodtype next_time;
00201 
00202                      icalspanlist *spanl = 
00203                          icalspanlist_new(cal,dtstart,
00204                                         icaltime_null_time());
00205 
00206                      next_time = icalspanlist_next_free_time(
00207                         spanl,icalcomponent_get_dtstart(c));
00208 
00209                      newc = icalcomponent_new_clone(c);
00210                      
00211                      icalcomponent_set_dtstart(newc,next_time.start);
00212                      
00213 
00214                      /* Hack, the duration of the counterproposed
00215                            meeting may be longer than the free time
00216                            available */
00217                      icalcomponent_set_duration(newc,
00218                           icalcomponent_get_duration(c));
00219                      
00220                      reply = icalmessage_new_counterpropose_reply(c, 
00221                                                              newc,
00222                                                              this_user,
00223                         "Unfortunately, I have another commitment that \
00224 conflicts with this meeting. I am proposing a time that works better for me.");
00225                     
00226                      icalset_add_component(out,reply);
00227 
00228                   } else {
00229                      /* Decline the meeting */
00230                     
00231                      reply = icalmessage_new_decline_reply(c,
00232                                                        this_user,
00233                                      "I can't make it to this meeting");
00234 
00235                      icalset_add_component(out,reply);
00236 
00237                   }
00238 
00239                 
00240               }
00241               break; 
00242            }
00243            case ICAL_XLICCLASS_PUBLISHFREEBUSY: {
00244               /* Store the busy time information in a file named after
00245                  the sender */
00246               break;
00247            }
00248            
00249            case ICAL_XLICCLASS_PUBLISHUPDATE: { 
00250               /* Only accept publish updates from self. If self, fall
00251                  throught to ICAL_XLICCLASS_REQUESTUPDATE */
00252            }
00253 
00254            case ICAL_XLICCLASS_REQUESTUPDATE: { 
00255               /* always accept the changes */
00256               break; 
00257            }
00258          
00259            case ICAL_XLICCLASS_REQUESTRESCHEDULE: { 
00260               /* Use same rules as REQUEST_NEW */
00261               icalcomponent *overlaps;
00262               overlaps = icalclassify_find_overlaps(cal,c);
00263 
00264               break; 
00265            }
00266            case ICAL_XLICCLASS_REQUESTDELEGATE: { 
00267              
00268               break; 
00269            }
00270            case ICAL_XLICCLASS_REQUESTNEWORGANIZER: { 
00271               break; 
00272            }
00273            case ICAL_XLICCLASS_REQUESTFORWARD: { 
00274               break; 
00275            }
00276            case ICAL_XLICCLASS_REQUESTSTATUS: { 
00277               break; 
00278            }
00279 
00280            case ICAL_XLICCLASS_REQUESTFREEBUSY: { 
00281               break; 
00282            }
00283            case ICAL_XLICCLASS_REPLYACCEPT: { 
00284               /* Change the PARTSTAT of the sender */
00285               break; 
00286            }
00287            case ICAL_XLICCLASS_REPLYDECLINE: { 
00288               /* Change the PARTSTAT of the sender */
00289               break; 
00290            }
00291            case ICAL_XLICCLASS_REPLYCRASHERACCEPT: { 
00292               /* Add the crasher to the ATTENDEE list with the
00293                  appropriate PARTSTAT */
00294               break; 
00295            }
00296            case ICAL_XLICCLASS_REPLYCRASHERDECLINE: { 
00297               /* Add the crasher to the ATTENDEE list with the
00298                  appropriate PARTSTAT */
00299               break; 
00300            }
00301            case ICAL_XLICCLASS_ADDINSTANCE: { 
00302               break; 
00303            }
00304            case ICAL_XLICCLASS_CANCELEVENT: { 
00305               /* Remove the component */
00306               break; 
00307            }
00308            case ICAL_XLICCLASS_CANCELINSTANCE: { 
00309               break; 
00310            }
00311            case ICAL_XLICCLASS_CANCELALL: { 
00312               /* Remove the component */        
00313               break; 
00314            }
00315            case ICAL_XLICCLASS_REFRESH: { 
00316               /* Resend the latest copy of the request */
00317               break; 
00318            }
00319            case ICAL_XLICCLASS_COUNTER: { 
00320               break; 
00321            }
00322            case ICAL_XLICCLASS_DECLINECOUNTER: { 
00323               break; 
00324            }
00325            case ICAL_XLICCLASS_MALFORMED: { 
00326               /* Send back an error */
00327               break; 
00328            } 
00329            case ICAL_XLICCLASS_OBSOLETE: { 
00330               printf(" ** Got an obsolete component:\n%s",
00331                      icalcomponent_as_ical_string(c));
00332               /* Send back an error */
00333               break; 
00334            } 
00335            case ICAL_XLICCLASS_MISSEQUENCED: { 
00336               printf(" ** Got a missequenced component:\n%s",
00337                      icalcomponent_as_ical_string(c));
00338               /* Send back an error */
00339               break; 
00340            }
00341            case ICAL_XLICCLASS_UNKNOWN: { 
00342               printf(" ** Don't know what to do with this component:\n%s",
00343                      icalcomponent_as_ical_string(c));
00344               /* Send back an error */
00345               break; 
00346            }
00347             case ICAL_XLICCLASS_X:
00348             case ICAL_XLICCLASS_REPLYDELEGATE:
00349             default: {
00350             }
00351        }
00352 
00353 #if(0)
00354        if (reply != 0){     
00355            
00356            /* Don't send the reply if the RSVP parameter indicates not to*/
00357            icalcomponent *reply_inner;
00358            icalproperty *attendee;
00359            icalparameter *rsvp;
00360            
00361            reply_inner = icalcomponent_get_first_real_component(reply);
00362            attendee = icalcomponent_get_first_property(reply_inner,
00363                                                  ICAL_ATTENDEE_PROPERTY);
00364            rsvp = icalproperty_get_first_parameter(attendee,
00365                                               ICAL_RSVP_PARAMETER);
00366            
00367            if(rsvp == 0 || icalparameter_get_rsvp(rsvp) == 1){
00368               icalrestriction_check(reply);
00369               send_message(reply,this_user);
00370            }
00371            
00372            icalcomponent_free(reply);
00373        }
00374 #endif 
00375 
00376        if(reply !=0){
00377            printf("%s\n",icalcomponent_as_ical_string(reply));
00378        }
00379        
00380        next_c =   icalset_get_next_component(f);
00381        
00382        if(dont_remove == 0){
00383            /*icalset_remove_component(f,c);
00384              icalset_add_component(trash,c);*/
00385        }
00386     }
00387 
00388 #if (0)
00389         
00390     for(c = icalset_get_first_component(out);
00391        c!=0;
00392        c = icalset_get_next_component(out)){
00393        
00394        printf("%s",icalcomponent_as_ical_string(c));
00395 
00396     }
00397 #endif
00398 
00399     icalset_free(f);
00400     icalset_free(trash);
00401     icalset_free(cal);
00402     icalset_free(out);
00403 
00404     return 0;
00405 }
00406 
00407