summaryrefslogtreecommitdiff
path: root/testing/toxic/chat.c
blob: 19f5c9724735d9742f882585b136f7553d95c930 (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
/*
 * Toxic -- Tox Curses Client
 */

#include <curses.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <time.h>

#include "../../core/Messenger.h"
#include "../../core/network.h"

#include "windows.h"

typedef struct {
  int friendnum;

  char line[256];
  size_t pos;

  WINDOW* history;
  WINDOW* linewin;

} ChatContext;

extern void fix_name(uint8_t* name);

static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) {
  ChatContext* ctx = (ChatContext*) self->x;
  uint8_t nick[MAX_NAME_LENGTH] = {0};

  time_t now;
  time(&now);
  struct tm * timeinfo;
  timeinfo = localtime(&now);

  if(ctx->friendnum != num)
    return;

  getname(num, (uint8_t*) &nick);

  msg[len-1] = '\0';
  nick[MAX_NAME_LENGTH-1] = '\0';

  fix_name(msg);
  fix_name(nick);

  int inthour = timeinfo->tm_hour;
  int intmin = timeinfo->tm_min;
  char min[2];
  char hour[2];
  sprintf(hour,"%d",inthour);
  if (intmin < 10) {
    sprintf(min,"0%d",intmin);
  } else {
    sprintf(min,"%d",intmin);
  }


  wattron(ctx->history, COLOR_PAIR(2));
  wprintw(ctx->history,"%s",hour);
  wprintw(ctx->history,":%s  ",min);
  wattron(ctx->history, COLOR_PAIR(4));
  wprintw(ctx->history, "%s: ", nick);
  wattroff(ctx->history, COLOR_PAIR(4));
  wprintw(ctx->history, "%s\n", msg);

  self->blink = true;
}

static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t len) {
  ChatContext* ctx = (ChatContext*) self->x;

  if(ctx->friendnum != num)
    return;

  nick[len-1] = '\0';
  fix_name(nick);

  snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num);

  wattron(ctx->history, COLOR_PAIR(3));
  wprintw(ctx->history, " * Your partner changed nick to '%s'\n", nick);
  wattroff(ctx->history, COLOR_PAIR(3));
}

static void chat_onStatusChange(ToxWindow* self, int num, uint8_t* status, uint16_t len) {

}

static void chat_onKey(ToxWindow* self, int key) {
  ChatContext* ctx = (ChatContext*) self->x;

  time_t now;
  time(&now);
  struct tm * timeinfo;
  timeinfo = localtime(&now);

  if(isprint(key)) {

    if(ctx->pos != sizeof(ctx->line)-1) {
      ctx->line[ctx->pos++] = key;
      ctx->line[ctx->pos] = '\0';
    }
  }
  else if(key == '\n') {

    int inthour = timeinfo->tm_hour; //Pretty bad, but it gets the job done
    int intmin = timeinfo->tm_min;
    char min[2];
    char hour[2];
    sprintf(hour,"%d",inthour);
    if (intmin < 10) {
      sprintf(min,"0%d",intmin);
    } else {
      sprintf(min,"%d",intmin);
    }
    wattron(ctx->history, COLOR_PAIR(2));
    wprintw(ctx->history,"%s",hour);
    wprintw(ctx->history,":%s  ",min);
    wattron(ctx->history, COLOR_PAIR(1));
    wprintw(ctx->history, "you: ", ctx->line);
    wattroff(ctx->history, COLOR_PAIR(1));

    wprintw(ctx->history, "%s\n", ctx->line);

    if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) {
      wattron(ctx->history, COLOR_PAIR(3));
      wprintw(ctx->history, " * Failed to send message.\n");
      wattroff(ctx->history, COLOR_PAIR(3));
    }

    ctx->line[0] = '\0';
    ctx->pos = 0;
  }
  else if(key == 0x107 || key == 0x8 || key == 0x7f) {
    if(ctx->pos != 0) {
      ctx->line[--ctx->pos] = '\0';
    }
  }
  
}

static void chat_onDraw(ToxWindow* self) {
  curs_set(1);
  int x, y;
  ChatContext* ctx = (ChatContext*) self->x;

  getmaxyx(self->window, y, x);

  (void) x;
  if(y < 3)
    return;

  wclear(ctx->linewin);
  mvwhline(ctx->linewin, 0, 0, '_', COLS);
  mvwprintw(self->window, y-1, 0, "%s\n", ctx->line);

  wrefresh(self->window);
}

static void chat_onInit(ToxWindow* self) {
  int x, y;
  ChatContext* ctx = (ChatContext*) self->x;

  getmaxyx(self->window, y, x);

  ctx->history = subwin(self->window, y - 4, x, 0, 0);
  scrollok(ctx->history, 1);

  ctx->linewin = subwin(self->window, 2, x, y - 3, 0);
}

ToxWindow new_chat(int friendnum) {
  ToxWindow ret;

  memset(&ret, 0, sizeof(ret));

  ret.onKey = &chat_onKey;
  ret.onDraw = &chat_onDraw;
  ret.onInit = &chat_onInit;
  ret.onMessage = &chat_onMessage;
  ret.onNickChange = &chat_onNickChange;
  ret.onStatusChange = &chat_onStatusChange;

  uint8_t nick[MAX_NAME_LENGTH] = {0};
  getname(friendnum, (uint8_t*) &nick);
  fix_name(nick);
  
  snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum);

  ChatContext* x = calloc(1, sizeof(ChatContext));
  x->friendnum = friendnum;

  ret.x = (void*) x;

  return ret;
}