Discord library in plain C
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // Created by Memer on 25.08.18.
  3. // Copyright (c) 2018 Alexander Memer. All rights reserved.
  4. //
  5. #include <discord.h>
  6. #include <jansson.h>
  7. #include <string.h>
  8. void ld_channel_obj_to_type(json_t *src, guild_channel_t *channel)
  9. {
  10. json_t *id = json_object_get(src, "id");
  11. channel->id = strdup(json_string_value(id));
  12. json_decref(id);
  13. json_t *type = json_object_get(src, "type");
  14. channel->type = (int)json_integer_value(type);
  15. json_decref(type);
  16. if (channel->type == GUILD_TEXT || channel->type == GUILD_CATEGORY)
  17. {
  18. json_t *guild_id = json_object_get(src, "guild_id");
  19. channel->guild_id = strdup(json_string_value(guild_id));
  20. json_decref(guild_id);
  21. json_t *permission_overwrites = json_object_get(src, "permission_overwrites");
  22. size_t osize = json_array_size(permission_overwrites);
  23. channel->permission_overwrites = calloc(osize, sizeof(overwrite_t));
  24. size_t index;
  25. json_t *value;
  26. json_array_foreach(permission_overwrites, index, value)
  27. {
  28. overwrite_t temp;
  29. temp.id = strdup((char*)json_string_value(json_object_get(value, "id")));
  30. temp.type = strdup((char*)json_string_value(json_object_get(value, "type")));
  31. temp.deny = (int)json_integer_value(json_object_get(value, "deny"));
  32. temp.allow = (int)json_integer_value(json_object_get(value, "allow"));
  33. channel->permission_overwrites[index] = temp;
  34. }
  35. json_decref(permission_overwrites);
  36. json_t *name = json_object_get(src, "name");
  37. channel->name = strdup(json_string_value(name));
  38. json_decref(name);
  39. if (channel->type == GUILD_TEXT)
  40. {
  41. // Topic is nullable
  42. json_t *topic = json_object_get(src, "topic");
  43. if (json_is_string(topic)) {
  44. channel->topic = strdup(json_string_value(topic));
  45. }
  46. json_decref(topic);
  47. json_t *nsfw = json_object_get(src, "nsfw");
  48. channel->nsfw = json_boolean_value(nsfw);
  49. json_decref(nsfw);
  50. json_t *last_message_id = json_object_get(src, "last_message_id");
  51. if (last_message_id) {
  52. if (json_is_string(last_message_id)) {
  53. channel->last_message_id = strdup(json_string_value(last_message_id));
  54. }
  55. json_decref(last_message_id);
  56. }
  57. }
  58. json_t *parent_id = json_object_get(src, "parent_id");
  59. if (json_is_string(parent_id))
  60. {
  61. channel->parent_id = strdup(json_string_value(parent_id));
  62. }
  63. json_decref(parent_id);
  64. json_t *position = json_object_get(src, "position");
  65. channel->position = (int)json_integer_value(position);
  66. json_decref(position);
  67. }
  68. else if (channel->type == GUILD_VOICE)
  69. {
  70. json_t *bitrate = json_object_get(src, "bitrate");
  71. channel->bitrate = (int)json_integer_value(bitrate);
  72. json_decref(bitrate);
  73. json_t *user_limit = json_object_get(src, "user_limit");
  74. channel->user_limit = (int)json_integer_value(user_limit);
  75. json_decref(user_limit);
  76. }
  77. else if (channel->type == DM || channel->type == GROUP_DM)
  78. {
  79. json_t *recipients = json_object_get(src, "recipients");
  80. size_t rsize = json_array_size(recipients);
  81. channel->recipients = calloc(rsize, sizeof(user_t));
  82. size_t index;
  83. json_t *value;
  84. json_array_foreach(recipients, index, value)
  85. {
  86. user_t temp;
  87. // TODO: Create function for converting json user objects to structs
  88. temp.id = (char*)json_string_value(json_object_get(value, "id"));
  89. temp.username = (char*)json_string_value(json_object_get(value, "username"));
  90. temp.discriminator = (char*)json_string_value(json_object_get(value, "discriminator"));
  91. json_t *avatar = json_object_get(value, "avatar");
  92. if (avatar)
  93. {
  94. temp.avatar = (char*)json_string_value(avatar);
  95. json_decref(avatar);
  96. }
  97. channel->recipients[index] = temp;
  98. }
  99. json_t *icon = json_object_get(src, "icon");
  100. if (icon)
  101. {
  102. if (json_is_string(icon))
  103. {
  104. channel->icon = strdup(json_string_value(icon));
  105. }
  106. json_decref(icon);
  107. }
  108. json_t *owner_id = json_object_get(src, "owner_id");
  109. channel->owner_id = strdup(json_string_value(owner_id));
  110. json_decref(owner_id);
  111. json_t *application_id = json_object_get(src, "application_id");
  112. if (application_id)
  113. {
  114. channel->application_id = strdup(json_string_value(application_id));
  115. json_decref(application_id);
  116. }
  117. }
  118. }