class.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <assert.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "conn.h"
  20. #include "lists.h"
  21. #include "class.h"
  22. struct list_head My_Classes[CLASS_COUNT];
  23. GLOBAL void
  24. Class_Init(void)
  25. {
  26. memset(My_Classes, 0, sizeof(My_Classes));
  27. }
  28. GLOBAL void
  29. Class_Exit(void)
  30. {
  31. int i;
  32. for (i = 0; i < CLASS_COUNT; Lists_Free(&My_Classes[i++]));
  33. }
  34. GLOBAL bool
  35. Class_GetMemberReason(const int Class, CLIENT *Client, char *reason, size_t len)
  36. {
  37. char str[COMMAND_LEN];
  38. assert(Class < CLASS_COUNT);
  39. assert(Client != NULL);
  40. strlcpy(str, "listed", sizeof(str));
  41. if (!Lists_CheckReason(&My_Classes[Class], Client, str, sizeof(str)))
  42. return false;
  43. switch(Class) {
  44. case CLASS_GLINE:
  45. snprintf(reason, len, "\"%s\" (G-Line)", str);
  46. break;
  47. case CLASS_KLINE:
  48. snprintf(reason, len, "\"%s\" (K-Line)", str);
  49. break;
  50. default:
  51. snprintf(reason, len, "%s", str);
  52. break;
  53. }
  54. return true;
  55. }
  56. /**
  57. * Check if a client is banned from this server: GLINE, KLINE.
  58. *
  59. * If a client isn't allowed to connect, it will be disconnected again.
  60. *
  61. * @param Client The client to check.
  62. * @return CONNECTED if client is allowed to join, DISCONNECTED if not.
  63. */
  64. GLOBAL bool
  65. Class_HandleServerBans(CLIENT *Client)
  66. {
  67. char reject[COMMAND_LEN];
  68. assert(Client != NULL);
  69. if (Class_GetMemberReason(CLASS_GLINE, Client, reject, sizeof(reject)) ||
  70. Class_GetMemberReason(CLASS_KLINE, Client, reject, sizeof(reject))) {
  71. Client_Reject(Client, reject, true);
  72. return DISCONNECTED;
  73. }
  74. return CONNECTED;
  75. }
  76. GLOBAL bool
  77. Class_AddMask(const int Class, const char *Pattern, time_t ValidUntil,
  78. const char *Reason)
  79. {
  80. char mask[MASK_LEN];
  81. assert(Class < CLASS_COUNT);
  82. assert(Pattern != NULL);
  83. assert(Reason != NULL);
  84. Lists_MakeMask(Pattern, mask, sizeof(mask));
  85. return Lists_Add(&My_Classes[Class], mask,
  86. ValidUntil, Reason, false);
  87. }
  88. GLOBAL void
  89. Class_DeleteMask(const int Class, const char *Pattern)
  90. {
  91. char mask[MASK_LEN];
  92. assert(Class < CLASS_COUNT);
  93. assert(Pattern != NULL);
  94. Lists_MakeMask(Pattern, mask, sizeof(mask));
  95. Lists_Del(&My_Classes[Class], mask);
  96. }
  97. GLOBAL struct list_head *
  98. Class_GetList(const int Class)
  99. {
  100. assert(Class < CLASS_COUNT);
  101. return &My_Classes[Class];
  102. }
  103. GLOBAL void
  104. Class_Expire(void)
  105. {
  106. Lists_Expire(&My_Classes[CLASS_GLINE], "G-Line");
  107. Lists_Expire(&My_Classes[CLASS_KLINE], "K-Line");
  108. }
  109. /* -eof- */