Debian Codegraph @

modules/contrib/src/graphtastical.c

raw
1 /*
2 * Copyright (c) 2007 William Pitcock
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * Creates a .dot file for use with neato which displays
6 * user->channel relationships.
7 *
8 * == How to generate the graphs and How It Works ==
9 * Graphtastical creates a .dot file for graphviz's neato
10 * filter to use. The DOT language describes a graph's
11 * structure to graphviz in an opaque way.
12 *
13 * Because Graphviz nodes use unique identifiers for
14 * interconnection, the channels.dot file contains also
15 * information about social networks.
16 *
17 * Eventually Graphtastical will dump other graph datafiles
18 * too.
19 *
20 * To make a file from the data dumped by Graphtastical,
21 * the following commands will do:
22 *
23 * $ cat channels.dot | neato -Tgif -o map-channels.gif
24 * $ cat channels.dot | neato -Tsvg -o map-channels.svg
25 *
26 * Some maps (for larger networks) are going to be large,
27 * so you may want to provide links to both the GIF and
28 * SVG files as some people may only be able to make use of
29 * one or the other. Why that is, I'm not sure, and I'm not
30 * covering it here.
31 *
32 * == Privacy concerns ==
33 * If you are running Graphtastical on a network that has
34 * privacy concerns; you probably shouldn't.
35 */
36
37 #include "atheme-compat.h"
38
39 static mowgli_eventloop_timer_t *channels_timer = NULL;
40 static mowgli_eventloop_timer_t *uchannels_timer = NULL;
41
42 /* write channels.dot */
43 static void
44 write_channels_dot_file(void *arg)
45 {
46 mychan_t *mc;
47 chanacs_t *ca;
48 mowgli_node_t *tn;
49 FILE *f;
50 int errno1, was_errored = 0;
51 mowgli_patricia_iteration_state_t state;
52 int root = 1;
53 mychan_t *pmc;
54
55 errno = 0;
56
57 /* write to a temporary file first */
58 if (!(f = fopen(DATADIR "/channels.dot.new", "w")))
59 {
60 errno1 = errno;
61 slog(LG_ERROR, "graphtastical: cannot create channels.dot.new: %s", strerror(errno1));
62 return;
63 }
64
65 fprintf(f, "graph channels {\n");
66 fprintf(f, "edge [color=blue len=7.5 fontname=\"Verdana\" fontsize=8]\n");
67 fprintf(f, "node [fontname=\"Verdana\" fontsize=8]\n");
68
69 slog(LG_DEBUG, "graphtastical: dumping mychans");
70
71 MOWGLI_PATRICIA_FOREACH(mc, &state, mclist)
72 {
73 fprintf(f, "\"%s\"", mc->name);
74
75 if (!root)
76 fprintf(f, "-- \"%s\"", pmc->name);
77
78 pmc = mc;
79
80 fprintf(f, "[fontname=\"Verdana\" fontsize=8]\n");
81
82 MOWGLI_ITER_FOREACH(tn, mc->chanacs.head)
83 {
84 ca = (chanacs_t *)tn->data;
85
86 if (ca->level & CA_AKICK)
87 continue;
88
89 fprintf(f, "\"%s\" -- \"%s\" [fontname=\"Verdana\" fontsize=8]\n", ca->entity ? ca->entity->name : ca->host, mc->name);
90 }
91 }
92
93 fprintf(f, "}\n");
94
95 was_errored = ferror(f);
96 was_errored |= fclose(f);
97 if (was_errored)
98 {
99 errno1 = errno;
100 slog(LG_ERROR, "graphtastical: cannot write to channels.dot.new: %s", strerror(errno1));
101 return;
102 }
103
104 /* now, replace the old database with the new one, using an atomic rename */
105 if ((srename(DATADIR "/channels.dot.new", DATADIR "/channels.dot")) < 0)
106 {
107 errno1 = errno;
108 slog(LG_ERROR, "graphtastical: cannot rename channels.dot.new to channels.dot: %s", strerror(errno1));
109 return;
110 }
111 }
112
113 /* write uchannels.dot */
114 static void
115 write_uchannels_dot_file(void *arg)
116 {
117 channel_t *c;
118 chanuser_t *cu;
119 mowgli_node_t *tn;
120 FILE *f;
121 int errno1, was_errored = 0;
122 mowgli_patricia_iteration_state_t state;
123
124 errno = 0;
125
126 /* write to a temporary file first */
127 if (!(f = fopen(DATADIR "/uchannels.dot.new", "w")))
128 {
129 errno1 = errno;
130 slog(LG_ERROR, "graphtastical: cannot create channels.dot.new: %s", strerror(errno1));
131 return;
132 }
133
134 fprintf(f, "graph uchannels {\n");
135 fprintf(f, "edge [color=blue len=7.5 fontname=\"Verdana\" fontsize=8]\n");
136 fprintf(f, "node [fontname=\"Verdana\" fontsize=8]\n");
137
138 slog(LG_DEBUG, "graphtastical: dumping chans");
139
140 MOWGLI_PATRICIA_FOREACH(c, &state, chanlist)
141 {
142 fprintf(f, "\"%s\"", c->name);
143
144 fprintf(f, "[fontname=\"Verdana\" fontsize=8]\n");
145
146 MOWGLI_ITER_FOREACH(tn, c->members.head)
147 {
148 cu = (chanuser_t *)tn->data;
149
150 fprintf(f, "\"%s\" -- \"%s\" [fontname=\"Verdana\" fontsize=8]\n", cu->user->nick, c->name);
151 }
152 }
153
154 fprintf(f, "}\n");
155
156 was_errored = ferror(f);
157 was_errored |= fclose(f);
158 if (was_errored)
159 {
160 errno1 = errno;
161 slog(LG_ERROR, "graphtastical: cannot write to uchannels.dot.new: %s", strerror(errno1));
162 return;
163 }
164
165 /* now, replace the old database with the new one, using an atomic rename */
166 if ((srename(DATADIR "/uchannels.dot.new", DATADIR "/uchannels.dot")) < 0)
167 {
168 errno1 = errno;
169 slog(LG_ERROR, "graphtastical: cannot rename uchannels.dot.new to uchannels.dot: %s", strerror(errno1));
170 return;
171 }
172 }
173
174 static void
175 mod_init(module_t *const restrict m)
176 {
177 write_channels_dot_file(NULL);
178 write_uchannels_dot_file(NULL);
179
180 channels_timer = mowgli_timer_add(base_eventloop, "write_channels_dot_file", write_channels_dot_file, NULL, 60);
181 uchannels_timer = mowgli_timer_add(base_eventloop, "write_uchannels_dot_file", write_uchannels_dot_file, NULL, 60);
182 }
183
184 static void
185 mod_deinit(const module_unload_intent_t intent)
186 {
187 mowgli_timer_destroy(base_eventloop, channels_timer);
188 mowgli_timer_destroy(base_eventloop, uchannels_timer);
189 }
190
191 SIMPLE_DECLARE_MODULE_V1("contrib/graphtastical", MODULE_UNLOAD_CAPABILITY_NEVER)