Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitchX
GitHub Repository: BitchX/BitchX1.3
Path: blob/master/source/log.c
1069 views
1
/*
2
* log.c: handles the irc session logging functions
3
*
4
*
5
* Written By Michael Sandrof
6
*
7
* Copyright(c) 1990
8
*
9
* See the COPYRIGHT file, or do a HELP IRCII COPYRIGHT
10
*/
11
12
13
#include "irc.h"
14
static char cvsrevision[] = "$Id: log.c 3 2008-02-25 09:49:14Z keaston $";
15
CVS_REVISION(log_c)
16
#include "struct.h"
17
18
#include <sys/stat.h>
19
20
#include "log.h"
21
#include "vars.h"
22
#include "screen.h"
23
#include "misc.h"
24
#include "output.h"
25
#include "ircaux.h"
26
#define MAIN_SOURCE
27
#include "modval.h"
28
29
FILE *irclog_fp = NULL;
30
31
void do_log(int flag, char *logfile, FILE **fp)
32
{
33
#ifdef PUBLIC_ACCESS
34
bitchsay("This command has been disabled on a public access system");
35
return;
36
#else
37
time_t t = now;
38
39
if (flag)
40
{
41
if (*fp)
42
say("Logging is already on");
43
else
44
{
45
if (!logfile)
46
return;
47
if (!(logfile = expand_twiddle(logfile)))
48
{
49
say("SET LOGFILE: No such user");
50
return;
51
}
52
53
if ((*fp = fopen(logfile, get_int_var(APPEND_LOG_VAR)?"a":"w")) != NULL)
54
{
55
say("Starting logfile %s", logfile);
56
chmod(logfile, S_IREAD | S_IWRITE);
57
fprintf(*fp, "IRC log started %.24s\n", ctime(&t));
58
fflush(*fp);
59
}
60
else
61
{
62
say("Couldn't open logfile %s: %s", logfile, strerror(errno));
63
*fp = NULL;
64
}
65
new_free(&logfile);
66
}
67
}
68
else
69
{
70
if (*fp)
71
{
72
fprintf(*fp, "IRC log ended %.24s\n", ctime(&t));
73
fflush(*fp);
74
fclose(*fp);
75
*fp = NULL;
76
say("Logfile ended");
77
}
78
}
79
#endif
80
}
81
82
/* logger: if flag is 0, logging is turned off, else it's turned on */
83
void logger(Window *win, char *unused, int flag)
84
{
85
char *logfile;
86
if ((logfile = get_string_var(LOGFILE_VAR)) == NULL)
87
{
88
say("You must set the LOGFILE variable first!");
89
set_int_var(LOG_VAR, 0);
90
return;
91
}
92
do_log(flag, logfile, &irclog_fp);
93
if (!irclog_fp && flag)
94
set_int_var(LOG_VAR, 0);
95
}
96
97
/*
98
* set_log_file: sets the log file name. If logging is on already, this
99
* closes the last log file and reopens it with the new name. This is called
100
* automatically when you SET LOGFILE.
101
*/
102
void set_log_file(Window *win, char *filename, int unused)
103
{
104
char *expanded;
105
106
if (filename)
107
{
108
if (strcmp(filename, get_string_var(LOGFILE_VAR)))
109
expanded = expand_twiddle(filename);
110
else
111
expanded = expand_twiddle(get_string_var(LOGFILE_VAR));
112
set_string_var(LOGFILE_VAR, expanded);
113
new_free(&expanded);
114
if (irclog_fp)
115
{
116
logger(current_window, NULL, 0);
117
logger(current_window, NULL, 1);
118
}
119
}
120
}
121
122
/*
123
* add_to_log: add the given line to the log file. If no log file is open
124
* this function does nothing.
125
*/
126
void BX_add_to_log(FILE *fp, time_t t, const char *line, int mangler)
127
{
128
#ifndef PUBLIC_ACCESS
129
if (fp && !inhibit_logging)
130
{
131
char *local_line;
132
int len = strlen(line) * 2 + 1;
133
local_line = alloca(len);
134
strcpy(local_line, line);
135
136
/* Do this first */
137
if (mangler)
138
mangle_line(local_line, mangler, len);
139
else if (!get_int_var(MIRCS_VAR))
140
{
141
char *tmp = alloca(strlen(local_line) + 1);
142
strip_control(local_line, tmp);
143
strcpy(local_line, tmp);
144
}
145
146
147
fprintf(fp, "%s\n", local_line);
148
fflush(fp);
149
}
150
#endif
151
}
152
153