1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
/* Bezerk
* Copyright (C) 1998 Tony Gale.
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <glib.h>
#include <ctype.h>
#include "message.h"
#include "ch_utils.h"
#include "msg_utils.h"
#include "debug.h"
GSList *messages=NULL;
extern GdkColor colour_white;
extern GdkColor colour_black;
extern GdkColor colour_blue;
extern GdkColor colour_green;
extern GdkColor colour_red;
extern GdkColor colour_brown;
extern GdkColor colour_purple;
extern GdkColor colour_orange;
extern GdkColor colour_yellow;
extern GdkColor colour_lightgreen;
extern GdkColor colour_cyan;
extern GdkColor colour_lightcyan;
extern GdkColor colour_lightblue;
extern GdkColor colour_pink;
extern GdkColor colour_grey;
extern GdkColor colour_lightgrey;
extern GdkFont *font_bold;
extern GdkFont *font_normal;
extern GdkFont *font_italic;
extern GdkFont *font_bold_italic;
extern GdkFont *font_normal;
extern GdkFont *font_bold;
extern GdkFont *font_italic;
extern GdkFont *font_bold_italic;
GdkColor *irc_colours[] = {
&colour_white,
&colour_black,
&colour_blue,
&colour_green,
&colour_red,
&colour_brown,
&colour_purple,
&colour_orange,
&colour_yellow,
&colour_lightgreen,
&colour_cyan,
&colour_lightcyan,
&colour_lightblue,
&colour_pink,
&colour_grey,
&colour_lightgrey
};
int colour_range = 15; /* 0 - 15 */
Message *message_new(MsgType type, Connection *connection, char *source)
{
Message *message;
bs_function_enter();
message = g_malloc(sizeof(Message));
message->type = type;
message->connection = connection;
if (source) {
message->source = g_strdup(source);
} else {
message->source = NULL;
}
message->parts = NULL;
messages = g_slist_append(messages, message);
bs_function_leave();
return(message);
}
MessagePart *message_part_new(GdkColor *fg_colour, GdkColor *bg_colour, GdkFont *font, char *text)
{
MessagePart *message_part;
bs_function_enter();
message_part = g_malloc(sizeof(MessagePart));
message_part->fg_colour = fg_colour;
message_part->bg_colour = bg_colour;
message_part->font = font;
message_part->text = g_strdup(text);
bs_function_leave();
return(message_part);
}
GSList *message_part_parse_new(GSList *parts_list, GdkColor *default_colour, char *text)
{
char *text_copy, *start, *current, *end;
int fg_number=0, bg_number=1;
GdkColor *fg_colour;
GdkColor *bg_colour;
unsigned bold = FALSE;
unsigned reverse = FALSE;
unsigned underline = FALSE;
bs_function_enter();
fg_colour = default_colour;
bg_colour = &colour_black;
text_copy = g_strdup(text);
start = current = text_copy;
end = start + strlen(text_copy);
while (*current && (current < end)) {
if (*current == '\003') {
current[0] = '\0';
if (strlen(start)) {
parts_list = g_slist_append(parts_list,
message_part_new( (reverse ? bg_colour : fg_colour),
(reverse ? fg_colour : bg_colour),
(bold ? (underline ? font_bold_italic : font_bold) : (underline ? font_italic : font_normal) ),
start));
}
current++;
if ( (current < end) && isdigit(current[0]) ) {
fg_number = current[0] - '0';
current++;
if ( (current < end) && isdigit(current[0])) {
fg_number *= 10;
fg_number += current[0] - '0';
current++;
}
if ( (current < end) && (current[0] == ',') ) {
start = current++;
if ( (current < end) && isdigit(current[0]) ) {
bg_number = current[0] - '0';
current++;
} else start = current--;
if ( (current < end) && isdigit(current[0])) {
bg_number *= 10;
bg_number += current[0] - '0';
current++;
}
}
if (fg_number > colour_range) {
fg_colour = default_colour;
} else {
fg_colour = irc_colours[fg_number];
}
if (bg_number > colour_range) {
bg_colour = &colour_black;
} else {
bg_colour = irc_colours[bg_number];
}
}
start = current;
fg_number = 0;
bg_number = 1;
} else if (*current == '\002') { /* ^B */
current[0] = '\0';
if (strlen(start)) {
parts_list = g_slist_append(parts_list,
message_part_new( (reverse ? bg_colour : fg_colour),
(reverse ? fg_colour : bg_colour),
(bold ? (underline ? font_bold_italic : font_bold) : (underline ? font_italic : font_normal) ),
start));
}
bold = !bold;
start = ++current;
} else if (*current == '\026') { /* ^V */
current[0] = '\0';
if (strlen(start)) {
parts_list = g_slist_append(parts_list,
message_part_new( (reverse ? bg_colour : fg_colour),
(reverse ? fg_colour : bg_colour),
(bold ? (underline ? font_bold_italic : font_bold) : (underline ? font_italic : font_normal) ),
start));
}
reverse = !reverse;
start = ++current;
} else if (*current == '\037') { /* ^_ */
current[0] = '\0';
if (strlen(start)) {
parts_list = g_slist_append(parts_list,
message_part_new( (reverse ? bg_colour : fg_colour),
(reverse ? fg_colour : bg_colour),
(bold ? (underline ? font_bold_italic : font_bold) : (underline ? font_italic : font_normal) ),
start));
}
underline = !underline;
start = ++current;
} else {
current++;
}
}
if (strlen(start)) {
parts_list = g_slist_append(parts_list,
message_part_new( (reverse ? bg_colour : fg_colour),
(reverse ? fg_colour : bg_colour),
(bold ? (underline ? font_bold_italic : font_bold) : (underline ? font_italic : font_normal) ),
start));
}
bs_function_leave();
return(parts_list);
}
void message_parts_free(GSList *message_parts)
{
GSList *glist_entry;
MessagePart *message_part;
bs_function_enter();
glist_entry = message_parts;
while (glist_entry) {
message_part = glist_entry->data;
g_free(message_part->text);
g_free(message_part);
glist_entry = g_slist_next(glist_entry);
}
g_slist_free(message_parts);
bs_function_leave();
return;
}
void messages_write()
{
GSList *message_ptr;
Message *message;
BezBaseWindow *window;
GSList *glist_entry;
MessagePart *message_part;
ChannelInfo *target_channel;
int numchars=0;
bs_function_enter();
message_ptr = messages;
while (message_ptr) {
message = message_ptr->data;
if (!message->connection) {
printf("\n\n\nERROR: message->connection == NULL\n\n\n");
message_ptr = g_slist_next(message_ptr);
continue;
}
if (!message->parts) {
message_ptr = g_slist_next(message_ptr);
continue;
}
switch (message->type)
{
case MT_CONSOLE:
window = BEZ_BASE_WINDOW(message->connection->console);
break;
case MT_CHANNEL:
if (!message->source) {
if (message->connection->current_window &&
(message->connection->current_window->type == BEZ_CHANNEL) ) {
window = BEZ_BASE_WINDOW(message->connection->current_window);
} else {
window = BEZ_BASE_WINDOW(message->connection->console);
}
} else {
target_channel = find_channel(message->connection->channels, message->source);
if ( (window = BEZ_BASE_WINDOW(target_channel->window)) == NULL ) {
/* Window hasn't been created yet, send to console window */;
window = BEZ_BASE_WINDOW(message->connection->console);
}
}
break;
case MT_NICK:
if ( !(window = BEZ_BASE_WINDOW(find_message(message->connection->messages, message->source))) ) {
if (message->connection->current_window) {
window = BEZ_BASE_WINDOW(message->connection->current_window);
} else {
window = BEZ_BASE_WINDOW(message->connection->console);
}
}
break;
default:
target_channel = find_channel(message->connection->channels, message->source);
if ( (window = BEZ_BASE_WINDOW(target_channel->window)) == NULL ) {
window = BEZ_BASE_WINDOW(message->connection->console);
}
}
gtk_text_freeze(GTK_TEXT(window->text));
if (window->numlines) {
gtk_text_insert (GTK_TEXT (window->text), NULL, NULL, NULL, "\n", -1);
}
glist_entry = message->parts;
while (glist_entry) {
message_part = glist_entry->data;
gtk_text_insert (GTK_TEXT (window->text),
(message_part->font ? message_part->font : font_normal),
message_part->fg_colour,
message_part->bg_colour, message_part->text, -1);
glist_entry = g_slist_next(glist_entry);
}
gtk_text_thaw(GTK_TEXT(window->text));
if (++(window->numlines) > 200) {
gtk_text_freeze(GTK_TEXT(window->text));
while( GTK_TEXT_INDEX( GTK_TEXT(window->text), numchars) != '\n') {
numchars++;
}
gtk_text_set_point( GTK_TEXT(window->text), 0);
gtk_text_forward_delete( GTK_TEXT(window->text), numchars+1);
gtk_text_set_point( GTK_TEXT(window->text),
gtk_text_get_length( GTK_TEXT(window->text)));
(window->numlines)--;
gtk_text_thaw(GTK_TEXT(window->text));
}
GTK_TEXT(window->text)->vadj->value = GTK_TEXT(window->text)->vadj->upper -
GTK_TEXT(window->text)->vadj->page_size;
gtk_signal_emit_by_name ( GTK_OBJECT(GTK_TEXT(window->text)->vadj), "value_changed");
message_parts_free(message->parts);
if (message->source) {
g_free(message->source);
}
g_free(message);
message_ptr = g_slist_next(message_ptr);
}
g_slist_free(messages);
messages = NULL;
bs_function_leave();
return;
}
|