+
+/* Creates the main window used in SILC client. This is called always
+ at the initialization of the client. If user wants to create more
+ than one windows a new windows are always created by calling
+ silc_client_add_window. */
+
+void silc_client_create_main_window(SilcClientInternal app)
+{
+ void *screen;
+
+ SILC_LOG_DEBUG(("Creating main window"));
+
+ app->screen = silc_screen_init();
+ app->screen->input_buffer = app->input_buffer->data;
+ app->screen->u_stat_line.program_name = silc_name;
+ app->screen->u_stat_line.program_version = silc_version;
+
+ /* Create the actual screen */
+ screen = (void *)silc_screen_create_output_window(app->screen);
+ silc_screen_create_input_window(app->screen);
+ silc_screen_init_upper_status_line(app->screen);
+ silc_screen_init_output_status_line(app->screen);
+
+ app->screen->bottom_line->nickname = silc_get_username();
+ silc_screen_print_bottom_line(app->screen, 0);
+}
+
+/* The main task on SILC client. This processes the key pressings user
+ has made. */
+
+SILC_TASK_CALLBACK(silc_client_process_key_press)
+{
+ SilcClient client = (SilcClient)context;
+ SilcClientInternal app = (SilcClientInternal)client->application;
+ int c;
+
+ /* There is data pending in stdin, this gets it directly */
+ c = wgetch(app->screen->input_win);
+ if (silc_client_bad_keys(c))
+ return;
+
+ SILC_LOG_DEBUG(("Pressed key: %d", c));
+
+ switch(c) {
+ /*
+ * Special character handling
+ */
+ case KEY_UP:
+ case KEY_DOWN:
+ break;
+ case KEY_RIGHT:
+ /* Right arrow */
+ SILC_LOG_DEBUG(("RIGHT"));
+ silc_screen_input_cursor_right(app->screen);
+ break;
+ case KEY_LEFT:
+ /* Left arrow */
+ SILC_LOG_DEBUG(("LEFT"));
+ silc_screen_input_cursor_left(app->screen);
+ break;
+ case KEY_BACKSPACE:
+ case KEY_DC:
+ case '\177':
+ case '\b':
+ /* Backspace */
+ silc_screen_input_backspace(app->screen);
+ break;
+ case '\011':
+ /* Tabulator */
+ break;
+ case KEY_IC:
+ /* Insert switch. Turns on/off insert on input window */
+ silc_screen_input_insert(app->screen);
+ break;
+ case CTRL('j'):
+ case '\r':
+ /* Enter, Return. User pressed enter we are ready to
+ process the message. */
+ silc_client_process_message(app);
+ break;
+ case CTRL('l'):
+ /* Refresh screen, Ctrl^l */
+ silc_screen_refresh_all(app->screen);
+ break;
+ case CTRL('a'):
+ case KEY_HOME:
+ case KEY_BEG:
+ /* Beginning, Home */
+ silc_screen_input_cursor_home(app->screen);
+ break;
+ case CTRL('e'):
+ case KEY_END:
+ /* End */
+ silc_screen_input_cursor_end(app->screen);
+ break;
+ case KEY_LL:
+ /* End */
+ break;
+ case CTRL('g'):
+ /* Bell, Ctrl^g */
+ beep();
+ break;
+ case KEY_DL:
+ case CTRL('u'):
+ /* Delete line */
+ silc_client_clear_input(app);
+ break;
+ default:
+ /*
+ * Other characters
+ */
+ if (c < 32) {
+ /* Control codes are printed as reversed */
+ c = (c & 127) | 64;
+ wattron(app->screen->input_win, A_REVERSE);
+ silc_screen_input_print(app->screen, c);
+ wattroff(app->screen->input_win, A_REVERSE);
+ } else {
+ /* Normal character */
+ silc_screen_input_print(app->screen, c);
+ }
+ }
+
+ silc_screen_print_coordinates(app->screen, 0);
+ silc_screen_refresh_win(app->screen->input_win);
+}
+
+static int silc_client_bad_keys(unsigned char key)
+{
+ /* these are explained in curses.h */
+ switch(key) {
+ case KEY_SF:
+ case KEY_SR:
+ case KEY_NPAGE:
+ case KEY_PPAGE:
+ case KEY_PRINT:
+ case KEY_A1:
+ case KEY_A3:
+ case KEY_B2:
+ case KEY_C1:
+ case KEY_C3:
+ case KEY_UNDO:
+ case KEY_EXIT:
+ case '\v': /* VT */
+ case '\E': /* we ignore ESC */
+ return TRUE;
+ default:
+ return FALSE;
+ }
+}
+
+/* Clears input buffer */
+
+static void silc_client_clear_input(SilcClientInternal app)
+{
+ silc_buffer_clear(app->input_buffer);
+ silc_buffer_pull_tail(app->input_buffer,
+ SILC_BUFFER_END(app->input_buffer));
+ silc_screen_input_reset(app->screen);
+}
+
+/* Processes messages user has typed on the screen. This either sends
+ a packet out to network or if command were written executes it. */
+
+static void silc_client_process_message(SilcClientInternal app)
+{
+ unsigned char *data;
+ unsigned int len;
+
+ SILC_LOG_DEBUG(("Start"));
+
+ data = app->input_buffer->data;
+ len = strlen(data);
+
+ if (data[0] == '/' && data[1] != ' ') {
+ /* Command */
+ unsigned int argc = 0;
+ unsigned char **argv, *tmpcmd;
+ unsigned int *argv_lens, *argv_types;
+ SilcClientCommand *cmd;
+ SilcClientCommandContext ctx;
+
+ /* Get the command */
+ tmpcmd = silc_client_parse_command(data);
+ cmd = silc_client_local_command_find(tmpcmd);
+ if (!cmd && (cmd = silc_client_command_find(tmpcmd)) == NULL) {
+ silc_say(app->client, app->current_win, "Invalid command: %s", tmpcmd);
+ silc_free(tmpcmd);
+ goto out;
+ }
+
+ /* Now parse all arguments */
+ silc_parse_command_line(data + 1, &argv, &argv_lens,
+ &argv_types, &argc, cmd->max_args);
+ silc_free(tmpcmd);
+
+ SILC_LOG_DEBUG(("Executing command: %s", cmd->name));
+
+ /* Allocate command context. This and its internals must be free'd
+ by the command routine receiving it. */
+ ctx = silc_calloc(1, sizeof(*ctx));
+ ctx->client = app->client;
+ ctx->conn = app->conn;
+ ctx->command = cmd;
+ ctx->argc = argc;
+ ctx->argv = argv;
+ ctx->argv_lens = argv_lens;
+ ctx->argv_types = argv_types;
+
+ /* Execute command */
+ (*cmd->cb)(ctx);
+
+ } else {
+ /* Normal message to a channel */
+ if (len && app->conn->current_channel &&
+ app->conn->current_channel->on_channel == TRUE) {
+ silc_print(app->client, "> %s", data);
+ silc_client_packet_send_to_channel(app->client,
+ app->conn->sock,
+ app->conn->current_channel,
+ data, strlen(data), TRUE);
+ }
+ }
+
+ out:
+ /* Clear the input buffer */
+ silc_client_clear_input(app);
+}
+
+/* Returns the command fetched from user typed command line */
+
+static char *silc_client_parse_command(unsigned char *buffer)
+{
+ char *ret;
+ const char *cp = buffer;
+ int len;
+
+ len = strcspn(cp, " ");
+ ret = silc_to_upper((char *)++cp);
+ ret[len - 1] = 0;
+
+ return ret;
+}
+
+/* Updates clock on the screen every minute. */
+
+SILC_TASK_CALLBACK(silc_client_update_clock)
+{
+ SilcClient client = (SilcClient)context;
+ SilcClientInternal app = (SilcClientInternal)client->application;
+
+ /* Update the clock on the screen */
+ silc_screen_print_clock(app->screen);
+
+ /* Re-register this same task */
+ silc_task_register(qptr, 0, silc_client_update_clock, context,
+ silc_client_time_til_next_min(), 0,
+ SILC_TASK_TIMEOUT,
+ SILC_TASK_PRI_LOW);
+
+ silc_screen_refresh_win(app->screen->input_win);
+}
+
+/* Runs commands user configured in configuration file. This is
+ called when initializing client. */
+
+SILC_TASK_CALLBACK(silc_client_run_commands)
+{
+ SilcClient client = (SilcClient)context;
+ SilcClientInternal app = (SilcClientInternal)client->application;
+ SilcClientConfigSectionCommand *cs;
+
+ SILC_LOG_DEBUG(("Start"));
+
+ cs = app->config->commands;
+ while(cs) {
+ unsigned int argc = 0;
+ unsigned char **argv, *tmpcmd;
+ unsigned int *argv_lens, *argv_types;
+ SilcClientCommand *cmd;
+ SilcClientCommandContext ctx;
+
+ /* Get the command */
+ tmpcmd = silc_client_parse_command(cs->command);
+ cmd = silc_client_local_command_find(tmpcmd);
+ if (!cmd && (cmd = silc_client_command_find(tmpcmd)) == NULL) {
+ silc_say(client, app->conn, "Invalid command: %s", tmpcmd);
+ silc_free(tmpcmd);
+ continue;
+ }
+
+ /* Now parse all arguments */
+ silc_parse_command_line(cs->command + 1, &argv, &argv_lens,
+ &argv_types, &argc, cmd->max_args);
+ silc_free(tmpcmd);
+
+ SILC_LOG_DEBUG(("Executing command: %s", cmd->name));
+
+ /* Allocate command context. This and its internals must be free'd
+ by the command routine receiving it. */
+ ctx = silc_calloc(1, sizeof(*ctx));
+ ctx->client = client;
+ ctx->conn = app->conn;
+ ctx->command = cmd;
+ ctx->argc = argc;
+ ctx->argv = argv;
+ ctx->argv_lens = argv_lens;
+ ctx->argv_types = argv_types;
+
+ /* Execute command */
+ (*cmd->cb)(ctx);
+
+ cs = cs->next;
+ }
+}