summaryrefslogtreecommitdiff
path: root/functions.c
blob: 0c814a3d184a2350bdcde6d32cdc52bdcd9cc5f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "functions.h"

// Global debug control
extern int debug;
extern char debugpath[PATH_MAX];
extern int background;

// Internal function just to replace nick in server greeting strings
// (as in ":servername 00x oldnick :Blablabla" -> ":servername 00x newnick :Blablabla")
void updategreetingnick(char *greeting, char *greetingnum, char *newnick, char *oldnick) {
  debugprint("updategreetingnick(): '%s' '%s' '%s' '%s'.\n", greeting, greetingnum, newnick, oldnick);

  // Find the position of the old nick in the current greeting
  char searchstr[MAXDATASIZE];
  snprintf(searchstr, MAXDATASIZE, " %s %s :", greetingnum, oldnick);
  char *ret;
  ret = strstr(greeting, searchstr);

  // If ret not found, try again without the colon (e.g. for greeting 004)
  if (ret == NULL) {
    snprintf(searchstr, MAXDATASIZE, " %s %s ", greetingnum, oldnick);
    ret = strstr(greeting, searchstr);
  }

  // Perhaps the new nick is already present (seen for instance when connecting to another bouncer like Miau)
  if (ret == NULL) {
    snprintf(searchstr, MAXDATASIZE, " %s %s ", greetingnum, newnick);
    ret = strstr(greeting, searchstr);
    if (ret != NULL) {
      debugprint("updategreetingnick(): newnick is already present, returning.\n");
      return;
    }
  }

  // If ret *still* not found, abandon ship
  if (ret == NULL) {
    printf("Error updating greeting string, substring not found.  Exiting!\n");
    exit(1);
  }

  int pos = ret - greeting + 5; // +5 for " 001 "

  // Copy the start of the old greeting into a new string
  char greetingtmp[MAXDATASIZE];
  strncpy(greetingtmp, greeting, pos);
  // Terminate it
  greetingtmp[pos] = '\0';

  // Now smash everything (start of old greeting + new nick + remainder of old greeting)
  // together into the new greeting, put in a new temporary string
  char greetingtmp2[MAXDATASIZE];
  if (!snprintf(greetingtmp2, MAXDATASIZE, "%s%s %s", greetingtmp, newnick, greeting + pos + strlen(oldnick) + 1)) {
    fprintf(stderr, "Error while preparing new greeting string!\n");
    exit(1);
  }

  // And finally copy back to source string
  strcpy(greeting, greetingtmp2);

  debugprint("updategreetingnick(): Built new greeting '%s' '%s', length '%ld'.\n", greetingnum, greeting, strlen(greeting));
}

// Print debugging output if enabled
// (To screen or to file depending on settings)
void debugprint(char *format, ...) {
  if (debug == DEBUG_NONE) return;

  va_list args;
  va_start(args, format);

  if (debug == DEBUG_FILE) {
    FILE *fp;
    int bytes = 0;
    fp = fopen(debugpath, "a");
    if ((bytes = vfprintf(fp, format, args)) < 0) {
      printf("error: could not write to debug file.\n");
      exit(1);
    }
    fclose(fp);
  } else if (debug == DEBUG_SCREEN) {
    vprintf(format, args);
  }

  va_end(args);
}

// Get stdin line with buffer overrun protection
int getstdin(char *prompt, char *buff, size_t sz) {
  int ch, extra;

  // Print optional prompt
  if (prompt != NULL) {
    printf ("%s", prompt);
    fflush (stdout);
  }

  // Get the intput from stdin
  if (fgets (buff, sz, stdin) == NULL) {
    return NO_INPUT;
  }

  // If it was too long, there'll be no newline. In that case, we flush
  // to end of line so that excess doesn't affect the next call.
  if (buff[strlen(buff) - 1] != '\n') { // strlen of the actually entered line, not the original array size
    extra = 0;
    while (((ch = getchar()) != '\n') && (ch != EOF)) {
      extra = 1;
    }
    return (extra == 1) ? TOO_LONG : OK;
  }

  // Otherwise remove newline and give string back to caller.
  buff[strlen(buff) - 1] = '\0';
  return OK;
}

// Append CR-LF to the end of a string (after cleaning up any existing trailing CR or LF)
void appendcrlf(char *string) {
  // Make sure it doesn't already end with CR or LF
  // (But only if string is at least two characters long already)
  if (strlen(string) >= 2) {
    while (string[strlen(string) - 1] == '\r' || string[strlen(string) - 1] == '\n') {
      string[strlen(string) - 1] = '\0';
    }
  }

  int startlen = strlen(string);
  string[startlen] = '\r'; // Add CR
  string[startlen + 1] = '\n'; // Add LF
  string[startlen + 2] = '\0'; // Finish with null terminator
}

// Remove leading colon ':' which is the starting character of a prefix in an IRC message
void stripprefix(char *string) {
  // Make a copy to work with
  char string2[strlen(string)];

  debugprint("stripprefix(): starting with '%s', strlen: %zd.\n", string, strlen(string));

  // Don't bother if this isn't a prefix with a leading ':'
  if (string[0] != ':') {
    debugprint("stripprefix(): no leading ':', returning.\n");
    return;
  }

  // Copy the old string into a new one, but...
  for (size_t i = 1; i < strlen(string); i++) {
    string2[i - 1] = string[i];
  }

  // Copy result back to original string
  strncpy(string, string2, strlen(string) - 1);

  // Finish with null terminator
  string[strlen(string) - 1] = '\0';

  debugprint("stripprefix(): finishing with '%s', strlen: %zd.\n", string, strlen(string));
}

// Extract final parameter from IRC message, removing the leading colon ':'
// e.g. given this string:
// ":irc.tghost.co.uk 332 blabounce #test :This is a test topic!"
// We want to end up with:
// "This is a test topic!"
void extractfinalparameter(char *string) {
  // Make a copy to work with
  char string2[strlen(string)];

  // Position of colon
  int colonpos = -1;

  debugprint("extractfinalparameter(): starting with '%s', strlen: %zd.\n", string, strlen(string));

  // Strip the colon at position 0 if there is one
  stripprefix(string);

  // Find the colon
  for (size_t i = 0; i < strlen(string); i++) {
    if (string[i] == ':') {
      debugprint("Found colon at position %zd!\n", i);
      colonpos = i;
      break;
    }
  }

  if (colonpos == -1) {
    debugprint("no colon found, returning\n");
    return;
  }

  // Build a new string starting from the next position after the colon
  int counter = 0;
  for (size_t i = colonpos + 1; i < strlen(string); i++) {
    string2[counter] = string[i];
    counter++;
  }

  // Copy result back to original string
  strncpy(string, string2, counter);

  // Finish with null terminator
  string[counter] = '\0';

  debugprint("extractfinalparameter(): finishing with '%s', strlen: %zd.\n", string, strlen(string));
}

// Extract the IRC nick from a prefix
// e.g. given this string:
// ":foo!bar@baz"
// We want to end up with:
// "foo"
void extractnickfromprefix(char *string) {
  // Position of bang
  int bangpos = -1;

  debugprint("extractnickfromprefix(): starting with '%s', strlen: %zd.\n", string, strlen(string));

  // Strip the colon at position 0 if there is one
  stripprefix(string);

  // Find the bang
  for (size_t i = 0; i < strlen(string); i++) {
    if (string[i] == '!') {
      debugprint("Found bang at position %zd!\n", i);
      bangpos = i;
      break;
    }
  }

  if (bangpos == -1) {
    debugprint("no bang found, returning\n");
    return;
  }

  // Terminate the string at whatever position we found the bang
  string[bangpos] = '\0';

  debugprint("extractnickfromprefix(): finishing with '%s', strlen: %zd.\n", string, strlen(string));
}

// Update an existing nickuserhost string with a new nick
void updatenickuserhost(char *nickuserhost, char *nick) {
  debugprint("updatenickuserhost(): updating '%s' with '%s'.\n", nickuserhost, nick);

  // Position of bang
  int bangpos = -1;

  // Find the bang
  for (size_t i = 0; i < strlen(nickuserhost); i++) {
    if (nickuserhost[i] == '!') {
      debugprint("Found bang at position %ld!\n", i);
      bangpos = i;
      break;
    }
  }

  if (bangpos == -1) {
    debugprint("No bang found in existing nickuserhost, quitting!\n");
    return;
  }

  // Make a new string combining the nick nick and the old nickuserhost + the offset of the bang
  char newstr[MAXDATASIZE];
  snprintf(newstr, MAXDATASIZE, "%s%s", nick, nickuserhost + bangpos);
  newstr[strlen(nickuserhost) - bangpos + strlen(nick)] = '\0';

  // Copy back to source string
  strcpy(nickuserhost, newstr);

  debugprint("updatenickuserhost(): new nickuserhost '%s', length '%ld'.\n", nickuserhost, strlen(nickuserhost));
}

// Update existing greeting strings with a new nickuserhost and new nick
void updategreetings(char *greeting001, char *greeting002, char *greeting003, char *greeting004, char *greeting005a, char *greeting005b, char *greeting005c, char *newnickuserhost, char *oldnickuserhost, char *newnick, char *oldnick) {
  debugprint("updategreetings(): updating greetings with new nickuserhost '%s' and nick '%s'.\n", newnickuserhost, newnick);

  // nickuserhost and greeting001's final component first
  // (final component as in ":servername 001 nick :Blablabla final!com@ponent"

  // Make copies of the nickuserhosts to work with
  char newnickuserhostcpy[MAXDATASIZE];
  strcpy(newnickuserhostcpy, newnickuserhost);
  stripprefix(newnickuserhostcpy);
  char oldnickuserhostcpy[MAXDATASIZE];
  strcpy(oldnickuserhostcpy, oldnickuserhost);
  stripprefix(oldnickuserhostcpy);

  // Find the position of the old nickuserhost in the current greeting 001
  char *ret;
  ret = strstr(greeting001, oldnickuserhostcpy);
  int pos = ret - greeting001;

  // Terminate greeting001 where the nickuserhost begins
  greeting001[pos] = '\0';

  // Stick the new nickuserhost in place in a temporary greeting 001 string
  char greetingtmp[MAXDATASIZE];
  snprintf(greetingtmp, MAXDATASIZE, "%s%s", greeting001, newnickuserhostcpy);

  // Terminate it
  greetingtmp[pos + strlen(newnickuserhostcpy)] = '\0';

  // Copy back to source greeting 001
  strcpy(greeting001, greetingtmp);

  debugprint("updategreetings(): new greeting 001 '%s', length '%ld'.\n", greeting001, strlen(greeting001));

  // Get the new nick

  char newnickcpy[MAXDATASIZE];
  strcpy(newnickcpy, newnick);
  extractnickfromprefix(newnickcpy);

  // greeting nicks next
  // (as in ":servername 00x oldnick :Blablabla" -> ":servername 00x newnick :Blablabla")
  updategreetingnick(greeting001, "001", newnickcpy, oldnick);
  updategreetingnick(greeting002, "002", newnickcpy, oldnick);
  updategreetingnick(greeting003, "003", newnickcpy, oldnick);
  updategreetingnick(greeting004, "004", newnickcpy, oldnick);
  if (greeting005a[0]) {
    updategreetingnick(greeting005a, "005", newnickcpy, oldnick);
  }
  if (greeting005b[0]) {
    updategreetingnick(greeting005b, "005", newnickcpy, oldnick);
  }
  if (greeting005c[0]) {
    updategreetingnick(greeting005c, "005", newnickcpy, oldnick);
  }
}