1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16 #define TOPIC_INTERNAL_ACCESS
17 #include "common/setup_before.h"
18 #include <stdio.h>
19 #ifdef HAVE_STDDEF_H
20 # include <stddef.h>
21 #else
22 # ifndef NULL
23 # define NULL ((void *)0)
24 # endif
25 #endif
26 #ifdef STDC_HEADERS
27 # include <stdlib.h>
28 #else
29 # ifdef HAVE_MALLOC_H
30 # include <malloc.h>
31 # endif
32 #endif
33 #ifdef HAVE_STRING_H
34 # include <string.h>
35 #else
36 # ifdef HAVE_STRINGS_H
37 # include <strings.h>
38 # endif
39 #endif
40 #include "compat/strrchr.h"
41 #include "compat/strdup.h"
42 #include "compat/strcasecmp.h"
43 #include "compat/pdir.h"
44 #include <errno.h>
45 #include "compat/strerror.h"
46 #ifdef TIME_WITH_SYS_TIME
47 # include <sys/time.h>
48 # include <time.h>
49 #else
50 # ifdef HAVE_SYS_TIME_H
51 # include <sys/time.h>
52 # else
53 # include <time.h>
54 # endif
55 #endif
56 #ifdef HAVE_SYS_TYPES_H
57 # include <sys/types.h>
58 #endif
59 #include "common/eventlog.h"
60 #include "common/list.h"
61 #include "common/field_sizes.h"
62 #include "common/xalloc.h"
63 #include "prefs.h"
64 #include "topic.h"
65 #include "common/setup_after.h"
66
67 static t_list * topiclist_head=NULL;
68
get_topic(char const * channel_name)69 t_topic * get_topic(char const * channel_name)
70 {
71 t_elem * curr;
72 t_topic * topic;
73
74 if (topiclist_head)
75 {
76 LIST_TRAVERSE(topiclist_head,curr)
77 {
78 if (!(topic = elem_get_data(curr)))
79 {
80 eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
81 continue;
82 }
83 if (strcasecmp(channel_name,topic->channel_name)==0) return topic;
84 }
85 }
86 return NULL;
87 }
88
channel_get_topic(char const * channel_name)89 char * channel_get_topic(char const * channel_name)
90 {
91 t_topic * topic;
92
93 if (!(topic = get_topic(channel_name)))
94 return NULL;
95 else
96 return topic->topic;
97 }
98
topiclist_save(char const * topic_file)99 int topiclist_save(char const * topic_file)
100 {
101 t_elem * curr;
102 t_topic * topic;
103 FILE * fp;
104
105 if (topiclist_head)
106 {
107
108 if ((fp = fopen(topic_file,"w"))==NULL)
109 {
110 eventlog(eventlog_level_error, __FUNCTION__,"can't open topic file");
111 return -1;
112 }
113
114 LIST_TRAVERSE(topiclist_head,curr)
115 {
116 if (!(topic = elem_get_data(curr)))
117 {
118 eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
119 continue;
120 }
121 if (topic->save == DO_SAVE_TOPIC)
122 fprintf(fp,"\"%s\",\"%s\"\n",topic->channel_name,topic->topic);
123 }
124
125 fclose(fp);
126 }
127
128 return 0;
129 }
130
topiclist_add_topic(char const * channel_name,char const * topic_text,int do_save)131 int topiclist_add_topic(char const * channel_name, char const * topic_text, int do_save)
132 {
133 t_topic * topic;
134
135 topic = xmalloc(sizeof(t_topic));
136 topic->channel_name = xstrdup(channel_name);
137 topic->topic = xstrdup(topic_text);
138 list_prepend_data(topiclist_head,topic);
139 topic->save = do_save;
140 return 0;
141 }
142
channel_set_topic(char const * channel_name,char const * topic_text,int do_save)143 int channel_set_topic(char const * channel_name, char const * topic_text, int do_save)
144 {
145 t_topic * topic;
146
147 if (!(channel_name))
148 {
149 eventlog(eventlog_level_error,__FUNCTION__,"got NULL channel_name");
150 return -1;
151 }
152
153 if (!(topic_text))
154 {
155 eventlog(eventlog_level_error,__FUNCTION__,"got NULL topic");
156 return -1;
157 }
158
159 if ((topic = get_topic(channel_name)))
160 {
161 xfree((void *)topic->topic);
162 topic->topic = xstrdup(topic_text);
163 }
164 else
165 {
166 topiclist_add_topic(channel_name, topic_text,do_save);
167 }
168
169 if (do_save == DO_SAVE_TOPIC)
170 {
171 if (topiclist_save(prefs_get_topicfile())<0)
172 {
173 eventlog(eventlog_level_error,__FUNCTION__,"error saving topic list");
174 return -1;
175 }
176 }
177
178 return 0;
179 }
180
topiclist_load(char const * topicfile)181 int topiclist_load(char const * topicfile)
182 {
183 FILE * fp;
184 char channel_name[CHANNEL_NAME_LEN];
185 char topic[MAX_TOPIC_LEN];
186
187 // make sure to unload previous topiclist before loading again
188 if (topiclist_head) topiclist_unload();
189
190 if ((fp = fopen(topicfile,"r"))==NULL)
191 {
192 eventlog(eventlog_level_error, __FUNCTION__,"can't open topic file");
193 return -1;
194 }
195
196 topiclist_head = list_create();
197
198 eventlog(eventlog_level_trace,__FUNCTION__,"start reading topic file");
199
200 while (fscanf(fp,"\"%[^\"]\",\"%[^\"]\"\n",channel_name,topic)==2)
201 {
202 topiclist_add_topic(channel_name,topic,DO_SAVE_TOPIC);
203 eventlog(eventlog_level_trace,__FUNCTION__,"channel: %s topic: \"%s\"",channel_name,topic);
204 }
205
206 eventlog(eventlog_level_trace,__FUNCTION__,"finished reading topic file");
207
208 fclose(fp);
209 return 0;
210 }
211
topiclist_unload(void)212 int topiclist_unload(void)
213 {
214 t_elem * curr;
215 t_topic * topic;
216
217 if (topiclist_head)
218 {
219 LIST_TRAVERSE(topiclist_head,curr)
220 {
221 if (!(topic = elem_get_data(curr)))
222 {
223 eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
224 continue;
225 }
226
227 if (topic->channel_name) xfree((void *)topic->channel_name);
228 if (topic->topic) xfree((void *)topic->topic);
229 xfree((void *)topic);
230 list_remove_elem(topiclist_head,&curr);
231 }
232 if (list_destroy(topiclist_head)<0)
233 return -1;
234
235 topiclist_head = NULL;
236 }
237 return 0;
238 }
239