parse.c
changeset 2 a48121a74017
parent 1 6a9a25d32364
child 5 2155c65c7455
equal deleted inserted replaced
1:6a9a25d32364 2:a48121a74017
     1 #include <loudmouth/loudmouth.h>
     1 #include <loudmouth/loudmouth.h>
     2 #include <string.h>
     2 #include <string.h>
     3 
     3 
     4 #include "parse.h"
     4 #include "parse.h"
     5 
     5 
     6 int parse_jingle (LmMessageNode *node, struct info_jingle *ij)
     6 int parse_jingle(LmMessageNode *node, struct info_jingle *ij)
     7 {
     7 {
     8   int nb_reason = 0;
     8   int nb_reason = 0;
     9   LmMessageNode *child = NULL;
     9   LmMessageNode *child = NULL;
    10   
    10   
    11   if(!strcmp(ij->name, "jingle"))
    11   if (!strcmp(ij->name, "jingle"))
    12     return PARSE_ERROR_NAME;
    12     return PARSE_ERROR_NAME;
    13 
    13 
    14   ij->action    = attrcpy(lm_message_node_get_attribute(node, "action"));
    14   ij->action    = attrcpy(lm_message_node_get_attribute(node, "action"));
    15   ij->initiator = attrcpy(lm_message_node_get_attribute(node, "initiator"));
    15   ij->initiator = attrcpy(lm_message_node_get_attribute(node, "initiator"));
    16   ij->responder = attrcpy(lm_message_node_get_attribute(node, "responder"));
    16   ij->responder = attrcpy(lm_message_node_get_attribute(node, "responder"));
    17   ij->sid       = attrcpy(lm_message_node_get_attribute(node, "sid"));
    17   ij->sid       = attrcpy(lm_message_node_get_attribute(node, "sid"));
    18    
    18    
    19   // check required
    19   // check required
    20   if(ij->action == NULL || ij->sid == NULL)
    20   if (ij->action == NULL || ij->sid == NULL)
    21     return PARSE_ERROR_REQUIRED;
    21     return PARSE_ERROR_REQUIRED;
    22     
    22     
    23   // check restrictions
    23   // check restrictions
    24  if(!check_restriction(ij->action, {"content-accept", "content-add",
    24   if (!check_restriction(ij->action, {"content-accept", "content-add",
    25   "content-modify", "content-reject", "content-remove", "description-info",
    25   "content-modify", "content-reject", "content-remove", "description-info",
    26   "security-info", "session-accept", "session-info", "session-initiate",
    26   "security-info", "session-accept", "session-info", "session-initiate",
    27   "session-terminate", "transport-accept", "transport-info", "transport-reject",
    27   "session-terminate", "transport-accept", "transport-info", "transport-reject",
    28   "transport-replace", NULL}))
    28   "transport-replace", NULL}))
    29    return PARSE_ERROR_RESTRICTION;
    29    return PARSE_ERROR_RESTRICTION;
    30   
    30   
    31   // check childs
    31   // check childs
    32   for(child = node->children; child; child = child->next) {
    32   for (child = node->children; child; child = child->next) {
    33     if(!strcmp(child->name, "reason"))
    33     if (!strcmp(child->name, "reason"))
    34       nb_reason++;
    34       nb_reason++;
    35   }
    35   }
    36   
    36   
    37   if(reason > 1)
    37   if (reason > 1)
    38     return PARSE_ERROR_TOO_MANY_CHILDS;
    38     return PARSE_ERROR_TOO_MANY_CHILDS;
    39     
    39     
    40   return PARSE_OK;
    40   return PARSE_OK;
    41 }
    41 }
    42 
    42 
    50 }
    50 }
    51 
    51 
    52 
    52 
    53 int parse_content(LmMessageNode* node, struct info_content* ic)
    53 int parse_content(LmMessageNode* node, struct info_content* ic)
    54 {
    54 {
    55   if(!strcmp(ic->name, "content"))
    55   if (!strcmp(ic->name, "content"))
    56     return PARSE_ERROR_NAME;
    56     return PARSE_ERROR_NAME;
    57 
    57 
    58   ic->creator     = attrcpy(lm_message_node_get_attribute(node, "creator"));
    58   ic->creator     = attrcpy(lm_message_node_get_attribute(node, "creator"));
    59   ic->disposition = attrcpy(lm_message_node_get_attribute(node, "disposition"));
    59   ic->disposition = attrcpy(lm_message_node_get_attribute(node, "disposition"));
    60   ic->name        = attrcpy(lm_message_node_get_attribute(node, "name"));
    60   ic->name        = attrcpy(lm_message_node_get_attribute(node, "name"));
    61   ic->senders     = attrcpy(lm_message_node_get_attribute(node, "senders"));
    61   ic->senders     = attrcpy(lm_message_node_get_attribute(node, "senders"));
    62 
    62 
    63   // Put default if none
    63   // Put default if none
    64   if(ic->disposition == NULL)
    64   if (ic->disposition == NULL)
    65     ic->disposition = attrcpy("session");
    65     ic->disposition = attrcpy("session");
    66 
    66 
    67   // check required
    67   // check required
    68   if(ic->creator == NULL || ic->name == NULL)
    68   if (ic->creator == NULL || ic->name == NULL)
    69     return PARSE_ERROR_REQUIRED;
    69     return PARSE_ERROR_REQUIRED;
    70 
    70 
    71   // check restrictions
    71   // check restrictions
    72   if(!check_restriction(ic->creator, {"initiator", "responder", NULL}))
    72   if (!check_restriction(ic->creator, {"initiator", "responder", NULL}))
    73     return PARSE_ERROR_RESTRICTION;
    73     return PARSE_ERROR_RESTRICTION;
    74   if(!check_restriction(ic->senders, {"both", "initiator", "none", "responder", NULL}))
    74   if (!check_restriction(ic->senders, {"both", "initiator", "none", "responder", NULL}))
    75     ic->senders = NULL; // because it's optional
    75     ic->senders = NULL; // because it's optional
    76     
    76     
    77   return PARSE_OK;
    77   return PARSE_OK;
    78 }
    78 }
    79 
    79 
    90 int check_restriction(const char* name, const char** values)
    90 int check_restriction(const char* name, const char** values)
    91 {
    91 {
    92   const char* value;
    92   const char* value;
    93   int found = 0;
    93   int found = 0;
    94   value = values[0];
    94   value = values[0];
    95   while(value && !found) {
    95   while (value && !found) {
    96     if(!strcmp(name, value))
    96     if (!strcmp(name, value))
    97       found = 1;
    97       found = 1;
    98     value++;
    98     value++;
    99   }
    99   }
   100   return found;
   100   return found;
   101 }
   101 }
   102 
   102 
   103 
   103 
   104 char* attrcpy(const char* attr)
   104 char* attrcpy(const char* attr)
   105 {
   105 {
   106   char *tmp = NULL;
   106   char *tmp = NULL;
   107   if(attr != NULL) {
   107   if (attr != NULL) {
   108     tmp = (char*) malloc((strlen(attr)+1) * sizeof(char));
   108     tmp = (char*) malloc((strlen(attr)+1) * sizeof(char));
   109     strcpy(tmp, attr);
   109     strcpy(tmp, attr);
   110   }
   110   }
   111   return tmp;
   111   return tmp;
   112 }
   112 }