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