Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 1238 lines (968 sloc) 27.034 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
/*
* Plugin mananger
* Copyright (C) 2010 Andreas Öman
* Copyright (C) 2012 Fábio Ferreira
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <assert.h>

#include "showtime.h"
#include "fileaccess/fileaccess.h"
#include "plugins.h"
#include "settings.h"
#include "htsmsg/htsmsg_json.h"
#include "backend/backend.h"
#include "misc/string.h"
#include "prop/prop_nodefilter.h"
#include "prop/prop_concat.h"
#include "notifications.h"
#include "upgrade.h"
#include "misc/strtab.h"

#if ENABLE_SPIDERMONKEY
#include "js/js.h"
#endif

typedef enum {
  PLUGIN_CAT_TV,
  PLUGIN_CAT_VIDEO,
  PLUGIN_CAT_MUSIC,
  PLUGIN_CAT_CLOUD,
  PLUGIN_CAT_GLWVIEW,
  PLUGIN_CAT_OTHER,
  PLUGIN_CAT_num,
} plugin_type_t;

static struct strtab catnames[] = {
  { "tv", PLUGIN_CAT_TV },
  { "video", PLUGIN_CAT_VIDEO },
  { "music", PLUGIN_CAT_MUSIC },
  { "cloud", PLUGIN_CAT_CLOUD },
  { "other", PLUGIN_CAT_OTHER },
  { "glwview", PLUGIN_CAT_GLWVIEW },
};



static const char *plugin_repo_url = "http://showtime.lonelycoder.com/plugins/plugins-v1.json";
static char *plugin_alt_repo_url;
static hts_mutex_t plugin_mutex;
static char *devplugin;

static prop_t *plugin_root_installed;
static prop_t *plugin_root_repo;

static prop_t *plugin_root_model;
static prop_courier_t *plugin_courier;



LIST_HEAD(plugin_list, plugin);

static struct plugin_list plugins;


typedef struct plugin {
  LIST_ENTRY(plugin) pl_link;
  char *pl_id;
  char *pl_package;
  char *pl_title;

  prop_t *pl_inst_prop;

  char *pl_inst_ver;
  char *pl_repo_ver;
  char *pl_showtime_min_version;

  int pl_loaded;
  int pl_installed;
  int pl_can_upgrade;

  prop_t *pl_status;
  prop_t *pl_statustxt;
  prop_t *pl_minver;

  prop_t *pl_canInstall;
  prop_t *pl_canUpgrade;
  prop_t *pl_cantUpgrade;
  prop_t *pl_canUninstall;
  prop_t *pl_prop_installed;


  int pl_new_version_avail;

  void (*pl_unload)(struct plugin *pl);

  prop_vec_t *pl_unload_props;
  
} plugin_t;

static int plugin_install(plugin_t *pl, const char *package);
static void plugin_remove(plugin_t *pl);
static void plugin_autoupgrade(void);

static int autoupgrade;

/**
*
*/
static const char *
get_repo(void)
{
  return plugin_alt_repo_url && *plugin_alt_repo_url ?
    plugin_alt_repo_url : plugin_repo_url;
}

/**
*
*/
static void
set_alt_repo_url(void *opaque, const char *value)
{
  hts_mutex_lock(&plugin_mutex);
  mystrset(&plugin_alt_repo_url, value);
  hts_mutex_unlock(&plugin_mutex);
}

/**
*
*/
static void
set_autoupgrade(void *opaque, int value)
{
  hts_mutex_lock(&plugin_mutex);
  autoupgrade = value;

  plugin_autoupgrade();

  hts_mutex_unlock(&plugin_mutex);
}

/**
*
*/
static plugin_t *
plugin_find(const char *id)
{
  plugin_t *pl;
  LIST_FOREACH(pl, &plugins, pl_link)
    if(!strcmp(pl->pl_id, id))
      return pl;
  
  pl = calloc(1, sizeof(plugin_t));
  pl->pl_id = strdup(id);

  pl->pl_status = prop_create_root(NULL);
  pl->pl_statustxt = prop_create(pl->pl_status, "statustxt");
  pl->pl_minver = prop_create(pl->pl_status, "minver");
  pl->pl_canInstall = prop_create(pl->pl_status, "canInstall");
  pl->pl_canUpgrade = prop_create(pl->pl_status, "canUpgrade");
  pl->pl_cantUpgrade = prop_create(pl->pl_status, "cantUpgrade");
  pl->pl_canUninstall = prop_create(pl->pl_status, "canUninstall");
  pl->pl_prop_installed = prop_create(pl->pl_status, "installed");

  LIST_INSERT_HEAD(&plugins, pl, pl_link);
  return pl;
}


/**
*
*/
static void
update_global_state(void)
{
  plugin_t *pl;
  int num_upgradable = 0;
  LIST_FOREACH(pl, &plugins, pl_link)
    if(pl->pl_new_version_avail)
      num_upgradable++;

  prop_setv(prop_get_global(), "plugins", "status", "upgradeable", NULL,
PROP_SET_INT, num_upgradable);
}


/**
*
*/
static void
update_state(plugin_t *pl)
{
  int canInstall = 0;
  int canUninstall = 0;
  int canUpgrade = 0;
  int cantUpgrade = 0;
  rstr_t *status = NULL;

  int version_dep_ok =
    pl->pl_showtime_min_version == NULL ||
    showtime_parse_version_int(pl->pl_showtime_min_version) <=
    showtime_get_version_int();

  prop_set_void(pl->pl_minver);
  pl->pl_new_version_avail = 0;

  if(pl->pl_installed == 0) {

    if(!version_dep_ok) {
      status = _("Not installable");
      prop_set_string(pl->pl_minver, pl->pl_showtime_min_version);

    } else {

      status = _("Not installed");
      canInstall = 1;
    }

  } else if(!strcmp(pl->pl_inst_ver ?: "", pl->pl_repo_ver ?: "")) {
    status = _("Up to date");
    canUninstall = 1;
  } else {
    canUninstall = 1;

    if(pl->pl_repo_ver != NULL) {
      pl->pl_new_version_avail = 1;

      if(!version_dep_ok) {
status = _("Not upgradable");
prop_set_string(pl->pl_minver, pl->pl_showtime_min_version);
cantUpgrade = 1;
      } else {
status = _("Upgradable");
canUpgrade = 1;
      }
    }
  }

  pl->pl_can_upgrade = canUpgrade;
  prop_set_int(pl->pl_canInstall, canInstall);
  prop_set_int(pl->pl_canUninstall, canUninstall);
  prop_set_int(pl->pl_canUpgrade, canUpgrade);
  prop_set_int(pl->pl_cantUpgrade, cantUpgrade);
  prop_set_int(pl->pl_prop_installed, pl->pl_installed);
  prop_set_rstring(pl->pl_statustxt, status);
  rstr_release(status);
}


/**
*
*/
static void
plugin_event(void *opaque, prop_event_t event, ...)
{
  plugin_t *pl = opaque;
  va_list ap;
  prop_sub_t *s;
  event_t *e;
  prop_t *p;

  va_start(ap, event);

  switch(event) {
  default:
    break;

  case PROP_DESTROYED:
    s = va_arg(ap, prop_sub_t *);
    prop_unsubscribe(s);
    break;

  case PROP_EXT_EVENT:
    e = va_arg(ap, event_t *);
    p = va_arg(ap, prop_t *);

    if(event_is_type(e, EVENT_DYNAMIC_ACTION)) {
      if(!strcmp(e->e_payload, "install")) {
rstr_t *package = prop_get_string(p, "package", NULL);
plugin_install(pl, rstr_get(package));
rstr_release(package);
      }
      else if(!strcmp(e->e_payload, "upgrade"))
plugin_install(pl, NULL);
      else if(!strcmp(e->e_payload, "uninstall"))
plugin_remove(pl);
    }
    break;
  }
}


/**
*
*/
static void
plugin_fill_prop0(struct htsmsg *pm, struct prop *p,
const char *basepath, int as_installed,
plugin_t *pl)
{
  const char *title = htsmsg_get_str(pm, "title") ?: pl->pl_id;
  const char *icon = htsmsg_get_str(pm, "icon");
  const char *cat = htsmsg_get_str(pm, "category");

  if(cat != NULL)
    cat = val2str(str2val(cat, catnames), catnames);

  prop_subscribe(PROP_SUB_TRACK_DESTROY | PROP_SUB_SINGLETON,
PROP_TAG_CALLBACK, plugin_event, pl,
PROP_TAG_ROOT, p,
PROP_TAG_MUTEX, &plugin_mutex,
NULL);

  prop_t *metadata = prop_create(p, "metadata");

  prop_link(pl->pl_status, prop_create(p, "status"));

  prop_set_string(prop_create(metadata, "title"), title);
  prop_set_string(prop_create(metadata, "category"), cat ?: "other");

  prop_set_string_ex(prop_create(metadata, "description"),
NULL,
htsmsg_get_str(pm, "description"),
PROP_STR_RICH);

  prop_set_string(prop_create(metadata, "synopsis"),
htsmsg_get_str(pm, "synopsis"));

  prop_set_string(prop_create(metadata, "author"),
htsmsg_get_str(pm, "author"));

  prop_set_string(prop_create(metadata, "version"),
htsmsg_get_str(pm, "version"));

  if(as_installed) {

    prop_set_string(prop_create(metadata, "installed_version"),
htsmsg_get_str(pm, "version"));

    prop_link(prop_create(prop_create(prop_create(plugin_root_repo, pl->pl_id),
"metadata"), "version"),
prop_create(metadata, "available_version"));


  } else {

    prop_set_string(prop_create(metadata, "available_version"),
htsmsg_get_str(pm, "version"));

    prop_t *inst = prop_create(plugin_root_installed, pl->pl_id);
    prop_t *instm = prop_create(inst, "metadata");

    prop_link(prop_create(instm, "version"),
prop_create(metadata, "installed_version"));
  }

  if(icon != NULL) {
    if(basepath != NULL) {
      char url[512];
      snprintf(url, sizeof(url), "%s/%s", basepath, icon);
      prop_set_string(prop_create(metadata, "icon"), url);
    } else {
      char *iconurl = url_resolve_relative_from_base(get_repo(), icon);
      prop_set_string(prop_create(metadata, "icon"), iconurl);
      free(iconurl);
    }
  }
}



/**
*
*/
void
plugin_props_from_file(prop_t *prop, const char *zipfile)
{
  char path[200];
  char errbuf[200];
  char *buf;

  snprintf(path, sizeof(path), "zip://%s/plugin.json", zipfile);
  buf = fa_load(path, NULL, NULL, errbuf, sizeof(errbuf), NULL, 0, NULL, NULL);
  if(buf == NULL) {
    TRACE(TRACE_ERROR, "plugins", "Unable to open %s -- %s", path, errbuf);
    return;
  }
  htsmsg_t *pm = htsmsg_json_deserialize(buf);
  free(buf);

  if(pm == NULL)
    return;

  const char *id = htsmsg_get_str(pm, "id");

  if(id != NULL) {
    hts_mutex_lock(&plugin_mutex);
    plugin_t *pl = plugin_find(id);


    snprintf(path, sizeof(path), "zip://%s", zipfile);
    plugin_fill_prop0(pm, prop, path, 0, pl);
    prop_set(prop, "package", PROP_SET_STRING, zipfile);
    update_state(pl);
    hts_mutex_unlock(&plugin_mutex);
  }
  htsmsg_destroy(pm);

}


/**
*
*/
static void
plugin_prop_setup(htsmsg_t *pm, plugin_t *pl, const char *basepath,
int as_installed)
{
  prop_t *p;

  hts_mutex_assert(&plugin_mutex);

  if(as_installed) {
    if(pl->pl_inst_prop == NULL)
      pl->pl_inst_prop = prop_create(plugin_root_installed, pl->pl_id);

    p = pl->pl_inst_prop;

  } else {

    p = prop_create(plugin_root_repo, pl->pl_id);
  }

  prop_set(p, "type", PROP_SET_STRING, "plugin");

  plugin_fill_prop0(pm, p, basepath, as_installed, pl);
}


#if ENABLE_SPIDERMONKEY
/**
*
*/
static void
js_unload(plugin_t *pl)
{
  js_plugin_unload(pl->pl_id);
}
#endif



/**
*
*/
static void
plugin_unload(plugin_t *pl)
{
  if(pl->pl_unload) {
    pl->pl_unload(pl);
    pl->pl_unload = NULL;
  }

  if(pl->pl_unload_props != NULL) {
    prop_vec_destroy_entries(pl->pl_unload_props);
    prop_vec_release(pl->pl_unload_props);
    pl->pl_unload_props = NULL;
  }
}


/**
*
*/
static int
plugin_load(const char *url, char *errbuf, size_t errlen, int force,
int as_installed)
{
  char ctrlfile[URL_MAX];
  char *json;
  htsmsg_t *ctrl;

  snprintf(ctrlfile, sizeof(ctrlfile), "%s/plugin.json", url);

  if((json = fa_load(ctrlfile, NULL, NULL, errbuf, errlen, NULL, 0,
NULL, NULL)) == NULL)
    return -1;

  ctrl = htsmsg_json_deserialize(json);
  free(json);
  if(ctrl != NULL) {

    const char *type = htsmsg_get_str(ctrl, "type");
    const char *id = htsmsg_get_str(ctrl, "id");

    if(type == NULL) {
      snprintf(errbuf, errlen, "Missing \"type\" element in control file %s",
ctrlfile);
      htsmsg_destroy(ctrl);
      return -1;
    }


    if(id == NULL) {
      snprintf(errbuf, errlen, "Missing \"id\" element in control file %s",
ctrlfile);
      htsmsg_destroy(ctrl);
      return -1;
    }

    plugin_t *pl = plugin_find(id);

    if(!force && pl->pl_loaded) {
      snprintf(errbuf, errlen, "Plugin \"%s\" already loaded",
id);
      htsmsg_destroy(ctrl);
      return -1;
    }

    plugin_unload(pl);

    int r;
    char fullpath[URL_MAX];

    if(!strcmp(type, "javascript")) {

      const char *file = htsmsg_get_str(ctrl, "file");
      if(file == NULL) {
snprintf(errbuf, errlen, "Missing \"file\" element in control file %s",
ctrlfile);
htsmsg_destroy(ctrl);
return -1;
      }
      snprintf(fullpath, sizeof(fullpath), "%s/%s", url, file);

#if ENABLE_SPIDERMONKEY
      r = js_plugin_load(id, fullpath, errbuf, errlen);
      if(!r)
pl->pl_unload = js_unload;
#else
      snprintf(errbuf, errlen,
"Unable to load %s -- Javscript not enabled", fullpath);
      r = -1;
#endif

    } else if(!strcmp(type, "views")) {
      // No special tricks here, we always loads 'glwviews' from all plugins
      r = 0;
    } else {
      snprintf(errbuf, errlen, "Unknown type \"%s\" in control file %s",
type, ctrlfile);
      r = -1;
    }


    // Load bundled views

    if(!r) {
      assert(pl->pl_unload_props == NULL);

      htsmsg_t *list = htsmsg_get_list(ctrl, "glwviews");

      pl->pl_unload_props = prop_vec_create(10);

      if(list != NULL) {
htsmsg_field_t *f;
HTSMSG_FOREACH(f, list) {
htsmsg_t *o;
if((o = htsmsg_get_map_by_field(f)) == NULL)
continue;
const char *uit = htsmsg_get_str(o, "uitype") ?: "standard";
const char *class = htsmsg_get_str(o, "class");
const char *title = htsmsg_get_str(o, "title");
const char *file = htsmsg_get_str(o, "file");

if(class == NULL || title == NULL || file == NULL)
continue;
snprintf(fullpath, sizeof(fullpath), "%s/%s", url, file);
prop_t *r;
r = prop_create(prop_create(prop_get_global(), "glw"), "views");
r = prop_create(prop_create(r, uit), class);
prop_t *l = prop_create_r(r, fullpath);
prop_set_link(l, title, fullpath);

TRACE(TRACE_DEBUG, "plugins",
"Added view uitype:%s class:%s title:%s from %s",
uit, class, title, fullpath);

pl->pl_unload_props = prop_vec_append(pl->pl_unload_props, l);
prop_ref_dec(l);
}
      }
    }

    if(!r) {

      if(as_installed) {
plugin_prop_setup(ctrl, pl, url, 1);
mystrset(&pl->pl_inst_ver, htsmsg_get_str(ctrl, "version"));
pl->pl_installed = 1;
      }

      mystrset(&pl->pl_inst_ver, htsmsg_get_str(ctrl, "version"));
      mystrset(&pl->pl_title, htsmsg_get_str(ctrl, "title") ?: id);

      pl->pl_loaded = 1;
    }
    htsmsg_destroy(ctrl);
    update_state(pl);
    return r;

  } else {

    snprintf(errbuf, errlen, "Unable parse JSON control file %s",
ctrlfile);
    return -1;
  }
}




/**
*
*/
static void
plugin_load_installed(void)
{
  char path[200];
  char errbuf[200];
  fa_dir_entry_t *fde;

  snprintf(path, sizeof(path), "file://%s/installedplugins",
gconf.persistent_path);

  fa_dir_t *fd = fa_scandir(path, NULL, 0);

  if(fd != NULL) {
    TAILQ_FOREACH(fde, &fd->fd_entries, fde_link) {
      snprintf(path, sizeof(path), "zip://%s", rstr_get(fde->fde_url));
      if(plugin_load(path, errbuf, sizeof(errbuf), 0, 1)) {
TRACE(TRACE_ERROR, "plugins", "Unable to load %s\n%s", path, errbuf);
      }
    }
    fa_dir_free(fd);
  }
}



/**
*
*/
static htsmsg_t *
repo_get(const char *repo, char *errbuf, size_t errlen)
{
  char *result;
  htsmsg_t *json;

  TRACE(TRACE_DEBUG, "plugins", "Loading repo from %s", repo);

  result = fa_load(repo, NULL, NULL, errbuf, errlen, NULL, 0, NULL, NULL);
  if(result == NULL)
    return NULL;
  
  json = htsmsg_json_deserialize(result);
  free(result);

  if(json == NULL) {
    snprintf(errbuf, errlen, "Malformed JSON in repository");
    return NULL;
  }

  const int ver = htsmsg_get_u32_or_default(json, "version", 0);

  if(ver != 1) {
    snprintf(errbuf, errlen, "Unsupported repository version %d", ver);
    htsmsg_destroy(json);
    return NULL;
  }

  const char *msg = htsmsg_get_str(json, "message");
  if(msg != NULL) {
    snprintf(errbuf, errlen, "%s", msg);
    htsmsg_destroy(json);
    return NULL;
  }
  
  htsmsg_field_t *f = htsmsg_field_find(json, "plugins");
  if(f == NULL) {
    snprintf(errbuf, errlen, "Missing plugin list in repository");
    htsmsg_destroy(json);
    return NULL;
  }
  htsmsg_t *r = htsmsg_detach_submsg(f);
  htsmsg_destroy(json);

  return r;
}


/**
*
*/
static void
plugin_load_repo(void)
{
  char errbuf[512];
  htsmsg_t *r = repo_get(get_repo(), errbuf, sizeof(errbuf));
  
  if(r != NULL) {
    htsmsg_field_t *f;

    HTSMSG_FOREACH(f, r) {
      htsmsg_t *pm;
      if((pm = htsmsg_get_map_by_field(f)) == NULL)
continue;
      const char *id = htsmsg_get_str(pm, "id");
      if(id == NULL)
continue;
      plugin_t *pl = plugin_find(id);
      plugin_prop_setup(pm, pl, NULL, 0);
      mystrset(&pl->pl_repo_ver, htsmsg_get_str(pm, "version"));
      mystrset(&pl->pl_showtime_min_version,
htsmsg_get_str(pm, "showtimeVersion"));
      update_state(pl);

      const char *dlurl = htsmsg_get_str(pm, "downloadURL");
      if(dlurl != NULL) {
char *package = url_resolve_relative_from_base(get_repo(), dlurl);
free(pl->pl_package);
pl->pl_package = package;
      }
    }

    htsmsg_destroy(r);

  } else {
    TRACE(TRACE_ERROR, "plugins", "Unable to load repo %s -- %s",
get_repo(), errbuf);
  }
}


/**
*
*/
static void
plugins_load(void)
{
  plugin_load_installed();
  plugin_load_repo();
  update_global_state();
  plugin_autoupgrade();
}


/**
*
*/
static void
plugin_autoupgrade(void)
{
  plugin_t *pl;

  if(!autoupgrade)
    return;

  LIST_FOREACH(pl, &plugins, pl_link) {
    if(!pl->pl_can_upgrade)
      continue;
    if(plugin_install(pl, NULL))
      continue;
    notify_add(NULL, NOTIFY_INFO, NULL, 5,
_("Upgraded plugin %s to version %s"), pl->pl_title,
pl->pl_inst_ver);
  }
  update_global_state();
}

/**
*
*/
static void
plugins_setup_root_props(void)
{
  prop_concat_t *pc;
  struct prop_nf *pnf;
  prop_t *d, *p;
  htsmsg_t *store;

  if((store = htsmsg_store_load("pluginconf")) == NULL)
    store = htsmsg_create_map();

  plugin_root_installed = prop_create_root(NULL);
  plugin_root_repo = prop_create_root(NULL);
  plugin_root_model = prop_create_root(NULL);


  prop_set_int(prop_create(plugin_root_model, "safeui"), 1);
  prop_set_string(prop_create(plugin_root_model, "contents"), "plugins");
  prop_set_string(prop_create(plugin_root_model, "type"), "directory");

  prop_link(_p("Plugins"),
prop_create(prop_create(plugin_root_model, "metadata"), "title"));

  pc = prop_concat_create(prop_create(plugin_root_model, "nodes"), 0);

  // Top items

  prop_t *sta = prop_create_root(NULL);
  
  p = prop_create(sta, NULL);
  prop_set_string(prop_create(p, "type"), "directory");
  prop_link(_p("Browse available plugins"),
prop_create(prop_create(p, "metadata"), "title"));
  prop_set_string(prop_create(p, "url"), "plugin:repo");

  prop_concat_add_source(pc, sta, NULL);

  // Installed plugins

  prop_t *inst = prop_create_root(NULL);
  pnf = prop_nf_create(inst, plugin_root_installed, NULL, PROP_NF_AUTODESTROY);
  prop_nf_sort(pnf, "node.metadata.title", 0, 0, NULL, 1);


  d = prop_create_root(NULL);
  prop_link(_p("Installed plugins"),
prop_create(prop_create(d, "metadata"), "title"));
  prop_set_string(prop_create(d, "type"), "separator");
  prop_concat_add_source(pc, inst, d);


  // Settings

  settings_create_separator(settings_general,
_p("Plugins"));

  settings_create_string(settings_general, "alt_repo",
_p("Alternate plugin Repository URL"),
NULL, store, set_alt_repo_url, NULL,
SETTINGS_INITIAL_UPDATE, NULL,
settings_generic_save_settings,
(void *)"pluginconf");

  settings_create_bool(settings_general,
"autoupgrade", _p("Automatically upgrade plugins"),
1, store, set_autoupgrade, NULL,
SETTINGS_INITIAL_UPDATE, NULL,
settings_generic_save_settings,
(void *)"pluginconf");


}


/**
*
*/
static void *
plugin_thread(void *aux)
{
  hts_mutex_lock(&plugin_mutex);
  plugins_load();
  hts_mutex_unlock(&plugin_mutex);
  upgrade_init();
  return NULL;
}


/**
*
*/
void
plugins_init(const char *loadme, const char *repo, int sync_init)
{
  char url[PATH_MAX];
  if(repo) {
    fa_normalize(repo, url, sizeof(url));
    plugin_repo_url = strdup(url);
  }
  hts_mutex_init(&plugin_mutex);
  plugin_courier = prop_courier_create_waitable();

  plugins_setup_root_props();

  hts_mutex_lock(&plugin_mutex);

  if(loadme != NULL) {
    char errbuf[200];
    fa_normalize(loadme, url, sizeof(url));
    devplugin = strdup(url);
    if(plugin_load(devplugin, errbuf, sizeof(errbuf), 1, 0)) {
      TRACE(TRACE_ERROR, "plugins",
"Unable to load development plugin: %s\n%s", loadme, errbuf);
    } else {
      TRACE(TRACE_INFO, "plugins", "Loaded dev plugin %s", devplugin);
    }
    sync_init = 1;
  }

  if(sync_init) {
    plugins_load();
    upgrade_init();
  } else {
    hts_thread_create_detached("pluginsinit", plugin_thread, NULL,
THREAD_PRIO_LOW);
  }

  hts_mutex_unlock(&plugin_mutex);
}


/**
*
*/
void
plugins_reload_dev_plugin(void)
{
  char errbuf[200];
  if(devplugin == NULL)
    return;

  hts_mutex_lock(&plugin_mutex);

  if(plugin_load(devplugin, errbuf, sizeof(errbuf), 1, 0))
    TRACE(TRACE_ERROR, "plugins",
"Unable to reload development plugin: %s\n%s", devplugin, errbuf);
  else
    TRACE(TRACE_INFO, "plugins", "Reloaded dev plugin %s", devplugin);

  hts_mutex_unlock(&plugin_mutex);
}


/**
*
*/
static int
plugin_canhandle(const char *url)
{
  return !strncmp(url, "plugin:", strlen("plugin:"));
}


/**
*
*/
static void
plugin_remove(plugin_t *pl)
{
  char path[PATH_MAX];

  snprintf(path, sizeof(path), "%s/installedplugins/%s.zip",
gconf.persistent_path, pl->pl_id);
  unlink(path);

  TRACE(TRACE_DEBUG, "plugin", "Uninstalling %s", pl->pl_id);
  htsmsg_store_remove("plugins/%s", pl->pl_id);

  plugin_unload(pl);

  prop_destroy(pl->pl_inst_prop);
  pl->pl_inst_prop = NULL;
  pl->pl_installed = 0;
  pl->pl_loaded = 0;

  update_state(pl);
}


/**
*
*/
static int
plugin_install(plugin_t *pl, const char *package)
{
  char errbuf[200];
  char path[200];
  rstr_t *s;

  if(package == NULL)
    package = pl->pl_package;

  if(package == NULL) {
    prop_set_string(pl->pl_statustxt, "No package file specified");
    return -1;
  }

  TRACE(TRACE_INFO, "plugins", "Downloading plugin %s from %s",
pl->pl_id, package);

  s = _("Downloading");
  prop_set_rstring(pl->pl_statustxt, s);
  rstr_release(s);

  size_t size;

  char *buf = fa_load(package, &size, NULL,
errbuf, sizeof(errbuf), NULL, 0, NULL, NULL);

  if(buf == NULL) {
    prop_set_string(pl->pl_statustxt, errbuf);
    return -1;
  }

  if(size < 4 ||
     buf[0] != 0x50 || buf[1] != 0x4b || buf[2] != 0x03 || buf[3] != 0x04) {
    s = _("Corrupt plugin bundle");
    prop_set_rstring(pl->pl_statustxt, s);
    rstr_release(s);
    return -1;
  }
  
  TRACE(TRACE_INFO, "plugins", "Plugin %s valid ZIP archive %d bytes",
pl->pl_id, (int)size);
  s = _("Installing");
  prop_set_rstring(pl->pl_statustxt, s);
  rstr_release(s);

  snprintf(path, sizeof(path), "%s/installedplugins", gconf.persistent_path);
  mkdir(path, 0770);

  plugin_unload(pl);

  snprintf(path, sizeof(path), "%s/installedplugins/%s.zip",
gconf.persistent_path, pl->pl_id);
  unlink(path);

  int fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0660);
  if(fd == -1) {
    s = _("File open error");
    prop_set_rstring(pl->pl_statustxt, s);
    rstr_release(s);

    TRACE(TRACE_ERROR, "plugins", "Unable to write to %s -- %s",
path, strerror(errno));
    free(buf);
    return -1;
  }

  size_t r = write(fd, buf, size);
  free(buf);
  if(close(fd) || r != size) {
    s = _("Disk write error");
    prop_set_rstring(pl->pl_statustxt, s);
    rstr_release(s);
    TRACE(TRACE_ERROR, "plugins", "Unable to write to %s -- %s",
path, strerror(errno));
    return -1;
  }

  snprintf(path, sizeof(path),
"zip://file://%s/installedplugins/%s.zip", gconf.persistent_path,
pl->pl_id);

  if(plugin_load(path, errbuf, sizeof(errbuf), 1, 1)) {
    prop_set_string(pl->pl_statustxt, errbuf);
    TRACE(TRACE_ERROR, "plugins", "Unable to load %s -- %s", path, errbuf);
    return -1;
  }
  return 0;
}


/**
*
*/
static void
plugin_open_start(prop_t *page)
{
  prop_link(plugin_root_model, prop_create(page, "model"));
}


/**
*
*/
static void
plugin_open_repo(prop_t *page)
{
  int i;
  struct prop_nf *pnf;
  prop_concat_t *pc;
  prop_t *model = prop_create(page, "model");
  prop_set_string(prop_create(model, "type"), "directory");
  prop_set_int(prop_create(model, "safeui"), 1);
  prop_set_string(prop_create(model, "contents"), "plugins");

  prop_link(_p("Available plugins"),
prop_create(prop_create(model, "metadata"), "title"));

  pc = prop_concat_create(prop_create(model, "nodes"), 0); // XXX leak

  // Create filters per category

  for(i = 0; i < PLUGIN_CAT_num; i++) {
    const char *catname = val2str(i, catnames);
    prop_t *cat = prop_create(model, catname);
    pnf = prop_nf_create(cat, plugin_root_repo, NULL, PROP_NF_AUTODESTROY);
    
    prop_nf_pred_str_add(pnf, "node.metadata.category",
PROP_NF_CMP_NEQ, catname, NULL,
PROP_NF_MODE_EXCLUDE);
    prop_nf_sort(pnf, "node.metadata.title", 0, 0, NULL, 1);
    prop_nf_release(pnf);


    prop_t *header = prop_create_root(NULL);
    
    prop_set_string(prop_create(header, "type"), "separator");

    prop_t *gn = NULL;
    switch(i) {
    case PLUGIN_CAT_TV:
      gn = _p("Online TV");
      break;

    case PLUGIN_CAT_VIDEO:
      gn = _p("Video streaming");
      break;

    case PLUGIN_CAT_MUSIC:
      gn = _p("Music streaming");
      break;

    case PLUGIN_CAT_CLOUD:
      gn = _p("Cloud services");
      break;

    case PLUGIN_CAT_GLWVIEW:
      gn = _p("User interface extensions");
      break;

    default:
      gn = _p("Uncategorized");
      break;
    }

    prop_link(gn, prop_create(prop_create(header, "metadata"), "title"));
    prop_concat_add_source(pc, cat, header);
  }
}


/**
*
*/
static int
plugin_open_url(prop_t *page, const char *url)
{
  if(!strcmp(url, "plugin:start")) {
    plugin_open_start(page);
    return 0;
  }

  if(!strcmp(url, "plugin:repo")) {
    plugin_open_repo(page);
    return 0;
  }

  nav_open_error(page, "Invalid URI");
  return 0;
}




/**
*
*/
static backend_t be_plugin = {
  .be_canhandle = plugin_canhandle,
  .be_open = plugin_open_url,
};

BE_REGISTER(plugin);

/**
*
*/
void
plugin_open_file(prop_t *page, const char *url)
{
  char path[200];
  char errbuf[200];
  char *buf;

  snprintf(path, sizeof(path), "zip://%s/plugin.json", url);
  buf = fa_load(path, NULL, NULL, errbuf, sizeof(errbuf), NULL, 0, NULL, NULL);
  if(buf == NULL) {
    nav_open_errorf(page, _("Unable to load plugin.json: %s"), errbuf);
    return;
  }

  htsmsg_t *pm = htsmsg_json_deserialize(buf);
  free(buf);

  if(pm == NULL) {
    nav_open_errorf(page, _("Unable to load plugin.json: Malformed JSON"));
    return;
  }

  const char *id = htsmsg_get_str(pm, "id");

  if(id != NULL) {
    hts_mutex_lock(&plugin_mutex);
    plugin_t *pl = plugin_find(id);
    plugin_install(pl, url);
    hts_mutex_unlock(&plugin_mutex);
  } else {
    nav_open_errorf(page, _("Field \"id\" not found in plugin.json"));
  }
  htsmsg_destroy(pm);
}



Something went wrong with that request. Please try again.