Added SILC Server library.
[silc.git] / lib / silccore / silcnotify.c
1 /*
2
3   silcnotify.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2000 - 2005 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19 /* $Id$ */
20
21 #include "silc.h"
22 #include "silcnotify.h"
23
24 /******************************************************************************
25
26                                Notify Payload
27
28 ******************************************************************************/
29
30 struct SilcNotifyPayloadStruct {
31   SilcNotifyType type;
32   unsigned char argc;
33   SilcArgumentPayload args;
34 };
35
36 /* Parse notify payload buffer and return data into payload structure */
37
38 SilcNotifyPayload silc_notify_payload_parse(const unsigned char *payload,
39                                             SilcUInt32 payload_len)
40 {
41   SilcBufferStruct buffer;
42   SilcNotifyPayload newp;
43   SilcUInt16 len;
44   int ret;
45
46   SILC_LOG_DEBUG(("Parsing Notify payload"));
47
48   silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
49   newp = silc_calloc(1, sizeof(*newp));
50   if (!newp)
51     return NULL;
52
53   ret = silc_buffer_unformat(&buffer,
54                              SILC_STR_UI_SHORT(&newp->type),
55                              SILC_STR_UI_SHORT(&len),
56                              SILC_STR_UI_CHAR(&newp->argc),
57                              SILC_STR_END);
58   if (ret == -1)
59     goto err;
60
61   if (len > silc_buffer_len(&buffer))
62     goto err;
63
64   if (newp->argc) {
65     silc_buffer_pull(&buffer, 5);
66     newp->args = silc_argument_payload_parse(buffer.data, silc_buffer_len(&buffer),
67                                              newp->argc);
68     silc_buffer_push(&buffer, 5);
69   }
70
71   return newp;
72
73  err:
74   silc_free(newp);
75   return NULL;
76 }
77
78 /* Encode notify payload with variable argument list. If `argc' is > 0
79    argument payloads will be associated to the notify payload. Variable
80    arguments must be {usigned char *, SilcUInt32 (len)}. */
81
82 SilcBuffer silc_notify_payload_encode(SilcNotifyType type, SilcUInt32 argc,
83                                       va_list ap)
84 {
85   SilcBuffer buffer;
86   SilcBuffer args = NULL;
87   unsigned char **argv;
88   SilcUInt32 *argv_lens = NULL, *argv_types = NULL;
89   unsigned char *x;
90   SilcUInt32 x_len, len = 0;
91   int i, k = 0;
92
93   if (argc) {
94     argv = silc_calloc(argc, sizeof(unsigned char *));
95     if (!argv)
96       return NULL;
97     argv_lens = silc_calloc(argc, sizeof(SilcUInt32));
98     if (!argv_lens) {
99       silc_free(argv);
100       return NULL;
101     }
102     argv_types = silc_calloc(argc, sizeof(SilcUInt32));
103     if (!argv_types) {
104       silc_free(argv_lens);
105       silc_free(argv);
106       return NULL;
107     }
108
109     for (i = 0, k = 0; i < argc; i++) {
110       x = va_arg(ap, unsigned char *);
111       x_len = va_arg(ap, SilcUInt32);
112
113       if (!x || !x_len)
114         continue;
115
116       argv[k] = silc_memdup(x, x_len);
117       if (!argv[k])
118         return NULL;
119       argv_lens[k] = x_len;
120       argv_types[k] = i + 1;
121       k++;
122     }
123
124     args = silc_argument_payload_encode(k, argv, argv_lens, argv_types);
125     len = silc_buffer_len(args);
126
127     for (i = 0; i < k; i++)
128       silc_free(argv[i]);
129     silc_free(argv);
130     silc_free(argv_lens);
131     silc_free(argv_types);
132   }
133
134   len += 5;
135   buffer = silc_buffer_alloc_size(len);
136   if (!buffer)
137     return NULL;
138   silc_buffer_format(buffer,
139                      SILC_STR_UI_SHORT(type),
140                      SILC_STR_UI_SHORT(len),
141                      SILC_STR_UI_CHAR(k),
142                      SILC_STR_END);
143
144   if (k) {
145     silc_buffer_pull(buffer, 5);
146     silc_buffer_format(buffer,
147                        SILC_STR_UI_XNSTRING(args->data, silc_buffer_len(args)),
148                        SILC_STR_END);
149     silc_buffer_push(buffer, 5);
150     silc_buffer_free(args);
151   }
152
153   return buffer;
154 }
155
156 /* Same as above but takes argument from the `args' Argument Payload. */
157
158 SilcBuffer silc_notify_payload_encode_args(SilcNotifyType type,
159                                            SilcUInt32 argc,
160                                            SilcBuffer args)
161 {
162   SilcBuffer buffer;
163   SilcUInt32 len;
164
165   len = 5 + (args ? silc_buffer_len(args) : 0);
166   buffer = silc_buffer_alloc_size(len);
167   if (!buffer)
168     return NULL;
169   silc_buffer_format(buffer,
170                      SILC_STR_UI_SHORT(type),
171                      SILC_STR_UI_SHORT(len),
172                      SILC_STR_UI_CHAR(argc),
173                      SILC_STR_END);
174
175   if (args) {
176     silc_buffer_pull(buffer, 5);
177     silc_buffer_format(buffer,
178                        SILC_STR_UI_XNSTRING(args->data, silc_buffer_len(args)),
179                        SILC_STR_END);
180     silc_buffer_push(buffer, 5);
181   }
182
183   return buffer;
184 }
185
186 /* Frees notify payload */
187
188 void silc_notify_payload_free(SilcNotifyPayload payload)
189 {
190   if (payload) {
191     silc_argument_payload_free(payload->args);
192     silc_free(payload);
193   }
194 }
195
196 /* Return notify type */
197
198 SilcNotifyType silc_notify_get_type(SilcNotifyPayload payload)
199 {
200   return payload->type;
201 }
202
203 /* Return argument nums */
204
205 SilcUInt32 silc_notify_get_arg_num(SilcNotifyPayload payload)
206 {
207   return payload->argc;
208 }
209
210 /* Return argument payload */
211
212 SilcArgumentPayload silc_notify_get_args(SilcNotifyPayload payload)
213 {
214   return payload->args;
215 }