/* Monolith message. * - by Richard W.M. Jones * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id: message.c,v 1.4 2003/02/22 12:56:26 rich Exp $ */ #include "config.h" #include #include #ifdef HAVE_ASSERT_H #include #endif #ifdef HAVE_STRING_H #include #endif #ifdef HAVE_ALLOCA_H #include #endif #include #include #include #include #include #include "message.h" enum message_type { type_posting, type_enter, type_leave, type_blank }; struct message { enum message_type type; /* Type of message. */ union { struct { int hh, mm; /* Time of posting. */ const char *body; /* Message body (turned to HTML). */ int authorid; /* Author user ID (0 == anonymous). */ const char *original_ip; /* IP address as a string. */ } posting; } u; const char *username; /* Printable username (null == anonymous). */ }; message new_message_posting (pool pool, int hh, int mm, int authorid, const char *username, const char *original_ip, const char *body) { message m = pmalloc (pool, sizeof *m); m->type = type_posting; m->u.posting.hh = hh; m->u.posting.mm = mm; m->u.posting.authorid = authorid; m->u.posting.original_ip = pstrdup (pool, original_ip); m->u.posting.body = ml_smarttext_to_html (pool, body); m->username = username ? pstrdup (pool, username) : 0; return m; } message new_message_enter (pool pool, const char *username) { message m = pmalloc (pool, sizeof *m); m->type = type_enter; m->username = username ? pstrdup (pool, username) : 0; return m; } message new_message_leave (pool pool, const char *username) { message m = pmalloc (pool, sizeof *m); m->type = type_leave; m->username = username ? pstrdup (pool, username) : 0; return m; } message new_message_blank (pool pool) { message m = pmalloc (pool, sizeof *m); m->type = type_blank; return m; } static const char * colour_for_user (int userid) { switch (userid & 7) { case 0: return "#990033"; /* Also used for anonymous users. */ case 1: return "#ff0066"; case 2: return "#cc00cc"; case 3: return "#6600cc"; case 4: return "#3333ff"; case 5: return "#003366"; case 6: return "#009999"; case 7: return "#ff9900"; } /*NOTREACHED*/ return 0; } void message_show (const message m, io_handle io) { const char *username; const char *colour; const char *anon = "anonymous"; switch (m->type) { case type_posting: /* Construct the username. */ if (m->u.posting.authorid) /* Ugh. We promise not to modify it! */ username = m->username; else { int len = strlen (anon) + strlen (m->u.posting.original_ip) + 2; char *u; u = alloca (len); snprintf (u, len, "%s@%s", anon, m->u.posting.original_ip); username = u; } /* Pick a colour. */ colour = colour_for_user (m->u.posting.authorid); /* Display it. */ /* XXX Escaping! */ io_fprintf (io, "%02d:%02d " "<%s> " "%s
", m->u.posting.hh, m->u.posting.mm, username, colour, m->u.posting.body); break; case type_enter: if (m->username) { /* XXX Escaping! */ io_fprintf (io, "" "%s " "has entered the room
", m->username); } else { io_fprintf (io, "" "an anonymous user has entered the room
"); } break; case type_leave: if (m->username) { /* XXX Escaping! */ io_fprintf (io, "" "%s " "has left the room
", m->username); } else { io_fprintf (io, "" "an anonymous user has left the room
"); } break; case type_blank: io_fputs (" ", io); break; } }