summaryrefslogtreecommitdiff
path: root/src/message.c
blob: 716cf8d94056e01789b353bfd9fc71eb913af9c9 (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
/*
 *  ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
 *  motd.c: Message of the day functions.
 *
 *  Copyright (C) 2002 by the past and present ircd coders, and others.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *  USA
 *
 *  $Id: motd.c 1834 2013-04-19 19:50:27Z michael $
 */

#include "stdinc.h"
#include "list.h"
#include "message.h"
#include "ircd.h"
#include "fdlist.h"
#include "s_bsd.h"
#include "conf.h"
#include "send.h"
#include "numeric.h"
#include "client.h"
#include "irc_string.h"
#include "memory.h"
#include "s_serv.h"

/*
** init_message_file
**
*/
void
init_message_file(MessageType msgType, const char *fileName, MessageFile *msg)
{
  strlcpy(msg->fileName, fileName, sizeof(msg->fileName));
  msg->msgType = msgType;
  msg->contentsOfFile = NULL;
}

/*
** send_message_file
**
** This function split off so a server notice could be generated on a
** user requested motd, but not on each connecting client.
*/
int
send_message_file(struct Client *source_p, MessageFile *motdToPrint)
{
  MessageFileLine *linePointer;
  MessageType msgType;
  const char *from, *to;

  if (motdToPrint == NULL)
    return(-1);

  msgType = motdToPrint->msgType;

  from = ID_or_name(&me, source_p->from);
  to = ID_or_name(source_p, source_p->from);

  switch (msgType)
  {
    case USER_LINKS:
      if (motdToPrint->contentsOfFile != NULL)
      {
	for (linePointer = motdToPrint->contentsOfFile; linePointer;
	     linePointer = linePointer->next)
	{
	  sendto_one(source_p, ":%s 364 %s %s", /* XXX */
		     from, to, linePointer->line);
	}
      }
      break;

  case ISSUPPORT:
      if (motdToPrint->contentsOfFile != NULL)
      {
	for (linePointer = motdToPrint->contentsOfFile; linePointer;
	     linePointer = linePointer->next)
	{
	  sendto_one(source_p, form_str(RPL_ISUPPORT),
		     me.name, source_p->name, linePointer->line);
	}
      }
    break;

    default:
      break;
  }

  return(0);
}

/*
 * read_message_file() - original From CoMSTuD, added Aug 29, 1996
 *
 * inputs	- pointer to MessageFileptr
 * output	-
 * side effects	-
 */
int
read_message_file(MessageFile *MessageFileptr)
{
  /* used to clear out old MessageFile entries */
  MessageFileLine *mptr = 0;
  MessageFileLine *next_mptr = 0;

  /* used to add new MessageFile entries */
  MessageFileLine *newMessageLine = 0;
  MessageFileLine *currentMessageLine = 0;

  char buffer[MESSAGELINELEN];
  char *p;
  FILE *file;

  for (mptr = MessageFileptr->contentsOfFile; mptr; mptr = next_mptr)
  {
    next_mptr = mptr->next;
    MyFree(mptr);
  }

  MessageFileptr->contentsOfFile = NULL;

  if ((file = fopen(MessageFileptr->fileName, "r")) == NULL)
    return(-1);

  while (fgets(buffer, sizeof(buffer), file))
  {
    if ((p = strchr(buffer, '\n')) != NULL)
      *p = '\0';

    newMessageLine = (MessageFileLine *)MyMalloc(sizeof(MessageFileLine));
    strlcpy(newMessageLine->line, buffer, sizeof(newMessageLine->line));
    newMessageLine->next = NULL;

    if (MessageFileptr->contentsOfFile != NULL)
    {
      if (currentMessageLine)
        currentMessageLine->next = newMessageLine;

      currentMessageLine = newMessageLine;
    }
    else
    {
      MessageFileptr->contentsOfFile = newMessageLine;
      currentMessageLine = newMessageLine;
    }
  }

  fclose(file);
  return(0);
}

/*
 * init_MessageLine
 *
 * inputs	- NONE
 * output	- pointer to new MessageFile
 * side effects	- Use this when an internal Message File is wanted
 *		  without reading an actual file. The MessageFile 
 *		  is init'ed, but must have content added to it through
 *		  addto_MessageLine()
 */

MessageFile *
init_MessageLine(void)
{
  MessageFile *mf;
  MessageFileLine *mptr = NULL;

  mf = MyMalloc(sizeof(MessageFile));
  mf->msgType = ISSUPPORT;	/* XXX maybe pass it alone in args? */
  mptr = MyMalloc(sizeof(MessageFileLine));
  mf->contentsOfFile = mptr;
  return(mf);
}

/*
 * addto_MessageLine
 *
 * inputs	- Pointer to existing MessageFile
 *		- New string to add to this MessageFile
 * output	- NONE
 * side effects	- Use this when an internal MessageFile is wanted
 *		  without reading an actual file. Content is added
 *		  to this MessageFile through this function.
 */

void
addto_MessageLine(MessageFile *mf, const char *str)
{
  MessageFileLine *mptr = mf->contentsOfFile;
  MessageFileLine *nmptr = NULL;

  if (mptr == NULL)
  {
    mptr = MyMalloc(sizeof(MessageFileLine));
    strcpy(mptr->line, str);
    mf->contentsOfFile = mptr;
  }
  else
  {
    while (mptr->next != NULL)
      mptr = mptr->next;
    nmptr = MyMalloc(sizeof(MessageFileLine));
    strcpy(nmptr->line, str);
    mptr->next = nmptr;
  }
}

/*
 * destroy_MessageLine(MessageFile *mf)
 *
 * inputs	- pointer to the MessageFile to destroy
 * output	- NONE
 * side effects	- All the MessageLines attached to the given mf
 *		  Are freed then one MessageLine is recreated
 */
void
destroy_MessageLine(MessageFile *mf)
{
  MessageFileLine *mptr = mf->contentsOfFile;
  MessageFileLine *nmptr = NULL;

  if (mptr == NULL)
    return;

  for (mptr = mf->contentsOfFile; mptr != NULL; mptr = nmptr)
  {
    nmptr = mptr->next;
    MyFree(mptr);
  } 
  mf->contentsOfFile = NULL;
}