Created.
[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   unsigned char *message;
35   SilcArgumentPayload args;
36 };
37
38 /* Parse notify payload buffer and return data into payload structure */
39
40 SilcNotifyPayload silc_notify_payload_parse(SilcBuffer buffer)
41 {
42   SilcNotifyPayload new;
43   unsigned short len;
44
45   SILC_LOG_DEBUG(("Parsing Notify payload"));
46
47   new = silc_calloc(1, sizeof(*new));
48
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),
53                        SILC_STR_END);
54
55   if (len > buffer->len)
56     goto err;
57
58   silc_buffer_pull(buffer, 5);
59   silc_buffer_unformat(buffer,
60                        SILC_STR_UI_XNSTRING_ALLOC(&new->message, len),
61                        SILC_STR_END);
62
63   if (new->argc) {
64     silc_buffer_pull(buffer, len);
65     new->args = silc_argument_payload_parse(buffer, new->argc);
66     silc_buffer_push(buffer, len);
67   }
68
69   silc_buffer_push(buffer, 5);
70
71   return new;
72
73  err:
74   silc_free(new);
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 *, unsigned int (len)}. */
81
82 SilcBuffer silc_notify_payload_encode(SilcNotifyType type, char *message,
83                                       unsigned int argc, va_list ap)
84 {
85   SilcBuffer buffer;
86   SilcBuffer args = NULL;
87   unsigned char **argv;
88   unsigned int *argv_lens = NULL, *argv_types = NULL;
89   unsigned char *x;
90   unsigned int x_len;
91   int i, len = 0;
92
93   if (argc) {
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));
97     
98     for (i = 0; i < argc; i++) {
99       x = va_arg(ap, unsigned char *);
100       x_len = va_arg(ap, unsigned int);
101       
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;
106     }
107
108     args = silc_argument_payload_encode(argc, argv, argv_lens, argv_types);
109     len = args->len;
110
111     for (i = 0; i < argc; i++)
112       silc_free(argv[i]);
113     silc_free(argv);
114     silc_free(argv_lens);
115     silc_free(argv_types);
116   }
117     
118   i = strlen(message);
119   len += 5 + i;
120   buffer = silc_buffer_alloc(len);
121   silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
122
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),
128                      SILC_STR_END);
129
130   if (argc) {
131     silc_buffer_pull(buffer, 5 + i);
132     silc_buffer_format(buffer,
133                        SILC_STR_UI_XNSTRING(args->data, args->len),
134                        SILC_STR_END);
135     silc_buffer_push(buffer, 5 + i);
136     silc_buffer_free(args);
137   }
138
139   return buffer;
140 }
141
142 /* Free's notify payload */
143
144 void silc_notify_payload_free(SilcNotifyPayload payload)
145 {
146   if (payload) {
147     silc_argument_payload_free(payload->args);
148     silc_free(payload->message);
149     silc_free(payload);
150   }
151 }
152
153 /* Return notify type */
154
155 SilcNotifyType silc_notify_get_type(SilcNotifyPayload payload)
156 {
157   return payload->type;
158 }
159
160 /* Return argument nums */
161
162 unsigned int silc_notify_get_arg_num(SilcNotifyPayload payload)
163 {
164   return payload->argc;
165 }
166
167 /* Return notify message */
168
169 unsigned char *silc_notify_get_message(SilcNotifyPayload payload)
170 {
171   return payload->message;
172 }
173
174 /* Return argument payload */
175
176 SilcArgumentPayload silc_notify_get_args(SilcNotifyPayload payload)
177 {
178   return payload->args;
179 }