This repository has been archived on 2021-04-17. You can view files and clone it, but cannot push or open issues or pull requests.
uIRC/src/ctcp/ctcp.c

106 lines
6.4 KiB
C
Raw Normal View History

/*
* This file is part of uIRC. (https://git.redxen.eu/caskd/uIRC)
* Copyright (c) 2019-2021 Alex-David Denes
*
* uIRC is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* uIRC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with uIRC. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ctcp.h"
#include <stdint.h>
// Tables
static int_least8_t ctcp_formats[] = {
[IRC_CTCP_BOLD] = 0x02, [IRC_CTCP_COLOR] = 0x03, [IRC_CTCP_COLOR_HEX] = 0x04,
[IRC_CTCP_RESET] = 0x0F, [IRC_CTCP_MONOSPACE] = 0x11, [IRC_CTCP_REVERSE] = 0x16,
[IRC_CTCP_ITALICS] = 0x1D, [IRC_CTCP_STRIKETHROUGH] = 0x1E, [IRC_CTCP_UNDERLINE] = 0x1F,
};
static int_least8_t ctcp_named_colors[] = {
[IRC_CTCP_COLORS_WHITE] = 0, [IRC_CTCP_COLORS_BLACK] = 1, [IRC_CTCP_COLORS_BLUE] = 2, [IRC_CTCP_COLORS_GREEN] = 3,
[IRC_CTCP_COLORS_RED] = 4, [IRC_CTCP_COLORS_BROWN] = 5, [IRC_CTCP_COLORS_MAGENTA] = 6, [IRC_CTCP_COLORS_ORANGE] = 7,
[IRC_CTCP_COLORS_YELLOW] = 8, [IRC_CTCP_COLORS_LIGHT_GREEN] = 9, [IRC_CTCP_COLORS_CYAN] = 10, [IRC_CTCP_COLORS_LIGHT_CYAN] = 11,
[IRC_CTCP_COLORS_LIGHT_BLUE] = 12, [IRC_CTCP_COLORS_PINK] = 13, [IRC_CTCP_COLORS_GREY] = 14, [IRC_CTCP_COLORS_LIGHT_GREY] = 15,
[IRC_CTCP_COLORS_DEFAULT] = 99,
};
// CTCP to ANSI format mappings
static short ansi_formats[] = {
[0x02] = 1, [0x0F] = 0, [0x16] = 7, [0x1D] = 3, [0x1E] = 9, [0x1F] = 4,
};
// CTCP to ANSI color mappings
// See https://modern.ircdocs.horse/formatting.html
static short ansi_colors[] = {
[0] = 97, [1] = 30, [2] = 34, [3] = 32, [4] = 31, [5] = 94, [6] = 35, [7] = 166, [8] = 33, [9] = 92,
[10] = 36, [11] = 96, [12] = 94, [13] = 127, [14] = 90, [15] = 37, [16] = 52, [17] = 94, [18] = 100, [19] = 58,
[20] = 22, [21] = 29, [22] = 23, [23] = 24, [24] = 17, [25] = 54, [26] = 53, [27] = 89, [28] = 88, [29] = 130,
[30] = 142, [31] = 64, [32] = 28, [33] = 35, [34] = 30, [35] = 25, [36] = 18, [37] = 91, [38] = 90, [39] = 125,
[40] = 124, [41] = 166, [42] = 184, [43] = 106, [44] = 34, [45] = 49, [46] = 37, [47] = 33, [48] = 19, [49] = 129,
[50] = 127, [51] = 161, [52] = 196, [53] = 208, [54] = 226, [55] = 154, [56] = 46, [57] = 86, [58] = 51, [59] = 75,
[60] = 21, [61] = 171, [62] = 201, [63] = 198, [64] = 203, [65] = 215, [66] = 227, [67] = 191, [68] = 83, [69] = 122,
[70] = 87, [71] = 111, [72] = 63, [73] = 177, [74] = 207, [75] = 205, [76] = 217, [77] = 223, [78] = 229, [79] = 193,
[80] = 157, [81] = 158, [82] = 159, [83] = 153, [84] = 147, [85] = 183, [86] = 219, [87] = 212, [88] = 16, [89] = 233,
[90] = 235, [91] = 237, [92] = 239, [93] = 241, [94] = 244, [95] = 247, [96] = 250, [97] = 254, [98] = 231, [99] = 39,
};
// CTCP to RGB color mappings
static rgbcolor rgb_maps[] = {
[0] = { 0xFF, 0xFF, 0xFF }, [1] = { 0x00, 0x00, 0x00 }, [2] = { 0x00, 0x00, 0x7F }, [3] = { 0x00, 0x93, 0x00 },
[4] = { 0xFF, 0x00, 0x00 }, [5] = { 0x7F, 0x00, 0x00 }, [6] = { 0x9C, 0x00, 0x9C }, [7] = { 0xFC, 0x7F, 0x00 },
[8] = { 0xFF, 0xFF, 0x00 }, [9] = { 0x00, 0xFC, 0x00 }, [10] = { 0x00, 0x93, 0x93 }, [11] = { 0x00, 0xFF, 0xFF },
[12] = { 0x00, 0x00, 0xFC }, [13] = { 0xFF, 0x00, 0xFF }, [14] = { 0x7F, 0x7F, 0x7F }, [15] = { 0xD2, 0xD2, 0xD2 },
[16] = { 0x47, 0x00, 0x00 }, [17] = { 0x47, 0x21, 0x00 }, [18] = { 0x47, 0x47, 0x00 }, [19] = { 0x32, 0x47, 0x00 },
[20] = { 0x00, 0x47, 0x00 }, [21] = { 0x00, 0x47, 0x2c }, [22] = { 0x00, 0x47, 0x47 }, [23] = { 0x00, 0x27, 0x47 },
[24] = { 0x00, 0x00, 0x47 }, [25] = { 0x2e, 0x00, 0x47 }, [26] = { 0x47, 0x00, 0x47 }, [27] = { 0x47, 0x00, 0x2a },
[28] = { 0x74, 0x00, 0x00 }, [29] = { 0x74, 0x3a, 0x00 }, [30] = { 0x74, 0x74, 0x00 }, [31] = { 0x51, 0x74, 0x00 },
[32] = { 0x00, 0x74, 0x00 }, [33] = { 0x00, 0x74, 0x49 }, [34] = { 0x00, 0x74, 0x74 }, [35] = { 0x00, 0x40, 0x74 },
[36] = { 0x00, 0x00, 0x74 }, [37] = { 0x4b, 0x00, 0x74 }, [38] = { 0x74, 0x00, 0x74 }, [39] = { 0x74, 0x00, 0x45 },
[40] = { 0xb5, 0x00, 0x00 }, [41] = { 0xb5, 0x63, 0x00 }, [42] = { 0xb5, 0xb5, 0x00 }, [43] = { 0x7d, 0xb5, 0x00 },
[44] = { 0x00, 0xb5, 0x00 }, [45] = { 0x00, 0xb5, 0x71 }, [46] = { 0x00, 0xb5, 0xb5 }, [47] = { 0x00, 0x63, 0xb5 },
[48] = { 0x00, 0x00, 0xb5 }, [49] = { 0x75, 0x00, 0xb5 }, [50] = { 0xb5, 0x00, 0xb5 }, [51] = { 0xb5, 0x00, 0x6b },
[52] = { 0xff, 0x00, 0x00 }, [53] = { 0xff, 0x8c, 0x00 }, [54] = { 0xff, 0xff, 0x00 }, [55] = { 0xb2, 0xff, 0x00 },
[56] = { 0x00, 0xff, 0x00 }, [57] = { 0x00, 0xff, 0xa0 }, [58] = { 0x00, 0xff, 0xff }, [59] = { 0x00, 0x8c, 0xff },
[60] = { 0x00, 0x00, 0xff }, [61] = { 0xa5, 0x00, 0xff }, [62] = { 0xff, 0x00, 0xff }, [63] = { 0xff, 0x00, 0x98 },
[64] = { 0xff, 0x59, 0x59 }, [65] = { 0xff, 0xb4, 0x59 }, [66] = { 0xff, 0xff, 0x71 }, [67] = { 0xcf, 0xff, 0x60 },
[68] = { 0x6f, 0xff, 0x6f }, [69] = { 0x65, 0xff, 0xc9 }, [70] = { 0x6d, 0xff, 0xff }, [71] = { 0x59, 0xb4, 0xff },
[72] = { 0x59, 0x59, 0xff }, [73] = { 0xc4, 0x59, 0xff }, [74] = { 0xff, 0x66, 0xff }, [75] = { 0xff, 0x59, 0xbc },
[76] = { 0xff, 0x9c, 0x9c }, [77] = { 0xff, 0xd3, 0x9c }, [78] = { 0xff, 0xff, 0x9c }, [79] = { 0xe2, 0xff, 0x9c },
[80] = { 0x9c, 0xff, 0x9c }, [81] = { 0x9c, 0xff, 0xdb }, [82] = { 0x9c, 0xff, 0xff }, [83] = { 0x9c, 0xd3, 0xff },
[84] = { 0x9c, 0x9c, 0xff }, [85] = { 0xdc, 0x9c, 0xff }, [86] = { 0xff, 0x9c, 0xff }, [87] = { 0xff, 0x94, 0xd3 },
[88] = { 0x00, 0x00, 0x00 }, [89] = { 0x13, 0x13, 0x13 }, [90] = { 0x28, 0x28, 0x28 }, [91] = { 0x36, 0x36, 0x36 },
[92] = { 0x4d, 0x4d, 0x4d }, [93] = { 0x65, 0x65, 0x65 }, [94] = { 0x81, 0x81, 0x81 }, [95] = { 0x9f, 0x9f, 0x9f },
[96] = { 0xbc, 0xbc, 0xbc }, [97] = { 0xe2, 0xe2, 0xe2 }, [98] = { 0xff, 0xff, 0xff }, [99] = { 0xFF, 0xFF, 0xFF },
};
rgbcolor*
uirc_ctcp_to_rgb(uint_least8_t color)
{
if (color < sizeof(rgb_maps) / sizeof(rgbcolor)) return &rgb_maps[color];
return &rgb_maps[IRC_CTCP_RESET];
}
int_least8_t
IRC_CTCP_FORMAT(enum uirc_table_ctcp_formats fmt)
{
return ctcp_formats[fmt];
}
int_least8_t
IRC_CTCP_COLORS(enum uirc_table_ctcp_named_colors col)
{
return ctcp_named_colors[col];
}
// TODO: Write converters for ANSI