Changeset ab28a06


Ignore:
Timestamp:
May 8, 2010, 6:31:14 PM (14 years ago)
Author:
Nelson Elhage <nelhage@ksplice.com>
Branches:
master, release-1.10, release-1.7, release-1.8, release-1.9
Children:
05cfc78
Parents:
4225627
Message:
Don't complain about a missing config file.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/BarnOwl/Module/Twitter.pm

    r4225627 rab28a06  
    8080
    8181my $conffile = BarnOwl::get_config_dir() . "/twitter";
    82 open(my $fh, "<", "$conffile") || fail("Unable to read $conffile");
    83 my $raw_cfg = do {local $/; <$fh>};
    84 close($fh);
    85 eval {
    86     $raw_cfg = from_json($raw_cfg);
    87 };
    88 if($@) {
    89     fail("Unable to parse $conffile: $@");
    90 }
    91 
    92 $raw_cfg = [$raw_cfg] unless UNIVERSAL::isa $raw_cfg, "ARRAY";
    93 
    94 # Perform some sanity checking on the configuration.
    95 {
    96     my %nicks;
    97     my $default = 0;
     82
     83if (open(my $fh, "<", "$conffile")) {
     84    read_config($fh);
     85    close($fh);
     86}
     87
     88sub read_config {
     89    my $fh = shift;
     90
     91    my $raw_cfg = do {local $/; <$fh>};
     92    close($fh);
     93    eval {
     94        $raw_cfg = from_json($raw_cfg);
     95    };
     96    if($@) {
     97        fail("Unable to parse $conffile: $@");
     98    }
     99
     100    $raw_cfg = [$raw_cfg] unless UNIVERSAL::isa $raw_cfg, "ARRAY";
     101
     102    # Perform some sanity checking on the configuration.
     103  {
     104      my %nicks;
     105      my $default = 0;
     106
     107      for my $cfg (@$raw_cfg) {
     108          if(! exists $cfg->{user}) {
     109              fail("Account has no username set.");
     110          }
     111          my $user = $cfg->{user};
     112          if(! exists $cfg->{password}) {
     113              fail("Account $user has no password set.");
     114          }
     115          if(@$raw_cfg > 1&&
     116             !exists($cfg->{account_nickname}) ) {
     117              fail("Account $user has no account_nickname set.");
     118          }
     119          if($cfg->{account_nickname}) {
     120              if($nicks{$cfg->{account_nickname}}++) {
     121                  fail("Nickname " . $cfg->{account_nickname} . " specified more than once.");
     122              }
     123          }
     124          if($cfg->{default} || $cfg->{default_sender}) {
     125              if($default++) {
     126                  fail("Multiple accounts marked as 'default'.");
     127              }
     128          }
     129      }
     130  }
     131
     132    # If there is only a single account, make publish_tweets default to
     133    # true.
     134    if (scalar @$raw_cfg == 1 && !exists($raw_cfg->[0]{publish_tweets})) {
     135        $raw_cfg->[0]{publish_tweets} = 1;
     136    }
    98137
    99138    for my $cfg (@$raw_cfg) {
    100         if(! exists $cfg->{user}) {
    101             fail("Account has no username set.");
    102         }
    103         my $user = $cfg->{user};
    104         if(! exists $cfg->{password}) {
    105             fail("Account $user has no password set.");
    106         }
    107         if(@$raw_cfg > 1&&
    108            !exists($cfg->{account_nickname}) ) {
    109             fail("Account $user has no account_nickname set.");
    110         }
    111         if($cfg->{account_nickname}) {
    112             if($nicks{$cfg->{account_nickname}}++) {
    113                 fail("Nickname " . $cfg->{account_nickname} . " specified more than once.");
    114             }
    115         }
    116         if($cfg->{default} || $cfg->{default_sender}) {
    117             if($default++) {
    118                 fail("Multiple accounts marked as 'default'.");
    119             }
    120         }
    121     }
    122 }
    123 
    124 # If there is only a single account, make publish_tweets default to
    125 # true.
    126 if (scalar @$raw_cfg == 1 && !exists($raw_cfg->[0]{publish_tweets})) {
    127     $raw_cfg->[0]{publish_tweets} = 1;
    128 }
    129 
    130 for my $cfg (@$raw_cfg) {
    131     my $twitter_args = { username   => $cfg->{user},
    132                         password   => $cfg->{password},
    133                         source     => 'barnowl',
    134                     };
    135     if (defined $cfg->{service}) {
    136         my $service = $cfg->{service};
    137         $twitter_args->{apiurl} = $service;
    138         my $apihost = $service;
    139         $apihost =~ s/^\s*http:\/\///;
    140         $apihost =~ s/\/.*$//;
    141         $apihost .= ':80' unless $apihost =~ /:\d+$/;
    142         $twitter_args->{apihost} = $cfg->{apihost} || $apihost;
    143         my $apirealm = "Laconica API";
    144         $twitter_args->{apirealm} = $cfg->{apirealm} || $apirealm;
    145     } else {
    146         $cfg->{service} = 'http://twitter.com';
    147     }
    148 
    149     my $twitter_handle;
    150     eval {
    151          $twitter_handle = BarnOwl::Module::Twitter::Handle->new($cfg, %$twitter_args);
    152     };
    153     if ($@) {
    154         BarnOwl::error($@);
    155         next;
    156     }
    157     push @twitter_handles, $twitter_handle;
    158 }
    159 
    160 $default_handle = first {$_->{cfg}->{default}} @twitter_handles;
    161 if (!$default_handle && @twitter_handles) {
    162     $default_handle = $twitter_handles[0];
     139        my $twitter_args = { username   => $cfg->{user},
     140                             password   => $cfg->{password},
     141                             source     => 'barnowl',
     142                         };
     143        if (defined $cfg->{service}) {
     144            my $service = $cfg->{service};
     145            $twitter_args->{apiurl} = $service;
     146            my $apihost = $service;
     147            $apihost =~ s/^\s*http:\/\///;
     148            $apihost =~ s/\/.*$//;
     149            $apihost .= ':80' unless $apihost =~ /:\d+$/;
     150            $twitter_args->{apihost} = $cfg->{apihost} || $apihost;
     151            my $apirealm = "Laconica API";
     152            $twitter_args->{apirealm} = $cfg->{apirealm} || $apirealm;
     153        } else {
     154            $cfg->{service} = 'http://twitter.com';
     155        }
     156
     157        my $twitter_handle;
     158        eval {
     159            $twitter_handle = BarnOwl::Module::Twitter::Handle->new($cfg, %$twitter_args);
     160        };
     161        if ($@) {
     162            BarnOwl::error($@);
     163            next;
     164        }
     165        push @twitter_handles, $twitter_handle;
     166    }
     167
     168    $default_handle = first {$_->{cfg}->{default}} @twitter_handles;
     169    if (!$default_handle && @twitter_handles) {
     170        $default_handle = $twitter_handles[0];
     171    }
     172
    163173}
    164174
Note: See TracChangeset for help on using the changeset viewer.