File indexing completed on 2024-04-28 15:35:15

0001 /***************************************************************************
0002  *   Copyright (C) 2004 by Tomas Mecir                                     *
0003  *   kmuddy@kmuddy.org                                                     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU Library General Public License as       *
0007  *   published by the Free Software Foundation; either version 2 of the    *
0008  *   License, or (at your option) any later version.                       *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU Library General Public License for more details.                  *
0014  ***************************************************************************/
0015 #ifndef CMXPSTATE_H
0016 #define CMXPSTATE_H
0017 
0018 #include <list>
0019 #include <map>
0020 #include <string>
0021 
0022 #include "libmxp.h"
0023 
0024 using namespace std;
0025 
0026 struct closingTag {
0027   //tag name (lowercase)
0028   string name;
0029   //closing result, if there's exactly one
0030   mxpResult *closingresult;
0031   //usually only zero or one element, but sometimes more :-)
0032   list<mxpResult *> *closingresults;
0033 };
0034 
0035 enum mxpMode {
0036   openMode = 0,
0037   secureMode,
0038   lockedMode
0039 };
0040 
0041 class cResultHandler;
0042 class cElementManager;
0043 class cEntityManager;
0044 
0045 /**
0046 This class provides most of the "intelligence" of the library - others feed it with what they
0047 got, and this class decides what to send to the client and all that :-)
0048 
0049 In particular:
0050 Stores state variables of the MXP parser - current font/color/..., as well as a list of
0051 currently open tags. Also provides conversion from tag properties to mxpState.
0052 Also handles tag closing and auto-closing.
0053 
0054 It's up to the caller to verify that bad things don't happen - errors will be sent if
0055 secure tags are sent in open mode and so...
0056 
0057 @author Tomas Mecir
0058 */
0059 
0060 class cMXPState {
0061 public:
0062   /** constructor */
0063   cMXPState (cResultHandler *resh, cElementManager *elm, cEntityManager *enm);
0064   /** destructor */
0065   ~cMXPState ();
0066 
0067 //some user-adjustable parameters
0068   void setDefaultText (const string &font, int size, bool _bold, bool _italic, bool _underline,
0069       bool _strikeout, RGB fg, RGB bg);
0070   void setHeaderParams (int which, const string &font, int size, bool _bold, bool _italic,
0071       bool _underline, bool _strikeout, RGB fg, RGB bg);
0072   void setDefaultGaugeColor (RGB color);
0073   void setNonProportFont (string font);
0074   void setClient (string name, string version);
0075 
0076   //SUPPORTS stuff
0077   void supportsLink (bool supports);
0078   void supportsGauge (bool supports);
0079   void supportsStatus (bool supports);
0080   void supportsSound (bool supports);
0081   void supportsFrame (bool supports);
0082   void supportsImage (bool supports);
0083   void supportsRelocate (bool supports);
0084 
0085   /** switch current mode and default mode to OPEN - call this before processing any text, if
0086   you want to better conform to MXP, at the risk of problems with non-MXP MUDs
0087   No reason to use this on MXP-enabled MUDs though - they will go to secure mode anyway... */
0088   void switchToOpen ();
0089 
0090   //reset everything to default values
0091   void reset ();
0092 
0093 //modes, mode switching
0094   /** return current mode */
0095   mxpMode getMXPMode ();
0096   /** switch mode; IMPORTANT: WILL NOT CLOSE TAGS OR ANYTHING!!! */
0097   void setMXPMode (mxpMode m);
0098   /** line tag received - act accordingly */
0099   void gotLineTag (int number);
0100   /** we should close all tags - called by gotLineTag, */
0101   void closeAllTags ();
0102 
0103 //regular text
0104   /** got regular text */
0105   void gotText (const string &text, bool expandentities = true);
0106   /** got new-line character */
0107   void gotNewLine ();
0108 
0109 //flags
0110   /** called when a flagged tag was received, either at its beginning or at its ending; the tag
0111   may not necessarily follow, as it's possible to flag some text without giving it any more
0112   attributes */
0113   void gotFlag (bool begin, string flag);
0114 
0115 //value retrieving (so that we can supply default parameters)
0116   bool isBold () { return bold; };
0117   bool isItalic () { return italic; };
0118   bool isUnderline () { return underline; };
0119   bool isStrikeout () { return strikeout; };
0120   RGB fgColor () { return fgcolor; };
0121   RGB bgColor () { return bgcolor; };
0122   string fontFace () { return curfont; };
0123   int fontSize () { return cursize; };
0124 
0125 //tags:
0126 //variables
0127   /** called upon !ENTITY tag (erase==true means erase the variable) */
0128   void gotVariable (const string &name, const string &value, bool erase = false);
0129   /** called upon the VAR tag */
0130   void gotVAR (const string &name);
0131 
0132 //text formatting (OPEN tags)
0133   void gotBOLD ();
0134   void gotITALIC ();
0135   void gotUNDERLINE ();
0136   void gotSTRIKEOUT ();
0137   void gotCOLOR (RGB fg, RGB bg);
0138   void gotHIGH ();
0139   void gotFONT (const string &face, int size, RGB fg, RGB bg);
0140 //line spacing
0141   void gotNOBR ();
0142   void gotP ();
0143   void gotBR ();
0144   void gotSBR ();
0145 //links
0146   /** got A-link; must wait for closing tag before sending anything to the client */
0147   void gotA (const string &href, const string &hint, const string &expire);
0148   /** got SEND-link; must wait for </SEND>, then expand &text; if needed */
0149   void gotSEND (const string &command, const string &hint, bool prompt, const string &expire);
0150   void gotEXPIRE (const string &name);
0151 //version control
0152   void gotVERSION ();
0153   void gotSUPPORT (list<string> params);
0154 //optional tags go next
0155 //other HTML tags
0156   /** got one of H1, ..., H6 (thus, which is in range 1..6) */
0157   void gotHtag (int which);
0158   void gotHR ();
0159   void gotSMALL ();
0160   void gotTT ();
0161 //MSP compatibility
0162   void gotSOUND (const string &fname, int vol, int count, int priority, const string &type,
0163       const string &url);
0164   void gotMUSIC (const string &fname, int vol, int count, bool contifrereq, const string &type,
0165       const string &url);
0166 //gauges / status bars
0167   void gotGAUGE (const string &entity, const string &maxentity, const string &caption, RGB color);
0168   void gotSTAT (const string &entity, const string &maxentity, const string &caption);
0169 //frames and cursor control
0170   void gotFRAME (const string &name, const string &action, const string &title, bool internal,
0171       const string &align, int left, int top, int width, int height, bool scrolling,
0172       bool floating);
0173   void gotDEST (const string &name, int x = -1, int y = -1, bool eol = false, bool eof = false);
0174 //crosslinking servers
0175   void gotRELOCATE (const string &hostname, int port);
0176   void gotUSER ();
0177   void gotPASSWORD ();
0178 //images
0179   void gotIMAGE (const string &fname, const string &url, const string &type, int height,
0180       int width, int hspace, int vspace, const string &align, bool ismap);
0181 
0182 //closing tags
0183   /** we got a closing-tag; caller must ensure that everything is okay (i.e. such closing tag
0184   can exist, tag is elementary (not user-defined), we're in correct mode, ...)
0185   also, the name should be a shorter variant (e.g. "b", not "bold") */
0186   void gotClosingTag (const string &name);
0187 
0188 //mxpResult handling
0189   /** create a closing mxpResult; must be called BEFORE applyResult() !!! */
0190   mxpResult *createClosingResult (mxpResult *what);
0191   /** apply this result */
0192   void applyResult (mxpResult *what);
0193 
0194 //screen, window and font size, used by FRAME and IMAGE tags
0195   void setScreenProps (int sx, int sy, int wx, int wy, int fx, int fy);
0196   /** compute coordinated, input can contain 'c' or '%' as the last parameter */
0197   int computeCoord (const string &coord, bool isX = true, bool inWindow = false);
0198 protected:
0199   /** close this tag :-) and delete the argument */
0200   void closeTag (closingTag *tag);
0201   /** add a closing tag to the list of pending closing tags */
0202   void addClosingTag (const string &name, mxpResult *res = 0, list<mxpResult *> *res2 = 0);
0203 
0204   /** stuff common for all tags */
0205   void commonTagHandler ();
0206   /** things to do after a tag is processed */
0207   void commonAfterTagHandler ();
0208 
0209   /** redirect output to frame <name> */
0210   void redirectTo (const string &name);
0211 
0212   cResultHandler *results;
0213   cElementManager *elements;
0214   cEntityManager *entities;
0215 
0216   /** list of closing tags */
0217   list<closingTag *> closingTags;
0218 
0219   /** list of existing frames */
0220   map<string, bool> frames;
0221 
0222   /** current mode */
0223   mxpMode mode;
0224   /** current default mode */
0225   mxpMode defaultmode;
0226   /** temporary secure mode? */
0227   bool tempMode;
0228   /** did we just leave secure mode?*/
0229   bool wasSecureMode;
0230 
0231   //all the parameters:
0232 
0233   //text attributes
0234   bool bold, italic, underline, strikeout;
0235   RGB fgcolor, bgcolor;
0236   string curfont;
0237   int cursize;
0238   //variables
0239   bool inVar;
0240   string varName, varValue;
0241   //paragraphs
0242   bool inParagraph;  //in P tag; no method returns this
0243   bool ignoreNextNewLine;  //after NOBR; no method returns this
0244   //links
0245   bool inLink, isALink;
0246   string lastcmd;
0247   string linkText;
0248   bool gotmap;
0249   //current window
0250   string curWindow, prevWindow;
0251 
0252   //initial LOCKED state?
0253   bool initiallyLocked;
0254   
0255   //currently implemented MXP version
0256   string mxpVersion;
0257 
0258   //user-defined values
0259   RGB defaultfg, defaultbg;
0260   string defaultfont;
0261   int defaultsize;
0262   char defaultattribs;
0263 
0264   string Hfont[6];
0265   RGB Hfg[6], Hbg[6];
0266   int Hsize[6];
0267   char Hattribs[6];
0268   string ttFont;
0269   RGB gaugeColor;
0270 
0271   string clientName, clientVersion;
0272 
0273   //SUPPORTS stuff...
0274   bool suplink, supgauge, supstatus, supsound, supframe, supimage, suprelocate;
0275   
0276   //screen and font parameters
0277   int sX, sY;      // width/height of the screen
0278   int wX, wY;      // width/height of the output window
0279   int fX, fY;      // width/height of character X
0280 };
0281 
0282 #endif