| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | #------------------------------- | ||
| 4 | # flickr_make_stored_config.pl | ||
| 5 | #_______________________________ | ||
| 6 | |||
| 7 | use warnings; | ||
| 8 | use strict; | ||
| 9 | use Data::Dumper; | ||
| 10 | use Term::ReadLine; | ||
| 11 | use Storable qw( retrieve_fd store_fd ); | ||
| 12 | use Pod::Usage; | ||
| 13 | use Getopt::Long; | ||
| 14 | |||
| 15 | my $config = {}; | ||
| 16 | my $inconfig = {}; | ||
| 17 | my $cli_args = {}; | ||
| 18 | my $heads_up = 0; | ||
| 19 | |||
| 20 | GetOptions ( | ||
| 21 | $cli_args, | ||
| 22 | 'config_in=s', | ||
| 23 | 'config_out=s', | ||
| 24 | 'api_type=s', | ||
| 25 | 'frob=s', | ||
| 26 | 'api_key=s', | ||
| 27 | 'api_secret=s', | ||
| 28 | 'key=s', | ||
| 29 | 'secret=s', | ||
| 30 | 'token=s', | ||
| 31 | 'consumer_key=s', | ||
| 32 | 'consumer_secret=s', | ||
| 33 | 'callback=s', | ||
| 34 | 'token=s', | ||
| 35 | 'token_secret=s', | ||
| 36 | 'help', | ||
| 37 | 'man', | ||
| 38 | 'usage' | ||
| 39 | ); | ||
| 40 | |||
| 41 | |||
| 42 | #------------------------------------------------------------- | ||
| 43 | # Respond to help-type arguments or if missing required params | ||
| 44 | #_____________________________________________________________ | ||
| 45 | |||
| 46 | if ($cli_args->{'help'} or $cli_args->{'usage'} or $cli_args->{'man'} or !$cli_args->{'config_out'}) { | ||
| 47 | |||
| 48 | pod2usage({ -verbose => 2 }); | ||
| 49 | |||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | #------------------------------------------------------------------ | ||
| 54 | # If an incoming config is specified and exists, read it if we can. | ||
| 55 | #__________________________________________________________________ | ||
| 56 | |||
| 57 | if (defined($cli_args->{'config_in'}) and -e $cli_args->{'config_in'}) { | ||
| 58 | |||
| 59 | open my $CONFIG_IN, '<', $cli_args->{'config_in'} or die "\nCannot open $cli_args->{'config_in'} for read: $!\n"; | ||
| 60 | $inconfig = retrieve_fd($CONFIG_IN); | ||
| 61 | close($CONFIG_IN) or die "\nClose error $!\n"; | ||
| 62 | |||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | #--------------------------------- | ||
| 67 | # Create a term incase we need it. | ||
| 68 | #_________________________________ | ||
| 69 | |||
| 70 | my $term = Term::ReadLine->new('Flickr Configurer'); | ||
| 71 | $term->ornaments(0); | ||
| 72 | |||
| 73 | my $which_rl = $term->ReadLine; | ||
| 74 | |||
| 75 | if ($which_rl eq "Term::ReadLine::Perl" or $which_rl eq "Term::ReadLine::Perl5") { | ||
| 76 | |||
| 77 | warn "\n\nTerm::ReadLine::Perl and Term::ReadLine::Perl5 may display prompts" . | ||
| 78 | "\nincorrectly. If this is the case for you, try adding \"PERL_RL=Stub\"" . | ||
| 79 | "\nto the environment variables passed in with make test\n\n"; | ||
| 80 | |||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | #------------------ | ||
| 85 | # Flickr or OAuth ? | ||
| 86 | #__________________ | ||
| 87 | |||
| 88 | if (defined($cli_args->{'api_type'}) and $cli_args->{'api_type'} =~ m/^f.*/i ) { | ||
| 89 | |||
| 90 | $cli_args->{'api_type'} = 'flickr'; | ||
| 91 | |||
| 92 | } | ||
| 93 | else { | ||
| 94 | |||
| 95 | $cli_args->{'api_type'} = 'oauth'; | ||
| 96 | |||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | #-------------------------------------------------------------------- | ||
| 102 | # build config in layers. 1st undef, then config_in and finally args. | ||
| 103 | # Prompt if missing key/secret | ||
| 104 | # save key and secret as api_key and api_secret, moving api away from | ||
| 105 | # un-specified key type | ||
| 106 | #____________________________________________________________________ | ||
| 107 | |||
| 108 | if ( $cli_args->{'api_type'} eq 'flickr' ) { | ||
| 109 | |||
| 110 | $config->{'api_key'} = undef; | ||
| 111 | $config->{'api_secret'} = undef; | ||
| 112 | $config->{'frob'} = undef; | ||
| 113 | $config->{'callback'} = undef; | ||
| 114 | $config->{'token'} = undef; | ||
| 115 | |||
| 116 | if (defined($inconfig->{'key'})) { $config->{'api_key'} = $inconfig->{'key'}; $heads_up++; } | ||
| 117 | if (defined($inconfig->{'secret'})) { $config->{'api_secret'} = $inconfig->{'secret'}; $heads_up++; } | ||
| 118 | if (defined($inconfig->{'api_key'})) { $config->{'api_key'} = $inconfig->{'api_key'}; } | ||
| 119 | if (defined($inconfig->{'api_secret'})) { $config->{'api_secret'} = $inconfig->{'api_secret'}; } | ||
| 120 | if (defined($inconfig->{'frob'})) { $config->{'frob'} = $inconfig->{'frob'}; } | ||
| 121 | if (defined($inconfig->{'callback'})) { $config->{'callback'} = $inconfig->{'callback'}; } | ||
| 122 | if (defined($inconfig->{'token'})) { $config->{'token'} = $inconfig->{'token'}; } | ||
| 123 | |||
| 124 | if (defined($cli_args->{'key'})) { $config->{'api_key'} = $cli_args->{'key'}; $heads_up++;} | ||
| 125 | if (defined($cli_args->{'secret'})) { $config->{'api_secret'} = $cli_args->{'secret'}; $heads_up++;} | ||
| 126 | if (defined($cli_args->{'api_key'})) { $config->{'api_key'} = $cli_args->{'api_key'}; } | ||
| 127 | if (defined($cli_args->{'api_secret'})) { $config->{'api_secret'} = $cli_args->{'api_secret'}; } | ||
| 128 | if (defined($cli_args->{'frob'})) { $config->{'frob'} = $cli_args->{'frob'}; } | ||
| 129 | if (defined($cli_args->{'callback'})) { $config->{'callback'} = $cli_args->{'callback'}; } | ||
| 130 | if (defined($cli_args->{'token'})) { $config->{'token'} = $cli_args->{'token'}; } | ||
| 131 | |||
| 132 | if ($heads_up > 0) { warn "\n\nNote: key and secret are changing to api_key and api_secret as part of the\nmove to OAuth to help make it more evident that the Flickr authentication is being used.\n\n"; } | ||
| 133 | |||
| 134 | unless (defined($config->{'api_key'})) { | ||
| 135 | |||
| 136 | $config->{'api_key'} = get_key($cli_args->{'api_type'}); | ||
| 137 | } | ||
| 138 | |||
| 139 | unless (defined($config->{'api_secret'})) { | ||
| 140 | |||
| 141 | $config->{'api_secret'} = get_secret($cli_args->{'api_type'}); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | else { | ||
| 147 | |||
| 148 | $config->{'consumer_key'} = undef; | ||
| 149 | $config->{'consumer_secret'} = undef; | ||
| 150 | $config->{'callback'} = undef; | ||
| 151 | $config->{'token_secret'} = undef; | ||
| 152 | $config->{'token'} = undef; | ||
| 153 | |||
| 154 | if (defined($inconfig->{'consumer_key'})) { $config->{'consumer_key'} = $inconfig->{'consumer_key'}; } | ||
| 155 | if (defined($inconfig->{'consumer_secret'})) { $config->{'consumer_secret'} = $inconfig->{'consumer_secret'}; } | ||
| 156 | if (defined($inconfig->{'callback'})) { $config->{'callback'} = $inconfig->{'callback'}; } | ||
| 157 | if (defined($inconfig->{'token_secret'})) { | ||
| 158 | $config->{'token_secret'} = $inconfig->{'token_secret'}; | ||
| 159 | } | ||
| 160 | if (defined($inconfig->{'token'})) { $config->{'token'} = $inconfig->{'token'}; } | ||
| 161 | |||
| 162 | |||
| 163 | if (defined($cli_args->{'consumer_key'})) { $config->{'consumer_key'} = $cli_args->{'consumer_key'}; } | ||
| 164 | if (defined($cli_args->{'consumer_secret'})) { $config->{'consumer_secret'} = $cli_args->{'consumer_secret'}; } | ||
| 165 | if (defined($cli_args->{'callback'})) { $config->{'callback'} = $cli_args->{'callback'}; } | ||
| 166 | if (defined($cli_args->{'token_secret'})) { | ||
| 167 | $config->{'token_secret'} = $cli_args->{'token_secret'}; | ||
| 168 | } | ||
| 169 | if (defined($cli_args->{'token'})) { $config->{'token'} = $cli_args->{'token'}; } | ||
| 170 | |||
| 171 | |||
| 172 | unless (defined($config->{'consumer_key'})) { | ||
| 173 | |||
| 174 | $config->{'consumer_key'} = get_key($cli_args->{'api_type'}); | ||
| 175 | } | ||
| 176 | |||
| 177 | unless (defined($config->{'consumer_secret'})) { | ||
| 178 | |||
| 179 | $config->{'consumer_secret'} = get_secret($cli_args->{'api_type'}); | ||
| 180 | } | ||
| 181 | |||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | #------------------------------- | ||
| 186 | # Display config and store same. | ||
| 187 | #_______________________________ | ||
| 188 | |||
| 189 | print "\n\nSaving\n\n",Dumper($config),"\nin ",$cli_args->{'config_out'}," using Storable\n\n"; | ||
| 190 | |||
| 191 | open my $CONFIG_OUT, '>', $cli_args->{'config_out'} or die "\nCannot open $cli_args->{'config_out'} for write: $!\n"; | ||
| 192 | store_fd $config, $CONFIG_OUT; | ||
| 193 | close($CONFIG_OUT) or die "\nClose error $!\n"; | ||
| 194 | |||
| 195 | |||
| 196 | exit; | ||
| 197 | |||
| 198 | |||
| 199 | #-------------- | ||
| 200 | # Subroutiones | ||
| 201 | #______________ | ||
| 202 | |||
| 203 | sub get_key { | ||
| 204 | |||
| 205 | my $authtype = shift; | ||
| 206 | |||
| 207 | my $loop = 0; | ||
| 208 | my $getkey; | ||
| 209 | |||
| 210 | while ($loop == 0) { | ||
| 211 | |||
| 212 | my $keyprompt = 'OAuth consumer key for Flickr'; | ||
| 213 | |||
| 214 | if ($authtype eq 'flickr') { | ||
| 215 | |||
| 216 | $keyprompt = 'Flickr api key'; | ||
| 217 | |||
| 218 | } | ||
| 219 | |||
| 220 | print "\n"; | ||
| 221 | $getkey = $term->readline("Enter your " . $keyprompt .": "); | ||
| 222 | |||
| 223 | if ($getkey =~ m/^[0-9a-f]+$/i) { | ||
| 224 | |||
| 225 | print "\n$keyprompt: ",$getkey," accepted\n"; | ||
| 226 | $loop++ | ||
| 227 | |||
| 228 | } | ||
| 229 | else { | ||
| 230 | |||
| 231 | print "\n$keyprompt ",$getkey,"is not a hex number\n"; | ||
| 232 | |||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | return $getkey; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub get_secret { | ||
| 240 | |||
| 241 | my $authtype = shift; | ||
| 242 | |||
| 243 | my $loop = 0; | ||
| 244 | my $getsecret; | ||
| 245 | |||
| 246 | while ($loop == 0) { | ||
| 247 | |||
| 248 | my $secretprompt = 'OAuth consumer secret for Flickr'; | ||
| 249 | |||
| 250 | if ($authtype eq 'flickr') { | ||
| 251 | |||
| 252 | $secretprompt = 'Flickr api secret'; | ||
| 253 | |||
| 254 | } | ||
| 255 | |||
| 256 | print "\n"; | ||
| 257 | $getsecret = $term->readline("Enter your " . $secretprompt .": "); | ||
| 258 | |||
| 259 | if ($getsecret =~ m/^[0-9a-f]+$/i) { | ||
| 260 | |||
| 261 | print "\n$secretprompt: ",$getsecret," accepted\n"; | ||
| 262 | $loop++ | ||
| 263 | |||
| 264 | } | ||
| 265 | else { | ||
| 266 | |||
| 267 | print "\n$secretprompt ",$getsecret,"is not a hex number\n"; | ||
| 268 | |||
| 269 | } | ||
| 270 | } | ||
| 271 | |||
| 272 | return $getsecret; | ||
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | __END__ |