summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2014-10-07 15:27:16 +0200
committerYorhel <git@yorhel.nl>2014-10-07 15:27:16 +0200
commit6a60a75b3c6a40cf90f017e0e0983ca9caaa57e0 (patch)
tree0461cedca923bf96b72462c8fae8938e792b2ace
parent4611514dfc8b2033e8adea159f54c0d2793a2e6c (diff)
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
-rw-r--r--Makefile.am18
-rw-r--r--src/main.c1
-rw-r--r--src/uit_userlist.c62
3 files changed, 29 insertions, 52 deletions
diff --git a/Makefile.am b/Makefile.am
index c24e7d7..371be16 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -53,6 +53,7 @@ ncdc_SOURCES=\
src/fl_local.c\
src/fl_save.c\
src/fl_util.c\
+ src/geoip.c\
src/hub.c\
src/listen.c\
src/main.c\
@@ -62,6 +63,10 @@ ncdc_SOURCES=\
src/strutil.c\
src/tth.c\
src/ui.c\
+ src/ui_colors.c\
+ src/ui_listing.c\
+ src/ui_logwindow.c\
+ src/ui_textinput.c\
src/uit_conn.c\
src/uit_dl.c\
src/uit_fl.c\
@@ -70,10 +75,6 @@ ncdc_SOURCES=\
src/uit_msg.c\
src/uit_search.c\
src/uit_userlist.c\
- src/ui_colors.c\
- src/ui_listing.c\
- src/ui_logwindow.c\
- src/ui_textinput.c\
src/util.c\
src/vars.c
@@ -120,6 +121,7 @@ src/fl_load.$(OBJEXT): src/fl_load.h
src/fl_local.$(OBJEXT): src/fl_local.h
src/fl_save.$(OBJEXT): src/fl_save.h
src/fl_util.$(OBJEXT): src/fl_util.h
+src/geoip.$(OBJEXT): src/geoip.h
src/hub.$(OBJEXT): src/hub.h
src/listen.$(OBJEXT): src/listen.h
src/main.$(OBJEXT): src/main.h
@@ -129,6 +131,10 @@ src/search.$(OBJEXT): src/search.h
src/strutil.$(OBJEXT): src/strutil.h
src/tth.$(OBJEXT): src/tth.h
src/ui.$(OBJEXT): src/ui.h
+src/ui_colors.$(OBJEXT): src/ui_colors.h
+src/ui_listing.$(OBJEXT): src/ui_listing.h
+src/ui_logwindow.$(OBJEXT): src/ui_logwindow.h
+src/ui_textinput.$(OBJEXT): src/ui_textinput.h
src/uit_conn.$(OBJEXT): src/uit_conn.h
src/uit_dl.$(OBJEXT): src/uit_dl.h
src/uit_fl.$(OBJEXT): src/uit_fl.h
@@ -137,9 +143,5 @@ src/uit_main.$(OBJEXT): src/uit_main.h
src/uit_msg.$(OBJEXT): src/uit_msg.h
src/uit_search.$(OBJEXT): src/uit_search.h
src/uit_userlist.$(OBJEXT): src/uit_userlist.h
-src/ui_colors.$(OBJEXT): src/ui_colors.h
-src/ui_listing.$(OBJEXT): src/ui_listing.h
-src/ui_logwindow.$(OBJEXT): src/ui_logwindow.h
-src/ui_textinput.$(OBJEXT): src/ui_textinput.h
src/util.$(OBJEXT): src/util.h
src/vars.$(OBJEXT): src/vars.h
diff --git a/src/main.c b/src/main.c
index 31f7c83..7a5da82 100644
--- a/src/main.c
+++ b/src/main.c
@@ -451,6 +451,7 @@ int main(int argc, char **argv) {
listen_global_init();
cc_global_init();
dl_init_global();
+ geoip_reinit();
ui_cmdhist_init("history");
ui_init(bracketed_paste);
diff --git a/src/uit_userlist.c b/src/uit_userlist.c
index dd98d89..db5766f 100644
--- a/src/uit_userlist.c
+++ b/src/uit_userlist.c
@@ -43,7 +43,7 @@ typedef struct tab_t {
gboolean hide_mail : 1;
gboolean hide_conn : 1;
gboolean hide_ip : 1;
- int cw_user, cw_share, cw_conn, cw_desc, cw_mail, cw_tag, cw_ip;
+ int cw_user, cw_country, cw_share, cw_conn, cw_desc, cw_mail, cw_tag, cw_ip;
} tab_t;
@@ -93,11 +93,6 @@ static const char *get_name(GSequenceIter *iter) {
}
-#ifdef USE_GEOIP
-static GeoIP *geoip4 = NULL;
-static GeoIP *geoip6 = NULL;
-#endif
-
ui_tab_t *uit_userlist_create(hub_t *hub) {
tab_t *t = g_new0(tab_t, 1);
@@ -109,14 +104,6 @@ ui_tab_t *uit_userlist_create(hub_t *hub) {
t->hide_mail = TRUE;
t->hide_ip = TRUE;
-#ifdef USE_GEOIP
- // init these when the first userlist tab is opened
- if(!geoip4) {
- geoip4 = GeoIP_open_type(GEOIP_COUNTRY_EDITION, GEOIP_MEMORY_CACHE);
- geoip6 = GeoIP_open_type(GEOIP_COUNTRY_EDITION_V6, GEOIP_MEMORY_CACHE);
- }
-#endif
-
GSequence *users = g_sequence_new(NULL);
// populate the list
// 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
if(user->hastls)
mvaddch(row, 4, 't');
-#ifdef USE_GEOIP
- const char *country = NULL;
- if(!ip4_isany(user->ip4))
- country = GeoIP_country_code_by_addr(geoip4, ip4_unpack(user->ip4));
- else if(!ip6_isany(user->ip6))
- country = GeoIP_country_code_by_addr_v6(geoip6, ip6_unpack(user->ip6));
- if(country)
- mvaddstr(row, 6, country);
- int j=9;
-#else
- int j=6;
-#endif
-
- if (t->cw_user > 1)
+ int j = 6;
+ DRAW_COL(row, j, t->cw_country,
+ !ip4_isany(user->ip4) ? geoip_country4(ip4_unpack(user->ip4)) :
+ !ip6_isany(user->ip6) ? geoip_country6(ip6_unpack(user->ip6)) : "");
+ if(t->cw_user > 1)
ui_listing_draw_match(list, iter, row, j, str_offset_from_columns(user->name, t->cw_user-1));
j += t->cw_user;
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
*/
static void calc_widths(tab_t *t) {
// available width
-#ifdef USE_GEOIP
- int w = wincols-9;
-#else
int w = wincols-6;
-#endif
+
+ // Country code column (fixed size)
+ t->cw_country = geoip_available ? 3 : 0;
+ w -= t->cw_country;
// share has a fixed size
t->cw_share = 12;
@@ -277,20 +255,16 @@ static void t_draw(ui_tab_t *tab) {
// header
attron(UIC(list_header));
mvhline(1, 0, ' ', wincols);
-#ifdef USE_GEOIP
- mvaddstr(1, 2, "opt CC");
- int i = 9;
-#else
mvaddstr(1, 2, "opt");
int i = 6;
-#endif
- DRAW_COL(1, i, t->cw_user, "Username");
- DRAW_COL(1, i, t->cw_share, "Share");
- DRAW_COL(1, i, t->cw_desc, "Description");
- DRAW_COL(1, i, t->cw_tag, "Tag");
- DRAW_COL(1, i, t->cw_mail, "E-Mail");
- DRAW_COL(1, i, t->cw_conn, "Connection");
- DRAW_COL(1, i, t->cw_ip, "IP");
+ DRAW_COL(1, i, t->cw_country, "CC");
+ DRAW_COL(1, i, t->cw_user, "Username");
+ DRAW_COL(1, i, t->cw_share, "Share");
+ DRAW_COL(1, i, t->cw_desc, "Description");
+ DRAW_COL(1, i, t->cw_tag, "Tag");
+ DRAW_COL(1, i, t->cw_mail, "E-Mail");
+ DRAW_COL(1, i, t->cw_conn, "Connection");
+ DRAW_COL(1, i, t->cw_ip, "IP");
attroff(UIC(list_header));
// rows