Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / libosip2 / src / osipparser2 / osip_contact.c
1 /*
2   The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3   Copyright (C) 2001,2002,2003  Aymeric MOIZARD jack@atosc.org
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20
21 #include <stdlib.h>
22 #include <stdio.h>
23
24 #include <osipparser2/osip_port.h>
25 #include <osipparser2/osip_message.h>
26 #include <osipparser2/osip_parser.h>
27
28 /* adds the contact header to message.              */
29 /* INPUT : const char *hvalue | value of header.    */
30 /* OUTPUT: osip_message_t *sip | structure to save results.  */
31 /* returns -1 on error. */
32 int
33 osip_message_set_contact (osip_message_t * sip, const char *hvalue)
34 {
35   int i;
36   osip_contact_t *contact;
37
38   if (hvalue == NULL || hvalue[0] == '\0')
39     return 0;
40
41   i = osip_contact_init (&contact);
42   if (i != 0)
43     return -1;
44   i = osip_contact_parse (contact, hvalue);
45   if (i != 0)
46     {
47       osip_contact_free (contact);
48       return -1;
49     }
50   sip->message_property = 2;
51   osip_list_add (sip->contacts, contact, -1);
52   return 0;                     /* ok */
53 }
54
55 /* parses a contact header.                                 */
56 /* INPUT : const char *string | pointer to a contact string.*/
57 /* OUTPUT: osip_contact_t *contact | structure to save results.  */
58 /* returns -1 on error. */
59 int
60 osip_contact_parse (osip_contact_t * contact, const char *hvalue)
61 {
62   if (contact == NULL)
63     return -1;
64   if (strncmp (hvalue, "*", 1) == 0)
65     {
66       contact->displayname = osip_strdup (hvalue);
67       return 0;
68     }
69   return osip_from_parse ((osip_from_t *) contact, hvalue);
70 }
71
72 int
73 osip_contact_init (osip_contact_t ** contact)
74 {
75   return osip_from_init ((osip_from_t **) contact);
76 }
77
78 /* returns the pos of contact header.                      */
79 /* INPUT : int pos | pos of contact header.                */
80 /* INPUT : osip_message_t *sip | sip message.                       */
81 /* OUTPUT: osip_contact_t *contact | structure to save results. */
82 /* returns -1 on error. */
83 int
84 osip_message_get_contact (const osip_message_t * sip, int pos,
85                           osip_contact_t ** dest)
86 {
87   *dest = NULL;
88   if (sip == NULL)
89     return -1;
90   if (osip_list_size (sip->contacts) <= pos)
91     return -1;                  /* does not exist */
92   *dest = (osip_contact_t *) osip_list_get (sip->contacts, pos);
93   return pos;
94 }
95
96 /* returns the contact header as a string.*/
97 /* INPUT : osip_contact_t *contact | contact.  */
98 /* returns null on error. */
99 int
100 osip_contact_to_str (const osip_contact_t * contact, char **dest)
101 {
102   if (contact == NULL)
103     return -1;
104   if (contact->displayname != NULL)
105     {
106       if (strncmp (contact->displayname, "*", 1) == 0)
107         {
108           *dest = osip_strdup ("*");
109           return 0;
110         }
111     }
112   return osip_from_to_str ((osip_from_t *) contact, dest);
113 }
114
115 /* deallocates a osip_contact_t structure.  */
116 /* INPUT : osip_contact_t *| contact. */
117 void
118 osip_contact_free (osip_contact_t * contact)
119 {
120   osip_from_free ((osip_from_t *) contact);
121 }
122
123 int
124 osip_contact_clone (const osip_contact_t * contact, osip_contact_t ** dest)
125 {
126   return osip_from_clone ((osip_from_t *) contact, (osip_from_t **) dest);
127 }