]> code.citadel.org Git - citadel.git/blob - daphne/utils.cpp
* citadel.spec: commented out Icon line and the openldap-servers dependency.
[citadel.git] / daphne / utils.cpp
1 // utility functions not belonging to any particular class
2
3 #include "includes.hpp"
4 #include <time.h>
5
6
7 // Extract a field from a string returned by the server
8 //
9 void extract(wxString& outputbuf, wxString inputbuf, int parmnum) {
10         int i;
11         wxStringTokenizer *tok = new wxStringTokenizer(inputbuf, "|", FALSE);
12
13         for (i=0; tok->HasMoreTokens(); ++i) {
14                 outputbuf = tok->NextToken();
15                 if (i == parmnum) {
16                         return;
17                 }
18         }
19         outputbuf = "";
20 }
21
22 int extract_int(wxString inputbuf, int parmnum) {
23         wxString buf;
24
25         extract(buf, inputbuf, parmnum);
26         return atoi((const char *)buf);
27 }
28
29
30
31 // Convert traditional Citadel variformat text to HTML
32 void variformat_to_html(wxString& outputbuf,
33                         wxString inputbuf,
34                         bool add_header_and_footer) {
35
36         wxString buf, ch;
37         int pos;
38
39         outputbuf.Empty();
40
41         // Escape out any reserved characters
42         buf = "";
43         for (pos=0; pos<inputbuf.Length(); ++pos) {
44                 ch = inputbuf.Mid(pos, 1);
45                 if (ch == "<")
46                         buf.Append("&lt;");
47                 else if (ch == ">")
48                         buf.Append("&gt;");
49                 else if (ch == "\"")
50                         buf.Append("&quot;");
51                 else
52                         buf.Append(ch);
53         }
54
55         if (add_header_and_footer) {
56                 outputbuf.Append("<HTML><BODY>");
57         }
58
59         // Parse the body of the text
60         while (buf.Length() > 0) {
61                 pos = buf.Find('\n', FALSE);
62                 if (pos < 0) {
63                         buf = buf + "\n";
64                         pos = buf.Find('\n', FALSE);
65                 }
66                 if (outputbuf.Length() > 0) {
67                         if (buf.Left(1) == " ") {
68                                 outputbuf.Append("<BR>\n");
69                         } else {
70                                 outputbuf.Append(" ");
71                         }
72                 }
73                 outputbuf.Append(buf.Left(pos));
74                 buf = buf.Mid(pos+1);
75         }
76
77         if (add_header_and_footer) {
78                 outputbuf.Append("</BODY></HTML>\n");
79         }
80 }
81
82
83 wxString generate_html_header(CitMessage *message,
84                         wxString ThisRoom,
85                         wxString ThisNode) {
86
87         wxString ret;
88         int verbosity = 2;      // FIX add a prefs option for this
89
90         switch(verbosity) {
91
92         case 2:
93                 ret = "&nbsp;&nbsp;&nbsp;<H3>";
94                 ret += asctime(localtime(&message->timestamp));
95                 ret += " from " + message->author;
96                 if (message->room.CmpNoCase(ThisRoom))
97                         ret += " in " + message->room + "> ";
98                 if (message->nodename.CmpNoCase(ThisNode))
99                         ret += " @ " + message->nodename;
100                 if (message->recipient.Length() > 0)
101                         ret += " to " + message->recipient;
102                 ret += "</h3><br>";
103                 return ret;
104
105         case 3:
106                 ret = "" ;
107                 ret += "<TT>Date: </TT>";
108                 ret += asctime(localtime(&message->timestamp));
109                 ret += "<BR>";
110                 ret += "<TT>From: </TT>" + message->author;
111                 ret += " @ " + message->nodename + "<BR>";
112                 if (message->recipient.Length() > 0)
113                         ret += "<TT>To:   </TT>" + message->recipient + "<BR>";
114                 ret += "<BR>\n";
115                 return ret;
116                 
117         }
118 }
119
120
121
122
123
124
125 // Generic exit stuff
126 void cleanup(int exitcode) {
127         delete ini;             // Write configuration to disk
128         exit(exitcode);         // Go away.
129 }