Author: Carlo Wood (via Isomer <isomer@coders.net>)
[ircu2.10.12-pk.git] / ircd / destruct_event.c
1 /*
2  * IRC - Internet Relay Chat, ircd/destruct_event.c
3  * Copyright (C) 2002 Carlo Wood <carlo@alinoe.com>
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 1, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * $Id$
20  */
21 #include "config.h"
22
23 #include "channel.h"    /* destruct_channel */
24 #include "s_debug.h"
25 #include "ircd_alloc.h"
26 #include "ircd.h"
27 #include "ircd_events.h"
28
29 #include <assert.h>
30 #include <stdlib.h>
31
32 struct DestructEvent {
33   struct DestructEvent* next_event;
34   struct DestructEvent* prev_event;
35   time_t expires;
36   struct Channel* chptr;
37 };
38
39 static struct DestructEvent* minute_list_top;
40 static struct DestructEvent* minute_list_bottom;
41 static struct DestructEvent* days_list_top;
42 static struct DestructEvent* days_list_bottom;
43
44 void schedule_destruct_event_1m(struct Channel* chptr)
45 {
46   struct DestructEvent* new_event;
47
48   /* Ignore request when we already have a destruct request */
49   if (chptr->destruct_event)
50     return;
51
52   /* Create a new destruct event and add it at the top of the list. */
53   new_event = (struct DestructEvent*)MyMalloc(sizeof(struct DestructEvent));
54   new_event->expires = TStime() + 60;   /* 1 minute from now */
55   new_event->next_event = NULL;
56   new_event->prev_event = minute_list_top;
57   new_event->chptr = chptr;
58
59   if (minute_list_top)
60     minute_list_top->next_event = new_event;
61   minute_list_top = new_event;
62   if (!minute_list_bottom)
63     minute_list_bottom = new_event;
64
65   chptr->destruct_event = new_event;
66 }
67
68 void schedule_destruct_event_48h(struct Channel* chptr)
69 {
70   struct DestructEvent* new_event;
71
72   /* Ignore request when we already have a destruct request */
73   if (chptr->destruct_event)
74     return;
75
76   /* Create a new destruct event and add it at the top of the list. */
77   new_event = (struct DestructEvent*)MyMalloc(sizeof(struct DestructEvent));
78   new_event->expires = TStime() + 172800;       /* 48 hours from now */
79   new_event->next_event = NULL;
80   new_event->prev_event = days_list_top;
81   new_event->chptr = chptr;
82
83   if (days_list_top)
84     days_list_top->next_event = new_event;
85   days_list_top = new_event;
86   if (!days_list_bottom)
87     days_list_bottom = new_event;
88
89   chptr->destruct_event = new_event;
90 }
91
92 void remove_destruct_event(struct Channel* chptr)
93 {
94   struct DestructEvent* event = chptr->destruct_event;
95
96   assert(event != NULL);
97
98   /* unlink event */
99   if (event->prev_event)
100     event->prev_event->next_event = event->next_event;
101   if (event->next_event)
102     event->next_event->prev_event = event->prev_event;
103
104   /* correct top and bottom pointers */
105   if (days_list_top == event)
106     days_list_top = event->prev_event;
107   if (minute_list_top == event)
108     minute_list_top = event->prev_event;
109   if (days_list_bottom == event)
110     days_list_bottom = event->next_event;
111   if (minute_list_bottom == event)
112     minute_list_bottom = event->next_event;
113
114   /* Free memory */
115   MyFree(event);
116
117   chptr->destruct_event = NULL;
118 }
119
120 void exec_expired_destruct_events(struct Event* ev)
121 {
122   int i = 0;
123   struct DestructEvent** list_bottom;
124   for(list_bottom = &minute_list_bottom; i < 2; ++i, list_bottom = &days_list_bottom)
125   {
126     while (*list_bottom && TStime() >= (*list_bottom)->expires)
127     {
128       struct Channel* chptr = (*list_bottom)->chptr;
129       remove_destruct_event(chptr);
130       destruct_channel(chptr);
131     }
132   }
133 }
134