Ciaran Farrell committed 988a911

Fix bug that stopped script on receiving code 59920 instead of forcing login (59920 means missing or invalid session cookie)

Comments (0)

Files changed (2)

 this default in the ~/.gwise.cfg file'''%deflen
 help_to = '''Analog to the 'to' recipients of an email. All to recipients can
 see who the other 'to' recipients as well as the other cc recipients are'''
+help_recur = '''Specify the recurrence key so that the decline and its options
+can be applied to all items in the recurrence chain'''
+help_comment = '''Add a comment to the decline. Only one comment can be added
+so that if you specify multiple appointments to decline, the same comment will
+appear in each decline'''
+help_items = '''Specify the appointments to decline. You can use the short url
+type keys (e.g. from termine list) or the full Groupwise keys (e.g. from the 
+output of termine list -a). Enter as many appointments to decline as you like'''
 parser = argparse.ArgumentParser(parents=[parentparser],
 tcreate.add_argument('to', nargs='*', help=help_to)
+tdecline = subparsers.add_parser('decline', help='Decline one or more appointments')
+                    default='')
+                    default=None,required=False)
+tdecline.add_argument('items', nargs='*', help=help_items)
 args = parser.parse_args()
 if not args.verbose:
 elif hasattr(args,'to'):
   logger.debug('parsed to to be %s'
   action = 'create'
+elif hasattr(args,'recurrence'):
+  logger.debug('parser recurrence to %s'%args.recurrence)
+  action = 'decline'
   # this will probably never be reached as the parser will catch the error
   parser.error('Could not determine what action you want to take')
+  elif action == 'decline':
+    logger.debug('action is decline')
+    if not len(args.items):
+      logger.debug('User did not specify any items - cannot proceed')
+      sys.stderr.write('You must specify at least one appointment to decline\n')
+      sys.exit(1)
+    try:
+      gw = Groupwise(fl=fl)
+      soapstring = gw.declineAppointments(args.items,comment=args.comment,
+                      recurrence=args.recurrence)
+    except GWForceLoginException,e:
+      logger.warn(str(e))
+      logger.debug('Setting fl to False to avoid loop')
+      fl = False
+    except GWInitException,e:
+      logger.warn(str(e))
+    else:
+      print "back to termine"
+      ok=True
   elif action == 'create':
     logger.debug('action is create')
         if hasattr(resp.folder,'id'):
+          logger.debug('getFolderRequest failed: (GW CODE %d)%s'%(int(resp.status.code),
+            str(resp.status.description)))
           raise GWItemFetchException,'Could not retrieve Address Book id'
+        if int(resp.status.code)==59920:
+          logger.debug('Session cookie is not present or expired - login required')
+          raise GWSessionException,'session cookie not present or expired'
+        logger.debug('getFolderRequest failed: (GW CODE %d)%s'%(int(resp.status.code),
+          str(resp.status.description)))
         raise GWFatalException,resp.status.description
   def getAppointments(self,window=None):
       logger.warn('Groupwise returned status %d for sendItemRequest'%int(sir.status.code))
       print sir
-      raise GWItemSendException("GWItemSendException - oh noes") 
+      raise GWItemSendException("GWItemSendException - oh noes")
+  def declineAppointments(self,apps,comment=None,recurrence=None):
+    '''Decline all appointments in list apps. These will be either the short
+        keys stored in the cache or the full groupwise keys'''
+    logger.debug('Groupwise.declineAppointments with apps=%s, comment=%s,recurrence=%s)'%(apps,
+                  comment,recurrence))
+    gwitemlist = []
+    notfound = []
+    cache = GWIdCache()
+    for app in apps:
+      if len(app)==5:
+        logger.debug('%s is 5 chars long - checking if it can be expanded'%app)
+        try:
+          xpanded = cache.expand(app)
+        except KeyError,e:
+          logger.debug('%s could not be expanded - adding to not found'%app)
+          notfound.append(app)
+        else:
+          logger.debug('%s was expanded to %s'%(app,xpanded))
+          gwitemlist.append(xpanded)
+      elif ((len(app) > 5) and ('@' in app)):
+        logger.debug('Assuming %s is a full Groupwise item id'%app)
+        gwitemlist.append(app)
+      else:
+        logger.debug('Assuming %s is neither a shortened id nor a full Groupwise id'%app)
+        notfound.append(app)
+    if len(gwitemlist) > 0:
+      logger.debug('Have %d potentially valid ids - creating the item list'%len(gwitemlist))
+      itemlist = self.client.factory.create('ns2:ItemList')
+      for item in gwitemlist:
+        gwItem = self.client.factory.create('ns2:Item')
+ = item
+        logger.debug('Adding %s to the list of items to decline'%item)
+        itemlist.item.append(gwItem)
+      resp = self.client.service.declineRequest(gwitemlist[0])
+      print resp
+    else:
+      logger.debug('No point in proceeding as user did not send even one valid appointment id')
+      raise GWItemFetchException, 'No valid appointment ids found'
 class GWIdCache:
   def __init__(self):