m_pubnetinfo.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright (C) All Rights Reserved
  2. ** Written by Gottem <support@gottem.nl>
  3. ** Website: https://gitgud.malvager.net/Wazakindjes/unrealircd_mods
  4. ** License: https://gitgud.malvager.net/Wazakindjes/unrealircd_mods/raw/master/LICENSE
  5. */
  6. // One include for all cross-platform compatibility thangs
  7. #include "unrealircd.h"
  8. // Command string
  9. #define MSG_PUBNETINFO "PUBNETINFO"
  10. // Dem macros yo
  11. CMD_FUNC(m_pubnetinfo); // Register command function
  12. // Muh globals
  13. static ModuleInfo *pubnetinfoMI = NULL; // Store ModuleInfo so we can use it to check for errors in MOD_LOAD
  14. Command *pubnetinfoCmd; // Pointer(s) to the command(s) we're gonna add
  15. // Dat dere module header
  16. ModuleHeader MOD_HEADER(m_pubnetinfo) = {
  17. "m_pubnetinfo", // Module name
  18. "$Id: v1.0 2018/03/03 Gottem$", // Version
  19. "Display public network/server information such as SSL/TLS links", // Description
  20. "3.2-b8-1", // Modversion, not sure wat do
  21. NULL
  22. };
  23. // Initialisation routine (register hooks, commands and modes or create structs etc)
  24. MOD_INIT(m_pubnetinfo) {
  25. // If command already exists for some reason, bail out
  26. if(CommandExists(MSG_PUBNETINFO)) {
  27. config_error("Command %s already exists", MSG_PUBNETINFO);
  28. return MOD_FAILED;
  29. }
  30. pubnetinfoCmd = CommandAdd(modinfo->handle, MSG_PUBNETINFO, m_pubnetinfo, 2, M_USER | M_SERVER);
  31. pubnetinfoMI = modinfo; // Store module info yo
  32. return MOD_SUCCESS; // Let MOD_LOAD handle errors
  33. }
  34. // Actually load the module here (also command overrides as they may not exist in MOD_INIT yet)
  35. MOD_LOAD(m_pubnetinfo) {
  36. // Did the module throw an error during initialisation, or is pubnetinfoCmd null even?
  37. if(ModuleGetError(pubnetinfoMI->handle) != MODERR_NOERROR || !pubnetinfoCmd) {
  38. // Display error string kek
  39. config_error("A critical error occurred when loading module %s: %s", MOD_HEADER(m_pubnetinfo).name, ModuleGetErrorStr(pubnetinfoMI->handle));
  40. return MOD_FAILED; // No good
  41. }
  42. return MOD_SUCCESS; // We good
  43. }
  44. // Called on unload/rehash obv
  45. MOD_UNLOAD(m_pubnetinfo) {
  46. // Clean up any structs and other shit
  47. return MOD_SUCCESS; // We good
  48. }
  49. CMD_FUNC(m_pubnetinfo) {
  50. /* Gets args: aClient *cptr, aClient *sptr, int parc, char *parv[]
  51. **
  52. ** cptr: Pointer to directly attached client -- if remote user this is the remote server instead
  53. ** sptr: Pointer to user executing command
  54. ** parc: Amount of arguments (also includes the command in the count)
  55. ** parv: Contains the actual args, first one starts at parv[1]
  56. **
  57. ** So "PUBNETINFO test" would result in parc = 2 and parv[1] = "test"
  58. ** Also, parv[0] seems to always be NULL, so better not rely on it fam
  59. */
  60. aClient *acptr, *from;
  61. int ssl, localhost;
  62. char *serv;
  63. serv = NULL;
  64. from = (IsPerson(sptr) ? sptr : NULL); // Respond to client executing command by default
  65. // In case of forwarding, first arg is user to respond to and second is the server we gettin info fer
  66. if(!BadPtr(parv[1]) && !BadPtr(parv[2]) && IsServer(sptr)) {
  67. from = find_person(parv[1], NULL);
  68. serv = parv[2];
  69. }
  70. // Return silently if client executing command is not a person or wasn't found, or was a server forward without enough args
  71. if(!from)
  72. return 0;
  73. // Checkem lol
  74. list_for_each_entry(acptr, &global_server_list, client_node) {
  75. // Skip ourselves obv, also if we got a request for a specific server and this isn't the one ;]
  76. if(IsMe(acptr) || (serv && stricmp(acptr->name, serv)))
  77. continue;
  78. // Can only get proper status if the server is directly linked to us =]
  79. if(stricmp(acptr->serv->up, me.name)) {
  80. // Only forward if the user requesting this shit is local to us imo
  81. if(MyClient(from))
  82. sendto_one(acptr->from, ":%s %s %s :%s", me.name, MSG_PUBNETINFO, from->name, acptr->name);
  83. continue;
  84. }
  85. // Initialise to unknown (fallback) ;]
  86. ssl = -1;
  87. localhost = -1;
  88. // Checkem link config
  89. if(acptr->serv->conf)
  90. ssl = ((acptr->serv->conf->outgoing.options & CONNECT_SSL) ? 1 : 0);
  91. // Checkem IP
  92. if(acptr->ip)
  93. localhost = is_loopback_ip(acptr->ip);
  94. // Shit out status string
  95. sendnotice(from, "[pubnetinfo] Link %s: localhost connection = %s -- SSL = %s",
  96. acptr->name,
  97. (localhost == 1 ? "yes" : (localhost == 0 ? "no" : "unknown")),
  98. (ssl == 1 ? "yes" : (ssl == 0 ? "no" : "unknown"))
  99. );
  100. }
  101. return 0; // All good
  102. }