1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 ICLOG.CC                                  */
4 /*                                                                           */
5 /* (C) 1995-96  Ullrich von Bassewitz                                        */
6 /*              Wacholderweg 14                                              */
7 /*              D-70597 Stuttgart                                            */
8 /* EMail:       uz@ibb.schwaben.com                                          */
9 /*                                                                           */
10 /*****************************************************************************/
11 
12 
13 
14 // $Id$
15 //
16 // $Log$
17 //
18 //
19 
20 
21 
22 #include <stdio.h>
23 
24 #include "event.h"
25 #include "strcvt.h"
26 #include "datetime.h"
27 #include "filepath.h"
28 
29 #include "icevents.h"
30 #include "devstate.h"
31 #include "iccli.h"
32 #include "iclog.h"
33 
34 
35 
36 /*****************************************************************************/
37 /*                             class CallLogger                              */
38 /*****************************************************************************/
39 
40 
41 
42 class CallLogger: public EventHandler {
43 
44     static String ExpandName (const String& Filename, const Time& T, unsigned Dev);
45     // Expand the name to a real file name.
46 
47     static void LogOutgoing (const String& Filename, String Msg,
48                              const Time& T, unsigned Dev);
49     // Log the message of the outgoing call to one file
50 
51     static void LogIncoming (const String& Filename, String Msg, const Time& T);
52     // Log the message of an incoming call to one file
53 
54 public:
55     virtual void HandleEvent (Event& E);
56     // Handle incoming events
57 
58 };
59 
60 
61 
ExpandName(const String & Filename,const Time & T,unsigned Dev)62 String CallLogger::ExpandName (const String& Filename, const Time& T, unsigned Dev)
63 // Expand the name to a real file name.
64 {
65     // Expand private escape sequences
66     unsigned I = 0;
67     unsigned Len = Filename.Len ();
68     String S (Len);
69     while (I < Len) {
70 
71         // Get the next character from the source
72         char C = Filename [I++];
73 
74         // Look after '%' only if another char follows
75         if (C == '%' && I < Len) {
76 
77             C = Filename [I++];
78             switch (C) {
79 
80                 case 'E':
81                     // %E - Insert extension number
82                     S += U32Str (Dev+21);
83                     break;
84 
85                 default:
86                     S += '%';
87                     S += C;
88                     break;
89 
90             }
91 
92         } else {
93 
94             // Just copy
95             S += C;
96 
97         }
98     }
99 
100     return MakeAbsolute (T.DateTimeStr (S));
101 }
102 
103 
104 
LogOutgoing(const String & Filename,String Msg,const Time & T,unsigned Dev)105 void CallLogger::LogOutgoing (const String& Filename, String Msg,
106                               const Time& T, unsigned Dev)
107 // Log the message of the outgoing call to one file
108 {
109     // If the filename is empty, bail out early
110     if (Filename.IsEmpty ()) {
111         return;
112     }
113 
114     // Expand the filename.
115     String ExpandedName = ExpandName (Filename, T, Dev);
116 
117     // Open the file
118     FILE* F = fopen (ExpandedName.GetStr (), "a+t");
119     if (F == NULL) {
120         // Got an error - ignore it
121         return;
122     }
123 
124     // Convert the string to the local conventions
125     Msg.OutputCvt ();
126 
127     // Write the message to the file
128     fputs (Msg.GetStr (), F);
129     fputc ('\n', F);
130 
131     // Close the file
132     fclose (F);
133 }
134 
135 
136 
LogIncoming(const String & Filename,String Msg,const Time & T)137 void CallLogger::LogIncoming (const String& Filename, String Msg,
138                               const Time& T)
139 // Log the message of an incoming call to one file
140 {
141     // If the filename is empty, bail out early
142     if (Filename.IsEmpty ()) {
143         return;
144     }
145 
146     // Expand the filename.
147     String ExpandedName = MakeAbsolute (T.DateTimeStr (Filename));
148 
149     // Open the file
150     FILE* F = fopen (ExpandedName.GetStr (), "a+t");
151     if (F == NULL) {
152         // Got an error - ignore it
153         return;
154     }
155 
156     // Convert the string to the local conventions
157     Msg.OutputCvt ();
158 
159     // Write the message to the file
160     fputs (Msg.GetStr (), F);
161     fputc ('\n', F);
162 
163     // Close the file
164     fclose (F);
165 }
166 
167 
168 
HandleEvent(Event & E)169 void CallLogger::HandleEvent (Event& E)
170 // Handle incoming events
171 {
172     String Msg (80);
173     DevStateInfo* DS;
174     CLI* C;
175 
176     switch (E.What) {
177 
178         case evCallComplete:
179             // Call is complete. E.Info.O is the DevStateInfo object
180             DS = (DevStateInfo*) E.Info.O;
181 
182             // Check if we should log the call
183             if (LogZeroCostCalls || DS->CallCharges > 0) {
184 
185                 // Ok, get the log message and convert it to the output format
186                 Msg = DS->LogMsg ();
187                 Msg.OutputCvt ();
188 
189                 // Log it in all three files
190                 LogOutgoing (OutgoingLog1, Msg, DS->CallStart, DS->DevNum);
191                 LogOutgoing (OutgoingLog2, Msg, DS->CallStart, DS->DevNum);
192                 LogOutgoing (OutgoingLog3, Msg, DS->CallStart, DS->DevNum);
193 
194             }
195             break;
196 
197         case evIncomingCall:
198             // Got an incoming call, E.Info.O is the CLI info object
199             C = (CLI*) E.Info.O;
200 
201             // Build a message
202             Msg = C->LogMsg ();
203 
204             // Log it to all three files
205             LogIncoming (IncomingLog1, Msg, C->T);
206             LogIncoming (IncomingLog2, Msg, C->T);
207             LogIncoming (IncomingLog3, Msg, C->T);
208             break;
209 
210 
211     }
212 }
213 
214 
215 
216 /*****************************************************************************/
217 /*                                   Data                                    */
218 /*****************************************************************************/
219 
220 
221 
222 // Names of the logfiles
223 String OutgoingLog1 = "outgoing.log";
224 String OutgoingLog2 = "";
225 String OutgoingLog3 = "";
226 String IncomingLog1 = "incoming.log";
227 String IncomingLog2 = "";
228 String IncomingLog3 = "";
229 
230 // If true, log calls with a chargecount of zero
231 int LogZeroCostCalls    = 1;
232 
233 // Price of a charge unit
234 double PricePerUnit = 0.12;
235 
236 // A static copy of a CallLogger object
237 static CallLogger CL;
238 
239 
240 
241