source: message.c @ 7d4fbcd

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 7d4fbcd was 7d4fbcd, checked in by James M. Kretchmar <kretch@mit.edu>, 22 years ago
Initial check in
  • Property mode set to 100644
File size: 16.3 KB
Line 
1#include <zephyr/zephyr.h>
2#include <stdlib.h>
3#include <string.h>
4#include <sys/socket.h>
5#include <netdb.h>
6#include <sys/types.h>
7#include <sys/socket.h>
8#include <netinet/in.h>
9#include <arpa/inet.h>
10#include <time.h>
11#include "owl.h"
12
13void owl_message_create_admin(owl_message *m, char *header, char *text) {
14  char *indent;
15  time_t t;
16
17  m->id=owl_global_get_nextmsgid(&g);
18  m->type=OWL_MESSAGE_TYPE_ADMIN;
19  m->admintype=OWL_MESSAGE_ADMINTYPE_GENERIC;
20  m->delete=0;
21  m->sender=owl_strdup("-owl");
22  m->class=owl_strdup("");
23  m->inst=owl_strdup("");
24  m->recip=owl_strdup("");
25  m->opcode=owl_strdup("");
26  m->realm=owl_strdup("");
27  strcpy(m->hostname, "");
28  m->zwriteline=strdup("");
29
30  /* save the time */
31  t=time(NULL);
32  m->time=owl_strdup(ctime(&t));
33  m->time[strlen(m->time)-1]='\0';
34
35  m->body=owl_strdup(text);
36
37  /* do something to make it clear the notice shouldn't be used for now */
38
39  indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10);
40  owl_text_indent(indent, text, OWL_MSGTAB);
41  owl_fmtext_init_null(&(m->fmtext));
42  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
43  owl_fmtext_append_bold(&(m->fmtext), "OWL ADMIN ");
44  owl_fmtext_append_ztext(&(m->fmtext), header);
45  owl_fmtext_append_normal(&(m->fmtext), "\n");
46  owl_fmtext_append_ztext(&(m->fmtext), indent);
47  if (text[strlen(text)-1]!='\n') {
48    owl_fmtext_append_normal(&(m->fmtext), "\n");
49  }
50
51  owl_free(indent);
52}
53
54void owl_message_create_from_zephyr(owl_message *m, ZNotice_t *n) {
55  struct hostent *hent;
56  int k;
57  char *ptr;
58
59  m->id=owl_global_get_nextmsgid(&g);
60  m->type=OWL_MESSAGE_TYPE_ZEPHYR;
61 
62  /* first save the full notice */
63  memcpy(&(m->notice), n, sizeof(ZNotice_t));
64
65  /* a little gross, we'll reaplace \r's with ' ' for now */
66  owl_zephyr_hackaway_cr(&(m->notice));
67 
68  m->delete=0;
69
70  /* set other info */
71  m->sender=owl_strdup(n->z_sender);
72  m->class=owl_strdup(n->z_class);
73  m->inst=owl_strdup(n->z_class_inst);
74  m->recip=owl_strdup(n->z_recipient);
75  if (n->z_opcode) {
76    m->opcode=owl_strdup(n->z_opcode);
77  } else {
78    n->z_opcode=owl_strdup("");
79  }
80
81  if ((ptr=strchr(n->z_recipient, '@'))!=NULL) {
82    m->realm=owl_strdup(ptr+1);
83  } else {
84    m->realm=owl_strdup(ZGetRealm());
85  }
86
87  m->zwriteline=strdup("");
88
89  ptr=owl_zephyr_get_message(n, &k);
90  m->body=owl_malloc(k+10);
91  memcpy(m->body, ptr, k);
92  m->body[k]='\0';
93
94  /* save the hostname */
95  owl_function_debugmsg("About to do get hostbyaddr");
96  hent=gethostbyaddr((char *) &(n->z_uid.zuid_addr), sizeof(n->z_uid.zuid_addr), AF_INET);
97  if (hent && hent->h_name) {
98    strcpy(m->hostname, hent->h_name);
99  } else {
100    strcpy(m->hostname, inet_ntoa(n->z_sender_addr));
101  }
102
103  /* save the time */
104  m->time=owl_strdup(ctime((time_t *) &n->z_time.tv_sec));
105  m->time[strlen(m->time)-1]='\0';
106
107  /* create the formatted message */
108  if (owl_global_is_config_format(&g)) {
109    _owl_message_make_text_from_config(m);
110  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
111    _owl_message_make_text_from_notice_standard(m);
112  } else {
113    _owl_message_make_text_from_notice_simple(m);
114  }
115
116}
117
118
119void _owl_message_make_text_from_config(owl_message *m) {
120  char *body, *indent;
121 
122  owl_fmtext_init_null(&(m->fmtext));
123
124  /* get body from the config */
125  body=owl_config_getmsg(m, 1);
126 
127  /* indent */
128  indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_TAB+10);
129  owl_text_indent(indent, body, OWL_TAB);
130
131  /* fmtext_append.  This needs to change */
132  owl_fmtext_append_ztext(&(m->fmtext), indent);
133
134  owl_free(indent);
135  owl_free(body);
136}
137
138void _owl_message_make_text_from_notice_standard(owl_message *m) {
139  char *body, *indent, *ptr;
140  char frombuff[1024];
141  char zsigbuff[LINE];
142  ZNotice_t *n;
143  int len;
144
145  /* get the body */
146  n=&(m->notice);
147  ptr=(owl_zephyr_get_message(n, &len));
148  body=owl_malloc(len+20);
149  strncpy(body, ptr, len);
150  body[len]='\0';
151
152  /* add a newline if we need to */
153  if (body[0]!='\0' && body[strlen(body)-1]!='\n') {
154    strcat(body, "\n");
155  }
156
157  /* do the indenting into indent */
158  indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_MSGTAB+10);
159  owl_text_indent(indent, body, OWL_MSGTAB);
160
161  /* edit the from addr for printing */
162  strcpy(frombuff, m->sender);
163  ptr=strchr(frombuff, '@');
164  if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) {
165    *ptr='\0';
166  }
167
168  /* set the message for printing */
169  owl_fmtext_init_null(&(m->fmtext));
170  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
171
172  if (!strcasecmp(n->z_opcode, "ping")) {
173    owl_fmtext_append_bold(&(m->fmtext), "PING");
174    owl_fmtext_append_normal(&(m->fmtext), " from ");
175    owl_fmtext_append_bold(&(m->fmtext), frombuff);
176    owl_fmtext_append_normal(&(m->fmtext), "\n");
177  } else if (!strcasecmp(n->z_class, "login")) {
178    char *ptr, host[LINE], tty[LINE];
179    int len;
180
181    ptr=owl_zephyr_get_field(n, 1, &len);
182    strncpy(host, ptr, len);
183    host[len]='\0';
184    ptr=owl_zephyr_get_field(n, 3, &len);
185    strncpy(tty, ptr, len);
186    tty[len]='\0';
187   
188    if (!strcasecmp(n->z_opcode, "user_login")) {
189      owl_fmtext_append_bold(&(m->fmtext), "LOGIN");
190    } else if (!strcasecmp(n->z_opcode, "user_logout")) {
191      owl_fmtext_append_bold(&(m->fmtext), "LOGOUT");
192    }
193    owl_fmtext_append_normal(&(m->fmtext), " for ");
194    ptr=pretty_sender(n->z_class_inst);
195    owl_fmtext_append_bold(&(m->fmtext), ptr);
196    owl_free(ptr);
197    owl_fmtext_append_normal(&(m->fmtext), " at ");
198    owl_fmtext_append_normal(&(m->fmtext), host);
199    owl_fmtext_append_normal(&(m->fmtext), " ");
200    owl_fmtext_append_normal(&(m->fmtext), tty);
201    owl_fmtext_append_normal(&(m->fmtext), "\n");
202  } else {
203    owl_fmtext_append_normal(&(m->fmtext), m->class);
204    owl_fmtext_append_normal(&(m->fmtext), " / ");
205    owl_fmtext_append_normal(&(m->fmtext), m->inst);
206    owl_fmtext_append_normal(&(m->fmtext), " / ");
207    owl_fmtext_append_bold(&(m->fmtext), frombuff);
208    if (strcasecmp(owl_message_get_realm(m), ZGetRealm())) {
209      owl_fmtext_append_normal(&(m->fmtext), " {");
210      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_realm(m));
211      owl_fmtext_append_normal(&(m->fmtext), "} ");
212    }
213    if (n->z_opcode[0]!='\0') {
214      owl_fmtext_append_normal(&(m->fmtext), " [");
215      owl_fmtext_append_normal(&(m->fmtext), n->z_opcode);
216      owl_fmtext_append_normal(&(m->fmtext), "] ");
217    }
218
219    /* stick on the zsig */
220    _owl_message_get_zsig(m, zsigbuff, LINE);
221    if (zsigbuff[0]!='\0') {
222      owl_fmtext_append_normal(&(m->fmtext), "    (");
223      owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
224      owl_fmtext_append_normal(&(m->fmtext), ")");
225    }
226    owl_fmtext_append_normal(&(m->fmtext), "\n");
227    owl_fmtext_append_ztext(&(m->fmtext), indent);
228
229    /* make personal messages bold for smaat users */
230    if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
231      if (owl_message_is_personal(m)) {
232        owl_fmtext_addattr((&m->fmtext), OWL_FMTEXT_ATTR_BOLD);
233      }
234    }
235  }
236
237  owl_free(body);
238  owl_free(indent);
239}
240
241void _owl_message_make_text_from_notice_simple(owl_message *m) {
242  char *body, *indent, *ptr;
243  char frombuff[1024];
244  char zsigbuff[LINE];
245  ZNotice_t *n;
246  int len;
247
248  /* get the body */
249  n=&(m->notice);
250  ptr=(owl_zephyr_get_message(n, &len));
251  body=owl_malloc(len+20);
252  strncpy(body, ptr, len);
253  body[len]='\0';
254
255  /* add a newline if we need to */
256  if (body[0]!='\0' && body[strlen(body)-1]!='\n') {
257    strcat(body, "\n");
258  }
259
260  /* do the indenting into indent */
261  indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_MSGTAB+10);
262  owl_text_indent(indent, body, OWL_MSGTAB);
263
264  /* edit the from addr for printing */
265  strcpy(frombuff, m->sender);
266  ptr=strchr(frombuff, '@');
267  if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) {
268    *ptr='\0';
269  }
270
271  /* set the message for printing */
272  owl_fmtext_init_null(&(m->fmtext));
273  owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR);
274
275  if (!strcasecmp(n->z_opcode, "ping")) {
276    owl_fmtext_append_bold(&(m->fmtext), "PING");
277    owl_fmtext_append_normal(&(m->fmtext), " from ");
278    owl_fmtext_append_bold(&(m->fmtext), frombuff);
279    owl_fmtext_append_normal(&(m->fmtext), "\n");
280  } else if (!strcasecmp(n->z_class, "login")) {
281    char *ptr, host[LINE], tty[LINE];
282    int len;
283
284    ptr=owl_zephyr_get_field(n, 1, &len);
285    strncpy(host, ptr, len);
286    host[len]='\0';
287    ptr=owl_zephyr_get_field(n, 3, &len);
288    strncpy(tty, ptr, len);
289    tty[len]='\0';
290   
291    if (!strcasecmp(n->z_opcode, "user_login")) {
292      owl_fmtext_append_bold(&(m->fmtext), "LOGIN");
293    } else if (!strcasecmp(n->z_opcode, "user_logout")) {
294      owl_fmtext_append_bold(&(m->fmtext), "LOGOUT");
295    }
296    owl_fmtext_append_normal(&(m->fmtext), " for ");
297    ptr=pretty_sender(n->z_class_inst);
298    owl_fmtext_append_bold(&(m->fmtext), ptr);
299    owl_free(ptr);
300    owl_fmtext_append_normal(&(m->fmtext), " at ");
301    owl_fmtext_append_normal(&(m->fmtext), host);
302    owl_fmtext_append_normal(&(m->fmtext), " ");
303    owl_fmtext_append_normal(&(m->fmtext), tty);
304    owl_fmtext_append_normal(&(m->fmtext), "\n");
305  } else {
306    owl_fmtext_append_normal(&(m->fmtext), "From: ");
307    if (strcasecmp(m->class, "message")) {
308      owl_fmtext_append_normal(&(m->fmtext), "Class ");
309      owl_fmtext_append_normal(&(m->fmtext), m->class);
310      owl_fmtext_append_normal(&(m->fmtext), " / Instance ");
311      owl_fmtext_append_normal(&(m->fmtext), m->inst);
312      owl_fmtext_append_normal(&(m->fmtext), " / ");
313    }
314    owl_fmtext_append_normal(&(m->fmtext), frombuff);
315    if (strcasecmp(owl_message_get_realm(m), ZGetRealm())) {
316      owl_fmtext_append_normal(&(m->fmtext), " {");
317      owl_fmtext_append_normal(&(m->fmtext), owl_message_get_realm(m));
318      owl_fmtext_append_normal(&(m->fmtext), "} ");
319    }
320
321    /* stick on the zsig */
322    _owl_message_get_zsig(m, zsigbuff, LINE);
323    if (zsigbuff[0]!='\0') {
324      owl_fmtext_append_normal(&(m->fmtext), "    (");
325      owl_fmtext_append_ztext(&(m->fmtext), zsigbuff);
326      owl_fmtext_append_normal(&(m->fmtext), ")");
327    }
328    owl_fmtext_append_normal(&(m->fmtext), "\n");
329    owl_fmtext_append_ztext(&(m->fmtext), indent);
330
331    /* make personal messages bold for smaat users */
332    if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
333      if (owl_message_is_personal(m)) {
334        owl_fmtext_addattr((&m->fmtext), OWL_FMTEXT_ATTR_BOLD);
335      }
336    }
337  }
338
339  owl_free(body);
340  owl_free(indent);
341}
342
343void _owl_message_get_zsig(owl_message *m, char *buff, int size) {
344  char *ptr;
345  ZNotice_t n;
346  int len;
347  /* just a hackish thing for now.  We'll only present the first line
348     or the first 'size'. characters.  If the message is not
349     appropriate for having a zsig we'll return an empty string */
350  n=m->notice;
351
352
353  /* bail if it shouldn't have a zsig */
354  buff[0]='\0';
355  if (!strcasecmp(n.z_opcode, "ping")) {
356    return;
357  }
358
359  /* find the right length to copy */
360  len=strlen(n.z_message);
361  if (size < len) {
362    len=size;
363  }
364  if ((ptr=strchr(n.z_message, '\n'))!=NULL) {
365    if ((ptr-n.z_message) < len) {
366      len=ptr-n.z_message;
367    }
368  }
369
370  /* copy */
371  strncpy(buff, n.z_message, len);
372  buff[len]='\0';
373}
374
375
376int owl_message_get_numlines(owl_message *m) {
377  if (m == NULL) return(0);
378  return(owl_fmtext_num_lines(&(m->fmtext)));
379}
380
381
382void owl_message_mark_delete(owl_message *m) {
383  if (m == NULL) return;
384  m->delete=1;
385  /* _owl_message_make_text_from_notice(m); */
386}
387
388
389void owl_message_unmark_delete(owl_message *m) {
390  if (m == NULL) return;
391  m->delete=0;
392}
393
394
395int owl_message_set_admintype(owl_message *m, int admintype) {
396  if (m->type != OWL_MESSAGE_TYPE_ADMIN) return(-1);
397  m->admintype=admintype;
398  return(0);
399}
400
401
402int owl_message_get_admintype(owl_message *m) {
403  return(m->admintype);
404}
405
406void owl_message_set_admin_outgoing(owl_message *m, char *zwriteline) {
407  owl_message_set_admintype(m, OWL_MESSAGE_ADMINTYPE_OUTGOING);
408  if (m->zwriteline) {
409    owl_free(m->zwriteline);
410    m->zwriteline=owl_strdup(zwriteline);
411  }
412}
413
414char *owl_message_get_zwriteline(owl_message *m) {
415  return(m->zwriteline);
416}
417
418int owl_message_is_delete(owl_message *m) {
419  if (m == NULL) return(0);
420  if (m->delete==1) return(1);
421  return(0);
422}
423
424ZNotice_t *owl_message_get_notice(owl_message *m) {
425  return(&(m->notice));
426}
427
428
429void owl_message_free(owl_message *m) {
430  if (owl_message_is_zephyr(m)) {
431    ZFreeNotice(&(m->notice));
432  }
433  if (m->sender) owl_free(m->sender);
434  if (m->recip) owl_free(m->recip);
435  if (m->class) owl_free(m->class);
436  if (m->inst) owl_free(m->inst);
437  if (m->opcode) owl_free(m->opcode);
438  if (m->time) owl_free(m->time);
439  if (m->realm) owl_free(m->realm);
440  if (m->body) owl_free(m->body);
441  if (m->zwriteline) owl_free(m->zwriteline);
442 
443  owl_fmtext_free(&(m->fmtext));
444}
445
446
447char *owl_message_get_hostname(owl_message *m) {
448  return(m->hostname);
449}
450
451
452void owl_message_curs_waddstr(owl_message *m, WINDOW *win, int aline, int bline, int acol, int bcol, int color) {
453  owl_fmtext a, b;
454
455  owl_fmtext_truncate_lines(&(m->fmtext), aline, bline-aline+1, &a);
456  owl_fmtext_truncate_cols(&a, acol, bcol, &b);
457  if (color!=OWL_COLOR_DEFAULT) {
458    owl_fmtext_colorize(&b, color);
459  }
460  owl_fmtext_curs_waddstr(&b, win);
461
462  owl_fmtext_free(&a);
463  owl_fmtext_free(&b);
464}
465
466owl_fmtext *owl_message_get_fmtext(owl_message *m) {
467  return(&(m->fmtext));
468}
469
470void owl_message_set_class(owl_message *m, char *class) {
471  if (m->class) owl_free(m->class);
472  m->class=owl_strdup(class);
473}
474
475
476char *owl_message_get_class(owl_message *m) {
477  return(m->class);
478}
479
480void owl_message_set_instance(owl_message *m, char *inst) {
481  if (m->inst) owl_free(m->inst);
482  m->inst=owl_strdup(inst);
483}
484
485char *owl_message_get_instance(owl_message *m) {
486  return(m->inst);
487}
488
489void owl_message_set_sender(owl_message *m, char *sender) {
490  if (m->sender) owl_free(m->sender);
491  m->sender=owl_strdup(sender);
492}
493
494char *owl_message_get_sender(owl_message *m) {
495  return(m->sender);
496}
497
498void owl_message_set_recipient(owl_message *m, char *recip) {
499  if (m->recip) owl_free(m->recip);
500  m->recip=owl_strdup(recip);
501}
502
503char *owl_message_get_recipient(owl_message *m) {
504  /* this is very, very stupid for outgoing messages, we need to
505     fix that. */
506     
507  if (m->type==OWL_MESSAGE_TYPE_ZEPHYR) {
508    return(m->recip);
509  } else if (m->type==OWL_MESSAGE_TYPE_ADMIN &&
510             m->admintype==OWL_MESSAGE_ADMINTYPE_OUTGOING) {
511    return(m->zwriteline);
512  } else {
513    return(m->recip);
514  }
515}
516
517void owl_message_set_realm(owl_message *m, char *realm) {
518  if (m->realm) owl_free(m->realm);
519  m->realm=owl_strdup(realm);
520}
521
522char *owl_message_get_realm(owl_message *m) {
523  return(m->realm);
524}
525
526void owl_message_set_opcode(owl_message *m, char *opcode) {
527  if (m->opcode) free(m->opcode);
528  m->opcode=owl_strdup(opcode);
529}
530
531char *owl_message_get_opcode(owl_message *m) {
532  return(m->opcode);
533}
534
535char *owl_message_get_timestr(owl_message *m) {
536  return(m->time);
537}
538
539int owl_message_is_admin(owl_message *m) {
540  if (m->type==OWL_MESSAGE_TYPE_ADMIN) return(1);
541  return(0);
542}
543
544int owl_message_is_zephyr(owl_message *m) {
545  if (m->type==OWL_MESSAGE_TYPE_ZEPHYR) return(1);
546  return(0);
547}
548
549char *owl_message_get_text(owl_message *m) {
550  return(owl_fmtext_get_text(&(m->fmtext)));
551}
552
553char *owl_message_get_body(owl_message *m) {
554  return(m->body);
555}
556
557int owl_message_is_personal(owl_message *m) {
558  if (!strcasecmp(owl_message_get_class(m), "message") &&
559      !strcasecmp(owl_message_get_instance(m), "personal") &&
560      !strcmp(owl_message_get_recipient(m), ZGetSender())) {
561    return(1);
562  }
563  return(0);
564}
565
566int owl_message_is_private(owl_message *m) {
567  if (!strcmp(owl_message_get_recipient(m), ZGetSender())) return(1);
568  return(0);
569}
570
571int owl_message_is_mail(owl_message *m) {
572  if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_private(m)) {
573    return(1);
574  }
575  return(0);
576}
577
578int owl_message_is_ping(owl_message *m) {
579  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return(1);
580  return(0);
581}
582
583int owl_message_is_login(owl_message *m) {
584  if (!strcasecmp(owl_message_get_class(m), "login")) return(1);
585  return(0);
586  /* is this good enough? */
587}
588
589int owl_message_is_burningears(owl_message *m) {
590  /* we should add a global to cache the short zsender */
591  char sender[LINE], *ptr;
592
593  /* if the message is from us or to us, it doesn't count */
594  if (!strcasecmp(ZGetSender(), owl_message_get_sender(m))) return(0);
595  if (!strcasecmp(ZGetSender(), owl_message_get_recipient(m))) return(0);
596
597  strcpy(sender, ZGetSender());
598  ptr=strchr(sender, '@');
599  if (ptr) *ptr='\0';
600
601  if (stristr(owl_message_get_body(m), sender)) {
602    return(1);
603  }
604  return(0);
605}
606
607int owl_message_get_id(owl_message *m) {
608  return(m->id);
609}
610                                       
Note: See TracBrowser for help on using the repository browser.