Thu, 04 Apr 2002 03:04:57 +0000
[gaim-migrate @ 3142]
Upgraded jabber to most recent stable version
| 3127 | 1 | /* -------------------------------------------------------------------------- |
| 2 | * | |
| 3 | * License | |
| 4 | * | |
| 5 | * The contents of this file are subject to the Jabber Open Source License | |
| 6 | * Version 1.0 (the "JOSL"). You may not copy or use this file, in either | |
| 7 | * source code or executable form, except in compliance with the JOSL. You | |
| 8 | * may obtain a copy of the JOSL at http://www.jabber.org/ or at | |
| 9 | * http://www.opensource.org/. | |
| 10 | * | |
| 11 | * Software distributed under the JOSL is distributed on an "AS IS" basis, | |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the JOSL | |
| 13 | * for the specific language governing rights and limitations under the | |
| 14 | * JOSL. | |
| 15 | * | |
| 16 | * Copyrights | |
| 17 | * | |
| 18 | * Portions created by or assigned to Jabber.com, Inc. are | |
| 19 | * Copyright (c) 1999-2002 Jabber.com, Inc. All Rights Reserved. Contact | |
| 20 | * information for Jabber.com, Inc. is available at http://www.jabber.com/. | |
| 21 | * | |
| 22 | * Portions Copyright (c) 1998-1999 Jeremie Miller. | |
| 23 | * | |
| 24 | * Acknowledgements | |
| 25 | * | |
| 26 | * Special thanks to the Jabber Open Source Contributors for their | |
| 27 | * suggestions and support of Jabber. | |
| 28 | * | |
| 29 | * Alternatively, the contents of this file may be used under the terms of the | |
| 30 | * GNU General Public License Version 2 or later (the "GPL"), in which case | |
| 31 | * the provisions of the GPL are applicable instead of those above. If you | |
| 32 | * wish to allow use of your version of this file only under the terms of the | |
| 33 | * GPL and not to allow others to use your version of this file under the JOSL, | |
| 34 | * indicate your decision by deleting the provisions above and replace them | |
| 35 | * with the notice and other provisions required by the GPL. If you do not | |
| 36 | * delete the provisions above, a recipient may use your version of this file | |
| 37 | * under either the JOSL or the GPL. | |
| 38 | * | |
| 39 | * | |
| 40 | * --------------------------------------------------------------------------*/ | |
| 41 | #include "lib.h" | |
| 42 | ||
| 43 | void karma_copy(struct karma *new, struct karma *old) | |
| 44 | { | |
| 45 | new->init = old->init; | |
| 46 | new->val = old->val; | |
| 47 | new->bytes = old->bytes; | |
| 48 | new->max = old->max; | |
| 49 | new->inc = old->inc; | |
| 50 | new->dec = old->dec; | |
| 51 | new->penalty = old->penalty; | |
| 52 | new->restore = old->restore; | |
| 53 | new->last_update = old->last_update; | |
| 54 | new->reset_meter = old->reset_meter; | |
| 55 | } | |
| 56 | ||
| 57 | struct karma *karma_new(pool p) | |
| 58 | { | |
| 59 | struct karma *new; | |
| 60 | if(p == NULL) | |
| 61 | return NULL; | |
| 62 | ||
| 63 | new = pmalloco(p, sizeof(struct karma)); | |
| 64 | new->init = 0; | |
| 65 | new->bytes = 0; | |
| 66 | new->val = KARMA_INIT; | |
| 67 | new->max = KARMA_MAX; | |
| 68 | new->inc = KARMA_INC; | |
| 69 | new->dec = KARMA_DEC; | |
| 70 | new->penalty = KARMA_PENALTY; | |
| 71 | new->restore = KARMA_RESTORE; | |
| 72 | new->last_update = 0; | |
| 73 | new->reset_meter = KARMA_RESETMETER; | |
| 74 | ||
| 75 | return new; | |
| 76 | } | |
| 77 | ||
| 78 | void karma_increment(struct karma *k) | |
| 79 | { | |
| 80 | /* set the current time, and check if we can increment */ | |
| 81 | time_t cur_time = time(NULL); | |
| 82 | int punishment_over = 0; | |
| 83 | ||
| 84 | /* only increment every KARMA_HEARTBEAT seconds */ | |
| 85 | if( ( k->last_update + KARMA_HEARTBEAT > cur_time ) && k->last_update != 0) | |
| 86 | return; | |
| 87 | ||
| 88 | /* if incrementing will raise >= 0 */ | |
| 89 | if( ( k->val < 0 ) && ( k->val + k->inc >= 0 ) ) | |
| 90 | punishment_over = 1; | |
| 91 | ||
| 92 | /* increment the karma value */ | |
| 93 | k->val += k->inc; | |
| 94 | if( k->val > k->max ) k->val = k->max; /* can only be so good */ | |
| 95 | ||
| 96 | /* lower our byte count, if we have good karma */ | |
| 97 | if( k->val > 0 ) k->bytes -= ( KARMA_READ_MAX(k->val) ); | |
| 98 | if( k->bytes < 0 ) k->bytes = 0; | |
| 99 | ||
| 100 | /* our karma has *raised* to 0 */ | |
| 101 | if( punishment_over ) | |
| 102 | /* Set Restore value and clear byte meter */ | |
| 103 | { | |
| 104 | k->val = k->restore; | |
| 105 | /* Total absolution for transgression */ | |
| 106 | if(k->reset_meter) k->bytes = 0; | |
| 107 | } | |
| 108 | ||
| 109 | /* reset out counter */ | |
| 110 | k->last_update = cur_time; | |
| 111 | } | |
| 112 | ||
| 113 | void karma_decrement(struct karma *k, long bytes_read) | |
| 114 | { | |
| 115 | ||
| 116 | /* Increment the bytes read since last since last karma_increment */ | |
| 117 | k->bytes += bytes_read; | |
| 118 | ||
| 119 | /* Check if our byte meter has exceeded the Max bytes our meter is allowed. */ | |
| 120 | ||
| 121 | if(k->bytes > KARMA_READ_MAX(k->val)) | |
| 122 | { | |
| 123 | /* Our meter has exceeded it's allowable lower our karma */ | |
| 124 | k->val -= k->dec; | |
| 125 | ||
| 126 | /* if below zero, set to penalty */ | |
| 127 | if(k->val <= 0) k->val = k->penalty; | |
| 128 | } | |
| 129 | } | |
| 130 | ||
| 131 | /* returns 0 on okay check, 1 on bad check */ | |
| 132 | int karma_check(struct karma *k,long bytes_read) | |
| 133 | { | |
| 134 | /* Check the need to increase or decrease karma */ | |
| 135 | karma_increment(k); | |
| 136 | karma_decrement(k, bytes_read); | |
| 137 | ||
| 138 | /* check its karma */ | |
| 139 | if(k->val <= 0) | |
| 140 | return 1; /* bad */ | |
| 141 | ||
| 142 | /* everything is okay */ | |
| 143 | return 0; | |
| 144 | } |