1 /* SILC Client library tests */
4 #include "silcclient.h"
6 SilcClientOperations ops;
8 static void silc_running(SilcClient client, void *application);
9 static void silc_stopped(SilcClient client, void *context);
11 /******* MyBot code **********************************************************/
13 /* This is context for our MyBot client */
15 SilcClient client; /* The actual SILC Client */
16 SilcClientConnection conn; /* Connection to the server */
17 SilcPublicKey public_key; /* My public key */
18 SilcPrivateKey private_key; /* My private key */
21 /* Connect callback called after connected to remote server. */
24 silc_connected(SilcClient client, SilcClientConnection conn,
25 SilcClientConnectionStatus status,
26 SilcStatus error, const char *message,
29 MyBot mybot = client->application;
31 if (status == SILC_CLIENT_CONN_DISCONNECTED) {
32 SILC_LOG_DEBUG(("Disconnected %s", message ? message : ""));
33 silc_client_stop(client, silc_stopped, mybot);
37 if (status != SILC_CLIENT_CONN_SUCCESS &&
38 status != SILC_CLIENT_CONN_SUCCESS_RESUME) {
39 SILC_LOG_DEBUG(("Error connecting to server %d", status));
40 silc_client_stop(client, silc_stopped, mybot);
44 fprintf(stdout, "\nMyBot: Connected to server\n\n");
46 /* Now that we have connected to server, let's join a channel named
48 silc_client_command_call(client, conn, "JOIN mybot");
50 /* Save the connection context */
54 /* Running callback given to silc_client_init called to indicate that the
55 Client Library is running. After this Client API functions can be
58 static void silc_running(SilcClient client, void *application)
60 MyBot mybot = application;
62 SILC_LOG_DEBUG(("Client is running"));
64 /* Start connecting to server. This is asynchronous connecting so the
65 connection is actually created later after we run the client. */
66 silc_client_connect_to_server(mybot->client, NULL,
67 mybot->public_key, mybot->private_key,
68 "silc.silcnet.org", 706,
69 silc_connected, mybot);
72 /* Client stopped callback given to silc_client_stop. Called to indicate
73 that Client Library is stopped. */
75 static void silc_stopped(SilcClient client, void *context)
77 SILC_LOG_DEBUG(("Client stopped"));
80 /* Start the MyBot, by creating the SILC Client entity by using the
81 SILC Client Library API. */
85 SilcClientParams params;
87 /* Allocate the MyBot structure */
88 mybot = silc_calloc(1, sizeof(*mybot));
90 perror("Out of memory");
94 memset(¶ms, 0, sizeof(params));
95 params.threads = TRUE;
96 mybot->client = silc_client_alloc(&ops, ¶ms, mybot, NULL);
98 perror("Could not allocate SILC Client");
102 /* Now we initialize the client. */
103 if (!silc_client_init(mybot->client, silc_get_username(),
104 silc_net_localhost(), "I am the MyBot",
105 silc_running, mybot)) {
106 perror("Could not init client");
110 if (!silc_load_key_pair("mybot.pub", "mybot.prv", "",
112 &mybot->private_key)) {
113 /* The keys don't exist. Let's generate us a key pair then! There's
114 nice ready routine for that too. Let's do 2048 bit RSA key pair. */
115 fprintf(stdout, "MyBot: Key pair does not exist, generating it.\n");
116 if (!silc_create_key_pair("rsa", 2048, "mybot.pub", "mybot.prv", NULL, "",
118 &mybot->private_key, FALSE)) {
119 perror("Could not generated key pair");
124 /* And, then we are ready to go. Since we are really simple client we
125 don't have user interface and we don't have to deal with message loops
126 or interactivity. That's why we can just hand over the execution
127 to the library by calling silc_client_run. */
128 silc_client_run(mybot->client);
130 /* When we get here, we have quit the client, so clean up and exit */
131 silc_client_free(mybot->client);
136 /******* SILC Client Operations **********************************************/
138 /* The SILC Client Library requires these "client operations". They are
139 functions that the library may call at any time to indicate to application
140 that something happened, like message was received, or authentication
141 is required or something else. Since our MyBot is really simple client
142 we don't need most of the operations, so we just define them and don't
143 do anything in them. */
145 /* "say" client operation is a message from the client library to the
146 application. It may include error messages or something else. We
147 just dump them to screen. */
150 silc_say(SilcClient client, SilcClientConnection conn,
151 SilcClientMessageType type, char *msg, ...)
156 vsnprintf(str, sizeof(str) - 1, msg, va);
157 fprintf(stdout, "MyBot: %s\n", str);
162 /* Message for a channel. The `sender' is the sender of the message
163 The `channel' is the channel. The `message' is the message. Note
164 that `message' maybe NULL. The `flags' indicates message flags
165 and it is used to determine how the message can be interpreted
166 (like it may tell the message is multimedia message). */
169 silc_channel_message(SilcClient client, SilcClientConnection conn,
170 SilcClientEntry sender, SilcChannelEntry channel,
171 SilcMessagePayload payload,
172 SilcChannelPrivateKey key,
173 SilcMessageFlags flags, const unsigned char *message,
174 SilcUInt32 message_len)
176 /* Yay! We got a message from channel. */
178 if (flags & SILC_MESSAGE_FLAG_SIGNED)
179 fprintf(stdout, "[SIGNED] <%s> %s\n", sender->nickname, message);
181 fprintf(stdout, "<%s> %s\n", sender->nickname, message);
185 /* Private message to the client. The `sender' is the sender of the
186 message. The message is `message'and maybe NULL. The `flags'
187 indicates message flags and it is used to determine how the message
188 can be interpreted (like it may tell the message is multimedia
192 silc_private_message(SilcClient client, SilcClientConnection conn,
193 SilcClientEntry sender, SilcMessagePayload payload,
194 SilcMessageFlags flags,
195 const unsigned char *message,
196 SilcUInt32 message_len)
198 /* MyBot does not support private message receiving */
202 /* Notify message to the client. The notify arguments are sent in the
203 same order as servers sends them. The arguments are same as received
204 from the server except for ID's. If ID is received application receives
205 the corresponding entry to the ID. For example, if Client ID is received
206 application receives SilcClientEntry. Also, if the notify type is
207 for channel the channel entry is sent to application (even if server
208 does not send it because client library gets the channel entry from
209 the Channel ID in the packet's header). */
212 silc_notify(SilcClient client, SilcClientConnection conn,
213 SilcNotifyType type, ...)
220 /* Here we can receive all kinds of different data from the server, but
221 our simple bot is interested only in receiving the "not-so-important"
222 stuff, just for fun. :) */
224 case SILC_NOTIFY_TYPE_NONE:
225 /* Received something that we are just going to dump to screen. */
226 str = va_arg(va, char *);
227 fprintf(stdout, "--- %s\n", str);
230 case SILC_NOTIFY_TYPE_MOTD:
231 /* Received the Message of the Day from the server. */
232 str = va_arg(va, char *);
233 fprintf(stdout, "%s", str);
234 fprintf(stdout, "\n");
246 /* Command handler. This function is called always in the command function.
247 If error occurs it will be called as well. `conn' is the associated
248 client connection. `cmd_context' is the command context that was
249 originally sent to the command. `success' is FALSE if error occurred
250 during command. `command' is the command being processed. It must be
251 noted that this is not reply from server. This is merely called just
252 after application has called the command. Just to tell application
253 that the command really was processed. */
256 silc_command(SilcClient client, SilcClientConnection conn,
257 SilcBool success, SilcCommand command, SilcStatus status,
258 SilcUInt32 argc, unsigned char **argv)
260 /* If error occurred in client library with our command, print the error */
261 if (status != SILC_STATUS_OK)
262 fprintf(stderr, "MyBot: COMMAND %s: %s\n",
263 silc_get_command_name(command),
264 silc_get_status_message(status));
268 /* Command reply handler. This function is called always in the command reply
269 function. If error occurs it will be called as well. Normal scenario
270 is that it will be called after the received command data has been parsed
271 and processed. The function is used to pass the received command data to
274 `conn' is the associated client connection. `cmd_payload' is the command
275 payload data received from server and it can be ignored. It is provided
276 if the application would like to re-parse the received command data,
277 however, it must be noted that the data is parsed already by the library
278 thus the payload can be ignored. `success' is FALSE if error occurred.
279 In this case arguments are not sent to the application. The `status' is
280 the command reply status server returned. The `command' is the command
281 reply being processed. The function has variable argument list and each
282 command defines the number and type of arguments it passes to the
283 application (on error they are not sent). */
286 silc_command_reply(SilcClient client, SilcClientConnection conn,
287 SilcCommand command, SilcStatus status,
288 SilcStatus error, va_list ap)
290 /* If error occurred in client library with our command, print the error */
291 if (status != SILC_STATUS_OK)
292 fprintf(stderr, "MyBot: COMMAND REPLY %s: %s\n",
293 silc_get_command_name(command),
294 silc_get_status_message(status));
296 /* Check for successful JOIN. See
297 http://silcnet.org/docs/toolkit/command_reply_args.html for the
298 different arguments the client library returns. */
299 if (command == SILC_COMMAND_JOIN) {
300 SilcChannelEntry channel;
302 (void)va_arg(ap, SilcClientEntry);
303 channel = va_arg(ap, SilcChannelEntry);
305 fprintf(stdout, "MyBot: Joined '%s' channel\n", channel->channel_name);
307 /* Now send the "hello" to the channel */
308 silc_client_send_channel_message(client, conn, channel, NULL, 0, NULL,
309 "hello", strlen("hello"));
310 fprintf(stdout, "MyBot: Sent 'hello' to channel\n");
312 /* Now send digitally signed "hello" to the channel */
313 silc_client_send_channel_message(client, conn, channel, NULL,
314 SILC_MESSAGE_FLAG_SIGNED, NULL,
315 "hello, with signature",
316 strlen("hello, with signature"));
317 fprintf(stdout, "MyBot: Sent 'hello, with signature' to channel\n");
321 /* Find authentication method and authentication data by hostname and
322 port. The hostname may be IP address as well. When the authentication
323 method has been resolved the `completion' callback with the found
324 authentication method and authentication data is called. The `conn'
328 silc_get_auth_method(SilcClient client, SilcClientConnection conn,
329 char *hostname, SilcUInt16 port,
330 SilcAuthMethod auth_method,
331 SilcGetAuthMeth completion,
334 /* MyBot assumes that there is no authentication requirement in the
335 server and sends nothing as authentication. We just reply with
336 TRUE, meaning we know what is the authentication method. :). */
337 completion(SILC_AUTH_NONE, NULL, 0, context);
341 /* Verifies received public key. The `conn_type' indicates which entity
342 (server, client etc.) has sent the public key. If user decides to trust
343 the application may save the key as trusted public key for later
344 use. The `completion' must be called after the public key has been
348 silc_verify_public_key(SilcClient client, SilcClientConnection conn,
349 SilcConnectionType conn_type,
350 SilcPublicKey public_key,
351 SilcVerifyPublicKey completion, void *context)
353 silc_show_public_key(public_key);
354 completion(TRUE, context);
358 /* Ask (interact, that is) a passphrase from user. The passphrase is
359 returned to the library by calling the `completion' callback with
360 the `context'. The returned passphrase SHOULD be in UTF-8 encoded,
361 if not then the library will attempt to encode. */
364 silc_ask_passphrase(SilcClient client, SilcClientConnection conn,
365 SilcAskPassphrase completion, void *context)
367 /* MyBot does not support asking passphrases from users since there
368 is no user in our little client. We just reply with nothing. */
369 completion(NULL, 0, context);
373 /* Asks whether the user would like to perform the key agreement protocol.
374 This is called after we have received an key agreement packet or an
375 reply to our key agreement packet. This returns TRUE if the user wants
376 the library to perform the key agreement protocol and FALSE if it is not
377 desired (application may start it later by calling the function
378 silc_client_perform_key_agreement). If TRUE is returned also the
379 `completion' and `context' arguments must be set by the application. */
382 silc_key_agreement(SilcClient client, SilcClientConnection conn,
383 SilcClientEntry client_entry, const char *hostname,
384 SilcUInt16 protocol, SilcUInt16 port)
386 /* MyBot does not support incoming key agreement protocols, it's too
391 /* Notifies application that file transfer protocol session is being
392 requested by the remote client indicated by the `client_entry' from
393 the `hostname' and `port'. The `session_id' is the file transfer
394 session and it can be used to either accept or reject the file
395 transfer request, by calling the silc_client_file_receive or
396 silc_client_file_close, respectively. */
399 silc_ftp(SilcClient client, SilcClientConnection conn,
400 SilcClientEntry client_entry, SilcUInt32 session_id,
401 const char *hostname, SilcUInt16 port)
403 /* MyBot does not support file transfer, it's too simple for that too. */
407 /* Our client operations for the MyBot. This structure is filled with
408 functions and given as argument to the silc_client_alloc function.
409 Even though our little bot does not need all these functions we must
410 provide them since the SILC Client Library wants them all. */
411 /* This structure and all the functions were taken from the
412 lib/silcclient/client_ops_example.c. */
413 SilcClientOperations ops = {
415 silc_channel_message,
416 silc_private_message,
420 silc_get_auth_method,
421 silc_verify_public_key,
427 int main(int argc, char **argv)
429 if (argc > 1 && !strcmp(argv[1], "-d")) {
430 silc_log_debug(TRUE);
431 silc_log_debug_hexdump(TRUE);
432 silc_log_quick(TRUE);
433 silc_log_set_debug_string("*client*,*message*");