allow coexistance of N build and AC build.
[tomato.git] / release / src / router / rc / transmission.c
blob4ec5c236911fb226b1ee3aa9b1941c6017c8bc30
1 /*
2 * transmission.c
4 * Copyright (C) 2011 shibby
6 */
7 #include <stdlib.h>
8 #include <rc.h>
9 #include <shutils.h>
10 #include <utils.h>
11 #include <syslog.h>
12 #include <sys/stat.h>
14 void start_bittorrent(void)
16 FILE *fp;
17 char *pb;
18 char *pc;
19 char *pd;
20 char *pe;
21 char *ph;
22 char *pi;
23 char *pj;
24 char *pk;
25 char *pl;
26 char *pm;
27 char *pn;
28 char *po;
29 char *pp;
30 char *pr;
31 char *ps;
32 char *pt;
33 char *pu;
35 // make sure its really stop
36 stop_bittorrent();
38 //only if enable...
39 if( !nvram_match( "bt_enable", "1" ) ) return;
41 //collecting data
42 if (nvram_match( "bt_rpc_enable", "1") ) { pb = "true"; } else { pb = "false"; }
43 if (nvram_match( "bt_dl_enable", "1") ) { pc = "true"; } else { pc = "false"; }
44 if (nvram_match( "bt_ul_enable", "1") ) { pd = "true"; } else { pd = "false"; }
45 if (nvram_match( "bt_incomplete", "1") ) { pe = "true"; } else { pe = "false"; }
46 if (nvram_match( "bt_ratio_enable", "1") ) { ph = "true"; } else { ph = "false"; }
47 if (nvram_match( "bt_dht", "1") ) { pi = "true"; } else { pi = "false"; }
48 if (nvram_match( "bt_pex", "1") ) { pj = "true"; } else { pj = "false"; }
49 if (nvram_match( "bt_settings", "down_dir" ) ){ pk = nvram_safe_get( "bt_dir" ); }
50 else if (nvram_match( "bt_settings", "custom" ) ) { pk = nvram_safe_get( "bt_settings_custom" ); }
51 else { pk = nvram_safe_get( "bt_settings" ); }
52 if (nvram_match( "bt_auth", "1") ) { pl = "true"; } else { pl = "false"; }
53 if (nvram_match( "bt_blocklist", "1") ) { pm = "true"; } else { pm = "false"; }
54 if (nvram_match( "bt_binary", "internal" ) ) { pn = "/usr/bin"; }
55 else if (nvram_match( "bt_binary", "optware" ) ) { pn = "/opt/bin"; }
56 else { pn = nvram_safe_get( "bt_binary_custom" ); }
57 if (nvram_match( "bt_lpd", "1") ) { po = "true"; } else { po = "false"; }
58 if (nvram_match( "bt_utp", "1") ) { pp = "true"; } else { pp = "false"; }
59 if (nvram_match( "bt_ratio_idle_enable", "1") ) { pr = "true"; } else { pr = "false"; }
60 if (nvram_match( "bt_dl_queue_enable", "1") ) { pt = "true"; } else { pt = "false"; }
61 if (nvram_match( "bt_ul_queue_enable", "1") ) { pu = "true"; } else { pu = "false"; }
64 //writing data to file
65 if( !( fp = fopen( "/tmp/settings.json", "w" ) ) )
67 perror( "/tmp/settings.json" );
68 return;
71 fprintf( fp, "{\n" );
72 fprintf( fp, "\"peer-port\": %s, \n", nvram_safe_get( "bt_port" ) );
73 fprintf( fp, "\"speed-limit-down-enabled\": %s, \n", pc );
74 fprintf( fp, "\"speed-limit-up-enabled\": %s, \n", pd );
75 fprintf( fp, "\"speed-limit-down\": %s, \n", nvram_safe_get( "bt_dl" ) );
76 fprintf( fp, "\"speed-limit-up\": %s, \n", nvram_safe_get( "bt_ul" ) );
77 fprintf( fp, "\"rpc-enabled\": %s, \n", pb );
78 fprintf( fp, "\"rpc-bind-address\": \"0.0.0.0\", \n");
79 fprintf( fp, "\"rpc-port\": %s, \n", nvram_safe_get( "bt_port_gui" ) );
80 fprintf( fp, "\"rpc-whitelist-enabled\": false, \n");
81 fprintf( fp, "\"rpc-username\": \"%s\", \n", nvram_safe_get( "bt_login" ) );
82 fprintf( fp, "\"rpc-password\": \"%s\", \n", nvram_safe_get( "bt_password" ) );
83 fprintf( fp, "\"download-dir\": \"%s\", \n", nvram_safe_get( "bt_dir" ) );
84 fprintf( fp, "\"incomplete-dir-enabled\": \"%s\", \n", pe );
85 fprintf( fp, "\"incomplete-dir\": \"%s/.incomplete\", \n", nvram_safe_get( "bt_dir" ) );
86 fprintf( fp, "\"peer-limit-global\": %s, \n", nvram_safe_get( "bt_peer_limit_global" ) );
87 fprintf( fp, "\"peer-limit-per-torrent\": %s, \n", nvram_safe_get( "bt_peer_limit_per_torrent" ) );
88 fprintf( fp, "\"upload-slots-per-torrent\": %s, \n", nvram_safe_get( "bt_ul_slot_per_torrent" ) );
89 fprintf( fp, "\"dht-enabled\": %s, \n", pi );
90 fprintf( fp, "\"pex-enabled\": %s, \n", pj );
91 fprintf( fp, "\"lpd-enabled\": %s, \n", po );
92 fprintf( fp, "\"utp-enabled\": %s, \n", pp );
93 fprintf( fp, "\"ratio-limit-enabled\": %s, \n", ph );
94 fprintf( fp, "\"ratio-limit\": %s, \n", nvram_safe_get( "bt_ratio" ) );
95 fprintf( fp, "\"idle-seeding-limit-enabled\": %s, \n", pr );
96 fprintf( fp, "\"idle-seeding-limit\": %s, \n", nvram_safe_get( "bt_ratio_idle" ) );
97 fprintf( fp, "\"blocklist-enabled\": %s, \n", pm );
98 fprintf( fp, "\"blocklist-url\": \"%s\", \n", nvram_safe_get( "bt_blocklist_url" ) );
99 fprintf( fp, "\"download-queue-enabled\": %s, \n", pt );
100 fprintf( fp, "\"download-queue-size\": %s, \n", nvram_safe_get( "bt_dl_queue_size" ) );
101 fprintf( fp, "\"seed-queue-enabled\": %s, \n", pu );
102 fprintf( fp, "\"seed-queue-size\": %s, \n", nvram_safe_get( "bt_ul_queue_size" ) );
103 fprintf( fp, "\"message-level\": %s, \n", nvram_safe_get( "bt_message" ) );
104 fprintf( fp, "%s\n", nvram_safe_get("bt_custom"));
105 fprintf( fp, "\"rpc-authentication-required\": %s \n", pl );
106 fprintf( fp, "}\n");
108 fclose( fp );
109 chmod( "/tmp/settings.json", 0644 );
111 //start file
112 if( !( fp = fopen( "/tmp/start_transmission.sh", "w" ) ) )
114 perror( "/tmp/start_transmission.sh" );
115 return;
118 fprintf( fp, "#!/bin/sh\n" );
119 fprintf( fp, "sleep %s\n", nvram_safe_get( "bt_sleep" ) );
121 if ( nvram_match( "bt_incomplete", "1") )
123 fprintf( fp, "if [ ! -d \"%s/.incomplete\" ]; then\n", pk );
124 fprintf( fp, "mkdir %s/.incomplete\n", pk );
125 fprintf( fp, "fi\n");
128 fprintf( fp, "if [ ! -d \"%s/.settings\" ]; then\n", pk );
129 fprintf( fp, "mkdir %s/.settings\n", pk );
130 fprintf( fp, "fi\n");
131 fprintf( fp, "mv /tmp/settings.json %s/.settings\n", pk );
133 fprintf( fp, "rm %s/.settings/blocklists/*\n", pk );
135 if ( nvram_match( "bt_blocklist", "1") )
137 fprintf( fp, "wget %s -O %s/.settings/blocklists/level1.gz\n", nvram_safe_get( "bt_blocklist_url" ), pk );
138 fprintf( fp, "gunzip %s/.settings/blocklists/level1.gz\n", pk );
141 //crash fix?
142 fprintf( fp, "EVENT_NOEPOLL=1; export EVENT_NOEPOLL\n" );
145 fprintf( fp, "%s/transmission-daemon -g %s/.settings\n", pn, pk );
146 fprintf( fp, "logger \"Transmission daemon successfully started\" \n");
147 fprintf( fp, "sleep 2\n" );
150 fprintf( fp, "/usr/bin/btcheck addcru\n");
152 fclose( fp );
154 chmod( "/tmp/start_transmission.sh", 0755 );
156 xstart( "/tmp/start_transmission.sh" );
157 return;
161 void stop_bittorrent(void)
163 FILE *fp;
165 //stop file
166 if( !( fp = fopen( "/tmp/stop_transmission.sh", "w" ) ) )
168 perror( "/tmp/stop_transmission.sh" );
169 return;
172 fprintf( fp, "#!/bin/sh\n" );
173 fprintf( fp, "killall -KILL transmission-daemon\n");
174 fprintf( fp, "logger \"Transmission daemon successfully stoped\" \n");
175 fprintf( fp, "sleep 2\n");
176 fprintf( fp, "/usr/bin/btcheck addcru\n");
178 fclose( fp );
179 chmod( "/tmp/stop_transmission.sh", 0755 );
181 xstart( "/tmp/stop_transmission.sh" );
182 return;