1 | #include <stdarg.h> |
---|
2 | #include <stdlib.h> /* getenv() */ |
---|
3 | #include <unistd.h> /* write() */ |
---|
4 | #include <glib.h> |
---|
5 | #include <glib/gprintf.h> |
---|
6 | #include <glib/gi18n.h> |
---|
7 | |
---|
8 | #define CURL_DISABLE_TYPECHECK /* otherwise -Wunreachable-code goes insane */ |
---|
9 | #include <curl/curl.h> |
---|
10 | |
---|
11 | #include <libtransmission/transmission.h> |
---|
12 | #include <libtransmission/utils.h> |
---|
13 | #include <libtransmission/version.h> |
---|
14 | |
---|
15 | #include "blocklist.h" |
---|
16 | #include "tr-core.h" |
---|
17 | #include "tr-prefs.h" |
---|
18 | #include "util.h" |
---|
19 | |
---|
20 | #define BLOCKLIST_DATE "blocklist-date" |
---|
21 | |
---|
22 | /*** |
---|
23 | **** |
---|
24 | ***/ |
---|
25 | |
---|
26 | struct idle_data |
---|
27 | { |
---|
28 | TrCore * core; |
---|
29 | gboolean isDone; |
---|
30 | char * str; |
---|
31 | }; |
---|
32 | static gboolean |
---|
33 | emitProgressIdle( gpointer gdata ) |
---|
34 | { |
---|
35 | struct idle_data * data = gdata; |
---|
36 | |
---|
37 | tr_core_blocksig( data->core, data->isDone, data->str ); |
---|
38 | |
---|
39 | g_free( data->str ); |
---|
40 | g_free( data ); |
---|
41 | return FALSE; |
---|
42 | } |
---|
43 | |
---|
44 | static void |
---|
45 | emitProgress( TrCore * core, |
---|
46 | gboolean isDone, |
---|
47 | const char * fmt, |
---|
48 | ... ) |
---|
49 | { |
---|
50 | struct idle_data * data = tr_new0( struct idle_data, 1 ); |
---|
51 | va_list args; |
---|
52 | |
---|
53 | data->core = core; |
---|
54 | data->isDone = isDone; |
---|
55 | va_start( args, fmt ); |
---|
56 | g_vasprintf( &data->str, fmt, args ); |
---|
57 | va_end( args ); |
---|
58 | |
---|
59 | tr_inf( "%s", data->str ); |
---|
60 | g_idle_add( emitProgressIdle, data ); |
---|
61 | } |
---|
62 | |
---|
63 | /*** |
---|
64 | **** |
---|
65 | ***/ |
---|
66 | |
---|
67 | static size_t |
---|
68 | writeFunc( void * ptr, |
---|
69 | size_t size, |
---|
70 | size_t nmemb, |
---|
71 | void * fd ) |
---|
72 | { |
---|
73 | const size_t byteCount = size * nmemb; |
---|
74 | |
---|
75 | return write( *(int*)fd, ptr, byteCount ); |
---|
76 | } |
---|
77 | |
---|
78 | static gpointer |
---|
79 | blocklistThreadFunc( gpointer gcore ) |
---|
80 | { |
---|
81 | int fd; |
---|
82 | int rules; |
---|
83 | gboolean ok = TRUE; |
---|
84 | char * filename = NULL; |
---|
85 | char * filename2 = NULL; |
---|
86 | const char * url = "http://update.transmissionbt.com/level1.gz"; |
---|
87 | TrCore * core = TR_CORE( gcore ); |
---|
88 | |
---|
89 | emitProgress( core, FALSE, _( "Retrieving blocklist..." ) ); |
---|
90 | |
---|
91 | if( ok ) |
---|
92 | { |
---|
93 | GError * err = NULL; |
---|
94 | fd = g_file_open_tmp( "transmission-blockfile-XXXXXX", &filename, |
---|
95 | &err ); |
---|
96 | if( err ) |
---|
97 | { |
---|
98 | emitProgress( core, TRUE, _( |
---|
99 | "Unable to get blocklist: %s" ), err->message ); |
---|
100 | g_clear_error( &err ); |
---|
101 | ok = FALSE; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | if( ok ) |
---|
106 | { |
---|
107 | long verbose = getenv( "TR_CURL_VERBOSE" ) == NULL ? 0L : 1L; |
---|
108 | |
---|
109 | CURL * curl = curl_easy_init( ); |
---|
110 | curl_easy_setopt( curl, CURLOPT_URL, url ); |
---|
111 | curl_easy_setopt( curl, CURLOPT_ENCODING, "deflate" ); |
---|
112 | curl_easy_setopt( curl, CURLOPT_FOLLOWLOCATION, 1L ); |
---|
113 | curl_easy_setopt( curl, CURLOPT_USERAGENT, "Transmission/" LONG_VERSION_STRING ); |
---|
114 | curl_easy_setopt( curl, CURLOPT_VERBOSE, verbose ); |
---|
115 | curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, writeFunc ); |
---|
116 | curl_easy_setopt( curl, CURLOPT_WRITEDATA, &fd ); |
---|
117 | curl_easy_setopt( curl, CURLOPT_NOPROGRESS, 1 ); |
---|
118 | ok = !curl_easy_perform( curl ); |
---|
119 | curl_easy_cleanup( curl ); |
---|
120 | close( fd ); |
---|
121 | } |
---|
122 | |
---|
123 | if( !ok ) |
---|
124 | { |
---|
125 | emitProgress( core, TRUE, _( "Unable to get blocklist." ) ); |
---|
126 | } |
---|
127 | |
---|
128 | if( ok ) |
---|
129 | { |
---|
130 | char * cmd; |
---|
131 | emitProgress( core, FALSE, _( "Uncompressing blocklist..." ) ); |
---|
132 | filename2 = g_strdup_printf( "%s.txt", filename ); |
---|
133 | cmd = g_strdup_printf( "zcat %s > %s ", filename, filename2 ); |
---|
134 | tr_dbg( "%s", cmd ); |
---|
135 | (void) system( cmd ); |
---|
136 | g_free( cmd ); |
---|
137 | } |
---|
138 | |
---|
139 | if( ok ) |
---|
140 | { |
---|
141 | emitProgress( core, FALSE, _( "Parsing blocklist..." ) ); |
---|
142 | rules = tr_blocklistSetContent( tr_core_session( core ), filename2 ); |
---|
143 | } |
---|
144 | |
---|
145 | if( ok ) |
---|
146 | { |
---|
147 | emitProgress( core, TRUE, _( |
---|
148 | "Blocklist updated with %'d entries" ), rules ); |
---|
149 | pref_int_set( BLOCKLIST_DATE, time( NULL ) ); |
---|
150 | } |
---|
151 | |
---|
152 | g_free( filename2 ); |
---|
153 | g_free( filename ); |
---|
154 | return NULL; |
---|
155 | } |
---|
156 | |
---|
157 | /*** |
---|
158 | **** |
---|
159 | ***/ |
---|
160 | |
---|
161 | void |
---|
162 | gtr_blocklist_update( TrCore * core ) |
---|
163 | { |
---|
164 | g_thread_create( blocklistThreadFunc, core, TRUE, NULL ); |
---|
165 | } |
---|
166 | |
---|
167 | void |
---|
168 | gtr_blocklist_maybe_autoupdate( TrCore * core ) |
---|
169 | { |
---|
170 | if( pref_flag_get( PREF_KEY_BLOCKLIST_UPDATES_ENABLED ) |
---|
171 | && ( time( NULL ) - pref_int_get( BLOCKLIST_DATE ) > |
---|
172 | ( 60 * 60 * 24 * 7 ) ) ) |
---|
173 | gtr_blocklist_update( core ); |
---|
174 | } |
---|
175 | |
---|