class.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. */
  11. #include "portab.h"
  12. /**
  13. * @file
  14. * User class management.
  15. */
  16. #include "imp.h"
  17. #include <assert.h>
  18. #include <string.h>
  19. #include "defines.h"
  20. #include "array.h"
  21. #include "conn.h"
  22. #include "client.h"
  23. #include "lists.h"
  24. #include "match.h"
  25. #include "stdio.h"
  26. #include "exp.h"
  27. #include "class.h"
  28. struct list_head My_Classes[CLASS_COUNT];
  29. GLOBAL void
  30. Class_Init(void)
  31. {
  32. memset(My_Classes, 0, sizeof(My_Classes));
  33. }
  34. GLOBAL void
  35. Class_Exit(void)
  36. {
  37. int i;
  38. for (i = 0; i < CLASS_COUNT; Lists_Free(&My_Classes[i++]));
  39. }
  40. GLOBAL bool
  41. Class_GetMemberReason(const int Class, CLIENT *Client, char *reason, size_t len)
  42. {
  43. char str[COMMAND_LEN];
  44. assert(Class < CLASS_COUNT);
  45. assert(Client != NULL);
  46. strlcpy(str, "listed", sizeof(str));
  47. if (!Lists_CheckReason(&My_Classes[Class], Client, str, sizeof(str)))
  48. return false;
  49. switch(Class) {
  50. case CLASS_GLINE:
  51. snprintf(reason, len, "\"%s\" (G-Line)", str);
  52. break;
  53. case CLASS_KLINE:
  54. snprintf(reason, len, "\"%s\" (K-Line)", str);
  55. break;
  56. default:
  57. snprintf(reason, len, "%s", str);
  58. break;
  59. }
  60. return true;
  61. }
  62. /**
  63. * Check if a client is banned from this server: GLINE, KLINE.
  64. *
  65. * If a client isn't allowed to connect, it will be disconnected again.
  66. *
  67. * @param Client The client to check.
  68. * @return CONNECTED if client is allowed to join, DISCONNECTED if not.
  69. */
  70. GLOBAL bool
  71. Class_HandleServerBans(CLIENT *Client)
  72. {
  73. char reject[COMMAND_LEN];
  74. assert(Client != NULL);
  75. if (Class_GetMemberReason(CLASS_GLINE, Client, reject, sizeof(reject)) ||
  76. Class_GetMemberReason(CLASS_KLINE, Client, reject, sizeof(reject))) {
  77. Client_Reject(Client, reject, true);
  78. return DISCONNECTED;
  79. }
  80. return CONNECTED;
  81. }
  82. GLOBAL bool
  83. Class_AddMask(const int Class, const char *Pattern, time_t ValidUntil,
  84. const char *Reason)
  85. {
  86. char mask[MASK_LEN];
  87. assert(Class < CLASS_COUNT);
  88. assert(Pattern != NULL);
  89. assert(Reason != NULL);
  90. Lists_MakeMask(Pattern, mask, sizeof(mask));
  91. return Lists_Add(&My_Classes[Class], mask,
  92. ValidUntil, Reason);
  93. }
  94. GLOBAL void
  95. Class_DeleteMask(const int Class, const char *Pattern)
  96. {
  97. char mask[MASK_LEN];
  98. assert(Class < CLASS_COUNT);
  99. assert(Pattern != NULL);
  100. Lists_MakeMask(Pattern, mask, sizeof(mask));
  101. Lists_Del(&My_Classes[Class], mask);
  102. }
  103. GLOBAL struct list_head *
  104. Class_GetList(const int Class)
  105. {
  106. assert(Class < CLASS_COUNT);
  107. return &My_Classes[Class];
  108. }
  109. GLOBAL void
  110. Class_Expire(void)
  111. {
  112. Lists_Expire(&My_Classes[CLASS_GLINE], "G-Line");
  113. Lists_Expire(&My_Classes[CLASS_KLINE], "K-Line");
  114. }
  115. /* -eof- */