summaryrefslogtreecommitdiff
path: root/src/html.cpp
blob: 50cf1e64096b2d4c5f15d40d863dde850d97f5e8 (plain)
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
// class html implementation.

#ifndef s_html_CXX
#define s_html_CXX

#include <fstream>
#include "html.h"
#include "s_chat.h"
#include "s_mutx.h"

using namespace std;

html::html( )
{
  set_name( s_conf::get
              ().get_val( "HTMLTEMP" ) );
  pthread_mutex_init( &mut_map_vals, NULL );
}

html::~html( )
{
  pthread_mutex_destroy( &mut_map_vals );
}

void
html::clear_cache( )
{
  pthread_mutex_lock  ( &mut_map_vals );
  clear_vals();
  pthread_mutex_unlock( &mut_map_vals );
}

string
html::parse( map_string &map_params )
{
  string s_file = map_params["request"];

  // check if s_file is in the container.
  pthread_mutex_lock  ( &mut_map_vals );
  string s_templ = get_val( s_file );
  pthread_mutex_unlock( &mut_map_vals );

  // if not, read file.
  if ( s_templ.empty() )
  {
    auto string   s_path  = get_name();
    auto ifstream fs_templ( s_path.append( s_file ).c_str(), ios::binary );

    if ( ! fs_templ )
    {

      cerr << "File not found: " << s_file << endl;
      if(map_params["request"]==s_conf::get
            ().get_val( "NOTFOUND"  ))
          return "";

      map_params["request"] = s_conf::get
                                ().get_val( "NOTFOUND" );
      return parse( map_params );

    }

    auto char c_buf;
    while( !fs_templ.eof() )
    {
      fs_templ.get( c_buf );
      s_templ+=c_buf;
    }

    fs_templ.close();

#ifdef VERBOSE

    pthread_mutex_lock  ( &s_mutx::get
                            ().mut_stdout );
    cout << TECACHE << s_path << endl;
    pthread_mutex_unlock( &s_mutx::get
                            ().mut_stdout );
#endif

    // cache file.
    pthread_mutex_lock  ( &mut_map_vals );
    map_vals[ s_file ] = s_templ;
    pthread_mutex_unlock( &mut_map_vals );
  }

  // find %%KEY%% token and substituate those.
  auto unsigned int pos[2];
  pos[0] = pos[1] = 0;

  do
  {
    pos[0] = s_templ.find( "%%", pos[1] );

    if ( pos[0] == string::npos )
      break;

    pos[0] += 2;
    pos[1]  = s_templ.find( "%%", pos[0] );

    if ( pos[0] == string::npos )
      break;

    // get key and val.
    auto string s_key = s_templ.substr( pos[0], pos[1]-pos[0] );
    auto string s_val = s_conf::get
                          ().get_val( s_key );

    // if s_val is empty use map_params.
    if ( s_val.empty() )
      s_val = map_params[ s_key ];

    // substituate key with val.
    s_templ.replace( pos[0]-2, pos[1]-pos[0]+4, s_val );

    // calculate the string displacement.
    auto int i_dif = s_val.length() - ( pos[1] - pos[0] + 4);

    pos[1] += 2 + i_dif;

  }
  while( true );

  return s_templ;
}

void
html::online_list( user *p_user, map_string &map_params )
{
  // prepare user_list.
  string s_list     ( ""     );
  string s_seperator( "<br>" );

  p_user->get_p_room()->get_user_list( s_list, s_seperator );

  // use the collected data as a message in html-templates.
  map_params["MESSAGE"] = s_list;

  // renew the timestamp.
  p_user->renew_stamp();

  // send a ping to the client chat stream.
  p_user->msg_post( new string("\n") );
}

#endif