PageRenderTime 67ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/DDG/Goodie/MacAddress.pm

http://github.com/duckduckgo/zeroclickinfo-goodies
Perl | 77 lines | 59 code | 12 blank | 6 comment | 5 complexity | a186bb29b71cce11333182605e10556e MD5 | raw file
Possible License(s): Apache-2.0
  1. package DDG::Goodie::MacAddress;
  2. # ABSTRACT: Vendor information lookup for MAC addresses
  3. use strict;
  4. use DDG::Goodie;
  5. zci answer_type => "mac_address";
  6. zci is_cached => 1;
  7. triggers startend => "mac address", "ethernet address";
  8. my %oui_db = map { chomp; my (@f) = split(/\\n/, $_, 2); ($f[0] => $f[1]); } share("oui_database.txt")->slurp;
  9. sub fmt_mac {
  10. my $mac = shift;
  11. $mac = lc($mac);
  12. $mac =~ s/..\K(?=.)/:/g;
  13. $mac;
  14. }
  15. sub build_infobox_element {
  16. my $query = shift;
  17. my @split = split ' ', $query;
  18. return {
  19. label => $query,
  20. url => 'https://duckduckgo.com/?q=' . (join '+', @split) . '&ia=answer',
  21. };
  22. }
  23. my $infobox = [ { heading => "Related Queries", },
  24. build_infobox_element('generate mac address'),
  25. build_infobox_element('random mac address'),
  26. ];
  27. handle remainder => sub {
  28. return unless $_;
  29. return unless $_ =~ m|^[-.:/ 0-9a-f]+$|i;
  30. $_ =~ s/[^0-9a-fA-F]//g;
  31. return unless (length($_) == 12 || length($_) == 16);
  32. my ($oui) = uc(substr($_, 0, 6));
  33. my ($info) = $oui_db{$oui};
  34. return unless $info;
  35. my ($name, $addr) = split(/\\n/, $info, 2);
  36. $addr = "No associated address" unless defined $addr;
  37. # If the info is all capitals, then try to add in some best guesses for
  38. # capitalization to make it more readable.
  39. #
  40. # Decide whether to do this replacement per-line, since there are often
  41. # errant unformatted lines amongst formatted ones.
  42. my (@lines) = split(/\\n/, $info);
  43. foreach my $line (@lines) {
  44. if ($line !~ m/[a-z]/) {
  45. $line =~ s/(\w+)/ucfirst(lc($1))/eg;
  46. }
  47. }
  48. my $owner = shift @lines;
  49. my $text_answer = "The OUI, ".fmt_mac($oui).", for this NIC is assigned to $name";
  50. return $text_answer, structured_answer => {
  51. data => {
  52. title => $owner,
  53. result => \@lines,
  54. input => fmt_mac($_),
  55. infoboxData => $infobox
  56. },
  57. templates => {
  58. options => {
  59. content => 'DDH.mac_address.content',
  60. },
  61. group => 'text'
  62. }
  63. };
  64. };
  65. 1;