Advertisement
Guest User

join mute unreal

a guest
Jul 21st, 2011
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.27 KB | None | 0 0
  1. /*
  2. * m_joinmute.c adds a new channel mode: +J <seconds> which stops
  3. * users which are not on channel for <seconds> from speaking.
  4. * (C) 2009 Dvlpr
  5. *
  6. * It is my first public module =P
  7. *
  8. * See file AUTHORS in IRC package for additional names of
  9. * the programmers.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 1, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25.  
  26. #include "config.h"
  27. #include "struct.h"
  28. #include "common.h"
  29. #include "sys.h"
  30. #include "numeric.h"
  31. #include "msg.h"
  32. #include "channel.h"
  33. #include <time.h>
  34. #include <sys/stat.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #ifdef _WIN32
  39. #include <io.h>
  40. #endif
  41. #include <fcntl.h>
  42. #include "h.h"
  43. #ifdef _WIN32
  44. #include "version.h"
  45. #endif
  46.  
  47. #define DelHook(x) if (x) HookDel(x); x = NULL
  48. Hook *HookJoin = NULL;
  49. Hook *HookPart = NULL;
  50. Hook *HookMessage = NULL;
  51. Hook *HookQuit = NULL;
  52. Hook *HookKick = NULL;
  53. static ModuleInfo *thismodinfo = NULL;
  54. int cb_part(aClient *cptr, aClient *sptr, aChannel *chptr, char *parv[]);
  55. int cb_join(aClient *cptr, aClient *sptr, aChannel *chptr, char *parv[]);
  56. int cb_quit(aClient *sptr, char *comment);
  57. int cb_kick(aClient *cptr, aClient *sptr, aClient *who, aChannel *chptr, char *comment);
  58. DLLFUNC char *cb_msg(aClient *, aClient *, aChannel *, char *, int);
  59. #ifdef HOOKTYPE_REHASH_COMPLETE
  60. static int cb_rehash_complete();
  61. Hook *HookRehashDone;
  62. #endif
  63. void add_user_to_memory(aClient *sptr, aChannel *chptr);
  64.  
  65.  
  66. typedef struct _channels_users_
  67. {
  68. struct _channel_users *prev, *next;
  69. aClient *source;
  70. aChannel *chptr;
  71. int joined_since;
  72. } UsersM;
  73.  
  74. UsersM *muted_users; /* List of data */
  75. UsersM *FindUserInMemory(aClient *sptr, aChannel *chptr);
  76. void del_user_from_memory(UsersM *u);
  77. Cmode_t EXTCMODE_JOINMUTE = 0L;
  78.  
  79. typedef struct {
  80. EXTCM_PAR_HEADER
  81. int seconds;
  82. } JoinMute;
  83.  
  84. int modeJ_is_ok(aClient *sptr, aChannel *chptr, char *para, int checkt, int what);
  85. CmodeParam * modeJ_put_param(CmodeParam *lst, char *para);
  86. char *modeJ_get_param(CmodeParam *lst);
  87. char *modeJ_conv_param(char *param);
  88. void modeJ_free_param(CmodeParam *lst);
  89. CmodeParam *modeJ_dup_struct(CmodeParam *src);
  90. int modeJ_sjoin_check(aChannel *chptr, CmodeParam *ourx, CmodeParam *theirx);
  91.  
  92. ModuleHeader MOD_HEADER(m_joinmute)
  93. = {
  94. "m_joinmute",
  95. "$Id: m_joinmute.c,v 0.1 2009/04/16 14:14 $",
  96. "Adds +J chmode: Join Mute for X seconds",
  97. "3.2-b8-1",
  98. NULL
  99. };
  100.  
  101. DLLFUNC int MOD_TEST(m_joinmute)(ModuleInfo *modinfo)
  102. {
  103.  
  104. return MOD_SUCCESS;
  105. }
  106.  
  107. DLLFUNC int MOD_INIT(m_joinmute)(ModuleInfo *modinfo)
  108. {
  109. CmodeInfo req;
  110. thismodinfo = modinfo;
  111. ModuleSetOptions(modinfo->handle, MOD_OPT_PERM);
  112. memset(&req, 0, sizeof(req));
  113. req.paracount = 1;
  114. req.flag = 'J';
  115. req.is_ok = modeJ_is_ok;
  116. req.put_param = modeJ_put_param;
  117. req.get_param = modeJ_get_param;
  118. req.conv_param = modeJ_conv_param;
  119. req.free_param = modeJ_free_param;
  120. req.dup_struct = modeJ_dup_struct;
  121. req.sjoin_check = modeJ_sjoin_check;
  122. CmodeAdd(thismodinfo->handle, req, &EXTCMODE_JOINMUTE);
  123. return MOD_SUCCESS;
  124. }
  125.  
  126. DLLFUNC int MOD_LOAD(m_joinmute)(int module_load)
  127. {
  128. HookJoin = HookAddEx(thismodinfo->handle, HOOKTYPE_LOCAL_JOIN, cb_join);
  129. HookPart = HookAddEx(thismodinfo->handle, HOOKTYPE_LOCAL_PART, cb_part);
  130. HookMessage = HookAddPCharEx(thismodinfo->handle, HOOKTYPE_CHANMSG, cb_msg);
  131. HookQuit = HookAddEx(thismodinfo->handle, HOOKTYPE_LOCAL_QUIT, cb_quit);
  132. HookKick = HookAddEx(thismodinfo->handle, HOOKTYPE_LOCAL_KICK, cb_kick);
  133.  
  134. #ifdef HOOKTYPE_REHASH_COMPLETE
  135. HookRehashDone = HookAddEx(thismodinfo->handle, HOOKTYPE_REHASH_COMPLETE, cb_rehash_complete);
  136. #endif
  137.  
  138. return MOD_SUCCESS;
  139. }
  140.  
  141. DLLFUNC int MOD_UNLOAD(m_joinmute)(int module_unload)
  142. {
  143. DelHook(HookJoin);
  144. DelHook(HookPart);
  145. DelHook(HookMessage);
  146. DelHook(HookQuit);
  147.  
  148. #ifdef HOOKTYPE_REHASH_COMPLETE
  149. DelHook(HookRehashDone);
  150. #endif
  151.  
  152. return MOD_SUCCESS;
  153. }
  154.  
  155. int modeJ_is_ok(aClient *sptr, aChannel *chptr, char *para, int checkt, int what)
  156. {
  157. int seconds;
  158. if(what == MODE_ADD)
  159. seconds = atoi(para);
  160. if ((checkt == EXCHK_ACCESS) || (checkt == EXCHK_ACCESS_ERR))
  161. {
  162. if (IsPerson(sptr) && is_chan_op(sptr, chptr))
  163. return EX_ALLOW;
  164. if (checkt == EXCHK_ACCESS_ERR) /* can only be due to being halfop */
  165. sendto_one(sptr, err_str(ERR_NOTFORHALFOPS), me.name, sptr->name, 'J');
  166. return EX_DENY;
  167. }
  168. else if (checkt == EXCHK_PARAM)
  169. {
  170. if(seconds < 0 || seconds > 65536)
  171. {
  172. sendnotice(sptr, "*** [ERROR] Seconds value is out of range!");
  173. return EX_DENY;
  174. }
  175. return EX_ALLOW;
  176. }
  177. return 0;
  178. }
  179.  
  180. CmodeParam * modeJ_put_param(CmodeParam *r_in, char *param)
  181. {
  182. JoinMute *r = (JoinMute *)r_in;
  183. int seconds = atoi(param);
  184.  
  185. if (!r)
  186. {
  187. /* Need to create one */
  188. r = (JoinMute *)malloc(sizeof(JoinMute));
  189. memset(r, 0, sizeof(JoinMute));
  190. r->flag = 'J';
  191. }
  192. if(seconds < 0 || seconds > 65536)
  193. seconds = 0;
  194. r->seconds = seconds;
  195. return (CmodeParam *)r;
  196. }
  197.  
  198. char *modeJ_get_param(CmodeParam *r_in)
  199. {
  200. JoinMute *r = (JoinMute *)r_in;
  201. static char retbuf[16];
  202.  
  203. if (!r)
  204. return NULL;
  205.  
  206. snprintf(retbuf, sizeof(retbuf), "%d", r->seconds);
  207. return retbuf;
  208. }
  209.  
  210. char *modeJ_conv_param(char *param_in)
  211. {
  212. static char retbuf[32];
  213. int num = atoi(param_in);
  214.  
  215. if(num < 0)
  216. num = 0;
  217. else if(num > 65536)
  218. num = 255;
  219. else if(!num)
  220. num = 0;
  221.  
  222. snprintf(retbuf, sizeof(retbuf), "%d", num);
  223. return retbuf;
  224. }
  225.  
  226. void modeJ_free_param(CmodeParam *r)
  227. {
  228. JoinMute *n = (JoinMute *)r;
  229. free(n);
  230. return;
  231. }
  232.  
  233. CmodeParam *modeJ_dup_struct(CmodeParam *r_in)
  234. {
  235. JoinMute *n = (JoinMute *)malloc(sizeof(JoinMute));
  236. memcpy(n, r_in, sizeof(JoinMute));
  237. return (CmodeParam *)n;
  238. }
  239.  
  240. int modeJ_sjoin_check(aChannel *chptr, CmodeParam *ourx, CmodeParam *theirx)
  241. {
  242. JoinMute *our = (JoinMute *)ourx;
  243. JoinMute *their = (JoinMute *)theirx;
  244. if (our->seconds == their->seconds)
  245. return EXSJ_SAME;
  246. if (our->seconds > their->seconds) /* Server with more seconds winning */
  247. return EXSJ_WEWON;
  248. else
  249. return EXSJ_THEYWON;
  250. }
  251.  
  252. /** add_user_to_memory
  253. * Adds user to currently muted users list
  254. * Allocates the memory for data
  255. */
  256.  
  257. void add_user_to_memory(aClient *sptr, aChannel *chptr)
  258. {
  259. UsersM *u = (UsersM *) MyMallocEx(sizeof(UsersM));
  260. if(!u)
  261. return;
  262. u->chptr = chptr;
  263. u->source = sptr;
  264. u->joined_since = TStime();
  265. AddListItem(u, muted_users);
  266. return;
  267. }
  268.  
  269. /** del_user_from_memory
  270. * Removes user from currently muted users list
  271. * Frees the memory allocated for data
  272. */
  273.  
  274. void del_user_from_memory(UsersM *u)
  275. {
  276. if(!u)
  277. return;
  278. DelListItem(u, muted_users);
  279. MyFree(u);
  280. return;
  281. }
  282.  
  283. /** FindUserInMemory
  284. * Scans through currently muted users
  285. * to find matching one, if the user is found
  286. * its pointer will be returned instantly
  287. */
  288. UsersM *FindUserInMemory(aClient *sptr, aChannel *chptr)
  289. {
  290. UsersM *uz;
  291. for(uz = muted_users; uz; uz = (UsersM *)uz->next)
  292. {
  293. if(!find_client(uz->source->name, NULL))
  294. {
  295. del_user_from_memory(uz);
  296. continue;
  297. }
  298. if(!IsMember(uz->source, uz->chptr))
  299. {
  300. del_user_from_memory(uz);
  301. continue;
  302. }
  303. if(!(uz->chptr->mode.extmode & EXTCMODE_JOINMUTE))
  304. {
  305. del_user_from_memory(uz);
  306. continue;
  307. }
  308. if((uz->source == sptr) && (uz->chptr == chptr))
  309. return uz;
  310. }
  311. return NULL;
  312. }
  313.  
  314.  
  315. /* Used on quit - just remove all entries with sptr as a user */
  316. void clear_matching_entries(aClient *sptr)
  317. {
  318. UsersM *userz;
  319. for(userz = muted_users; userz; userz = (UsersM *)userz->next)
  320. {
  321. if(userz->source == sptr)
  322. del_user_from_memory(userz);
  323. }
  324. }
  325.  
  326.  
  327. DLLFUNC char *cb_msg(aClient *cptr, aClient *sptr, aChannel *chptr, char *text, int notice)
  328. {
  329. JoinMute *para = (JoinMute *)extcmode_get_struct(chptr->mode.extmodeparam, 'J');
  330. UsersM *u;
  331. if(!MyClient(sptr))
  332. return text;
  333. u = FindUserInMemory(sptr, chptr); /* Let's find entry in muted users list :) */
  334. /* hops/vops/ops are allowed to send it... */
  335. if(is_chan_op(sptr, chptr) || is_half_op(sptr, chptr) || has_voice(sptr, chptr) || IsAnOper(sptr))
  336. return text;
  337. if(!(chptr->mode.extmode & EXTCMODE_JOINMUTE))
  338. return text;
  339. if(!u)
  340. return text; /* Not found - let it be sent! -- Dvlpr */
  341. if((TStime() - u->joined_since) > para->seconds) /* How we know if user allowed? we just check if current time - time when user
  342. * joined the channel is more then seconds and if yes, we allow sending text + deleting
  343. * user's data from muted users list */
  344. {
  345. del_user_from_memory(u);
  346. return text;
  347. }
  348. /* 404! :] -- Dvlpr */
  349. sendto_one(sptr, ":%s 404 %s %s :Cannot send to channel: you must wait %d seconds after joining"
  350. " to speak", me.name, sptr->name, chptr->chname, para->seconds);
  351. return NULL;
  352.  
  353. }
  354.  
  355. int cb_join(aClient *cptr, aClient *sptr, aChannel *chptr, char *parv[])
  356. {
  357. add_user_to_memory(sptr, chptr);
  358. return HOOK_CONTINUE;
  359. }
  360.  
  361. int cb_part(aClient *cptr, aClient *sptr, aChannel *chptr, char *parv[])
  362. {
  363. UsersM *u = FindUserInMemory(sptr, chptr);
  364. if(!u)
  365. return HOOK_CONTINUE;
  366. del_user_from_memory(u);
  367.  
  368. return HOOK_CONTINUE;
  369. }
  370.  
  371. int cb_quit(aClient *sptr, char *comment)
  372. {
  373. clear_matching_entries(sptr);
  374. return HOOK_CONTINUE;
  375. }
  376.  
  377. int cb_kick(aClient *cptr, aClient *sptr, aClient *who, aChannel *chptr, char *comment)
  378. {
  379. UsersM *u = FindUserInMemory(who, chptr);
  380. if(!u)
  381. return HOOK_CONTINUE;
  382. del_user_from_memory(u);
  383.  
  384. return HOOK_CONTINUE;
  385. }
  386.  
  387.  
  388. #ifdef HOOKTYPE_REHASH_COMPLETE
  389. static int cb_rehash_complete()
  390. {
  391. HookJoin = HookAddEx(thismodinfo->handle, HOOKTYPE_LOCAL_JOIN, cb_join);
  392. HookPart = HookAddEx(thismodinfo->handle, HOOKTYPE_LOCAL_PART, cb_part);
  393. HookMessage = HookAddPCharEx(thismodinfo->handle, HOOKTYPE_CHANMSG, cb_msg);
  394. HookQuit = HookAddEx(thismodinfo->handle, HOOKTYPE_LOCAL_QUIT, cb_quit);
  395. HookKick = HookAddEx(thismodinfo->handle, HOOKTYPE_LOCAL_KICK, cb_kick);
  396. return HOOK_CONTINUE;
  397. }
  398. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement