summaryrefslogtreecommitdiffstats
path: root/src/gui/CoreConnector.vala
blob: 556668691eccaaaa752f632c4247056429af6e1b (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
public class CoreConnector {
  static Eva.Term match_gui_init;
  static Eva.Term match_roster_update;
  static Eva.Term match_resource_entry;
  static Eva.Term match_avatar;
  static Eva.Term match_new_conversation;
  static Eva.Term match_chat_message;
  
  static construct {
    match_gui_init = Eva.parse("{gui_init,Terms}");
    match_roster_update = Eva.parse("{roster_update,JID,Name,Subscription,Groups,Resources,Avatar}");
    match_resource_entry = Eva.parse("{Name,{resource_entry,Priority,Show,Status}}");
    match_avatar = Eva.parse("{avatar,Data}");
    match_new_conversation = Eva.parse("{new_conversation,JID}");
    match_chat_message = Eva.parse("{chat_message,JID,Type,From,Body}");
  }
  
  Eva.PacketHandler con;
  
  public signal void update_contact(Contact contact);
  public signal void new_conversation(string jid);
  public signal void chat_message(string jid, string from, string type, string message);
  
  public void start() {
    con = new Eva.PacketHandler(new UnixInputStream(3, true), new UnixOutputStream(4, true), 4);
    con.received_term.connect(handle_term);
    con.start();
  }
  
  public void stop() {
    con.send(new Eva.Atom("stop"));
  }
  
  private static string from_utf8(Eva.Binary bin) {
    string ret = ((string)bin.data).ndup(bin.len);
    warn_if_fail(ret.validate());
    return ret;
  }
  
  private void handle_term(Eva.Term term) {
    Gee.Map<string, Eva.Term> match;
    
    if((match = term.match(match_gui_init)) != null) {
      Eva.Cons terms = match["Terms"] as Eva.Cons;
      
      while(terms != null) {
        Eva.Tuple tuple = terms.head as Eva.Tuple;
        
        if(tuple != null && tuple.size > 0) {
          Eva.Atom key = tuple[0] as Eva.Atom;
          
          /*if(key != null) {
            switch(key.value) {
              // None yet
            }
            }*/
        }
        
        terms = terms.tail as Eva.Cons;
      }
    }
    else if((match = term.match(match_roster_update)) != null) {
      Eva.Binary jid_term = match["JID"] as Eva.Binary;
      if(jid_term == null) {
        warn_if_reached();
        return;
      }
      string jid = from_utf8(jid_term);
      
      Eva.Binary name_term = match["Name"] as Eva.Binary;
      string? name;
      if (name_term is Eva.Binary)
        name = from_utf8(name_term);
      else
        name = null;
      
      Contact contact = new Contact(jid, name);

      Eva.Cons groups = match["Groups"] as Eva.Cons;
      while(groups != null) {
        Eva.Binary group_term = groups.head as Eva.Binary;
        
        if(group_term != null) {
          string group = from_utf8(group_term);
          contact.add_group(group);
        }
        
        groups = groups.tail as Eva.Cons;
      }
      
      Eva.Cons resources = match["Resources"] as Eva.Cons;
      while(resources != null) {
        Gee.Map<string, Eva.Term> r;
        
        if((r = resources.head.match(match_resource_entry)) != null) {
          Eva.Binary rname_term = r["Name"] as Eva.Binary;
          Eva.Numeric prio_term = r["Priority"] as Eva.Numeric;
          Eva.Atom show_term = r["Show"] as Eva.Atom;
          Eva.Binary status_term = r["Status"] as Eva.Binary;
          
          if(rname_term != null && prio_term != null && show_term != null) {
            string rname = from_utf8(rname_term);
            int prio = prio_term.int_value;
            
            Contact.Show show = Contact.Show.UNDEFINED;
            switch(show_term.value) {
            case "online":
              show = Contact.Show.ONLINE;
              break;
            case "away":
              show = Contact.Show.AWAY;
              break;
            case "chat":
              show = Contact.Show.CHAT;
              break;
            case "dnd":
              show = Contact.Show.DND;
              break;
            case "xa":
              show = Contact.Show.XA;
              break;
            }
            
            string? status = null;
            if(status_term != null)
              status = from_utf8(status_term);
            
            contact.update_resource(rname, new Contact.Resource(prio, show, status));
          }
        }
        
        resources = resources.tail as Eva.Cons;
      }
      
      Gee.Map<string, Eva.Term> avatar = match["Avatar"].match(match_avatar);
      if(avatar != null) {
        Eva.Binary avatarData = avatar["Data"] as Eva.Binary;
        
        if(avatarData != null) {
          InputStream avatarStream = new MemoryInputStream.from_data(avatarData.data, avatarData.len, null);
          try {
            contact.avatar = new Gdk.Pixbuf.from_stream_at_scale(avatarStream, 32, 32, true, null);
            avatarStream.close(null);
          } catch(Error e) {
            warn_if_reached();
          }
        }
      }
      update_contact(contact);
    }
    else if((match = term.match(match_new_conversation)) != null) {
      Eva.Binary jid_term = match["JID"] as Eva.Binary;
      if(jid_term == null) {
        warn_if_reached();
        return;
      }
      string jid = from_utf8(jid_term);
      
      new_conversation(jid);
    }
    else if((match = term.match(match_chat_message)) != null) {
      Eva.Binary jid_term = match["JID"] as Eva.Binary;
      if(jid_term == null) {
        warn_if_reached();
        return;
      }
      string jid = from_utf8(jid_term);
      
      Eva.Atom type_term = match["Type"] as Eva.Atom;
      if(type_term == null) {
        warn_if_reached();
        return;
      }
      string type = type_term.value;
      
      Eva.Binary from_term = match["From"] as Eva.Binary;
      if(from_term == null) {
        warn_if_reached();
        return;
      }
      string from = from_utf8(from_term);
      
      Eva.Binary body_term = match["Body"] as Eva.Binary;
      if(body_term == null) {
        warn_if_reached();
        return;
      }
      string body = from_utf8(body_term);
      
      chat_message(jid, type, from, body);
    }
    else {
      stdout.printf("Received unhandled term: %s\n", term.to_string());
    }
  }
    
  public void start_conversation(string jid) {
    char[] jid_utf8 = jid.to_utf8();
    con.send(Eva.parse("{start_conversation,~w}", new Eva.Binary(jid_utf8)));
  }
  
  public void send_message(string jid, string type, string message) {
    char[] jid_utf8 = jid.to_utf8();
    char[] message_utf8 = message.to_utf8();
    con.send(Eva.parse("{send_message,~w,~a,~w}", new Eva.Binary(jid_utf8), type, new Eva.Binary(message_utf8)));
  }
}