| 1 | /*
|
| 2 | * NNTPSwitch common functions
|
| 3 | *
|
| 4 | * $Id: common.c,v 1.28 2007-01-25 15:03:28 mjo Exp $
|
| 5 | */
|
| 6 |
|
| 7 | #include "aconfig.h"
|
| 8 | #include "log.h"
|
| 9 | #include "nntpd.h"
|
| 10 | #include "aprotos.h"
|
| 11 |
|
| 12 | MASTER *master = NULL;
|
| 13 | CLIENT *client = NULL;
|
| 14 |
|
| 15 |
|
| 16 | PROTO void die(const char *str, ...)
|
| 17 | {
|
| 18 | va_list ap;
|
| 19 | char s[1024];
|
| 20 |
|
| 21 | va_start(ap, str);
|
| 22 | vsprintf(s, str, ap);
|
| 23 |
|
| 24 | if ( errno )
|
| 25 | {
|
| 26 | syslog(LOG_ERR, "died: %s (%s)", s, strerror(errno));
|
| 27 | fprintf(stderr, "%d: died: %s (%s)\n", getpid (), s, strerror(errno));
|
| 28 | } else {
|
| 29 | syslog(LOG_ERR, "died: %s", s);
|
| 30 | fprintf(stderr, "%d: died: %s\n", getpid (), s);
|
| 31 | }
|
| 32 |
|
| 33 | va_end(ap);
|
| 34 | syslog_close ();
|
| 35 | exit(1);
|
| 36 | }
|
| 37 |
|
| 38 | PROTO void info(const char *str, ...)
|
| 39 | {
|
| 40 | va_list ap;
|
| 41 | char s[1024];
|
| 42 |
|
| 43 | va_start(ap, str);
|
| 44 | vsnprintf(s, sizeof(s), str, ap);
|
| 45 |
|
| 46 | syslog(LOG_NOTICE, "%s", s);
|
| 47 | fprintf(stderr, "info: %s\n", s);
|
| 48 | va_end(ap);
|
| 49 | }
|
| 50 |
|
| 51 | PROTO void error(const char *str, ...)
|
| 52 | {
|
| 53 | va_list ap;
|
| 54 | char s[1024];
|
| 55 |
|
| 56 | va_start(ap, str);
|
| 57 | vsnprintf(s, sizeof (s), str, ap);
|
| 58 |
|
| 59 | syslog(LOG_ERR, "%s", s);
|
| 60 | fprintf(stderr, "%d: error: %s\n", getpid (), s);
|
| 61 | va_end(ap);
|
| 62 | }
|
| 63 |
|
| 64 | PROTO char* strtolower(char *str)
|
| 65 | {
|
| 66 | int i=0;
|
| 67 | while(str[i])
|
| 68 | {
|
| 69 | str[i] = tolower(str[i]);
|
| 70 | i++;
|
| 71 | }
|
| 72 | return str;
|
| 73 | }
|
| 74 |
|
| 75 |
|
| 76 | /*
|
| 77 | * strip \r\n combinations
|
| 78 | */
|
| 79 | PROTO char* striprn(const char *s)
|
| 80 | {
|
| 81 | char *str, *end;
|
| 82 |
|
| 83 | str = strdup(s);
|
| 84 | end = str;
|
| 85 |
|
| 86 | str += strlen(end) - 1;
|
| 87 | while( *str == '\r' || *str == '\n' )
|
| 88 | {
|
| 89 | *str = '\0';
|
| 90 | str--;
|
| 91 | }
|
| 92 | return end;
|
| 93 | }
|
| 94 |
|
| 95 |
|
| 96 | PROTO int nullstr(const char *s)
|
| 97 | {
|
| 98 | if ( s == NULL ) return 1;
|
| 99 | if ( s[0] == '\0' ) return 1;
|
| 100 | return 0;
|
| 101 | }
|
| 102 |
|
| 103 |
|
| 104 | PROTO void chop(char *buf)
|
| 105 | {
|
| 106 | char *s;
|
| 107 |
|
| 108 | s = buf;
|
| 109 | s += strlen(buf) -1;
|
| 110 | while ( *s == '\r' || *s == '\n' || *s == ' ' )
|
| 111 | {
|
| 112 | *s = '\0';
|
| 113 | s--;
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 |
|
| 118 | /*
|
| 119 | * Strip "pattern" from buffer
|
| 120 | * tommy@news-service.com,*@news-service.com gets tommy
|
| 121 | * news-service@tommy,news-service@* gets tommy
|
| 122 | */
|
| 123 | PROTO char* strippat(char *buf, char *pat)
|
| 124 | {
|
| 125 | char *s;
|
| 126 | int l = strlen(buf)-1;
|
| 127 | int j = strlen(pat)-1;
|
| 128 |
|
| 129 | if ( pat[0] == '*' )
|
| 130 | {
|
| 131 | s = strdup(buf);
|
| 132 |
|
| 133 | while( pat[j] != '*' && l > 0 )
|
| 134 | {
|
| 135 | s[l] = 0;
|
| 136 | j--;
|
| 137 | l--;
|
| 138 | }
|
| 139 | return s;
|
| 140 | }
|
| 141 | else if ( pat[j] == '*' )
|
| 142 | {
|
| 143 | s = strdup(buf);
|
| 144 |
|
| 145 | while( *pat++ != '*' )
|
| 146 | s++;
|
| 147 | return s;
|
| 148 | }
|
| 149 | return NULL;
|
| 150 | }
|
| 151 |
|
| 152 |
|
| 153 | /*
|
| 154 | * checkbuf functions
|
| 155 | * check for \r\n.\r\n end of article in a buffer
|
| 156 | */
|
| 157 | PROTO void checkbuf_init(char *buf)
|
| 158 | {
|
| 159 | buf[0] = '\0';
|
| 160 | buf[1] = '\0';
|
| 161 | buf[2] = '\0';
|
| 162 | buf[3] = '\r';
|
| 163 | buf[4] = '\n';
|
| 164 | }
|
| 165 |
|
| 166 |
|
| 167 | PROTO int checkbuf_isend(char *buf, char *rcvbuf, int bread)
|
| 168 | {
|
| 169 | char chkbuf[5];
|
| 170 | int j, k;
|
| 171 |
|
| 172 | if ( bread < 5 )
|
| 173 | {
|
| 174 | k=0;
|
| 175 |
|
| 176 | /* compose the buf from our checkbuf and received data */
|
| 177 | for (j=0; j<5-bread; j++ )
|
| 178 | chkbuf[k++] = buf[j+bread];
|
| 179 | for (j=0; j<bread; j++)
|
| 180 | chkbuf[k++] = rcvbuf[j];
|
| 181 |
|
| 182 | /* check if it's the end.. */
|
| 183 | if (
|
| 184 | chkbuf[0] == '\r' &&
|
| 185 | chkbuf[1] == '\n' &&
|
| 186 | chkbuf[2] == '.' &&
|
| 187 | chkbuf[3] == '\r' &&
|
| 188 | chkbuf[4] == '\n'
|
| 189 | )
|
| 190 | return 1;
|
| 191 |
|
| 192 | } else {
|
| 193 | /* check if perhaps this is the end already */
|
| 194 | if (
|
| 195 | rcvbuf[bread-5] == '\r' &&
|
| 196 | rcvbuf[bread-4] == '\n' &&
|
| 197 | rcvbuf[bread-3] == '.' &&
|
| 198 | rcvbuf[bread-2] == '\r' &&
|
| 199 | rcvbuf[bread-1] == '\n'
|
| 200 | )
|
| 201 | return 1;
|
| 202 |
|
| 203 | /* copy the last 5 bytes of the receive buf to checkbuf */
|
| 204 | for (j=0; j<5; j++)
|
| 205 | buf[j] = rcvbuf[bread-5+j];
|
| 206 |
|
| 207 | }
|
| 208 | return 0;
|
| 209 | }
|
| 210 |
|
| 211 |
|
| 212 | /*
|
| 213 | * escape ' in strings
|
| 214 | * free() string after use
|
| 215 | */
|
| 216 | PROTO char * str_escape(char *instr)
|
| 217 | {
|
| 218 | char buf[MAX_STRING];
|
| 219 | char *p;
|
| 220 | int i = 0;
|
| 221 |
|
| 222 | for( p=instr; *p!=0; *p++ )
|
| 223 | {
|
| 224 | if ( *p == '\'' )
|
| 225 | buf[i++] = '\\';
|
| 226 |
|
| 227 | if ( *p == '\\' )
|
| 228 | buf[i++] = '\\';
|
| 229 |
|
| 230 | buf[i++] = *p;
|
| 231 | }
|
| 232 |
|
| 233 | buf[i++] = 0;
|
| 234 | return strdup(buf);
|
| 235 | }
|
| 236 |
|