1 /*
2 * log.c: handles the irc session logging functions
3 *
4 * Written By Michael Sandrof
5 *
6 * Copyright (c) 1990 Michael Sandrof.
7 * Copyright (c) 1991, 1992 Troy Rollo.
8 * Copyright (c) 1992-2014 Matthew R. Green.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include "irc.h"
36 IRCII_RCSID("@(#)$eterna: log.c,v 1.36 2015/11/20 09:23:54 mrg Exp $");
37
38 #include <sys/stat.h>
39
40 #include "log.h"
41 #include "vars.h"
42 #include "output.h"
43 #include "ircaux.h"
44
45 static FILE *irclog_fp;
46
47 void
do_log(int flag,u_char * logfile,FILE ** fpp)48 do_log(int flag, u_char *logfile, FILE **fpp)
49 {
50 time_t t;
51
52 if (logfile == NULL)
53 {
54 *fpp = NULL;
55 return;
56 }
57 t = time(0);
58 if (flag)
59 {
60 if (*fpp)
61 say("Logging is already on");
62 else
63 {
64 #ifdef DAEMON_UID
65 if (getuid() == DAEMON_UID)
66 {
67 say("You are not permitted to use LOG");
68 /* *fpp = NULL; unused */
69 }
70 else
71 #endif /* DAEMON_UID */
72 {
73 say("Starting logfile %s", logfile);
74 if ((*fpp = fopen(CP(logfile), "a")) != NULL)
75 {
76 #ifdef NEED_FCHMOD
77 chmod(logfile, S_IREAD | S_IWRITE);
78 #else
79 fchmod(fileno(*fpp),S_IREAD | S_IWRITE);
80 #endif /* NEED_FCHMOD */
81 fprintf(*fpp, "IRC log started %.16s\n",
82 ctime(&t));
83 fflush(*fpp);
84 }
85 else
86 {
87 say("Couldn't open logfile %s: %s",
88 logfile, strerror(errno));
89 *fpp = NULL;
90 }
91 }
92 }
93 }
94 else
95 {
96 if (*fpp)
97 {
98 fprintf(*fpp, "IRC log ended %.16s\n", ctime(&t));
99 fflush(*fpp);
100 fclose(*fpp);
101 *fpp = NULL;
102 say("Logfile ended");
103 }
104 }
105 return;
106 }
107
108 /* logger: if flag is 0, logging is turned off, else it's turned on */
109 void
logger(int flag)110 logger(int flag)
111 {
112 u_char *logfile;
113
114 if ((logfile = get_string_var(LOGFILE_VAR)) == NULL)
115 {
116 say("You must set the LOGFILE variable first!");
117 set_int_var(LOG_VAR, 0);
118 return;
119 }
120 do_log(flag, logfile, &irclog_fp);
121 if ((irclog_fp == NULL) && flag)
122 set_int_var(LOG_VAR, 0);
123 }
124
125 /*
126 * set_log_file: sets the log file name. If logging is on already, this
127 * closes the last log file and reopens it with the new name. This is called
128 * automatically when you SET LOGFILE.
129 */
130 void
set_log_file(u_char * filename)131 set_log_file(u_char *filename)
132 {
133 u_char *expanded;
134
135 if (filename)
136 {
137 if (my_strcmp(filename, get_string_var(LOGFILE_VAR)))
138 expanded = expand_twiddle(filename);
139 else
140 expanded = expand_twiddle(get_string_var(LOGFILE_VAR));
141 set_string_var(LOGFILE_VAR, expanded);
142 new_free(&expanded);
143 if (irclog_fp)
144 {
145 logger(0);
146 logger(1);
147 }
148 }
149 }
150
151 /*
152 * add_to_log: add the given line to the log file. If no log file is open
153 * this function does nothing.
154 */
155 void
add_to_log(FILE * fp,u_char * line)156 add_to_log(FILE *fp, u_char *line)
157 {
158 if (fp == NULL)
159 fp = irclog_fp;
160
161 if (fp)
162 {
163 fprintf(fp, "%s\n", line);
164 fflush(fp);
165 }
166 }
167