| 1 |
/* Ignore handling. |
| 2 |
* |
| 3 |
* IRC Services is copyright (c) 1996-2009 Andrew Church. |
| 4 |
* E-mail: <achurch@achurch.org> |
| 5 |
* Parts written by Andrew Kempe and others. |
| 6 |
* This program is free but copyrighted software; see the file GPL.txt for |
| 7 |
* details. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "services.h" |
| 11 |
|
| 12 |
/*************************************************************************/ |
| 13 |
|
| 14 |
/* ignore_init: Initialize ignore-related fields of a new User structure. */ |
| 15 |
|
| 16 |
void ignore_init(User *u) |
| 17 |
{ |
| 18 |
u->ignore = 0; |
| 19 |
u->lastcmd = time_msec(); |
| 20 |
u->lastcmd_s = time(NULL); |
| 21 |
} |
| 22 |
|
| 23 |
/*************************************************************************/ |
| 24 |
|
| 25 |
/* ignore_update: Update the user's ignore level. The "ignore level" is a |
| 26 |
* measure of how much time Services has spent processing |
| 27 |
* this user's commands, and is calculated as a decaying |
| 28 |
* average of the fraction of time spent on the user's |
| 29 |
* commands over the total time Services has been running-- |
| 30 |
* more specifically, the average over time of a function |
| 31 |
* whose value is 1 when Services is executing a command |
| 32 |
* from the user and 0 at all other times. The rate of |
| 33 |
* decay of the average, and hence the rate of response to |
| 34 |
* new commands, is dependent on the IgnoreDecay |
| 35 |
* configuration setting: the average decays by half every |
| 36 |
* IgnoreDecay milliseconds (note that the value is given as |
| 37 |
* seconds in the configuration file). |
| 38 |
* The `msec' parameter to this function is the length |
| 39 |
* of time taken by the most recent command (which caused |
| 40 |
* this update). `msec' may be specified as zero to update |
| 41 |
* the user's ignore level at any time. |
| 42 |
*/ |
| 43 |
|
| 44 |
void ignore_update(User *u, uint32 msec) |
| 45 |
{ |
| 46 |
time_t now; |
| 47 |
uint32 now_msec; |
| 48 |
|
| 49 |
if (!IgnoreDecay) { |
| 50 |
/* Ignore code disabled, just return */ |
| 51 |
return; |
| 52 |
} |
| 53 |
now = time(NULL); |
| 54 |
now_msec = time_msec(); |
| 55 |
if (now - u->lastcmd_s > 1000000) { |
| 56 |
/* Millisecond counter may have overflowed, just reset to 0 */ |
| 57 |
u->ignore = 0; |
| 58 |
} else { |
| 59 |
double zerolen = (now_msec - msec) - u->lastcmd; |
| 60 |
if (zerolen > 0) |
| 61 |
u->ignore *= pow(2, -(zerolen / IgnoreDecay)); |
| 62 |
} |
| 63 |
if (msec) { |
| 64 |
double factor; |
| 65 |
while (msec > IgnoreDecay) { |
| 66 |
u->ignore = u->ignore*0.5 + 0.5; |
| 67 |
msec -= IgnoreDecay; |
| 68 |
} |
| 69 |
factor = pow(2, -((double)msec / IgnoreDecay)); |
| 70 |
u->ignore = u->ignore*factor + (1-factor); |
| 71 |
} |
| 72 |
u->lastcmd = now_msec; |
| 73 |
u->lastcmd_s = now; |
| 74 |
} |
| 75 |
|
| 76 |
/*************************************************************************/ |
| 77 |
|
| 78 |
/* |
| 79 |
* Local variables: |
| 80 |
* c-file-style: "stroustrup" |
| 81 |
* c-file-offsets: ((case-label . *) (statement-case-intro . *)) |
| 82 |
* indent-tabs-mode: nil |
| 83 |
* End: |
| 84 |
* |
| 85 |
* vim: expandtab shiftwidth=4: |
| 86 |
*/ |