summaryrefslogtreecommitdiff
path: root/functions.c
blob: c6bea406b9cda28625302c42190e5c7072239d8d (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
#include "functions.h"

// Print a debugging message, if debugging enabled
int debugmode;
void debug(char *string) {
  if (debugmode) {
    printf("DEBUG: %s\n", string);
  }
}

// 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
  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)];

  printf("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] != ':') {
    printf("stripprefix(): no leading ':', returning.\n");
    return;
  }

  // Copy the old string into a new one, but...
  for (size_t i = 1; i < strlen(string); i++) {
    printf("i: %zd.  strlen: %zd.\n", i, strlen(string));
    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';

  printf("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;

  printf("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++) {
    printf("i: %zd.  strlen: %zd.\n", i, strlen(string));
    if (string[i] == ':') {
      printf("Found colon at position %zd!\n", i);
      colonpos = i;
      break;
    }
  }

  if (colonpos == -1) {
    printf("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++) {
    printf("i: %zd.  strlen: %zd.\n", i, strlen(string));
    string2[counter] = string[i];
    counter++;
  }

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

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

  printf("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;

  printf("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++) {
    printf("i: %zd.  strlen: %zd.\n", i, strlen(string));
    if (string[i] == '!') {
      printf("Found bang at position %zd!\n", i);
      bangpos = i;
      break;
    }
  }

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

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

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