cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

input.c (4493B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20#include "config.h"
     21#include "input.h"
     22#include "terminal/terminal.h"
     23#include "telnet.h"
     24
     25#include <guacamole/client.h>
     26#include <guacamole/mem.h>
     27#include <guacamole/recording.h>
     28#include <guacamole/user.h>
     29#include <libtelnet.h>
     30
     31#include <regex.h>
     32#include <stdlib.h>
     33#include <sys/types.h>
     34#include <unistd.h>
     35
     36int guac_telnet_user_mouse_handler(guac_user* user, int x, int y, int mask) {
     37
     38    guac_client* client = user->client;
     39    guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
     40    guac_telnet_settings* settings = telnet_client->settings;
     41    guac_terminal* term = telnet_client->term;
     42
     43    /* Skip if terminal not yet ready */
     44    if (term == NULL)
     45        return 0;
     46
     47    /* Report mouse position within recording */
     48    if (telnet_client->recording != NULL)
     49        guac_recording_report_mouse(telnet_client->recording, x, y,
     50                mask);
     51
     52    /* Send mouse if not searching for password or username */
     53    if (settings->password_regex == NULL && settings->username_regex == NULL)
     54        guac_terminal_send_mouse(term, user, x, y, mask);
     55
     56    return 0;
     57
     58}
     59
     60int guac_telnet_user_key_handler(guac_user* user, int keysym, int pressed) {
     61
     62    guac_client* client = user->client;
     63    guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
     64    guac_telnet_settings* settings = telnet_client->settings;
     65    guac_terminal* term = telnet_client->term;
     66
     67    /* Report key state within recording */
     68    if (telnet_client->recording != NULL)
     69        guac_recording_report_key(telnet_client->recording,
     70                keysym, pressed);
     71
     72    /* Skip if terminal not yet ready */
     73    if (term == NULL)
     74        return 0;
     75
     76    /* Stop searching for password */
     77    if (settings->password_regex != NULL) {
     78
     79        guac_client_log(client, GUAC_LOG_DEBUG,
     80                "Stopping password prompt search due to user input.");
     81
     82        regfree(settings->password_regex);
     83        guac_mem_free(settings->password_regex);
     84        settings->password_regex = NULL;
     85
     86    }
     87
     88    /* Stop searching for username */
     89    if (settings->username_regex != NULL) {
     90
     91        guac_client_log(client, GUAC_LOG_DEBUG,
     92                "Stopping username prompt search due to user input.");
     93
     94        regfree(settings->username_regex);
     95        guac_mem_free(settings->username_regex);
     96        settings->username_regex = NULL;
     97
     98    }
     99
    100    /* Intercept and handle Pause / Break / Ctrl+0 as "IAC BRK" */
    101    if (pressed && (
    102                keysym == 0xFF13                  /* Pause */
    103             || keysym == 0xFF6B                  /* Break */
    104             || (
    105                    guac_terminal_get_mod_ctrl(term)
    106                    && keysym == '0'
    107                )                                 /* Ctrl + 0 */
    108       )) {
    109
    110        /* Send IAC BRK */
    111        telnet_iac(telnet_client->telnet, TELNET_BREAK);
    112
    113        return 0;
    114    }
    115
    116    /* Send key */
    117    guac_terminal_send_key(term, keysym, pressed);
    118
    119    return 0;
    120
    121}
    122
    123int guac_telnet_user_size_handler(guac_user* user, int width, int height) {
    124
    125    /* Get terminal */
    126    guac_client* client = user->client;
    127    guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
    128    guac_terminal* terminal = telnet_client->term;
    129
    130    /* Skip if terminal not yet ready */
    131    if (terminal == NULL)
    132        return 0;
    133
    134    /* Resize terminal */
    135    guac_terminal_resize(terminal, width, height);
    136
    137    /* Update terminal window size if connected */
    138    if (telnet_client->telnet != NULL && telnet_client->naws_enabled)
    139        guac_telnet_send_naws(telnet_client->telnet,
    140                guac_terminal_get_columns(terminal),
    141                guac_terminal_get_rows(terminal));
    142
    143    return 0;
    144}
    145