class.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2012 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] = "listed";
  44. assert(Class < CLASS_COUNT);
  45. assert(Client != NULL);
  46. if (!Lists_CheckReason(&My_Classes[Class], Client, str, sizeof(str)))
  47. return false;
  48. switch(Class) {
  49. case CLASS_GLINE:
  50. snprintf(reason, len, "\"%s\" (G-Line)", str);
  51. break;
  52. case CLASS_KLINE:
  53. snprintf(reason, len, "\"%s\" (K-Line)", str);
  54. break;
  55. default:
  56. snprintf(reason, len, "%s", str);
  57. break;
  58. }
  59. return true;
  60. }
  61. /**
  62. * Check if a client is banned from this server: GLINE, KLINE.
  63. *
  64. * If a client isn't allowed to connect, it will be disconnected again.
  65. *
  66. * @param Client The client to check.
  67. * @return CONNECTED if client is allowed to join, DISCONNECTED if not.
  68. */
  69. GLOBAL bool
  70. Class_HandleServerBans(CLIENT *Client)
  71. {
  72. char reject[COMMAND_LEN];
  73. assert(Client != NULL);
  74. if (Class_GetMemberReason(CLASS_GLINE, Client, reject, sizeof(reject)) ||
  75. Class_GetMemberReason(CLASS_KLINE, Client, reject, sizeof(reject))) {
  76. Client_Reject(Client, reject, true);
  77. return DISCONNECTED;
  78. }
  79. return CONNECTED;
  80. }
  81. GLOBAL bool
  82. Class_AddMask(const int Class, const char *Pattern, time_t ValidUntil,
  83. const char *Reason)
  84. {
  85. char mask[MASK_LEN];
  86. assert(Class < CLASS_COUNT);
  87. assert(Pattern != NULL);
  88. assert(Reason != NULL);
  89. Lists_MakeMask(Pattern, mask, sizeof(mask));
  90. return Lists_Add(&My_Classes[Class], mask,
  91. ValidUntil, Reason);
  92. }
  93. GLOBAL void
  94. Class_DeleteMask(const int Class, const char *Pattern)
  95. {
  96. char mask[MASK_LEN];
  97. assert(Class < CLASS_COUNT);
  98. assert(Pattern != NULL);
  99. Lists_MakeMask(Pattern, mask, sizeof(mask));
  100. Lists_Del(&My_Classes[Class], mask);
  101. }
  102. GLOBAL struct list_head *
  103. Class_GetList(const int Class)
  104. {
  105. assert(Class < CLASS_COUNT);
  106. return &My_Classes[Class];
  107. }
  108. GLOBAL void
  109. Class_Expire(void)
  110. {
  111. Lists_Expire(&My_Classes[CLASS_GLINE], "G-Line");
  112. Lists_Expire(&My_Classes[CLASS_KLINE], "K-Line");
  113. }
  114. /* -eof- */