Added SILC Server library.
[silc.git] / lib / silccore / silcstatus.c
1 /*
2
3   silcstatus.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2003 - 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 "silcstatus.h"
23
24 /* Returns arguments by the status type. */
25
26 SilcUInt32 silc_status_get_args(SilcStatus status,
27                                 SilcArgumentPayload args,
28                                 void **ret_arg1, void **ret_arg2)
29 {
30   SilcUInt32 num, len;
31   unsigned char *tmp;
32
33   assert(ret_arg1 && ret_arg2);
34
35   num = silc_argument_get_arg_num(args);
36   if (num > 3)
37     return 0;
38   if (num == 0)
39     return 0;
40
41   switch (status) {
42
43   case SILC_STATUS_ERR_NO_SUCH_NICK:
44   case SILC_STATUS_ERR_NO_SUCH_CHANNEL:
45   case SILC_STATUS_ERR_NO_SUCH_SERVER:
46   case SILC_STATUS_ERR_NO_SUCH_SERVICE:
47   case SILC_STATUS_ERR_UNKNOWN_ALGORITHM:
48     tmp = silc_argument_get_arg_type(args, 2, &len);
49     if (!tmp)
50       return 0;
51     *ret_arg1 = silc_memdup(tmp, len);
52     if (!(*ret_arg1))
53       return 0;
54     num = 1;
55     break;
56
57   case SILC_STATUS_ERR_NO_SUCH_CLIENT_ID:
58   case SILC_STATUS_ERR_BAD_CLIENT_ID:
59     {
60       SilcClientID client_id;
61       tmp = silc_argument_get_arg_type(args, 2, &len);
62       if (!tmp)
63         return 0;
64       if (silc_id_payload_parse_id(tmp, len, NULL, &client_id,
65                                    sizeof(client_id)))
66         return 0;
67       *ret_arg1 = silc_id_dup(&client_id, SILC_ID_CLIENT);
68       if (!(*ret_arg1))
69         return 0;
70       num = 1;
71     }
72     break;
73
74   case SILC_STATUS_ERR_NO_SUCH_SERVER_ID:
75   case SILC_STATUS_ERR_BAD_SERVER_ID:
76     {
77       SilcServerID server_id;
78       tmp = silc_argument_get_arg_type(args, 2, &len);
79       if (!tmp)
80         return 0;
81       if (silc_id_payload_parse_id(tmp, len, NULL, &server_id,
82                                    sizeof(server_id)))
83         return 0;
84       *ret_arg1 = silc_id_dup(&server_id, SILC_ID_SERVER);
85       if (!(*ret_arg1))
86         return 0;
87       num = 1;
88     }
89     break;
90
91   case SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID:
92   case SILC_STATUS_ERR_BAD_CHANNEL_ID:
93   case SILC_STATUS_ERR_NOT_ON_CHANNEL:
94   case SILC_STATUS_ERR_CHANNEL_IS_FULL:
95   case SILC_STATUS_ERR_NOT_INVITED:
96   case SILC_STATUS_ERR_BANNED_FROM_CHANNEL:
97   case SILC_STATUS_ERR_NO_CHANNEL_PRIV:
98   case SILC_STATUS_ERR_NO_CHANNEL_FOPRIV:
99     {
100       SilcChannelID channel_id;
101       tmp = silc_argument_get_arg_type(args, 2, &len);
102       if (!tmp)
103         return 0;
104       if (silc_id_payload_parse_id(tmp, len, NULL, &channel_id,
105                                    sizeof(channel_id)))
106         return 0;
107       *ret_arg1 = silc_id_dup(&channel_id, SILC_ID_CHANNEL);
108       if (!(*ret_arg1))
109         return 0;
110       num = 1;
111     }
112     break;
113
114   case SILC_STATUS_ERR_USER_NOT_ON_CHANNEL:
115   case SILC_STATUS_ERR_USER_ON_CHANNEL:
116     {
117       SilcClientID client_id;
118       SilcChannelID channel_id;
119       SilcIdType type;
120       tmp = silc_argument_get_arg_type(args, 2, &len);
121       if (!tmp)
122         return 0;
123       if (silc_id_payload_parse_id(tmp, len, &type, &client_id,
124                                    sizeof(client_id)))
125         return 0;
126       *ret_arg1 = silc_id_dup(&client_id, type);
127       if (!(*ret_arg1))
128         return 0;
129       num = 1;
130       tmp = silc_argument_get_arg_type(args, 3, &len);
131       if (!tmp)
132         return num;
133       if (silc_id_payload_parse_id(tmp, len, &type, &channel_id,
134                                    sizeof(channel_id)))
135         return 0;
136       *ret_arg2 = silc_id_dup(&channel_id, type);
137       if (!(*ret_arg2))
138         return num;
139       num = 2;
140     }
141     break;
142
143   default:
144     return 0;
145     break;
146   }
147
148   return num;
149 }