make_identicons.pl 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use DBI;
  5. use DBD::SQLite;
  6. use JSON;
  7. use Getopt::Long;
  8. my $db; # = "homeserver.db";
  9. my $server = "http://localhost:8008";
  10. my $size = 320;
  11. GetOptions("db|d=s", \$db,
  12. "server|s=s", \$server,
  13. "width|w=i", \$size) or usage();
  14. usage() unless $db;
  15. my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","") || die $DBI::errstr;
  16. my $res = $dbh->selectall_arrayref("select token, name from access_tokens, users where access_tokens.user_id = users.id group by user_id") || die $DBI::errstr;
  17. foreach (@$res) {
  18. my ($token, $mxid) = ($_->[0], $_->[1]);
  19. my ($user_id) = ($mxid =~ m/@(.*):/);
  20. my ($url) = $dbh->selectrow_array("select avatar_url from profiles where user_id=?", undef, $user_id);
  21. if (!$url || $url =~ /#auto$/) {
  22. `curl -s -o tmp.png "$server/_matrix/media/v1/identicon?name=${mxid}&width=$size&height=$size"`;
  23. my $json = `curl -s -X POST -H "Content-Type: image/png" -T "tmp.png" $server/_matrix/media/v1/upload?access_token=$token`;
  24. my $content_uri = from_json($json)->{content_uri};
  25. `curl -X PUT -H "Content-Type: application/json" --data '{ "avatar_url": "${content_uri}#auto"}' $server/_matrix/client/api/v1/profile/${mxid}/avatar_url?access_token=$token`;
  26. }
  27. }
  28. sub usage {
  29. die "usage: ./make-identicons.pl\n\t-d database [e.g. homeserver.db]\n\t-s homeserver (default: http://localhost:8008)\n\t-w identicon size in pixels (default 320)";
  30. }