5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 2000 Pekka Riikonen
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.
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.
22 #include "silcincludes.h"
23 #include "silcnotify.h"
25 /******************************************************************************
29 ******************************************************************************/
31 struct SilcNotifyPayloadStruct {
34 unsigned char *message;
35 SilcArgumentPayload args;
38 /* Parse notify payload buffer and return data into payload structure */
40 SilcNotifyPayload silc_notify_payload_parse(SilcBuffer buffer)
42 SilcNotifyPayload new;
45 SILC_LOG_DEBUG(("Parsing Notify payload"));
47 new = silc_calloc(1, sizeof(*new));
49 silc_buffer_unformat(buffer,
50 SILC_STR_UI_SHORT(&new->type),
51 SILC_STR_UI_SHORT(&len),
52 SILC_STR_UI_CHAR(&new->argc),
55 if (len > buffer->len)
58 silc_buffer_pull(buffer, 5);
59 silc_buffer_unformat(buffer,
60 SILC_STR_UI_XNSTRING_ALLOC(&new->message, len),
64 silc_buffer_pull(buffer, len);
65 new->args = silc_argument_payload_parse(buffer, new->argc);
66 silc_buffer_push(buffer, len);
69 silc_buffer_push(buffer, 5);
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 *, unsigned int (len)}. */
82 SilcBuffer silc_notify_payload_encode(SilcNotifyType type, char *message,
83 unsigned int argc, va_list ap)
86 SilcBuffer args = NULL;
88 unsigned int *argv_lens = NULL, *argv_types = NULL;
94 argv = silc_calloc(argc, sizeof(unsigned char *));
95 argv_lens = silc_calloc(argc, sizeof(unsigned int));
96 argv_types = silc_calloc(argc, sizeof(unsigned int));
98 for (i = 0; i < argc; i++) {
99 x = va_arg(ap, unsigned char *);
100 x_len = va_arg(ap, unsigned int);
102 argv[i] = silc_calloc(x_len + 1, sizeof(unsigned char));
103 memcpy(argv[i], x, x_len);
104 argv_lens[i] = x_len;
105 argv_types[i] = i + 1;
108 args = silc_argument_payload_encode(argc, argv, argv_lens, argv_types);
111 for (i = 0; i < argc; i++)
114 silc_free(argv_lens);
115 silc_free(argv_types);
120 buffer = silc_buffer_alloc(len);
121 silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
123 silc_buffer_format(buffer,
124 SILC_STR_UI_SHORT(type),
125 SILC_STR_UI_SHORT(i),
126 SILC_STR_UI_CHAR(argc),
127 SILC_STR_UI_XNSTRING(message, i),
131 silc_buffer_pull(buffer, 5 + i);
132 silc_buffer_format(buffer,
133 SILC_STR_UI_XNSTRING(args->data, args->len),
135 silc_buffer_push(buffer, 5 + i);
136 silc_buffer_free(args);
142 /* Free's notify payload */
144 void silc_notify_payload_free(SilcNotifyPayload payload)
147 silc_argument_payload_free(payload->args);
148 silc_free(payload->message);
153 /* Return notify type */
155 SilcNotifyType silc_notify_get_type(SilcNotifyPayload payload)
157 return payload->type;
160 /* Return argument nums */
162 unsigned int silc_notify_get_arg_num(SilcNotifyPayload payload)
164 return payload->argc;
167 /* Return notify message */
169 unsigned char *silc_notify_get_message(SilcNotifyPayload payload)
171 return payload->message;
174 /* Return argument payload */
176 SilcArgumentPayload silc_notify_get_args(SilcNotifyPayload payload)
178 return payload->args;