A LOT updates. Cannot separate. :)
[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 int argc;
34   SilcArgumentPayload args;
35 };
36
37 /* Parse notify payload buffer and return data into payload structure */
38
39 SilcNotifyPayload silc_notify_payload_parse(SilcBuffer buffer)
40 {
41   SilcNotifyPayload new;
42   unsigned short len;
43
44   SILC_LOG_DEBUG(("Parsing Notify payload"));
45
46   new = silc_calloc(1, sizeof(*new));
47
48   silc_buffer_unformat(buffer,
49                        SILC_STR_UI_SHORT(&new->type),
50                        SILC_STR_UI_SHORT(&len),
51                        SILC_STR_UI_CHAR(&new->argc),
52                        SILC_STR_END);
53
54   if (len > buffer->len)
55     goto err;
56
57   if (new->argc) {
58     silc_buffer_pull(buffer, 5);
59     new->args = silc_argument_payload_parse(buffer, new->argc);
60     silc_buffer_push(buffer, 5);
61   }
62
63   return new;
64
65  err:
66   silc_free(new);
67   return NULL;
68 }
69
70 /* Encode notify payload with variable argument list. If `argc' is > 0
71    argument payloads will be associated to the notify payload. Variable
72    arguments must be {usigned char *, unsigned int (len)}. */
73
74 SilcBuffer silc_notify_payload_encode(SilcNotifyType type, unsigned int argc, 
75                                       va_list ap)
76 {
77   SilcBuffer buffer;
78   SilcBuffer args = NULL;
79   unsigned char **argv;
80   unsigned int *argv_lens = NULL, *argv_types = NULL;
81   unsigned char *x;
82   unsigned int x_len;
83   int i, len = 0;
84
85   if (argc) {
86     argv = silc_calloc(argc, sizeof(unsigned char *));
87     argv_lens = silc_calloc(argc, sizeof(unsigned int));
88     argv_types = silc_calloc(argc, sizeof(unsigned int));
89     
90     for (i = 0; i < argc; i++) {
91       x = va_arg(ap, unsigned char *);
92       x_len = va_arg(ap, unsigned int);
93       
94       argv[i] = silc_calloc(x_len + 1, sizeof(unsigned char));
95       memcpy(argv[i], x, x_len);
96       argv_lens[i] = x_len;
97       argv_types[i] = i + 1;
98     }
99
100     args = silc_argument_payload_encode(argc, argv, argv_lens, argv_types);
101     len = args->len;
102
103     for (i = 0; i < argc; i++)
104       silc_free(argv[i]);
105     silc_free(argv);
106     silc_free(argv_lens);
107     silc_free(argv_types);
108   }
109
110   len += 5;
111   buffer = silc_buffer_alloc(len);
112   silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
113   silc_buffer_format(buffer,
114                      SILC_STR_UI_SHORT(type),
115                      SILC_STR_UI_SHORT(len),
116                      SILC_STR_UI_CHAR(argc),
117                      SILC_STR_END);
118
119   if (argc) {
120     silc_buffer_pull(buffer, 5);
121     silc_buffer_format(buffer,
122                        SILC_STR_UI_XNSTRING(args->data, args->len),
123                        SILC_STR_END);
124     silc_buffer_push(buffer, 5);
125     silc_buffer_free(args);
126   }
127
128   return buffer;
129 }
130
131 /* Free's notify payload */
132
133 void silc_notify_payload_free(SilcNotifyPayload payload)
134 {
135   if (payload) {
136     silc_argument_payload_free(payload->args);
137     silc_free(payload);
138   }
139 }
140
141 /* Return notify type */
142
143 SilcNotifyType silc_notify_get_type(SilcNotifyPayload payload)
144 {
145   return payload->type;
146 }
147
148 /* Return argument nums */
149
150 unsigned int silc_notify_get_arg_num(SilcNotifyPayload payload)
151 {
152   return payload->argc;
153 }
154
155 /* Return argument payload */
156
157 SilcArgumentPayload silc_notify_get_args(SilcNotifyPayload payload)
158 {
159   return payload->args;
160 }