File: logfiles.c

package info (click to toggle)
epic5 3.0.3-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 5,328 kB
  • sloc: ansic: 75,810; makefile: 648; ruby: 227; python: 215; sh: 78; perl: 13
file content (1025 lines) | stat: -rw-r--r-- 25,547 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
/*
 * logfiles.c - General purpose log files
 *
 * Copyright 2002, 2012 EPIC Software Labs
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notices, the above paragraph (the one permitting redistribution),
 *    this list of conditions and the following disclaimer in the 
 *    documentation and/or other materials provided with the distribution.
 * 3. The names of the author(s) may not be used to endorse or promote 
 *    products derived from this software without specific prior written
 *    permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 * SUCH DAMAGE.
 */
#include "irc.h"
#include "levels.h"
#include "log.h"
#include "vars.h"
#include "output.h"
#include "ircaux.h"
#include "alias.h"
#include "list.h"
#include "server.h"
#include "window.h"
#include "functions.h"

#define MAX_TARGETS 32

#define LOG_TARGETS 0
#define LOG_WINDOWS 1
#define LOG_SERVERS 2

static const char *onoff[] = { "OFF", "ON" };
static const char *logtype[] = { "TARGETS", "WINDOWS", "SERVERS" };

/* This is NOT a "list.h" list; but "targets" is! */
struct Logfile {
	struct Logfile *next;
	int	refnum;

	char *	name;
	char *	filename;
	FILE *	log;
	int	servref;

	int	type;
	List	*targets;
	int	refnums[MAX_TARGETS];
	Mask	mask;

	char *	rewrite;
	int	mangler;
	char *	mangle_desc;
	int	active;

	time_t	activity;
};

typedef struct Logfile Logfile;
Logfile *logfiles = NULL;
int	logref = 0;
int	last_logref = -1;

static Logfile *	new_logfile (void)
{
	Logfile	*log, *ptr;
	int	i;

	log = (Logfile *)new_malloc(sizeof(Logfile));

	/* Move it to the end of the list. */
	for (ptr = logfiles; ptr && ptr->next;)
		ptr = ptr->next;
	if (ptr)
		ptr->next = log;
	else
		logfiles = log;

	log->next = NULL;
	log->refnum = ++logref;
	last_logref = log->refnum;
	log->name = malloc_sprintf(NULL, "%d", log->refnum);
	log->filename = NULL;
	log->log = NULL;
	log->servref = from_server;
	log->type = LOG_TARGETS;
	log->targets = NULL;
	for (i = 0; i < MAX_TARGETS; i++)
		log->refnums[i] = -1;
	mask_setall(&log->mask);
	log->rewrite = NULL;
	log->mangler = 0;
	log->mangle_desc = NULL;
	log->active = 0;
	time(&log->activity);

	return log;
}

static void	delete_logfile (Logfile *log)
{
	Logfile *prev;
	List *	next;

	if (log == logfiles)
		logfiles = log->next;
	else
	{
		for (prev = logfiles; prev; prev = prev->next)
			if (prev->next == log)
				break;
		if (prev)
			prev->next = log->next;
	}

	new_free(&log->name);
	if (log->active)
		do_log(0, log->filename, &log->log);
	new_free(&log->filename);

	while (log->targets)
	{
		next = log->targets->next;
		new_free((void **)&log->targets->name);
		new_free((void **)&log->targets->d);
		new_free((char **)&log->targets);
		log->targets = next;
	}

	new_free(&log->rewrite);
	new_free(&log->mangle_desc);
}

static Logfile *	get_log_by_desc (const char *desc)
{
	Logfile *log;

	if (is_number(desc))
	{
		int	number = atol(desc);

		for (log = logfiles; log; log = log->next)
			if (log->refnum == number)
				return log;
	}
	else
	{
		for (log = logfiles; log; log = log->next)
			if (!my_stricmp(log->name, desc))
				return log;
	}

	return NULL;
}

static int	is_logfile_name_unique (const char *desc)
{
	Logfile *log;

	for (log = logfiles; log; log = log->next)
		if (!my_stricmp(log->name, desc))
			return 0;
	return 1;
}

static void	clean_log_targets (Logfile *log)
{
	int	i;
	List *	next;

	for (i = 0; i < MAX_TARGETS; i++)
		log->refnums[i] = -1;

	while (log->targets)
	{
		next = log->targets->next;
		new_free(&log->targets->name);
		new_free((char **)&log->targets);
		log->targets = next;
	}
}

static char *logfile_get_targets (Logfile *log)
{
	List *	tmp;
	char *nicks = NULL;
	int	i;

	if (log->type == LOG_TARGETS || log->type == LOG_WINDOWS)
	{
		for (tmp = log->targets; tmp; tmp = tmp->next)
			malloc_strcat_wordlist(&nicks, ",", tmp->name);
	}
	else if (log->type == LOG_SERVERS)
	{
		for (i = 0; i < MAX_TARGETS; i++)
			if (log->refnums[i] != -1)
				malloc_strcat_wordlist(&nicks, ",", ltoa(log->refnums[i]));
	}

	return nicks;
}

/************************************************************************/
typedef Logfile *(*logfile_func) (Logfile *, char **);

static Logfile *logfile_activity (Logfile *log, char **args);
static Logfile *logfile_add (Logfile *log, char **args);
static Logfile *logfile_describe (Logfile *log, char **args);
static Logfile *logfile_filename (Logfile *log, char **args);
static Logfile *logfile_kill (Logfile *log, char **args);
static Logfile *logfile_list (Logfile *log, char **args);
static Logfile *logfile_mangle (Logfile *log, char **args);
static Logfile *logfile_name (Logfile *log, char **args);
static Logfile *logfile_new (Logfile *log, char **args);
static Logfile *logfile_off (Logfile *log, char **args);
static Logfile *logfile_on (Logfile *log, char **args);
static Logfile *logfile_refnum (Logfile *log, char **args);
static Logfile *logfile_remove (Logfile *log, char **args);
static Logfile *logfile_rewrite (Logfile *log, char **args);
static Logfile *logfile_type (Logfile *log, char **args);

static Logfile *	logfile_activity (Logfile *log, char **args)
{
	if (!log)
	{
		say("ACTIVITY: You need to specify a logfile first");
		return NULL;
	}

	time(&log->activity);
	return log;
}

static Logfile *	logfile_add (Logfile *log, char **args)
{
        char            *ptr;
        char            *arg = next_arg(*args, args);
	int		i;

	if (!log)
	{
		say("ADD: You need to specify a logfile first");
		return NULL;
	}

        if (!arg)
                say("ADD: Add nicknames/channels to be logged to this file");

        else while (arg)
        {
                if ((ptr = strchr(arg, ',')))
                        *ptr++ = 0;

		if (log->type == LOG_TARGETS)
		{
		    if (find_in_list(log->targets, arg) == NULL)
                    {
                        say("Added %s to log name list", arg);
			add_to_list(&log->targets, arg, NULL);
                    }
                    else
                        say("%s already on log name list", arg);
		}
		else if (log->type == LOG_SERVERS)
		{
		    int refnum;

		    if (log->type == LOG_SERVERS && !my_strnicmp("ALL", arg, 1))
			refnum = NOSERV;
		    else
			refnum = my_atol(arg);

		    for (i = 0; i < MAX_TARGETS; i++)
		    {
			if (log->refnums[i] == refnum)
			{
				say("%s already on log refnum list", arg);
				break;
			}
		    }
		    for (i = 0; i < MAX_TARGETS; i++)
		    {
			if (log->refnums[i] == -1)
			{
				say("Added %d to log name list", refnum);
				log->refnums[i] = refnum;
				break;
			}
		    }
		    if (i >= MAX_TARGETS)
			say("Could not add %d to log name list!", refnum);
		}
		else if (log->type == LOG_WINDOWS)
		{
		    if (!find_in_list(log->targets, arg))
                    {
                        say("Added %s to log window list", arg);
			add_to_list(&log->targets, arg, NULL);
                    }
		}
                arg = ptr;
        }

        return log;
}

static Logfile *	logfile_describe (Logfile *log, char **args)
{
	char *targets = NULL;

	if (!log)
	{
		say("DESCRIBE: You need to specify a logfile first");
		return NULL;
	}

	targets = logfile_get_targets(log);

	say(" Logfile refnum %d is %s", log->refnum, onoff[log->active]);
	say("\t Logical name: %s", log->name);
	say("\t     Filename: %s", log->filename ? log->filename : "<NONE>");
	say("\t         Type: %s", logtype[log->type]);
	if (log->servref == NOSERV)
		say("\t       Server: ALL");
	else
		say("\t       Server: %d", log->servref);
	say("\tTarget/Refnum: %s", targets ? targets : "<NONE>");
	say("\t        Level: %s", mask_to_str(&log->mask));
	say("\t Rewrite Rule: %s", log->rewrite ? log->rewrite : "<NONE>");
	say("\t Mangle rules: %s", log->mangle_desc ? log->mangle_desc : "<NONE>");

	new_free(&targets);
	return log;
}

static Logfile *	logfile_filename (Logfile *log, char **args)
{
	char *	arg = next_arg(*args, args);

	if (!log)
	{
		say("FILENAME: You need to specify a logfile first");
		return NULL;
	}

	if (!arg)
	{
		if (log->filename)
			say("Log %s is attached to %s", log->name, log->filename);
		else
			say("Log %s does not have a filename", log->name);
		return log;
	}

	if (log->active)
		logfile_off(log, NULL);

	malloc_strcpy(&log->filename, arg);

	if (log->active)
		logfile_on(log, NULL);
	return log;
}

static Logfile *	logfile_kill (Logfile *log, char **args)
{
	if (!log)
	{
		say("KILL: You need to specify a logfile first");
		return NULL;
	}

	delete_logfile(log);
	return NULL;
}

static Logfile *	logfile_level (Logfile *log, char **args)
{
        char *arg = new_next_arg(*args, args);
	char *rejects = NULL;

	if (!log)
	{
		say("LEVEL: You need to specify a logfile first");
		return NULL;
	}

	if (str_to_mask(&log->mask, arg, &rejects))
		standard_level_warning("/LOG LEVEL", &rejects);
	return log;
}

static Logfile *	logfile_list (Logfile *log, char **args)
{
	Logfile *l;
	char *targets = NULL;

	say("Logfiles:");
	for (l = logfiles; l; l = l->next)
	{
		targets = logfile_get_targets(l);
		say("Log %2d [%s] logging %s is %s, file %s server %s targets %s",
			l->refnum, l->name, logtype[l->type],
			onoff[l->active],
			l->filename ? l->filename : "<NONE>", 
			l->servref == NOSERV ?  "ALL" : ltoa(l->servref),
			targets ? targets : "<NONE>");
		new_free(&targets);
	}
	return log;
}

static Logfile *	logfile_mangle (Logfile *log, char **args)
{
	char *	arg = next_arg(*args, args);

	if (!log)
	{
		say("MANGLE: You need to specify a logfile first");
		return NULL;
	}

	if (!arg)
		say("MANGLE: This logfile mangles %s", log->mangle_desc);
	else
	{
		new_free(&log->mangle_desc);
		log->mangler = parse_mangle(arg, log->mangler, &log->mangle_desc);
		say("MANGLE: Now mangling %s", log->mangle_desc);
	}
	return log;
}

static Logfile *	logfile_name (Logfile *log, char **args)
{
        char *arg;

	if (!log)
	{
		say("NAME: You need to specify a logfile first");
		return NULL;
	}

        if (!(arg = next_arg(*args, args)))
                say("You must specify a name for the logfile!");
	else
        {
                /* /log name -  unsets the window name */
                if (!strcmp(arg, "-"))
                        new_free(&log->name);

                /* /log name to existing name -- ignore this. */
                else if (log->name && (my_stricmp(log->name, arg) == 0))
                        return log;

                else if (is_logfile_name_unique(arg))
                        malloc_strcpy(&log->name, arg);

                else
                        say("%s is not unique!", arg);
        }

        return log;
}

static Logfile *	logfile_new (Logfile *log, char **args)
{
	return new_logfile();
}

static Logfile *	logfile_off (Logfile *log, char **args)
{
	if (!log)
	{
		say("OFF: You need to specify a logfile first");
		return NULL;
	}

	if (!log->filename)
	{
		say("OFF: You need to specify a filename for this log first");
		return log;
	}

	time(&log->activity);
	do_log(0, log->filename, &log->log);
	log->active = 0;
	return log;
}

static Logfile *	logfile_on (Logfile *log, char **args)
{
	if (!log)
	{
		say("ON: You need to specify a logfile first");
		return NULL;
	}

	if (!log->filename)
	{
		say("ON: You need to specify a filename for this log first");
		return log;
	}

	time(&log->activity);
	if (do_log(1, log->filename, &log->log))
		log->active = 1;
	return log;
}

static Logfile *	logfile_refnum (Logfile *log, char **args)
{
	char *arg = next_arg(*args, args);

	log = get_log_by_desc(arg);
	return log;
}

static Logfile *	logfile_remove (Logfile *log, char **args)
{
	char 		*arg = next_arg(*args, args);
	char            *ptr;
	List *		new_nl;
	int		i;

	if (!log)
	{
		say("REMOVE: You need to specify a logfile first");
		return NULL;
	}

        if (!arg)
                say("Remove: Remove nicknames/channels logged to this file");

	else while (arg)
        {
	        if ((ptr = strchr(arg, ',')) != NULL)
			*ptr++ = 0;

		if (log->type == LOG_TARGETS)
		{
		    if ((new_nl = remove_from_list(&log->targets, arg)))
		    {
			say("Removed %s from log target list", new_nl->name);
			new_free(&new_nl->name);
			new_free((char **)&new_nl);
		    }
		    else
			say("%s is not on the list for this log!", arg);
		}
		else if (log->type == LOG_SERVERS || log->type == LOG_WINDOWS)
		{
		    int refnum = my_atol(ptr);

		    for (i = 0; i < MAX_TARGETS; i++)
		    {
			if (log->refnums[i] == refnum)
			{
				say("Removed %d to log refnum list", refnum);
				log->refnums[i] = -1;
				break;
			}
		    }
		    if (i >= MAX_TARGETS)
			say("%s is not on the refnum list for this log!", arg);
		}

		arg = ptr;
        }

        return log;
}

static Logfile *	logfile_rewrite (Logfile *log, char **args)
{
        char *arg = new_next_arg(*args, args);

	if (!log)
	{
		say("REWRITE: You need to specify a logfile first");
		return NULL;
	}

	malloc_strcpy(&log->rewrite, arg);
	return log;
}

static Logfile *	logfile_server (Logfile *log, char **args)
{
        char *arg = new_next_arg(*args, args);

	if (!log)
	{
		say("SERVER: You need to specify a logfile first");
		return NULL;
	}

	if (!my_strnicmp(arg, "ALL", 1))
		log->servref = NOSERV;
	else if (!is_number(arg))
		say("/LOG SERVER: The log's server needs to be a number or ALL");
	else
		log->servref = str_to_servref(arg);

	return log;
}

static Logfile *	logfile_type (Logfile *log, char **args)
{
        char *arg = new_next_arg(*args, args);

	if (!log)
	{
		say("TYPE: You need to specify a logfile first");
		return NULL;
	}

	clean_log_targets(log);
	if (!my_strnicmp(arg, "SERVER", 1))
		log->type = LOG_SERVERS;
	else if (!my_strnicmp(arg, "WINDOW", 1))
		log->type = LOG_WINDOWS;
	else if (!my_strnicmp(arg, "TARGET", 1))
		log->type = LOG_TARGETS;
	else
		say("TYPE: Unknown type of log");

	return log;
}

typedef struct logfile_ops_T {
	const char *	command;
	logfile_func	func;
} logfile_ops;

static const logfile_ops options [] = {
	{ "ACTIVITY",	logfile_activity	},
	{ "ADD",	logfile_add		},
	{ "DESCRIBE",	logfile_describe	},
	{ "FILENAME",	logfile_filename	},
	{ "KILL",	logfile_kill		},
	{ "LEVEL",	logfile_level		},
	{ "LIST",	logfile_list		},
	{ "MANGLE",	logfile_mangle		},
	{ "NAME",	logfile_name		},
	{ "NEW",	logfile_new		},
	{ "OFF",	logfile_off		},
	{ "ON",		logfile_on		},
	{ "REFNUM",	logfile_refnum		},
	{ "REMOVE",	logfile_remove		},
	{ "REWRITE",	logfile_rewrite		},
	{ "SERVER",	logfile_server		},
	{ "TYPE",	logfile_type		},
	{ NULL,		NULL			}
};

BUILT_IN_COMMAND(logcmd)
{
        char    *arg;
        int     nargs = 0;
        Logfile	*log = NULL;

        while ((arg = next_arg(args, &args)))
        {
                int i;
                int len = strlen(arg);

                if (*arg == '-' || *arg == '/')         /* Ignore - or / */
                        arg++, len--;

                for (i = 0; options[i].func ; i++)
                {
                        if (!my_strnicmp(arg, options[i].command, len))
                        {
                                log = options[i].func(log, &args);
                                nargs++;
                                break;
                        }
                }

                if (!options[i].func)
                {
			Logfile *s_log;
                        if ((s_log = get_log_by_desc(arg)))
                        {
                                nargs++;
				log = s_log;
                        }
                        else
                                yell("LOG: Invalid option: [%s]", arg);
                }
        }

        if (!nargs)
                logfile_list(NULL, NULL);
}

/****************************************************************************/
void	add_to_logs (long window, int servref, const char *target, int level, const char *orig_str)
{
	Logfile *log;
	int	i;

	for (log = logfiles; log; log = log->next)
	{
	    if (log->type == LOG_WINDOWS)
	    {
		List *		item;
		int		matched = 0;

		/* Look to see if any targets apply */
		for (item = log->targets; item; item = item->next)
		{
			/* 
			 * A log target may be a number, which must match
			 * either the internal refnum or user refnum of 
			 * the output window.
			 */
			if (is_number(item->name))
			{
				int	refnum = my_atol(item->name);

				if (refnum == get_window_refnum(window) ||
				    refnum == get_window_user_refnum(window))
					matched = 1;
			}

			/*
			 * Or a log target may be a string, which must match
			 * the name of the output window (which must have a
			 * name, natch), or the uuid of the output window.
			 */
			else
			{
				if (get_window_name(window) &&
				    !my_stricmp(item->name, get_window_name(window)))
					matched = 1;
				if (get_window_uuid(window) &&
				    !my_stricmp(item->name, get_window_uuid(window)))
					matched = 1;
			}
		}

		/* If none of the targets matched the window, punt */
		if (!matched)
			continue;

		/* Ensure this log is logging this output level */
		if (!mask_isset(&log->mask, level))
			continue;

		/* OK, We're good! */
		time(&log->activity);
		add_to_log(log->refnum, log->log, window, orig_str, log->mangler, log->rewrite);
	    }

	    if (log->type == LOG_SERVERS)
	    {
		for (i = 0; i < MAX_TARGETS; i++) {
		    if (log->refnums[i] == NOSERV ||
			log->refnums[i] == servref) 
		    {
			if (!mask_isset(&log->mask, level))
				continue;
			time(&log->activity);
			add_to_log(log->refnum, log->log, window, orig_str, log->mangler, log->rewrite);
		    }
		}
	    }

	    else if (log->type == LOG_TARGETS)
	    {
		if (log->servref != NOSERV && (log->servref != servref))
			continue;

		if (!mask_isset(&log->mask, level))
			continue;

		if (log->targets && !target)
			continue;

		/* 
		 * XXX This used to use USE_WILDCARD but that was undocumented
		 * and it's never clear that was intended
		 */
		if (target && !find_in_list(log->targets, target))
			continue;

		/* OK!  We want to log it now! */
		time(&log->activity);
		add_to_log(log->refnum, log->log, window, orig_str, log->mangler, log->rewrite);
	    }
	}
}

/*****************************************************************************/
/* Used by function_logctl */
/*
 * $logctl(NEW)
 * $logctl(REFNUMS [ACTIVE|INACTIVE|ALL])
 * $logctl(REFNUM log-desc)
 * $logctl(ADD log-desc [target])
 * $logctl(DELETE log-desc [target])
 * $logctl(GET <refnum> [LIST])
 * $logctl(SET <refnum> [ITEM] [VALUE])
 * $logctl(MATCH [pattern])
 * $logctl(PMATCH [pattern])
 *
 * [LIST] and [ITEM] are one of the following
 *	REFNUM		The refnum for the log (GET only)
 *	NAME		The logical name for the log
 *	FILENAME	The filename this log writes to
 *	SERVER		The server this log associates with (-1 for any)
 *	TARGETS		All of the targets for this log
 *	LEVEL		The Lastlog Level for this log
 *	REWRITE		The rewrite rule for this log
 *	MANGLE		The mangle rule for this log
 *	STATUS		1 if log is on, 0 if log is off.
 *	TYPE		Either "TARGET", "WINDOW", or "SERVER"
 */
char *logctl	(char *input)
{
	char	*refstr;
	char	*listc;
	int	val;
	Logfile	*log;

	GET_FUNC_ARG(listc, input);
	if (!my_strnicmp(listc, "NEW", 3)) {
		log = new_logfile();
		RETURN_INT(log->refnum);
	} else if (!my_strnicmp(listc, "LAST_CREATED", 12)) {
		RETURN_INT(last_logref);
	} else if (!my_strnicmp(listc, "REFNUMS", 7)) {
		char *	retval = NULL;
		int	active;

		GET_FUNC_ARG(refstr, input);
		if (!my_stricmp(refstr, "ACTIVE"))
			active = 1;
		else if (!my_stricmp(refstr, "INACTIVE"))
			active = 0;
		else if (!my_stricmp(refstr, "ALL"))
			active = -1;
		else
			RETURN_EMPTY;

		for (log = logfiles; log; log = log->next)
		{
			if (active != -1 && active != log->active)
				continue;
			malloc_strcat_word(&retval, space, ltoa(log->refnum), DWORD_NO);
		}
		RETURN_MSTR(retval);
        } else if (!my_strnicmp(listc, "REFNUM", 6)) {
		GET_FUNC_ARG(refstr, input);
		if (!(log = get_log_by_desc(refstr)))
			RETURN_EMPTY;
		RETURN_INT(log->refnum);
        } else if (!my_strnicmp(listc, "ADD", 2)) {
		GET_FUNC_ARG(refstr, input);
		if (!(log = get_log_by_desc(refstr)))
			RETURN_EMPTY;
		logfile_add(log, &input);
		RETURN_INT(1);
        } else if (!my_strnicmp(listc, "DELETE", 2)) {
		GET_FUNC_ARG(refstr, input);
		if (!(log = get_log_by_desc(refstr)))
			RETURN_EMPTY;
		logfile_remove(log, &input);
		RETURN_INT(1);
        } else if (!my_strnicmp(listc, "GET", 2)) {
                GET_FUNC_ARG(refstr, input);
		if (!(log = get_log_by_desc(refstr)))
			RETURN_EMPTY;

                GET_FUNC_ARG(listc, input);
                if (!my_strnicmp(listc, "REFNUM", 3)) {
			RETURN_INT(log->refnum);
                } else if (!my_strnicmp(listc, "NAME", 3)) {
			RETURN_STR(log->name);
                } else if (!my_strnicmp(listc, "FILENAME", 3)) {
			RETURN_STR(log->filename);
                } else if (!my_strnicmp(listc, "SERVER", 3)) {
			RETURN_INT(log->servref);
                } else if (!my_strnicmp(listc, "TARGETS", 3)) {
			char *ret = logfile_get_targets(log);
			RETURN_MSTR(ret);
                } else if (!my_strnicmp(listc, "LEVEL", 3)) {
			const char *ret = mask_to_str(&log->mask);
			RETURN_STR(ret);
                } else if (!my_strnicmp(listc, "REWRITE", 3)) {
			RETURN_STR(log->rewrite);
                } else if (!my_strnicmp(listc, "MANGLE", 3)) {
			RETURN_STR(log->mangle_desc);
                } else if (!my_strnicmp(listc, "STATUS", 3)) {
			RETURN_INT(log->active);
                } else if (!my_strnicmp(listc, "TYPE", 3)) {
			RETURN_STR(logtype[log->type]);
		} else if (!my_strnicmp(listc, "ACTIVITY", 1)) {
			RETURN_INT(log->activity);
		}
        } else if (!my_strnicmp(listc, "SET", 1)) {
                GET_FUNC_ARG(refstr, input);
		if (!(log = get_log_by_desc(refstr)))
			RETURN_EMPTY;

		GET_FUNC_ARG(listc, input);
                if (!my_strnicmp(listc, "NAME", 3)) {
			logfile_name(log, &input);
			RETURN_INT(1);
                } else if (!my_strnicmp(listc, "FILENAME", 3)) {
			logfile_filename(log, &input);
			RETURN_INT(1);
                } else if (!my_strnicmp(listc, "SERVER", 3)) {
			logfile_server(log, &input);
			RETURN_INT(1);
                } else if (!my_strnicmp(listc, "TARGETS", 3)) {
			clean_log_targets(log);
			logfile_add(log, &input);
			RETURN_INT(1);
                } else if (!my_strnicmp(listc, "LEVEL", 3)) {
			logfile_level(log, &input);
			RETURN_INT(1);
                } else if (!my_strnicmp(listc, "REWRITE", 3)) {
			logfile_rewrite(log, &input);
			RETURN_INT(1);
                } else if (!my_strnicmp(listc, "MANGLE", 3)) {
			logfile_mangle(log, &input);
			RETURN_INT(1);
                } else if (!my_strnicmp(listc, "STATUS", 3)) {
			GET_INT_ARG(val, input);
			if (val)
				logfile_on(log, &input);
			else
				logfile_off(log, &input);
			RETURN_INT(1);
                } else if (!my_strnicmp(listc, "TYPE", 3)) {
			logfile_type(log, &input);
			RETURN_INT(1);
                } else if (!my_strnicmp(listc, "ACTIVITY", 1)) {
			logfile_activity(log, &input);
			RETURN_INT(1);
		}
        } else if (!my_strnicmp(listc, "MATCH", 1)) {
                RETURN_EMPTY;           /* Not implemented for now. */
        } else if (!my_strnicmp(listc, "PMATCH", 1)) {
                RETURN_EMPTY;           /* Not implemented for now. */
        } else if (!my_strnicmp(listc, "CURRENT", 1)) {
		RETURN_INT(current_log_refnum);
        } else
                RETURN_EMPTY;

        RETURN_EMPTY;
}

/*
 * The /WINDOW NUMBER command actually swaps the refnums of two windows:
 * It's possible that 'newref' isn't in use, so that's ok.
 */
void    logfiles_swap_windows (int oldref, int newref)
{
#if 0
	Logfile *log;
	int	i;

	for (log = logfiles; log; log = log->next)
        {
		if (log->type != LOG_WINDOWS)
			continue;

		for (i = 0; i < MAX_TARGETS; i++)
		{
			if (log->refnums[i] == newref)
				log->refnums[i] = oldref;
			else if (log->refnums[i] == oldref)
				log->refnums[i] = newref;
		}
        }
#endif
}

void    logfiles_merge_windows (int oldref, int newref)
{
#if 0
	Logfile *log;
	int	i;

	for (log = logfiles; log; log = log->next)
        {
		if (log->type != LOG_WINDOWS)
			continue;

		for (i = 0; i < MAX_TARGETS; i++)
		{
			/* If it is a number, an internal refnum, switch to new internal refnum */
			if (log->refnums[i] == get_window_refnum(oldref))
				log->refnum[i] = get_window_refnum(newref);
			/* If it is a number, a user refnum, switch to new user refnum */
			else if (log->refnums[i] == get_window_user_refnum(oldref))
				log->refnum[i] = get_window_user_refnum(newref);
			/* If it is a string, switch to new string */
		}
        }
#endif
}