]>
Commit | Line | Data |
---|---|---|
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 | /** @file | |
20 | * @brief Implementation of timed channel destruction events. | |
21 | * @version $Id: destruct_event.c,v 1.4 2004/12/11 05:13:44 klmitch Exp $ | |
22 | */ | |
23 | #include "config.h" | |
24 | ||
25 | #include "channel.h" /* destruct_channel */ | |
26 | #include "s_debug.h" | |
27 | #include "ircd_alloc.h" | |
28 | #include "ircd.h" | |
29 | #include "ircd_events.h" | |
30 | #include "ircd_log.h" | |
31 | #include "send.h" | |
32 | #include "msg.h" | |
33 | ||
34 | /* #include <assert.h> -- Now using assert in ircd_log.h */ | |
35 | #include <stdlib.h> | |
36 | ||
37 | /** Structure describing a destruction event. */ | |
38 | struct DestructEvent { | |
39 | struct DestructEvent* next_event; /**< Next event in the queue. */ | |
40 | struct DestructEvent* prev_event; /**< Previous event in the queue. */ | |
41 | time_t expires; /**< When the destruction should happen. */ | |
42 | struct Channel* chptr; /**< Channel to destroy. */ | |
43 | }; | |
44 | ||
45 | /** Head of short-delay destruction events. */ | |
46 | static struct DestructEvent* minute_list_top; | |
47 | /** Tail of short-delay destruction events. */ | |
48 | static struct DestructEvent* minute_list_bottom; | |
49 | /** Head of long-delay destruction events. */ | |
50 | static struct DestructEvent* days_list_top; | |
51 | /** Tail of long-delay destruction events. */ | |
52 | static struct DestructEvent* days_list_bottom; | |
53 | ||
54 | /** Schedule a short-delay destruction event for \a chptr. | |
55 | * @param[in] chptr Channel to destroy. | |
56 | */ | |
57 | void schedule_destruct_event_1m(struct Channel* chptr) | |
58 | { | |
59 | struct DestructEvent* new_event; | |
60 | ||
61 | /* Ignore request when we already have a destruct request */ | |
62 | if (chptr->destruct_event) | |
63 | return; | |
64 | ||
65 | /* Create a new destruct event and add it at the top of the list. */ | |
66 | new_event = (struct DestructEvent*)MyMalloc(sizeof(struct DestructEvent)); | |
67 | new_event->expires = TStime() + 60; /* 1 minute from now */ | |
68 | new_event->next_event = NULL; | |
69 | new_event->prev_event = minute_list_top; | |
70 | new_event->chptr = chptr; | |
71 | ||
72 | if (minute_list_top) | |
73 | minute_list_top->next_event = new_event; | |
74 | minute_list_top = new_event; | |
75 | if (!minute_list_bottom) | |
76 | minute_list_bottom = new_event; | |
77 | ||
78 | chptr->destruct_event = new_event; | |
79 | } | |
80 | ||
81 | /** Schedule a long-delay destruction event for \a chptr. | |
82 | * @param[in] chptr Channel to destroy. | |
83 | */ | |
84 | void schedule_destruct_event_48h(struct Channel* chptr) | |
85 | { | |
86 | struct DestructEvent* new_event; | |
87 | ||
88 | /* Ignore request when we already have a destruct request */ | |
89 | if (chptr->destruct_event) | |
90 | return; | |
91 | ||
92 | /* Create a new destruct event and add it at the top of the list. */ | |
93 | new_event = (struct DestructEvent*)MyMalloc(sizeof(struct DestructEvent)); | |
94 | new_event->expires = TStime() + 172800; /* 48 hours from now */ | |
95 | new_event->next_event = NULL; | |
96 | new_event->prev_event = days_list_top; | |
97 | new_event->chptr = chptr; | |
98 | ||
99 | if (days_list_top) | |
100 | days_list_top->next_event = new_event; | |
101 | days_list_top = new_event; | |
102 | if (!days_list_bottom) | |
103 | days_list_bottom = new_event; | |
104 | ||
105 | chptr->destruct_event = new_event; | |
106 | } | |
107 | ||
108 | /** Unlink a destruction event for a channel. | |
109 | * @param[in] chptr Channel that is being destroyed early. | |
110 | */ | |
111 | void remove_destruct_event(struct Channel* chptr) | |
112 | { | |
113 | struct DestructEvent* event = chptr->destruct_event; | |
114 | ||
115 | assert(event != NULL); | |
116 | ||
117 | /* unlink event */ | |
118 | if (event->prev_event) | |
119 | event->prev_event->next_event = event->next_event; | |
120 | if (event->next_event) | |
121 | event->next_event->prev_event = event->prev_event; | |
122 | ||
123 | /* correct top and bottom pointers */ | |
124 | if (days_list_top == event) | |
125 | days_list_top = event->prev_event; | |
126 | if (minute_list_top == event) | |
127 | minute_list_top = event->prev_event; | |
128 | if (days_list_bottom == event) | |
129 | days_list_bottom = event->next_event; | |
130 | if (minute_list_bottom == event) | |
131 | minute_list_bottom = event->next_event; | |
132 | ||
133 | /* Free memory */ | |
134 | MyFree(event); | |
135 | ||
136 | chptr->destruct_event = NULL; | |
137 | } | |
138 | ||
139 | /** Execute expired channel destruction events. | |
140 | * @param[in] ev Expired timer event (ignored). | |
141 | */ | |
142 | void exec_expired_destruct_events(struct Event* ev) | |
143 | { | |
144 | int i = 0; | |
145 | struct DestructEvent** list_bottom; | |
146 | for(list_bottom = &minute_list_bottom; i < 2; ++i, list_bottom = &days_list_bottom) | |
147 | { | |
148 | while (*list_bottom && TStime() >= (*list_bottom)->expires) | |
149 | { | |
150 | struct Channel* chptr = (*list_bottom)->chptr; | |
151 | /* Send DESTRUCT message */ | |
152 | sendcmdto_serv_butone(&me, CMD_DESTRUCT, 0, "%s %Tu", chptr->chname, chptr->creationtime); | |
153 | remove_destruct_event(chptr); | |
154 | destruct_channel(chptr); | |
155 | } | |
156 | } | |
157 | } | |
158 |