class.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. char Reject_Reason[COMMAND_LEN];
  30. GLOBAL void
  31. Class_Init(void)
  32. {
  33. memset(My_Classes, 0, sizeof(My_Classes));
  34. }
  35. GLOBAL void
  36. Class_Exit(void)
  37. {
  38. int i;
  39. for (i = 0; i < CLASS_COUNT; Lists_Free(&My_Classes[i++]));
  40. }
  41. GLOBAL char *
  42. Class_GetMemberReason(const int Class, CLIENT *Client)
  43. {
  44. char *reason;
  45. assert(Class < CLASS_COUNT);
  46. assert(Client != NULL);
  47. reason = Lists_CheckReason(&My_Classes[Class], Client);
  48. if (!reason)
  49. return NULL;
  50. if (!*reason)
  51. reason = "listed";
  52. switch(Class) {
  53. case CLASS_GLINE:
  54. snprintf(Reject_Reason, sizeof(Reject_Reason),
  55. "\"%s\" (G-Line)", reason);
  56. return Reject_Reason;
  57. case CLASS_KLINE:
  58. snprintf(Reject_Reason, sizeof(Reject_Reason),
  59. "\"%s\" (K-Line)", reason);
  60. return Reject_Reason;
  61. }
  62. return reason;
  63. }
  64. /**
  65. * Check if a client is banned from this server: GLINE, KLINE.
  66. *
  67. * If a client isn't allowed to connect, it will be disconnected again.
  68. *
  69. * @param Client The client to check.
  70. * @return CONNECTED if client is allowed to join, DISCONNECTED if not.
  71. */
  72. GLOBAL bool
  73. Class_HandleServerBans(CLIENT *Client)
  74. {
  75. char *rejectptr;
  76. assert(Client != NULL);
  77. rejectptr = Class_GetMemberReason(CLASS_GLINE, Client);
  78. if (!rejectptr)
  79. rejectptr = Class_GetMemberReason(CLASS_KLINE, Client);
  80. if (rejectptr) {
  81. Client_Reject(Client, rejectptr, true);
  82. return DISCONNECTED;
  83. }
  84. return CONNECTED;
  85. }
  86. GLOBAL bool
  87. Class_AddMask(const int Class, const char *Mask, time_t ValidUntil,
  88. const char *Reason)
  89. {
  90. assert(Class < CLASS_COUNT);
  91. assert(Mask != NULL);
  92. assert(Reason != NULL);
  93. return Lists_Add(&My_Classes[Class], Lists_MakeMask(Mask),
  94. ValidUntil, Reason);
  95. }
  96. GLOBAL void
  97. Class_DeleteMask(const int Class, const char *Mask)
  98. {
  99. assert(Class < CLASS_COUNT);
  100. assert(Mask != NULL);
  101. Lists_Del(&My_Classes[Class], Lists_MakeMask(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- */