Skip to content

Commit 6a60a75

Browse files
author
Yorhel
committed
Add geoip wrappers in geoip.c + use run-time availability detection
This simplifies uit_userlist.c a bit, and the run-time detection makes it possible to hide the column when ncdc has been compiled with GeoIP support, but no databases are available. TODO: - Proper error handling/reporting when opening a DB file - An option to manually specify a database path to open
1 parent 4611514 commit 6a60a75

File tree

3 files changed

+29
-52
lines changed

3 files changed

+29
-52
lines changed

Makefile.am

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ ncdc_SOURCES=\
5353
src/fl_local.c\
5454
src/fl_save.c\
5555
src/fl_util.c\
56+
src/geoip.c\
5657
src/hub.c\
5758
src/listen.c\
5859
src/main.c\
@@ -62,6 +63,10 @@ ncdc_SOURCES=\
6263
src/strutil.c\
6364
src/tth.c\
6465
src/ui.c\
66+
src/ui_colors.c\
67+
src/ui_listing.c\
68+
src/ui_logwindow.c\
69+
src/ui_textinput.c\
6570
src/uit_conn.c\
6671
src/uit_dl.c\
6772
src/uit_fl.c\
@@ -70,10 +75,6 @@ ncdc_SOURCES=\
7075
src/uit_msg.c\
7176
src/uit_search.c\
7277
src/uit_userlist.c\
73-
src/ui_colors.c\
74-
src/ui_listing.c\
75-
src/ui_logwindow.c\
76-
src/ui_textinput.c\
7778
src/util.c\
7879
src/vars.c
7980

@@ -120,6 +121,7 @@ src/fl_load.$(OBJEXT): src/fl_load.h
120121
src/fl_local.$(OBJEXT): src/fl_local.h
121122
src/fl_save.$(OBJEXT): src/fl_save.h
122123
src/fl_util.$(OBJEXT): src/fl_util.h
124+
src/geoip.$(OBJEXT): src/geoip.h
123125
src/hub.$(OBJEXT): src/hub.h
124126
src/listen.$(OBJEXT): src/listen.h
125127
src/main.$(OBJEXT): src/main.h
@@ -129,6 +131,10 @@ src/search.$(OBJEXT): src/search.h
129131
src/strutil.$(OBJEXT): src/strutil.h
130132
src/tth.$(OBJEXT): src/tth.h
131133
src/ui.$(OBJEXT): src/ui.h
134+
src/ui_colors.$(OBJEXT): src/ui_colors.h
135+
src/ui_listing.$(OBJEXT): src/ui_listing.h
136+
src/ui_logwindow.$(OBJEXT): src/ui_logwindow.h
137+
src/ui_textinput.$(OBJEXT): src/ui_textinput.h
132138
src/uit_conn.$(OBJEXT): src/uit_conn.h
133139
src/uit_dl.$(OBJEXT): src/uit_dl.h
134140
src/uit_fl.$(OBJEXT): src/uit_fl.h
@@ -137,9 +143,5 @@ src/uit_main.$(OBJEXT): src/uit_main.h
137143
src/uit_msg.$(OBJEXT): src/uit_msg.h
138144
src/uit_search.$(OBJEXT): src/uit_search.h
139145
src/uit_userlist.$(OBJEXT): src/uit_userlist.h
140-
src/ui_colors.$(OBJEXT): src/ui_colors.h
141-
src/ui_listing.$(OBJEXT): src/ui_listing.h
142-
src/ui_logwindow.$(OBJEXT): src/ui_logwindow.h
143-
src/ui_textinput.$(OBJEXT): src/ui_textinput.h
144146
src/util.$(OBJEXT): src/util.h
145147
src/vars.$(OBJEXT): src/vars.h

src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ int main(int argc, char **argv) {
451451
listen_global_init();
452452
cc_global_init();
453453
dl_init_global();
454+
geoip_reinit();
454455
ui_cmdhist_init("history");
455456
ui_init(bracketed_paste);
456457

src/uit_userlist.c

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ typedef struct tab_t {
4343
gboolean hide_mail : 1;
4444
gboolean hide_conn : 1;
4545
gboolean hide_ip : 1;
46-
int cw_user, cw_share, cw_conn, cw_desc, cw_mail, cw_tag, cw_ip;
46+
int cw_user, cw_country, cw_share, cw_conn, cw_desc, cw_mail, cw_tag, cw_ip;
4747
} tab_t;
4848

4949

@@ -93,11 +93,6 @@ static const char *get_name(GSequenceIter *iter) {
9393
}
9494

9595

96-
#ifdef USE_GEOIP
97-
static GeoIP *geoip4 = NULL;
98-
static GeoIP *geoip6 = NULL;
99-
#endif
100-
10196

10297
ui_tab_t *uit_userlist_create(hub_t *hub) {
10398
tab_t *t = g_new0(tab_t, 1);
@@ -109,14 +104,6 @@ ui_tab_t *uit_userlist_create(hub_t *hub) {
109104
t->hide_mail = TRUE;
110105
t->hide_ip = TRUE;
111106

112-
#ifdef USE_GEOIP
113-
// init these when the first userlist tab is opened
114-
if(!geoip4) {
115-
geoip4 = GeoIP_open_type(GEOIP_COUNTRY_EDITION, GEOIP_MEMORY_CACHE);
116-
geoip6 = GeoIP_open_type(GEOIP_COUNTRY_EDITION_V6, GEOIP_MEMORY_CACHE);
117-
}
118-
#endif
119-
120107
GSequence *users = g_sequence_new(NULL);
121108
// populate the list
122109
// g_sequence_sort() uses insertion sort? in that case it is faster to insert
@@ -179,20 +166,11 @@ static void draw_row(ui_listing_t *list, GSequenceIter *iter, int row, void *dat
179166
if(user->hastls)
180167
mvaddch(row, 4, 't');
181168

182-
#ifdef USE_GEOIP
183-
const char *country = NULL;
184-
if(!ip4_isany(user->ip4))
185-
country = GeoIP_country_code_by_addr(geoip4, ip4_unpack(user->ip4));
186-
else if(!ip6_isany(user->ip6))
187-
country = GeoIP_country_code_by_addr_v6(geoip6, ip6_unpack(user->ip6));
188-
if(country)
189-
mvaddstr(row, 6, country);
190-
int j=9;
191-
#else
192-
int j=6;
193-
#endif
194-
195-
if (t->cw_user > 1)
169+
int j = 6;
170+
DRAW_COL(row, j, t->cw_country,
171+
!ip4_isany(user->ip4) ? geoip_country4(ip4_unpack(user->ip4)) :
172+
!ip6_isany(user->ip6) ? geoip_country6(ip6_unpack(user->ip6)) : "");
173+
if(t->cw_user > 1)
196174
ui_listing_draw_match(list, iter, row, j, str_offset_from_columns(user->name, t->cw_user-1));
197175
j += t->cw_user;
198176
DRAW_COL(row, j, t->cw_share, user->hasinfo ? str_formatsize(user->sharesize) : "");
@@ -224,11 +202,11 @@ static void draw_row(ui_listing_t *list, GSequenceIter *iter, int row, void *dat
224202
*/
225203
static void calc_widths(tab_t *t) {
226204
// available width
227-
#ifdef USE_GEOIP
228-
int w = wincols-9;
229-
#else
230205
int w = wincols-6;
231-
#endif
206+
207+
// Country code column (fixed size)
208+
t->cw_country = geoip_available ? 3 : 0;
209+
w -= t->cw_country;
232210

233211
// share has a fixed size
234212
t->cw_share = 12;
@@ -277,20 +255,16 @@ static void t_draw(ui_tab_t *tab) {
277255
// header
278256
attron(UIC(list_header));
279257
mvhline(1, 0, ' ', wincols);
280-
#ifdef USE_GEOIP
281-
mvaddstr(1, 2, "opt CC");
282-
int i = 9;
283-
#else
284258
mvaddstr(1, 2, "opt");
285259
int i = 6;
286-
#endif
287-
DRAW_COL(1, i, t->cw_user, "Username");
288-
DRAW_COL(1, i, t->cw_share, "Share");
289-
DRAW_COL(1, i, t->cw_desc, "Description");
290-
DRAW_COL(1, i, t->cw_tag, "Tag");
291-
DRAW_COL(1, i, t->cw_mail, "E-Mail");
292-
DRAW_COL(1, i, t->cw_conn, "Connection");
293-
DRAW_COL(1, i, t->cw_ip, "IP");
260+
DRAW_COL(1, i, t->cw_country, "CC");
261+
DRAW_COL(1, i, t->cw_user, "Username");
262+
DRAW_COL(1, i, t->cw_share, "Share");
263+
DRAW_COL(1, i, t->cw_desc, "Description");
264+
DRAW_COL(1, i, t->cw_tag, "Tag");
265+
DRAW_COL(1, i, t->cw_mail, "E-Mail");
266+
DRAW_COL(1, i, t->cw_conn, "Connection");
267+
DRAW_COL(1, i, t->cw_ip, "IP");
294268
attroff(UIC(list_header));
295269

296270
// rows

0 commit comments

Comments
 (0)