1 /*
2  * Copyright Colten Edwards 1996
3  */
4 
5 #include "irc.h"
6 static char cvsrevision[] = "$Id: readlog.c 3 2008-02-25 09:49:14Z keaston $";
7 CVS_REVISION(readlog_c)
8 #include "struct.h"
9 
10 #include <sys/stat.h>
11 
12 #include "ircterm.h"
13 #include "server.h"
14 #include "vars.h"
15 #include "ircaux.h"
16 #include "input.h"
17 #include "window.h"
18 #include "screen.h"
19 #include "output.h"
20 #include "status.h"
21 #include "misc.h"
22 #define MAIN_SOURCE
23 #include "modval.h"
24 #ifdef WANT_HEBREW
25 #include "hebrew.h"
26 #endif
27 
28 FILE * msg_fp = NULL;
29 
30 static  Window  *msg_window = NULL;
31 static  int     finished_msg_paging = 0;
32 static  Screen  *msg_screen = NULL;
33 static  int     use_msg_window = 0;
34 static	void log_prompt (char *name, char *line);
35 static	void set_msg_screen (Screen *);
36 static	char	*(*read_log_func) (char *, int, FILE *);
37 	void	log_put_it(const char *topic, const char *format, ...);
38 
BUILT_IN_COMMAND(remove_log)39 BUILT_IN_COMMAND(remove_log)
40 {
41 	char *expand;
42 	char *filename = NULL;
43 	int old_display = window_display;
44 
45 	int  reset_logptr = 0;
46 
47 	if ((get_string_var(MSGLOGFILE_VAR) == NULL) || (get_string_var(CTOOLZ_DIR_VAR) == NULL))
48 		return;
49 	malloc_sprintf(&filename, "%s/%s", get_string_var(CTOOLZ_DIR_VAR), get_string_var(MSGLOGFILE_VAR));
50 	expand = expand_twiddle(filename);
51 	new_free(&filename);
52 	window_display = 0;
53 	reset_logptr = logmsg(LOG_CURRENT, NULL, 3, NULL);
54 	log_toggle(0, NULL);
55 	window_display = old_display;
56 	if (unlink(expand))
57 	{
58 		bitchsay("Error unlinking: %s", expand);
59 		new_free(&expand);
60 		return;
61 	}
62 	window_display = 0;
63 	set_int_var(MSGCOUNT_VAR, 0);
64 	if (reset_logptr)
65 		log_toggle(1, NULL);
66 	window_display = old_display;
67 	bitchsay("Removed %s.", expand);
68 	status_update(1);
69 	new_free(&expand);
70 }
71 
72 static int in_read_log = 0;
73 
BUILT_IN_COMMAND(readlog)74 BUILT_IN_COMMAND(readlog)
75 {
76 	char *expand;
77 	struct	stat	stat_buf;
78 	char	*filename = NULL;
79 	char buffer[BIG_BUFFER_SIZE + 1];
80 
81 	read_log_func = fgets;
82 	if (!get_string_var(MSGLOGFILE_VAR))
83 		if (!args || (args && !*args))
84 			return;
85 	if (msg_window)
86 		return;
87 
88 	if (command)
89 	{
90 		in_read_log = 1;
91 		if (my_stricmp(command, "MORE"))
92 		{
93 			if (args && !my_strnicmp(args, "-resume", 2))
94 			{
95 				next_arg(args, &args);
96 				read_log_func = (char *(*)(char *, int, FILE *))global[RFGETS];
97 			}
98 		}
99 	}
100 	if (args && *args)
101 		malloc_sprintf(&filename, "%s", args);
102 	else
103 		malloc_sprintf(&filename, "%s/%s", get_string_var(CTOOLZ_DIR_VAR), get_string_var(MSGLOGFILE_VAR));
104 
105 	expand = expand_twiddle(filename);
106 	new_free(&filename);
107 	stat(expand, &stat_buf);
108 	strcpy(buffer, expand);
109 
110 	if (stat_buf.st_mode & S_IFDIR)
111 		return;
112 
113 	if ((msg_fp = fopen(expand, "r")) == NULL)
114 	{
115 		log_put_it(expand, "%s Error Opening Log file %s", thing_ansi, expand);
116 		new_free(&expand);
117 		msg_fp = NULL;
118 		return;
119 	}
120 	if (read_log_func == (char *(*)(char *, int, FILE *))global[RFGETS])
121 		fseek(msg_fp, 0, SEEK_END);
122 	new_free(&expand);
123 	msg_window = current_window;
124 	msg_screen = current_window->screen;
125 	log_prompt(buffer, NULL);
126 }
127 
128 /*
129  * show_help:  show's either a page of text from a help_fp, or the whole
130  * thing, depending on the value of HELP_PAGER_VAR.  If it gets to the end,
131  * (in either case it will eventally), it closes the file, and returns 0
132  * to indicate this.
133  */
show_log(Window * window,char * name)134 static	int show_log(Window *window, char *name)
135 {
136 	Window	*old_window;
137 	int	rows = 0;
138 	char	line[500];
139 
140 	if (window)
141 	{
142 		old_window = current_window;
143 		current_window = window;
144 	}
145 	else
146 	{
147 		old_window = NULL;
148 		window = current_window;
149 	}
150 	if (get_int_var(HELP_PAGER_VAR))
151 		rows = window->display_size - (window->double_status + 2);
152 	while (--rows)
153 	{
154  		if ((*read_log_func)(line, 499, msg_fp))
155 		{
156 			if (*(line + strlen(line) - 1) == '\n')
157 			*(line + strlen(line) - 1) = (char) 0;
158 			#ifdef WANT_HEBREW
159 			if (get_int_var(HEBREW_TOGGLE_VAR))
160 				hebrew_process(line);
161 			#endif
162 			log_put_it(name, "%s", line);
163 		}
164 		else
165 		{
166 			if (msg_fp) fclose(msg_fp);
167 			set_msg_screen(NULL);
168 			msg_fp = NULL;
169 			return (0);
170 		}
171 	}
172 	return (1);
173 }
174 
remove_away_log(char * stuff,char * line)175 void remove_away_log(char *stuff, char *line)
176 {
177 	if ((line && toupper(*line) == 'Y'))
178 		remove_log(NULL, NULL, NULL, NULL);
179 	in_read_log = 0;
180 }
181 
set_msg_screen(Screen * screen)182 static	void set_msg_screen(Screen *screen)
183 {
184 	msg_screen = screen;
185 	if (!msg_screen && msg_window)
186 	{
187 		if (use_msg_window)
188 		{
189 			int display = window_display;
190 
191 			window_display = 0;
192 			delete_window(msg_window);
193 			window_display = display;
194 		}
195 		msg_window = NULL;
196 		update_all_windows();
197 	}
198 }
199 
200 /*
201  * log_prompt: The main procedure called to display the log file
202  * currently being accessed.  Using add_wait_prompt(), it sets it
203  * self up to be recalled when the next page is asked for.   If
204  * called when we have finished paging the log file, we exit, as
205  * there is nothing left to show.  If line is 'q' or 'Q', exit the
206  * log pager, clean up, etc..  If all is cool for now, we call
207  * show_help, and either if its finished, exit, or prompt for the
208  * next page.
209  */
210 
log_prompt(char * name,char * line)211 static	void log_prompt(char *name, char *line)
212 {
213 
214 	if (line && *line && (toupper(*line) == 'Q'))
215 	{
216 		finished_msg_paging = 1;
217 		if (msg_fp)
218 			fclose(msg_fp);
219 		msg_fp = NULL;
220 		set_msg_screen(NULL);
221 		if (!in_read_log)
222 			add_wait_prompt("Delete msg log [y/N]? ", remove_away_log, empty_string, WAIT_PROMPT_LINE,1);
223 		return;
224 	}
225 
226 	if (show_log(msg_window, name))
227 	{
228 		add_wait_prompt("*** Hit any key for more, 'q' to quit ***",
229 			log_prompt, name, WAIT_PROMPT_KEY,1);
230 	}
231 	else
232 	{
233 		if (msg_fp)
234 			fclose(msg_fp);
235 		set_msg_screen(NULL);
236 		msg_fp = NULL;
237 		if (!in_read_log)
238 			add_wait_prompt("Delete msg log [y/N]? ", remove_away_log, empty_string, WAIT_PROMPT_LINE,1);
239 	}
240 }
241 
242