File: keyfile.c

package info (click to toggle)
ctrlproxy 3.0.7-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 1,668 kB
  • ctags: 2,045
  • sloc: ansic: 15,358; sh: 2,805; xml: 1,078; python: 346; makefile: 302; perl: 18
file content (123 lines) | stat: -rw-r--r-- 2,867 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
/* 
	ctrlproxy: A modular IRC proxy
	(c) 2003-2008 Jelmer Vernooij <jelmer@nl.linux.org>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "ctrlproxy.h"
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "keyfile.h"
#include "irc.h"

gboolean keyfile_read_file(const char *filename, char commentchar, GList **nicks)
{
    GIOChannel *gio;
    char *ret;
    gsize nr, term;

    gio = g_io_channel_new_file(filename, "r", NULL);

    if (gio == NULL) {
        return FALSE;
    }

    while (G_IO_STATUS_NORMAL == g_io_channel_read_line(gio, &ret, &nr, &term, NULL))
    {
        char **parts; 
		struct keyfile_entry *e;

		if (ret[0] == commentchar) {
        	g_free(ret);
			continue;
		}

		ret[term] = '\0';

        parts = g_strsplit(ret, "\t", 3);
        g_free(ret);

		if (!parts[0] || !parts[1]) {
			g_strfreev(parts);
			continue;
		}
			
		e = g_new0(struct keyfile_entry, 1);
		e->nick = parts[0];
		e->pass = parts[1];
		if (!parts[2] || !strcmp(parts[2], "*")) {
			e->network = NULL;
			g_free(parts[2]);
		} else {
			e->network = parts[2];
		}
	
		*nicks = g_list_append(*nicks, e);   
        g_free(parts);
    }

	g_io_channel_shutdown(gio, TRUE, NULL);
	g_io_channel_unref(gio);

	return TRUE;
}

gboolean keyfile_write_file(GList *nicks, const char *header, 
							const char *filename)
{
	GList *gl;
	int fd;
	gboolean empty = TRUE;

	fd = open(filename, O_WRONLY | O_CREAT, 0600);

    if (fd == -1) {
		log_global(LOG_WARNING, "Unable to write nickserv file `%s': %s", filename, strerror(errno));
        return FALSE;
    }

	if (write(fd, header, strlen(header)) < 0) {
		log_global(LOG_WARNING, "error writing file header: %s", strerror(errno));
		close(fd);
		return FALSE;
	}

	for (gl = nicks; gl; gl = gl->next) {
		struct keyfile_entry *n = gl->data;
        char *line;

		empty = FALSE;
        
        line = g_strdup_printf("%s\t%s\t%s\n", n->nick, n->pass, n->network?n->network:"*");
		if (write(fd, line, strlen(line)) < 0) {
			log_global(LOG_WARNING, "error writing line `%s': %s", line, strerror(errno));
			g_free(line);
			close(fd);
			return FALSE;
		}

        g_free(line);
	}
    
    close(fd);

	return TRUE;
}