Author: Carlo Wood (run@alinoe.com> (Via Isomer>
[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 #include "send.h"
29 #include "msg.h"
30
31 #include <assert.h>
32 #include <stdlib.h>
33
34 struct DestructEvent {
35   struct DestructEvent* next_event;
36   struct DestructEvent* prev_event;
37   time_t expires;
38   struct Channel* chptr;
39 };
40
41 static struct DestructEvent* minute_list_top;
42 static struct DestructEvent* minute_list_bottom;
43 static struct DestructEvent* days_list_top;
44 static struct DestructEvent* days_list_bottom;
45
46 void schedule_destruct_event_1m(struct Channel* chptr)
47 {
48   struct DestructEvent* new_event;
49
50   /* Ignore request when we already have a destruct request */
51   if (chptr->destruct_event)
52     return;
53
54   /* Create a new destruct event and add it at the top of the list. */
55   new_event = (struct DestructEvent*)MyMalloc(sizeof(struct DestructEvent));
56   new_event->expires = TStime() + 60;   /* 1 minute from now */
57   new_event->next_event = NULL;
58   new_event->prev_event = minute_list_top;
59   new_event->chptr = chptr;
60
61   if (minute_list_top)
62     minute_list_top->next_event = new_event;
63   minute_list_top = new_event;
64   if (!minute_list_bottom)
65     minute_list_bottom = new_event;
66
67   chptr->destruct_event = new_event;
68 }
69
70 void schedule_destruct_event_48h(struct Channel* chptr)
71 {
72   struct DestructEvent* new_event;
73
74   /* Ignore request when we already have a destruct request */
75   if (chptr->destruct_event)
76     return;
77
78   /* Create a new destruct event and add it at the top of the list. */
79   new_event = (struct DestructEvent*)MyMalloc(sizeof(struct DestructEvent));
80   new_event->expires = TStime() + 172800;       /* 48 hours from now */
81   new_event->next_event = NULL;
82   new_event->prev_event = days_list_top;
83   new_event->chptr = chptr;
84
85   if (days_list_top)
86     days_list_top->next_event = new_event;
87   days_list_top = new_event;
88   if (!days_list_bottom)
89     days_list_bottom = new_event;
90
91   chptr->destruct_event = new_event;
92 }
93
94 void remove_destruct_event(struct Channel* chptr)
95 {
96   struct DestructEvent* event = chptr->destruct_event;
97
98   assert(event != NULL);
99
100   /* unlink event */
101   if (event->prev_event)
102     event->prev_event->next_event = event->next_event;
103   if (event->next_event)
104     event->next_event->prev_event = event->prev_event;
105
106   /* correct top and bottom pointers */
107   if (days_list_top == event)
108     days_list_top = event->prev_event;
109   if (minute_list_top == event)
110     minute_list_top = event->prev_event;
111   if (days_list_bottom == event)
112     days_list_bottom = event->next_event;
113   if (minute_list_bottom == event)
114     minute_list_bottom = event->next_event;
115
116   /* Free memory */
117   MyFree(event);
118
119   chptr->destruct_event = NULL;
120 }
121
122 void exec_expired_destruct_events(struct Event* ev)
123 {
124   int i = 0;
125   struct DestructEvent** list_bottom;
126   for(list_bottom = &minute_list_bottom; i < 2; ++i, list_bottom = &days_list_bottom)
127   {
128     while (*list_bottom && TStime() >= (*list_bottom)->expires)
129     {
130       struct Channel* chptr = (*list_bottom)->chptr;
131       /* Send DESTRUCT message */
132       sendcmdto_serv_butone(&me, CMD_DESTRUCT, 0, "%s %Tu", chptr->chname, chptr->creationtime);
133       remove_destruct_event(chptr);
134       destruct_channel(chptr);
135     }
136   }
137 }
138