File indexing completed on 2024-04-14 14:32:22

0001 /***************************************************************************
0002                           cmsp.h  -  MUD Sound Protocol
0003                              -------------------
0004     begin                : Ne mar 16 2003
0005     copyright            : (C) 2003 by Tomas Mecir
0006     email                : kmuddy@kmuddy.com
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #ifndef CMSP_H
0019 #define CMSP_H
0020 
0021 #include <qstring.h>
0022 #include <qstringlist.h>
0023 #include <string>
0024 
0025 #include <cactionbase.h>
0026 #include <kmuddy_export.h>
0027 
0028 class cDownloader;
0029 class cSoundPlayer;
0030 
0031 /**
0032 MUD Sound Protocol implementation.
0033 
0034 Implementation notes (those that are not specified in the protocol):
0035 - sound trigger is only caught at beginning of line (whitespaces can precede
0036   it, but nothing else)
0037 - anything following the sound trigger till end of line is discarded
0038 - unterminated sound triggers are discarded as well
0039 - unknown trigger parameters are ignored (to ensure compatibility with
0040   future version of protocol, if any)
0041 - only one download can be active at any time
0042 - official protocol specification is poor
0043 
0044   *@author Tomas Mecir
0045   */
0046 
0047 class KMUDDY_EXPORT cMSP : public cActionBase {
0048 public: 
0049   cMSP (int sess);
0050   ~cMSP () override;
0051   void reset (const QString &serverName);
0052   /** enable MSP support */
0053   void enableMSP ();
0054   /** disable MSP support */
0055   void disableMSP ();
0056   /** when MSP is not allowed, sound triggers are received, but ignored */
0057   void setMSPAllowed (bool allow) { mspallowed = allow; };
0058   /** enable/disable downloading stuff from the server */
0059   void setDownloadAllowed (bool allow) { dloadallowed = allow; };
0060   /** Parse server output, looking for MSP tags. If some tag is found, parse
0061   it and act as required. The argument may be modified if needed (the MSP
0062   tag needs to be removed to prevent it from being displayed) */
0063   std::string parseServerOutput (const std::string &output);
0064 
0065   void setGlobalPaths (const QStringList &paths) { globaldirs = paths; };
0066   
0067   /** process a request - called after successful parsing, may also be called separately */
0068   void processRequest (bool isSOUND, QString fName, int volume, int repeats, int priority,
0069       QString type, QString url);
0070 protected:
0071   /** get next token */
0072   QString nextToken (QString &from);
0073   /** received corrupted trigger! */
0074   void corruptedTrigger (const QString &reason);
0075 
0076   /** parse !!SOUND / !!MUSIC trigger */
0077   void parseTrigger (const QString &seq, bool isSOUND);
0078   /** sound off */
0079   void soundOff ();
0080   /** music off */
0081   void musicOff ();
0082   /** play sound file */
0083   void playSound (const QString &path, int volume, int repeats, int priority);
0084   /** play music file */
0085   void playMusic (const QString &path, int volume, int repeats, bool continueIfRerequested);
0086 
0087   /** download file, as specified in the dl_* attributes, then play it */
0088   void downloadFile ();
0089   /** called by cDownloader */
0090   void downloadCompleted ();
0091   /** called by cDownloader */
0092   void downloadFailed (const QString &reason);
0093   
0094   /** get file name, if any, according to MSP rules (random choice if
0095   multiple matches) */
0096   QString getFileName (QString where, QString what);
0097   /** find a sound file in a list of paths specified by the user, returning
0098   a path to the file if it's found, QString() if it isn't */
0099   QString findFile (const QString &path);
0100 
0101   bool mspenabled, mspallowed, dloadallowed;
0102 
0103   /** global directories where sounds can be stored */
0104   QStringList globaldirs;
0105   
0106   /** default URL */
0107   QString defaultURL;
0108 
0109   /** local directory, where downloaded files will be stored */
0110   QString localdir;
0111   
0112   /** current state of the MSP parser (finite state machine) */
0113   int state;
0114   
0115   /** sound players */
0116   cSoundPlayer *soundPlayer, *midiPlayer;
0117   
0118   /** true = in SOUND, false = in MUSIC; only valid if applicable*/
0119   bool inSOUND;
0120   std::string cachedString, triggerContents;
0121 
0122   /** download information */
0123   cDownloader *downloader;
0124   QString dl_fName, dl_type, dl_url;
0125   int dl_volume, dl_repeats, dl_priority;
0126   bool dl_issound;
0127 
0128   friend class cDownloader;
0129 };
0130 
0131 #endif //CMSP_H