about summary refs log tree commit diff stats
path: root/html/linux/105string-equal.subx.html
blob: 11a8824f11df0ada83026c79b45e894cbb8ef9d9 (plain) (blame)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - linux/105string-equal.subx</title>
<meta name="Generator" content="Vim/8.1">
<meta name="plugin-version" content="vim8.1_v1">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal-light">
<style type="text/css">
<!--
pre { font-family: monospace; color: #000000; background-color: #ffffd7; }
body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; }
a { color:inherit; }
* { font-size:12pt; font-size: 1em; }
.subxComment { color: #005faf; }
.subxFunction { color: #af5f00; text-decoration: underline; }
.LineNr { }
.subxTest { color: #5f8700; }
.subxS1Comment { color: #0000af; }
.CommentedCode { color: #8a8a8a; }
.SpecialChar { color: #d70000; }
.Normal { color: #000000; background-color: #ffffd7; padding-bottom: 1px; }
.Constant { color: #008787; }
.subxS2Comment { color: #8a8a8a; }
.subxH1Comment { color: #005faf; text-decoration: underline; }
-->
</style>

<script type='text/javascript'>
<!--

/* function to open any folds containing a jumped-to line before jumping to it */
function JumpToLine()
{
  var lineNum;
  lineNum = window.location.hash;
  lineNum = lineNum.substr(1); /* strip off '#' */

  if (lineNum.indexOf('L') == -1) {
    lineNum = 'L'+lineNum;
  }
  var lineElem = document.getElementById(lineNum);
  /* Always jump to new location even if the line was hidden inside a fold, or
   * we corrected the raw number to a line ID.
   */
  if (lineElem) {
    lineElem.scrollIntoView(true);
  }
  return true;
}
if ('onhashchange' in window) {
  window.onhashchange = JumpToLine;
}

-->
</script>
</head>
<body onload='JumpToLine();'>
<a href='https://github.com/akkartik/mu/blob/main/linux/105string-equal.subx'>https://github.com/akkartik/mu/blob/main/linux/105string-equal.subx</a>
<pre id='vimCodeElement'>
<span id="L1" class="LineNr">  1 </span><span class="subxComment"># Comparing 'regular' size-prefixed strings.</span>
<span id="L2" class="LineNr">  2 </span>
<span id="L3" class="LineNr">  3 </span>== code
<span id="L4" class="LineNr">  4 </span><span class="subxComment">#   instruction                     effective address                                                   register    displacement    immediate</span>
<span id="L5" class="LineNr">  5 </span><span class="subxS1Comment"># . op          subop               mod             rm32          base        index         scale       r32</span>
<span id="L6" class="LineNr">  6 </span><span class="subxS1Comment"># . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes</span>
<span id="L7" class="LineNr">  7 </span>
<span id="L8" class="LineNr">  8 </span><span class="SpecialChar">Entry</span>:  <span class="subxComment"># run all tests</span>
<span id="L9" class="LineNr">  9 </span><span class="CommentedCode">#?     e8/call test-compare-equal-strings/disp32</span>
<span id="L10" class="LineNr"> 10 </span>    e8/call  run-tests/disp32  <span class="subxComment"># 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.</span>
<span id="L11" class="LineNr"> 11 </span>    <span class="subxComment"># syscall(exit, Num-test-failures)</span>
<span id="L12" class="LineNr"> 12 </span>    8b/copy                         0/mod/indirect  5/rm32/.disp32           <span class="Normal"> . </span>           <span class="Normal"> . </span>          3/r32/ebx   <span class="SpecialChar"><a href='102test.subx.html#L89'>Num-test-failures</a></span>/disp32          <span class="subxComment"># copy *Num-test-failures to ebx</span>
<span id="L13" class="LineNr"> 13 </span>    e8/call  <a href='000init.subx.html#L18'>syscall_exit</a>/disp32
<span id="L14" class="LineNr"> 14 </span>
<span id="L15" class="LineNr"> 15 </span><span class="subxFunction">string-equal?</span>:  <span class="subxComment"># s: (addr array byte), benchmark: (addr array byte) -&gt; result/eax: boolean</span>
<span id="L16" class="LineNr"> 16 </span>    <span class="subxComment"># pseudocode:</span>
<span id="L17" class="LineNr"> 17 </span>    <span class="subxComment">#   if (s-&gt;size != benchmark-&gt;size) return false</span>
<span id="L18" class="LineNr"> 18 </span>    <span class="subxComment">#   return string-starts-with?(s, benchmark)</span>
<span id="L19" class="LineNr"> 19 </span>    <span class="subxComment">#</span>
<span id="L20" class="LineNr"> 20 </span>    <span class="subxS1Comment"># . prologue</span>
<span id="L21" class="LineNr"> 21 </span>    55/push-ebp
<span id="L22" class="LineNr"> 22 </span>    89/copy                         3/mod/direct    5/rm32/ebp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          4/r32/esp  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy esp to ebp</span>
<span id="L23" class="LineNr"> 23 </span>    <span class="subxS1Comment"># . save registers</span>
<span id="L24" class="LineNr"> 24 </span>    51/push-ecx
<span id="L25" class="LineNr"> 25 </span>    56/push-esi
<span id="L26" class="LineNr"> 26 </span>    57/push-edi
<span id="L27" class="LineNr"> 27 </span>    <span class="subxComment"># esi = s</span>
<span id="L28" class="LineNr"> 28 </span>    8b/copy                         1/mod/*+disp8   5/rm32/ebp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          6/r32/esi   8/disp8        <span class="Normal"> . </span>                <span class="subxComment"># copy *(ebp+8) to esi</span>
<span id="L29" class="LineNr"> 29 </span>    <span class="subxComment"># edi = benchmark</span>
<span id="L30" class="LineNr"> 30 </span>    8b/copy                         1/mod/*+disp8   5/rm32/ebp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          7/r32/edi   0xc/disp8      <span class="Normal"> . </span>                <span class="subxComment"># copy *(ebp+12) to edi</span>
<span id="L31" class="LineNr"> 31 </span>    <span class="subxComment"># ecx = s-&gt;size</span>
<span id="L32" class="LineNr"> 32 </span>    8b/copy                         0/mod/indirect  6/rm32/esi   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          1/r32/ecx  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy *esi to ecx</span>
<span id="L33" class="LineNr"> 33 </span><span class="Constant">$string-equal?:sizes</span>:
<span id="L34" class="LineNr"> 34 </span>    <span class="subxComment"># if (ecx != benchmark-&gt;size) return false</span>
<span id="L35" class="LineNr"> 35 </span>    39/compare                      0/mod/indirect  7/rm32/edi   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          1/r32/ecx  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># compare *edi and ecx</span>
<span id="L36" class="LineNr"> 36 </span>    b8/copy-to-eax  0/imm32/false
<span id="L37" class="LineNr"> 37 </span>    75/jump-if-!=  $string-equal?:end/disp8
<span id="L38" class="LineNr"> 38 </span><span class="Constant">$string-equal?:contents</span>:
<span id="L39" class="LineNr"> 39 </span>    <span class="subxComment"># string-starts-with?(s, benchmark)</span>
<span id="L40" class="LineNr"> 40 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L41" class="LineNr"> 41 </span>    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>          0xc/disp8      <span class="Normal"> . </span>                <span class="subxComment"># push *(ebp+12)</span>
<span id="L42" class="LineNr"> 42 </span>    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>          8/disp8        <span class="Normal"> . </span>                <span class="subxComment"># push *(ebp+8)</span>
<span id="L43" class="LineNr"> 43 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L44" class="LineNr"> 44 </span>    e8/call  <a href='105string-equal.subx.html#L57'>string-starts-with?</a>/disp32
<span id="L45" class="LineNr"> 45 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L46" class="LineNr"> 46 </span>    81          0/subop/add         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              8/imm32           <span class="subxComment"># add to esp</span>
<span id="L47" class="LineNr"> 47 </span><span class="Constant">$string-equal?:end</span>:
<span id="L48" class="LineNr"> 48 </span>    <span class="subxS1Comment"># . restore registers</span>
<span id="L49" class="LineNr"> 49 </span>    5f/pop-to-edi
<span id="L50" class="LineNr"> 50 </span>    5e/pop-to-esi
<span id="L51" class="LineNr"> 51 </span>    59/pop-to-ecx
<span id="L52" class="LineNr"> 52 </span>    <span class="subxS1Comment"># . epilogue</span>
<span id="L53" class="LineNr"> 53 </span>    89/copy                         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          5/r32/ebp  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy ebp to esp</span>
<span id="L54" class="LineNr"> 54 </span>    5d/pop-to-ebp
<span id="L55" class="LineNr"> 55 </span>    c3/return
<span id="L56" class="LineNr"> 56 </span>
<span id="L57" class="LineNr"> 57 </span><span class="subxFunction">string-starts-with?</span>:  <span class="subxComment"># s: (addr array byte), benchmark: (addr array byte) -&gt; result/eax: boolean</span>
<span id="L58" class="LineNr"> 58 </span>    <span class="subxComment"># pseudocode:</span>
<span id="L59" class="LineNr"> 59 </span>    <span class="subxComment">#   if (s-&gt;size &lt; benchmark-&gt;size) return false</span>
<span id="L60" class="LineNr"> 60 </span>    <span class="subxComment">#   currs = s-&gt;data</span>
<span id="L61" class="LineNr"> 61 </span>    <span class="subxComment">#   currb = benchmark-&gt;data</span>
<span id="L62" class="LineNr"> 62 </span>    <span class="subxComment">#   maxb = &amp;benchmark-&gt;data[benchmark-&gt;size]</span>
<span id="L63" class="LineNr"> 63 </span>    <span class="subxComment">#   while currb &lt; maxb</span>
<span id="L64" class="LineNr"> 64 </span>    <span class="subxComment">#     c1 = *currs</span>
<span id="L65" class="LineNr"> 65 </span>    <span class="subxComment">#     c2 = *currb</span>
<span id="L66" class="LineNr"> 66 </span>    <span class="subxComment">#     if (c1 != c2) return false</span>
<span id="L67" class="LineNr"> 67 </span>    <span class="subxComment">#     ++currs, ++currb</span>
<span id="L68" class="LineNr"> 68 </span>    <span class="subxComment">#   return true</span>
<span id="L69" class="LineNr"> 69 </span>    <span class="subxComment">#</span>
<span id="L70" class="LineNr"> 70 </span>    <span class="subxComment"># registers:</span>
<span id="L71" class="LineNr"> 71 </span>    <span class="subxComment">#   currs: esi</span>
<span id="L72" class="LineNr"> 72 </span>    <span class="subxComment">#   maxs: ecx</span>
<span id="L73" class="LineNr"> 73 </span>    <span class="subxComment">#   currb: edi</span>
<span id="L74" class="LineNr"> 74 </span>    <span class="subxComment">#   c1: eax</span>
<span id="L75" class="LineNr"> 75 </span>    <span class="subxComment">#   c2: ebx</span>
<span id="L76" class="LineNr"> 76 </span>    <span class="subxComment">#</span>
<span id="L77" class="LineNr"> 77 </span>    <span class="subxS1Comment"># . prologue</span>
<span id="L78" class="LineNr"> 78 </span>    55/push-ebp
<span id="L79" class="LineNr"> 79 </span>    89/copy                         3/mod/direct    5/rm32/ebp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          4/r32/esp  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy esp to ebp</span>
<span id="L80" class="LineNr"> 80 </span>    <span class="subxS1Comment"># . save registers</span>
<span id="L81" class="LineNr"> 81 </span>    51/push-ecx
<span id="L82" class="LineNr"> 82 </span>    52/push-edx
<span id="L83" class="LineNr"> 83 </span>    56/push-esi
<span id="L84" class="LineNr"> 84 </span>    57/push-edi
<span id="L85" class="LineNr"> 85 </span>    <span class="subxComment"># esi = s</span>
<span id="L86" class="LineNr"> 86 </span>    8b/copy                         1/mod/*+disp8   5/rm32/ebp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          6/r32/esi   8/disp8        <span class="Normal"> . </span>                <span class="subxComment"># copy *(ebp+8) to esi</span>
<span id="L87" class="LineNr"> 87 </span>    <span class="subxComment"># edi = benchmark</span>
<span id="L88" class="LineNr"> 88 </span>    8b/copy                         1/mod/*+disp8   5/rm32/ebp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          7/r32/edi   0xc/disp8      <span class="Normal"> . </span>                <span class="subxComment"># copy *(ebp+12) to edi</span>
<span id="L89" class="LineNr"> 89 </span>    <span class="subxComment"># var bsize/ecx: int = benchmark-&gt;size</span>
<span id="L90" class="LineNr"> 90 </span>    8b/copy                         0/mod/indirect  7/rm32/edi   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          1/r32/ecx  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy *edi to ecx</span>
<span id="L91" class="LineNr"> 91 </span><span class="Constant">$string-starts-with?:sizes</span>:
<span id="L92" class="LineNr"> 92 </span>    <span class="subxComment"># if (s-&gt;size &lt; bsize) return false</span>
<span id="L93" class="LineNr"> 93 </span>    39/compare                      0/mod/indirect  6/rm32/esi   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          1/r32/ecx  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># compare *esi with ecx</span>
<span id="L94" class="LineNr"> 94 </span>    7c/jump-if-&lt;  $string-starts-with?:false/disp8
<span id="L95" class="LineNr"> 95 </span>    <span class="subxComment"># var currs/esi: (addr byte) = s-&gt;data</span>
<span id="L96" class="LineNr"> 96 </span>    81          0/subop/add         3/mod/direct    6/rm32/esi   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              4/imm32           <span class="subxComment"># add to esi</span>
<span id="L97" class="LineNr"> 97 </span>    <span class="subxComment"># var currb/edi: (addr byte) = benchmark-&gt;data</span>
<span id="L98" class="LineNr"> 98 </span>    81          0/subop/add         3/mod/direct    7/rm32/edi   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              4/imm32           <span class="subxComment"># add to edi</span>
<span id="L99" class="LineNr"> 99 </span>    <span class="subxComment"># var maxb/ecx: (addr byte) = &amp;benchmark-&gt;data[benchmark-&gt;size]</span>
<span id="L100" class="LineNr">100 </span>    01/add                          3/mod/direct    1/rm32/ecx   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          7/r32/edi  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># add edi to ecx</span>
<span id="L101" class="LineNr">101 </span>    <span class="subxComment"># var c1/eax: byte = 0</span>
<span id="L102" class="LineNr">102 </span>    31/xor                          3/mod/direct    0/rm32/eax   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          0/r32/eax  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># clear eax</span>
<span id="L103" class="LineNr">103 </span>    <span class="subxComment"># var c2/edx: byte = 0</span>
<span id="L104" class="LineNr">104 </span>    31/xor                          3/mod/direct    2/rm32/edx   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          2/r32/edx  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># clear edx</span>
<span id="L105" class="LineNr">105 </span><span class="Constant">$string-starts-with?:loop</span>:
<span id="L106" class="LineNr">106 </span>    <span class="subxComment"># if (currs &gt;= maxs) return true</span>
<span id="L107" class="LineNr">107 </span>    39/compare                      3/mod/direct    7/rm32/edi   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          1/r32/ecx  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># compare edi with ecx</span>
<span id="L108" class="LineNr">108 </span>    73/jump-if-addr&gt;=  $string-starts-with?:true/disp8
<span id="L109" class="LineNr">109 </span>    <span class="subxComment"># c1 = *currs</span>
<span id="L110" class="LineNr">110 </span>    8a/copy-byte                    0/mod/indirect  6/rm32/esi   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          0/r32/AL   <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy byte at *esi to AL</span>
<span id="L111" class="LineNr">111 </span>    <span class="subxComment"># c2 = *currb</span>
<span id="L112" class="LineNr">112 </span>    8a/copy-byte                    0/mod/indirect  7/rm32/edi   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          2/r32/DL   <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy byte at *edi to DL</span>
<span id="L113" class="LineNr">113 </span>    <span class="subxComment"># if (c1 != c2) return false</span>
<span id="L114" class="LineNr">114 </span>    39/compare                      3/mod/direct    0/rm32/eax   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          2/r32/edx  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># compare eax and edx</span>
<span id="L115" class="LineNr">115 </span>    75/jump-if-!=  $string-starts-with?:false/disp8
<span id="L116" class="LineNr">116 </span>    <span class="subxComment"># ++currs</span>
<span id="L117" class="LineNr">117 </span>    46/increment-esi
<span id="L118" class="LineNr">118 </span>    <span class="subxComment"># ++currb</span>
<span id="L119" class="LineNr">119 </span>    47/increment-edi
<span id="L120" class="LineNr">120 </span>    eb/jump  $string-starts-with?:<span class="Constant">loop</span>/disp8
<span id="L121" class="LineNr">121 </span><span class="Constant">$string-starts-with?:true</span>:
<span id="L122" class="LineNr">122 </span>    b8/copy-to-eax  1/imm32
<span id="L123" class="LineNr">123 </span>    eb/jump  $string-starts-with?:end/disp8
<span id="L124" class="LineNr">124 </span><span class="Constant">$string-starts-with?:false</span>:
<span id="L125" class="LineNr">125 </span>    b8/copy-to-eax  0/imm32
<span id="L126" class="LineNr">126 </span><span class="Constant">$string-starts-with?:end</span>:
<span id="L127" class="LineNr">127 </span>    <span class="subxS1Comment"># . restore registers</span>
<span id="L128" class="LineNr">128 </span>    5f/pop-to-edi
<span id="L129" class="LineNr">129 </span>    5e/pop-to-esi
<span id="L130" class="LineNr">130 </span>    5a/pop-to-edx
<span id="L131" class="LineNr">131 </span>    59/pop-to-ecx
<span id="L132" class="LineNr">132 </span>    <span class="subxS1Comment"># . epilogue</span>
<span id="L133" class="LineNr">133 </span>    89/copy                         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          5/r32/ebp  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy ebp to esp</span>
<span id="L134" class="LineNr">134 </span>    5d/pop-to-ebp
<span id="L135" class="LineNr">135 </span>    c3/return
<span id="L136" class="LineNr">136 </span>
<span id="L137" class="LineNr">137 </span><span class="subxH1Comment"># - tests</span>
<span id="L138" class="LineNr">138 </span>
<span id="L139" class="LineNr">139 </span><span class="subxTest">test-compare-empty-with-empty-string</span>:
<span id="L140" class="LineNr">140 </span>    <span class="subxComment"># eax = string-equal?(&quot;&quot;, &quot;&quot;)</span>
<span id="L141" class="LineNr">141 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L142" class="LineNr">142 </span>    68/push  <span class="Constant">&quot;&quot;</span>/imm32
<span id="L143" class="LineNr">143 </span>    68/push  <span class="Constant">&quot;&quot;</span>/imm32
<span id="L144" class="LineNr">144 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L145" class="LineNr">145 </span>    e8/call  <a href='105string-equal.subx.html#L15'>string-equal?</a>/disp32
<span id="L146" class="LineNr">146 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L147" class="LineNr">147 </span>    81          0/subop/add         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              8/imm32           <span class="subxComment"># add to esp</span>
<span id="L148" class="LineNr">148 </span>    <span class="subxComment"># check-ints-equal(eax, 1, msg)</span>
<span id="L149" class="LineNr">149 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L150" class="LineNr">150 </span>    68/push  <span class="Constant">&quot;F - test-compare-empty-with-empty-string&quot;</span>/imm32
<span id="L151" class="LineNr">151 </span>    68/push  1/imm32/true
<span id="L152" class="LineNr">152 </span>    50/push-eax
<span id="L153" class="LineNr">153 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L154" class="LineNr">154 </span>    e8/call  <a href='102test.subx.html#L23'>check-ints-equal</a>/disp32
<span id="L155" class="LineNr">155 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L156" class="LineNr">156 </span>    81          0/subop/add         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              0xc/imm32         <span class="subxComment"># add to esp</span>
<span id="L157" class="LineNr">157 </span>    c3/return
<span id="L158" class="LineNr">158 </span>
<span id="L159" class="LineNr">159 </span><span class="subxTest">test-compare-empty-with-non-empty-string</span>:  <span class="subxComment"># also checks size-mismatch code path</span>
<span id="L160" class="LineNr">160 </span>    <span class="subxComment"># eax = string-equal?(&quot;&quot;, &quot;Abc&quot;)</span>
<span id="L161" class="LineNr">161 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L162" class="LineNr">162 </span>    68/push  <span class="Constant">&quot;Abc&quot;</span>/imm32
<span id="L163" class="LineNr">163 </span>    68/push  <span class="Constant">&quot;&quot;</span>/imm32
<span id="L164" class="LineNr">164 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L165" class="LineNr">165 </span>    e8/call  <a href='105string-equal.subx.html#L15'>string-equal?</a>/disp32
<span id="L166" class="LineNr">166 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L167" class="LineNr">167 </span>    81          0/subop/add         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              8/imm32           <span class="subxComment"># add to esp</span>
<span id="L168" class="LineNr">168 </span>    <span class="subxComment"># check-ints-equal(eax, 0, msg)</span>
<span id="L169" class="LineNr">169 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L170" class="LineNr">170 </span>    68/push  <span class="Constant">&quot;F - test-compare-empty-with-non-empty-string&quot;</span>/imm32
<span id="L171" class="LineNr">171 </span>    68/push  0/imm32/false
<span id="L172" class="LineNr">172 </span>    50/push-eax
<span id="L173" class="LineNr">173 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L174" class="LineNr">174 </span>    e8/call  <a href='102test.subx.html#L23'>check-ints-equal</a>/disp32
<span id="L175" class="LineNr">175 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L176" class="LineNr">176 </span>    81          0/subop/add         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              0xc/imm32         <span class="subxComment"># add to esp</span>
<span id="L177" class="LineNr">177 </span>    c3/return
<span id="L178" class="LineNr">178 </span>
<span id="L179" class="LineNr">179 </span><span class="subxTest">test-compare-equal-strings</span>:
<span id="L180" class="LineNr">180 </span>    <span class="subxComment"># eax = string-equal?(&quot;Abc&quot;, &quot;Abc&quot;)</span>
<span id="L181" class="LineNr">181 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L182" class="LineNr">182 </span>    68/push  <span class="Constant">&quot;Abc&quot;</span>/imm32
<span id="L183" class="LineNr">183 </span>    68/push  <span class="Constant">&quot;Abc&quot;</span>/imm32
<span id="L184" class="LineNr">184 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L185" class="LineNr">185 </span>    e8/call  <a href='105string-equal.subx.html#L15'>string-equal?</a>/disp32
<span id="L186" class="LineNr">186 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L187" class="LineNr">187 </span>    81          0/subop/add         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              8/imm32           <span class="subxComment"># add to esp</span>
<span id="L188" class="LineNr">188 </span>    <span class="subxComment"># check-ints-equal(eax, 1, msg)</span>
<span id="L189" class="LineNr">189 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L190" class="LineNr">190 </span>    68/push  <span class="Constant">&quot;F - test-compare-equal-strings&quot;</span>/imm32
<span id="L191" class="LineNr">191 </span>    68/push  1/imm32/true
<span id="L192" class="LineNr">192 </span>    50/push-eax
<span id="L193" class="LineNr">193 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L194" class="LineNr">194 </span>    e8/call  <a href='102test.subx.html#L23'>check-ints-equal</a>/disp32
<span id="L195" class="LineNr">195 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L196" class="LineNr">196 </span>    81          0/subop/add         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              0xc/imm32         <span class="subxComment"># add to esp</span>
<span id="L197" class="LineNr">197 </span>    c3/return
<span id="L198" class="LineNr">198 </span>
<span id="L199" class="LineNr">199 </span><span class="subxTest">test-compare-inequal-strings-equal-sizes</span>:
<span id="L200" class="LineNr">200 </span>    <span class="subxComment"># eax = string-equal?(&quot;Abc&quot;, &quot;Adc&quot;)</span>
<span id="L201" class="LineNr">201 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L202" class="LineNr">202 </span>    68/push  <span class="Constant">&quot;Adc&quot;</span>/imm32
<span id="L203" class="LineNr">203 </span>    68/push  <span class="Constant">&quot;Abc&quot;</span>/imm32
<span id="L204" class="LineNr">204 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L205" class="LineNr">205 </span>    e8/call  <a href='105string-equal.subx.html#L15'>string-equal?</a>/disp32
<span id="L206" class="LineNr">206 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L207" class="LineNr">207 </span>    81          0/subop/add         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              8/imm32           <span class="subxComment"># add to esp</span>
<span id="L208" class="LineNr">208 </span>    <span class="subxComment"># check-ints-equal(eax, 0, msg)</span>
<span id="L209" class="LineNr">209 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L210" class="LineNr">210 </span>    68/push  <span class="Constant">&quot;F - test-compare-inequal-strings-equal-sizes&quot;</span>/imm32
<span id="L211" class="LineNr">211 </span>    68/push  0/imm32/false
<span id="L212" class="LineNr">212 </span>    50/push-eax
<span id="L213" class="LineNr">213 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L214" class="LineNr">214 </span>    e8/call  <a href='102test.subx.html#L23'>check-ints-equal</a>/disp32
<span id="L215" class="LineNr">215 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L216" class="LineNr">216 </span>    81          0/subop/add         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              0xc/imm32         <span class="subxComment"># add to esp</span>
<span id="L217" class="LineNr">217 </span>    c3/return
<span id="L218" class="LineNr">218 </span>
<span id="L219" class="LineNr">219 </span><span class="subxComment"># helper for later tests</span>
<span id="L220" class="LineNr">220 </span><span class="subxFunction">check-strings-equal</span>:  <span class="subxComment"># s: (addr array byte), expected: (addr array byte), msg: (addr array byte)</span>
<span id="L221" class="LineNr">221 </span>    <span class="subxS1Comment"># . prologue</span>
<span id="L222" class="LineNr">222 </span>    55/push-ebp
<span id="L223" class="LineNr">223 </span>    89/copy                         3/mod/direct    5/rm32/ebp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          4/r32/esp  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy esp to ebp</span>
<span id="L224" class="LineNr">224 </span>    <span class="subxS1Comment"># . save registers</span>
<span id="L225" class="LineNr">225 </span>    50/push-eax
<span id="L226" class="LineNr">226 </span>    <span class="subxComment"># var eax: boolean = string-equal?(s, expected)</span>
<span id="L227" class="LineNr">227 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L228" class="LineNr">228 </span>    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>          0xc/disp8      <span class="Normal"> . </span>                <span class="subxComment"># push *(ebp+12)</span>
<span id="L229" class="LineNr">229 </span>    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>          8/disp8        <span class="Normal"> . </span>                <span class="subxComment"># push *(ebp+8)</span>
<span id="L230" class="LineNr">230 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L231" class="LineNr">231 </span>    e8/call  <a href='105string-equal.subx.html#L15'>string-equal?</a>/disp32
<span id="L232" class="LineNr">232 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L233" class="LineNr">233 </span>    81          0/subop/add         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              8/imm32           <span class="subxComment"># add to esp</span>
<span id="L234" class="LineNr">234 </span>    <span class="subxComment"># check-ints-equal(eax, 1, msg)</span>
<span id="L235" class="LineNr">235 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L236" class="LineNr">236 </span>    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>          0x10/disp8     <span class="Normal"> . </span>                <span class="subxComment"># push *(ebp+16)</span>
<span id="L237" class="LineNr">237 </span>    68/push  1/imm32
<span id="L238" class="LineNr">238 </span>    50/push-eax
<span id="L239" class="LineNr">239 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L240" class="LineNr">240 </span>    e8/call  <a href='102test.subx.html#L23'>check-ints-equal</a>/disp32
<span id="L241" class="LineNr">241 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L242" class="LineNr">242 </span>    81          0/subop/add         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              0xc/imm32         <span class="subxComment"># add to esp</span>
<span id="L243" class="LineNr">243 </span><span class="Constant">$check-strings-equal:end</span>:
<span id="L244" class="LineNr">244 </span>    <span class="subxS1Comment"># . restore registers</span>
<span id="L245" class="LineNr">245 </span>    58/pop-to-eax
<span id="L246" class="LineNr">246 </span>    <span class="subxS1Comment"># . epilogue</span>
<span id="L247" class="LineNr">247 </span>    89/copy                         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          5/r32/ebp  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy ebp to esp</span>
<span id="L248" class="LineNr">248 </span>    5d/pop-to-ebp
<span id="L249" class="LineNr">249 </span>    c3/return
<span id="L250" class="LineNr">250 </span>
<span id="L251" class="LineNr">251 </span><span class="subxComment"># test the helper</span>
<span id="L252" class="LineNr">252 </span><span class="subxTest">test-check-strings-equal</span>:
<span id="L253" class="LineNr">253 </span>    <span class="subxComment"># check-strings-equal(&quot;Abc&quot;, &quot;Abc&quot;)</span>
<span id="L254" class="LineNr">254 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L255" class="LineNr">255 </span>    68/push  <span class="Constant">&quot;Abc&quot;</span>/imm32
<span id="L256" class="LineNr">256 </span>    68/push  <span class="Constant">&quot;Abc&quot;</span>/imm32
<span id="L257" class="LineNr">257 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L258" class="LineNr">258 </span>    e8/call  <a href='105string-equal.subx.html#L220'>check-strings-equal</a>/disp32
<span id="L259" class="LineNr">259 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L260" class="LineNr">260 </span>    81          0/subop/add         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              8/imm32           <span class="subxComment"># add to esp</span>
<span id="L261" class="LineNr">261 </span>    c3/return
<span id="L262" class="LineNr">262 </span>
<span id="L263" class="LineNr">263 </span><span class="subxS2Comment"># . . vim&#0058;nowrap:textwidth=0</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
log_info("No node attribute found"); return 0; } // validate sha1 gchar **split = g_strsplit(node, "#", -1); char *given_sha1 = split[1]; char *generated_sha1 = stanza_create_caps_sha1_from_query(query); if (g_strcmp0(given_sha1, generated_sha1) != 0) { log_warning("Generated sha-1 does not match given:"); log_warning("Generated : %s", generated_sha1); log_warning("Given : %s", given_sha1); } else { log_info("Valid SHA-1 hash found: %s", given_sha1); if (caps_cache_contains(given_sha1)) { log_info("Capabilties already cached: %s", given_sha1); } else { log_info("Capabilities not cached: %s, storing", given_sha1); EntityCapabilities *capabilities = stanza_create_caps_from_query_element(query); caps_add_by_ver(given_sha1, capabilities); caps_destroy(capabilities); } caps_map_jid_to_ver(from, given_sha1); } g_free(generated_sha1); g_strfreev(split); return 0; } static int _caps_response_for_jid_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { char *jid = (char *)userdata; const char *id = xmpp_stanza_get_id(stanza); xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); const char *type = xmpp_stanza_get_type(stanza); // ignore non result if ((g_strcmp0(type, "get") == 0) || (g_strcmp0(type, "set") == 0)) { return 1; } if (id) { log_info("Capabilities response handler fired for id %s", id); } else { log_info("Capabilities response handler fired"); } const char *from = xmpp_stanza_get_from(stanza); if (!from) { log_info("No from attribute"); return 0; } // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); log_warning("Error received for capabilities response from %s: ", from, error_message); free(error_message); return 0; } if (query == NULL) { log_info("No query element found."); return 0; } const char *node = xmpp_stanza_get_attribute(query, STANZA_ATTR_NODE); if (node == NULL) { log_info("No node attribute found"); return 0; } log_info("Associating capabilities with: %s", jid); EntityCapabilities *capabilities = stanza_create_caps_from_query_element(query); caps_add_by_jid(jid, capabilities); return 0; } static int _caps_response_legacy_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *id = xmpp_stanza_get_id(stanza); xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); char *expected_node = (char *)userdata; const char *type = xmpp_stanza_get_type(stanza); // ignore non result if ((g_strcmp0(type, "get") == 0) || (g_strcmp0(type, "set") == 0)) { return 1; } if (id) { log_info("Capabilities response handler fired for id %s", id); } else { log_info("Capabilities response handler fired"); } const char *from = xmpp_stanza_get_from(stanza); if (!from) { log_info("No from attribute"); return 0; } // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); log_warning("Error received for capabilities response from %s: ", from, error_message); free(error_message); return 0; } if (query == NULL) { log_info("No query element found."); return 0; } const char *node = xmpp_stanza_get_attribute(query, STANZA_ATTR_NODE); if (node == NULL) { log_info("No node attribute found"); return 0; } // nodes match if (g_strcmp0(expected_node, node) == 0) { log_info("Legacy capabilities, nodes match %s", node); if (caps_cache_contains(node)) { log_info("Capabilties already cached: %s", node); } else { log_info("Capabilities not cached: %s, storing", node); EntityCapabilities *capabilities = stanza_create_caps_from_query_element(query); caps_add_by_ver(node, capabilities); caps_destroy(capabilities); } caps_map_jid_to_ver(from, node); // node match fail } else { log_info("Legacy Capabilities nodes do not match, expeceted %s, given %s.", expected_node, node); } return 0; } static int _room_list_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { gchar *filter = (gchar*)userdata; const char *id = xmpp_stanza_get_id(stanza); const char *from = xmpp_stanza_get_from(stanza); if (prefs_get_boolean(PREF_ROOM_LIST_CACHE) && !g_hash_table_contains(rooms_cache, from)) { g_hash_table_insert(rooms_cache, strdup(from), xmpp_stanza_copy(stanza)); } log_debug("Response to query: %s", id); xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); if (query == NULL) { return 0; } cons_show(""); if (filter) { cons_show("Rooms list response received: %s, filter: %s", from, filter); } else { cons_show("Rooms list response received: %s", from); } xmpp_stanza_t *child = xmpp_stanza_get_children(query); if (child == NULL) { cons_show(" No rooms found."); return 0; } GPatternSpec *glob = NULL; if (filter != NULL) { gchar *filter_lower = g_utf8_strdown(filter, -1); GString *glob_str = g_string_new("*"); g_string_append(glob_str, filter_lower); g_string_append(glob_str, "*"); glob = g_pattern_spec_new(glob_str->str); g_string_free(glob_str, TRUE); } gboolean matched = FALSE; while (child) { const char *stanza_name = xmpp_stanza_get_name(child); if (stanza_name && (g_strcmp0(stanza_name, STANZA_NAME_ITEM) == 0)) { const char *item_jid = xmpp_stanza_get_attribute(child, STANZA_ATTR_JID); gchar *item_jid_lower = NULL; if (item_jid) { Jid *jidp = jid_create(item_jid); if (jidp && jidp->localpart) { item_jid_lower = g_utf8_strdown(jidp->localpart, -1); } jid_destroy(jidp); } const char *item_name = xmpp_stanza_get_attribute(child, STANZA_ATTR_NAME); gchar *item_name_lower = NULL; if (item_name) { item_name_lower = g_utf8_strdown(item_name, -1); } if ((item_jid_lower) && ((glob == NULL) || ((g_pattern_match(glob, strlen(item_jid_lower), item_jid_lower, NULL)) || (item_name_lower && g_pattern_match(glob, strlen(item_name_lower), item_name_lower, NULL))))) { if (glob) { matched = TRUE; } GString *item = g_string_new(item_jid); if (item_name) { g_string_append(item, " ("); g_string_append(item, item_name); g_string_append(item, ")"); } cons_show(" %s", item->str); g_string_free(item, TRUE); } g_free(item_jid_lower); g_free(item_name_lower); } child = xmpp_stanza_get_next(child); } if (glob && matched == FALSE) { cons_show(" No rooms found matching filter: %s", filter); } if (glob) { g_pattern_spec_free(glob); } return 0; } static int _command_list_result_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *id = xmpp_stanza_get_id(stanza); const char *type = xmpp_stanza_get_type(stanza); char *from = strdup(xmpp_stanza_get_from(stanza)); if (id) { log_debug("IQ command list result handler fired, id: %s.", id); } else { log_debug("IQ command list result handler fired."); } // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); log_debug("Error retrieving command list for %s: %s", from, error_message); ProfWin *win = wins_get_by_string(from); if (win) { win_command_list_error(win, error_message); } free(error_message); free(from); return 0; } GSList *cmds = NULL; xmpp_stanza_t *query = xmpp_stanza_get_child_by_ns(stanza, XMPP_NS_DISCO_ITEMS); if (query) { xmpp_stanza_t *child = xmpp_stanza_get_children(query); while (child) { const char *name = xmpp_stanza_get_name(child); if (g_strcmp0(name, "item") == 0) { const char *node = xmpp_stanza_get_attribute(child, STANZA_ATTR_NODE); if (node) { cmds = g_slist_insert_sorted(cmds, (gpointer)node, (GCompareFunc)g_strcmp0); } } child = xmpp_stanza_get_next(child); } } ProfWin *win = wins_get_by_string(from); if (win == NULL) { win = wins_get_console(); } win_handle_command_list(win, cmds); g_slist_free(cmds); free(from); return 0; } static int _command_exec_response_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *id = xmpp_stanza_get_id(stanza); const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_from(stanza); char *command = userdata; if (id) { log_debug("IQ command exec response handler fired, id: %s.", id); } else { log_debug("IQ command exec response handler fired."); } ProfWin *win = wins_get_by_string(from); if (win == NULL) { /* No more window associated with this command. * Fallback to console. */ win = wins_get_console(); } // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); log_debug("Error executing command %s for %s: %s", command, from, error_message); win_command_exec_error(win, command, error_message); free(error_message); return 0; } xmpp_stanza_t *cmd = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_COMMAND); if (!cmd) { log_error("No command element for command response"); win_command_exec_error(win, command, "Malformed command response"); return 0; } const char *status = xmpp_stanza_get_attribute(cmd, STANZA_ATTR_STATUS); if (g_strcmp0(status, "completed") == 0) { win_handle_command_exec_status(win, command, "completed"); xmpp_stanza_t *note = xmpp_stanza_get_child_by_name(cmd, "note"); if (note) { const char *type = xmpp_stanza_get_attribute(note, "type"); const char *value = xmpp_stanza_get_text(note); win_handle_command_exec_result_note(win, type, value); } xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(cmd, STANZA_NS_DATA); if (x) { xmpp_stanza_t *roster = xmpp_stanza_get_child_by_ns(x, XMPP_NS_ROSTER); if (roster) { /* Special handling of xep-0133 roster in response */ GSList *list = NULL; xmpp_stanza_t *child = xmpp_stanza_get_children(roster); while (child) { const char *barejid = xmpp_stanza_get_attribute(child, STANZA_ATTR_JID); gchar *barejid_lower = g_utf8_strdown(barejid, -1); const char *name = xmpp_stanza_get_attribute(child, STANZA_ATTR_NAME); const char *sub = xmpp_stanza_get_attribute(child, STANZA_ATTR_SUBSCRIPTION); const char *ask = xmpp_stanza_get_attribute(child, STANZA_ATTR_ASK); GSList *groups = NULL; groups = roster_get_groups_from_item(child); gboolean pending_out = FALSE; if (ask && (strcmp(ask, "subscribe") == 0)) { pending_out = TRUE; } PContact contact = p_contact_new(barejid_lower, name, groups, sub, NULL, pending_out); list = g_slist_insert_sorted(list, contact, (GCompareFunc)roster_compare_name); child = xmpp_stanza_get_next(child); } cons_show_roster(list); g_slist_free(list); } else { DataForm *form = form_create(x); ProfConfWin *confwin = (ProfConfWin*)wins_new_config(from, form, NULL, NULL, NULL); confwin_handle_configuration(confwin, form); } } } else if (g_strcmp0(status, "executing") == 0) { win_handle_command_exec_status(win, command, "executing"); /* Looking for a jabber:x:data type form */ xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(cmd, STANZA_NS_DATA); if (x == NULL) { return 0; } const char *form_type = xmpp_stanza_get_type(x); if (g_strcmp0(form_type, "form") != 0) { log_error("Unsupported payload in command response"); win_command_exec_error(win, command, "Unsupported command response"); return 0; } const char *sessionid = xmpp_stanza_get_attribute(cmd, "sessionid"); DataForm *form = form_create(x); CommandConfigData *data = malloc(sizeof(CommandConfigData)); if (sessionid == NULL) { data->sessionid = NULL; } else { data->sessionid = strdup(sessionid); } data->command = command; ProfConfWin *confwin = (ProfConfWin*)wins_new_config(from, form, iq_submit_command_config, iq_cancel_command_config, data); confwin_handle_configuration(confwin, form); } else if (g_strcmp0(status, "canceled") == 0) { win_handle_command_exec_status(win, command, "canceled"); xmpp_stanza_t *note = xmpp_stanza_get_child_by_name(cmd, "note"); if (note) { const char *type = xmpp_stanza_get_attribute(note, "type"); const char *value = xmpp_stanza_get_text(note); win_handle_command_exec_result_note(win, type, value); } } else { log_error("Unsupported command status %s", status); win_command_exec_error(win, command, "Malformed command response"); } return 0; } static int _enable_carbons_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *type = xmpp_stanza_get_type(stanza); if (g_strcmp0(type, "error") == 0) { char *error_message = stanza_get_error_message(stanza); cons_show_error("Server error enabling message carbons: %s", error_message); log_debug("Error enabling carbons: %s", error_message); free(error_message); } else { log_debug("Message carbons enabled."); } return 0; } static int _disable_carbons_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *type = xmpp_stanza_get_type(stanza); if (g_strcmp0(type, "error") == 0) { char *error_message = stanza_get_error_message(stanza); cons_show_error("Server error disabling message carbons: %s", error_message); log_debug("Error disabling carbons: %s", error_message); free(error_message); } else { log_debug("Message carbons disabled."); } return 0; } static int _manual_pong_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *from = xmpp_stanza_get_from(stanza); const char *type = xmpp_stanza_get_type(stanza); GDateTime *sent = (GDateTime *)userdata; // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); if (!error_message) { cons_show_error("Error returned from pinging %s.", from); } else { cons_show_error("Error returned from pinging %s: %s.", from, error_message); } free(error_message); return 0; } GDateTime *now = g_date_time_new_now_local(); GTimeSpan elapsed = g_date_time_difference(now, sent); int elapsed_millis = elapsed / 1000; g_date_time_unref(now); if (from == NULL) { cons_show("Ping response from server: %dms.", elapsed_millis); } else { cons_show("Ping response from %s: %dms.", from, elapsed_millis); } return 0; } static int _autoping_timed_send(xmpp_conn_t *const conn, void *const userdata) { if (connection_get_status() != JABBER_CONNECTED) { return 1; } if (connection_supports(XMPP_FEATURE_PING) == FALSE) { log_warning("Server doesn't advertise %s feature, disabling autoping.", XMPP_FEATURE_PING); prefs_set_autoping(0); cons_show_error("Server ping not supported, autoping disabled."); xmpp_conn_t *conn = connection_get_conn(); xmpp_timed_handler_delete(conn, _autoping_timed_send); return 1; } if (autoping_wait) { log_debug("Autoping: Existing ping already in progress, aborting"); return 1; } xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; xmpp_stanza_t *iq = stanza_create_ping_iq(ctx, NULL); const char *id = xmpp_stanza_get_id(iq); log_debug("Autoping: Sending ping request: %s", id); // add pong handler iq_id_handler_add(id, _auto_pong_id_handler, NULL, NULL); iq_send_stanza(iq); xmpp_stanza_release(iq); autoping_wait = TRUE; if (autoping_time) { g_timer_destroy(autoping_time); } autoping_time = g_timer_new(); return 1; } static int _auto_pong_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { iq_autoping_timer_cancel(); const char *id = xmpp_stanza_get_id(stanza); if (id == NULL) { log_debug("Autoping: Pong handler fired."); return 0; } log_debug("Autoping: Pong handler fired: %s.", id); const char *type = xmpp_stanza_get_type(stanza); if (type == NULL) { return 0; } if (g_strcmp0(type, STANZA_TYPE_ERROR) != 0) { return 0; } // show warning if error char *error_msg = stanza_get_error_message(stanza); log_warning("Server ping (id=%s) responded with error: %s", id, error_msg); free(error_msg); // turn off autoping if error type is 'cancel' xmpp_stanza_t *error = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ERROR); if (error == NULL) { return 0; } const char *errtype = xmpp_stanza_get_type(error); if (errtype == NULL) { return 0; } if (g_strcmp0(errtype, "cancel") == 0) { log_warning("Server ping (id=%s) error type 'cancel', disabling autoping.", id); prefs_set_autoping(0); cons_show_error("Server ping not supported, autoping disabled."); xmpp_conn_t *conn = connection_get_conn(); xmpp_timed_handler_delete(conn, _autoping_timed_send); } return 0; } static int _version_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *id = xmpp_stanza_get_id(stanza); if (id) { log_debug("IQ version result handler fired, id: %s.", id); } else { log_debug("IQ version result handler fired."); } const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_from(stanza); if (g_strcmp0(type, STANZA_TYPE_RESULT) != 0) { if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); ui_handle_software_version_error(from, error_message); free(error_message); } else { ui_handle_software_version_error(from, "unknown error"); log_error("Software version result with unrecognised type attribute."); } return 0; } const char *jid = xmpp_stanza_get_from(stanza); xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); if (query == NULL) { log_error("Software version result received with no query element."); return 0; } const char *ns = xmpp_stanza_get_ns(query); if (g_strcmp0(ns, STANZA_NS_VERSION) != 0) { log_error("Software version result received without namespace."); return 0; } char *name_str = NULL; char *version_str = NULL; char *os_str = NULL; xmpp_stanza_t *name = xmpp_stanza_get_child_by_name(query, "name"); xmpp_stanza_t *version = xmpp_stanza_get_child_by_name(query, "version"); xmpp_stanza_t *os = xmpp_stanza_get_child_by_name(query, "os"); if (name) { name_str = xmpp_stanza_get_text(name); } if (version) { version_str = xmpp_stanza_get_text(version); } if (os) { os_str = xmpp_stanza_get_text(os); } if (g_strcmp0(jid, (char*)userdata) != 0) { log_warning("From attribute specified different JID, using original JID."); } xmpp_conn_t *conn = connection_get_conn(); xmpp_ctx_t *ctx = xmpp_conn_get_context(conn); Jid *jidp = jid_create((char*)userdata); const char *presence = NULL; if (muc_active(jidp->barejid)) { Occupant *occupant = muc_roster_item(jidp->barejid, jidp->resourcepart); presence = string_from_resource_presence(occupant->presence); } else { PContact contact = roster_get_contact(jidp->barejid); if (contact) { Resource *resource = p_contact_get_resource(contact, jidp->resourcepart); if (!resource) { ui_handle_software_version_error(jidp->fulljid, "Unknown resource"); if (name_str) xmpp_free(ctx, name_str); if (version_str) xmpp_free(ctx, version_str); if (os_str) xmpp_free(ctx, os_str); return 0; } presence = string_from_resource_presence(resource->presence); } else { presence = "offline"; } } ui_show_software_version(jidp->fulljid, presence, name_str, version_str, os_str); jid_destroy(jidp); if (name_str) xmpp_free(ctx, name_str); if (version_str) xmpp_free(ctx, version_str); if (os_str) xmpp_free(ctx, os_str); return 0; } static void _ping_get_handler(xmpp_stanza_t *const stanza) { xmpp_ctx_t * const ctx = connection_get_ctx(); const char *id = xmpp_stanza_get_id(stanza); const char *to = xmpp_stanza_get_to(stanza); const char *from = xmpp_stanza_get_from(stanza); if (id) { log_debug("IQ ping get handler fired, id: %s.", id); } else { log_debug("IQ ping get handler fired."); } if ((from == NULL) || (to == NULL)) { return; } xmpp_stanza_t *pong = xmpp_iq_new(ctx, STANZA_TYPE_RESULT, id); xmpp_stanza_set_to(pong, from); xmpp_stanza_set_from(pong, to); iq_send_stanza(pong); xmpp_stanza_release(pong); } static void _version_get_handler(xmpp_stanza_t *const stanza) { xmpp_ctx_t * const ctx = connection_get_ctx(); const char *id = xmpp_stanza_get_id(stanza); const char *from = xmpp_stanza_get_from(stanza); if (id) { log_debug("IQ version get handler fired, id: %s.", id); } else { log_debug("IQ version get handler fired."); } if (from) { xmpp_stanza_t *response = xmpp_iq_new(ctx, STANZA_TYPE_RESULT, id); xmpp_stanza_set_to(response, from); xmpp_stanza_t *query = xmpp_stanza_new(ctx); xmpp_stanza_set_name(query, STANZA_NAME_QUERY); xmpp_stanza_set_ns(query, STANZA_NS_VERSION); xmpp_stanza_t *name = xmpp_stanza_new(ctx); xmpp_stanza_set_name(name, "name"); xmpp_stanza_t *name_txt = xmpp_stanza_new(ctx); xmpp_stanza_set_text(name_txt, "Profanity"); xmpp_stanza_add_child(name, name_txt); xmpp_stanza_t *version = xmpp_stanza_new(ctx); xmpp_stanza_set_name(version, "version"); xmpp_stanza_t *version_txt = xmpp_stanza_new(ctx); GString *version_str = g_string_new(PACKAGE_VERSION); if (strcmp(PACKAGE_STATUS, "development") == 0) { #ifdef HAVE_GIT_VERSION g_string_append(version_str, "dev."); g_string_append(version_str, PROF_GIT_BRANCH); g_string_append(version_str, "."); g_string_append(version_str, PROF_GIT_REVISION); #else g_string_append(version_str, "dev"); #endif } xmpp_stanza_set_text(version_txt, version_str->str); xmpp_stanza_add_child(version, version_txt); xmpp_stanza_add_child(query, name); xmpp_stanza_add_child(query, version); xmpp_stanza_add_child(response, query); iq_send_stanza(response); g_string_free(version_str, TRUE); xmpp_stanza_release(name_txt); xmpp_stanza_release(version_txt); xmpp_stanza_release(name); xmpp_stanza_release(version); xmpp_stanza_release(query); xmpp_stanza_release(response); } } static void _disco_items_get_handler(xmpp_stanza_t *const stanza) { xmpp_ctx_t * const ctx = connection_get_ctx(); const char *id = xmpp_stanza_get_id(stanza); const char *from = xmpp_stanza_get_from(stanza); if (id) { log_debug("IQ disco items get handler fired, id: %s.", id); } else { log_debug("IQ disco items get handler fired."); } if (from) { xmpp_stanza_t *response = xmpp_iq_new(ctx, STANZA_TYPE_RESULT, xmpp_stanza_get_id(stanza)); xmpp_stanza_set_to(response, from); xmpp_stanza_t *query = xmpp_stanza_new(ctx); xmpp_stanza_set_name(query, STANZA_NAME_QUERY); xmpp_stanza_set_ns(query, XMPP_NS_DISCO_ITEMS); xmpp_stanza_add_child(response, query); iq_send_stanza(response); xmpp_stanza_release(response); } } static void _last_activity_get_handler(xmpp_stanza_t *const stanza) { xmpp_ctx_t *ctx = connection_get_ctx(); const char *from = xmpp_stanza_get_from(stanza); if (!from) { return; } if (prefs_get_boolean(PREF_LASTACTIVITY)) { int idls_secs = ui_get_idle_time() / 1000; char str[50]; sprintf(str, "%d", idls_secs); xmpp_stanza_t *response = xmpp_iq_new(ctx, STANZA_TYPE_RESULT, xmpp_stanza_get_id(stanza)); xmpp_stanza_set_to(response, from); xmpp_stanza_t *query = xmpp_stanza_new(ctx); xmpp_stanza_set_name(query, STANZA_NAME_QUERY); xmpp_stanza_set_attribute(query, STANZA_ATTR_XMLNS, STANZA_NS_LASTACTIVITY); xmpp_stanza_set_attribute(query, "seconds", str); xmpp_stanza_add_child(response, query); xmpp_stanza_release(query); iq_send_stanza(response); xmpp_stanza_release(response); } else { xmpp_stanza_t *response = xmpp_iq_new(ctx, STANZA_TYPE_ERROR, xmpp_stanza_get_id(stanza)); xmpp_stanza_set_to(response, from); xmpp_stanza_t *error = xmpp_stanza_new(ctx); xmpp_stanza_set_name(error, STANZA_NAME_ERROR); xmpp_stanza_set_type(error, "cancel"); xmpp_stanza_t *service_unavailable = xmpp_stanza_new(ctx); xmpp_stanza_set_name(service_unavailable, "service-unavailable"); xmpp_stanza_set_ns(service_unavailable, "urn:ietf:params:xml:ns:xmpp-stanzas"); xmpp_stanza_add_child(error, service_unavailable); xmpp_stanza_release(service_unavailable); xmpp_stanza_add_child(response, error); xmpp_stanza_release(error); iq_send_stanza(response); xmpp_stanza_release(response); } } static void _disco_info_get_handler(xmpp_stanza_t *const stanza) { xmpp_ctx_t * const ctx = connection_get_ctx(); const char *from = xmpp_stanza_get_from(stanza); xmpp_stanza_t *incoming_query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); const char *node_str = xmpp_stanza_get_attribute(incoming_query, STANZA_ATTR_NODE); const char *id = xmpp_stanza_get_id(stanza); if (id) { log_debug("IQ disco info get handler fired, id: %s.", id); } else { log_debug("IQ disco info get handler fired."); } if (from) { xmpp_stanza_t *response = xmpp_iq_new(ctx, STANZA_TYPE_RESULT, xmpp_stanza_get_id(stanza)); xmpp_stanza_set_to(response, from); xmpp_stanza_t *query = stanza_create_caps_query_element(ctx); if (node_str) { xmpp_stanza_set_attribute(query, STANZA_ATTR_NODE, node_str); } xmpp_stanza_add_child(response, query); iq_send_stanza(response); xmpp_stanza_release(query); xmpp_stanza_release(response); } } static int _destroy_room_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *id = xmpp_stanza_get_id(stanza); if (id) { log_debug("IQ destroy room result handler fired, id: %s.", id); } else { log_debug("IQ destroy room result handler fired."); } const char *from = xmpp_stanza_get_from(stanza); if (from == NULL) { log_error("No from attribute for IQ destroy room result"); } else { sv_ev_room_destroy(from); } return 0; } static int _room_config_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *id = xmpp_stanza_get_id(stanza); const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_from(stanza); if (id) { log_debug("IQ room config handler fired, id: %s.", id); } else { log_debug("IQ room config handler fired."); } // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); ui_handle_room_configuration_form_error(from, error_message); free(error_message); return 0; } if (from == NULL) { log_warning("No from attribute for IQ config request result"); ui_handle_room_configuration_form_error(from, "No from attribute for room cofig response."); return 0; } xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); if (query == NULL) { log_warning("No query element found parsing room config response"); ui_handle_room_configuration_form_error(from, "No query element found parsing room config response"); return 0; } xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(query, STANZA_NS_DATA); if (x == NULL) { log_warning("No x element found with %s namespace parsing room config response", STANZA_NS_DATA); ui_handle_room_configuration_form_error(from, "No form configuration options available"); return 0; } const char *form_type = xmpp_stanza_get_type(x); if (g_strcmp0(form_type, "form") != 0) { log_warning("x element not of type 'form' parsing room config response"); ui_handle_room_configuration_form_error(from, "Form not of type 'form' parsing room config response."); return 0; } DataForm *form = form_create(x); ProfConfWin *confwin = (ProfConfWin*)wins_new_config(from, form, iq_submit_room_config, iq_room_config_cancel, NULL); confwin_handle_configuration(confwin, form); return 0; } static int _room_affiliation_set_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *id = xmpp_stanza_get_id(stanza); const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_from(stanza); ProfPrivilegeSet *affiliation_set = (ProfPrivilegeSet*)userdata; if (id) { log_debug("IQ affiliation set handler fired, id: %s.", id); } else { log_debug("IQ affiliation set handler fired."); } // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); log_debug("Error setting affiliation %s list for room %s, user %s: %s", affiliation_set->privilege, from, affiliation_set->item, error_message); ProfMucWin *mucwin = wins_get_muc(from); if (mucwin) { mucwin_affiliation_set_error(mucwin, affiliation_set->item, affiliation_set->privilege, error_message); } free(error_message); } return 0; } static int _room_role_set_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *id = xmpp_stanza_get_id(stanza); const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_from(stanza); ProfPrivilegeSet *role_set = (ProfPrivilegeSet*)userdata; if (id) { log_debug("IQ role set handler fired, id: %s.", id); } else { log_debug("IQ role set handler fired."); } // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); log_debug("Error setting role %s list for room %s, user %s: %s", role_set->privilege, from, role_set->item, error_message); ProfMucWin *mucwin = wins_get_muc(from); if (mucwin) { mucwin_role_set_error(mucwin, role_set->item, role_set->privilege, error_message); } free(error_message); } return 0; } static int _room_affiliation_list_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *id = xmpp_stanza_get_id(stanza); const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_from(stanza); char *affiliation = (char *)userdata; if (id) { log_debug("IQ affiliation list result handler fired, id: %s.", id); } else { log_debug("IQ affiliation list result handler fired."); } // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); log_debug("Error retrieving %s list for room %s: %s", affiliation, from, error_message); ProfMucWin *mucwin = wins_get_muc(from); if (mucwin) { mucwin_affiliation_list_error(mucwin, affiliation, error_message); } free(error_message); return 0; } GSList *jids = NULL; xmpp_stanza_t *query = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_ADMIN); if (query) { xmpp_stanza_t *child = xmpp_stanza_get_children(query); while (child) { const char *name = xmpp_stanza_get_name(child); if (g_strcmp0(name, "item") == 0) { const char *jid = xmpp_stanza_get_attribute(child, STANZA_ATTR_JID); if (jid) { jids = g_slist_insert_sorted(jids, (gpointer)jid, (GCompareFunc)g_strcmp0); } } child = xmpp_stanza_get_next(child); } } muc_jid_autocomplete_add_all(from, jids); ProfMucWin *mucwin = wins_get_muc(from); if (mucwin) { mucwin_handle_affiliation_list(mucwin, affiliation, jids); } g_slist_free(jids); return 0; } static int _room_role_list_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *id = xmpp_stanza_get_id(stanza); const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_from(stanza); char *role = (char *)userdata; if (id) { log_debug("IQ role list result handler fired, id: %s.", id); } else { log_debug("IQ role list result handler fired."); } // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); log_debug("Error retrieving %s list for room %s: %s", role, from, error_message); ProfMucWin *mucwin = wins_get_muc(from); if (mucwin) { mucwin_role_list_error(mucwin, role, error_message); } free(error_message); return 0; } GSList *nicks = NULL; xmpp_stanza_t *query = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_ADMIN); if (query) { xmpp_stanza_t *child = xmpp_stanza_get_children(query); while (child) { const char *name = xmpp_stanza_get_name(child); if (g_strcmp0(name, "item") == 0) { const char *nick = xmpp_stanza_get_attribute(child, STANZA_ATTR_NICK); if (nick) { nicks = g_slist_insert_sorted(nicks, (gpointer)nick, (GCompareFunc)g_strcmp0); } } child = xmpp_stanza_get_next(child); } } ProfMucWin *mucwin = wins_get_muc(from); if (mucwin) { mucwin_handle_role_list(mucwin, role, nicks); } g_slist_free(nicks); return 0; } static int _room_config_submit_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *id = xmpp_stanza_get_id(stanza); const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_from(stanza); if (id) { log_debug("IQ room config submit handler fired, id: %s.", id); } else { log_debug("IQ room config submit handler fired."); } // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); ui_handle_room_config_submit_result_error(from, error_message); free(error_message); return 0; } ui_handle_room_config_submit_result(from); return 0; } static int _room_kick_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *id = xmpp_stanza_get_id(stanza); const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_from(stanza); char *nick = (char *)userdata; if (id) { log_debug("IQ kick result handler fired, id: %s.", id); } else { log_debug("IQ kick result handler fired."); } // handle error responses ProfMucWin *mucwin = wins_get_muc(from); if (mucwin && (g_strcmp0(type, STANZA_TYPE_ERROR) == 0)) { char *error_message = stanza_get_error_message(stanza); mucwin_kick_error(mucwin, nick, error_message); free(error_message); } return 0; } static void _identity_destroy(DiscoIdentity *identity) { if (identity) { free(identity->name); free(identity->type); free(identity->category); free(identity); } } static void _item_destroy(DiscoItem *item) { if (item) { free(item->jid); free(item->name); free(item); } } static int _room_info_response_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *type = xmpp_stanza_get_type(stanza); ProfRoomInfoData *cb_data = (ProfRoomInfoData *)userdata; log_info("Received disco#info response for room: %s", cb_data->room); // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { ProfMucWin *mucwin = wins_get_muc(cb_data->room); if (mucwin && cb_data->display) { char *error_message = stanza_get_error_message(stanza); mucwin_room_info_error(mucwin, error_message); free(error_message); } return 0; } xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); if (query) { xmpp_stanza_t *child = xmpp_stanza_get_children(query); GSList *identities = NULL; GSList *features = NULL; while (child) { const char *stanza_name = xmpp_stanza_get_name(child); if (g_strcmp0(stanza_name, STANZA_NAME_FEATURE) == 0) { const char *var = xmpp_stanza_get_attribute(child, STANZA_ATTR_VAR); if (var) { features = g_slist_append(features, strdup(var)); } } else if (g_strcmp0(stanza_name, STANZA_NAME_IDENTITY) == 0) { const char *name = xmpp_stanza_get_attribute(child, STANZA_ATTR_NAME); const char *type = xmpp_stanza_get_type(child); const char *category = xmpp_stanza_get_attribute(child, STANZA_ATTR_CATEGORY); if (name || category || type) { DiscoIdentity *identity = malloc(sizeof(struct disco_identity_t)); if (name) { identity->name = strdup(name); } else { identity->name = NULL; } if (category) { identity->category = strdup(category); } else { identity->category = NULL; } if (type) { identity->type = strdup(type); } else { identity->type = NULL; } identities = g_slist_append(identities, identity); } } child = xmpp_stanza_get_next(child); } muc_set_features(cb_data->room, features); ProfMucWin *mucwin = wins_get_muc(cb_data->room); if (mucwin) { #ifdef HAVE_OMEMO if (muc_anonymity_type(mucwin->roomjid) == MUC_ANONYMITY_TYPE_NONANONYMOUS && omemo_automatic_start(cb_data->room)) { omemo_start_muc_sessions(cb_data->room); mucwin->is_omemo = TRUE; } #endif if (cb_data->display) { mucwin_room_disco_info(mucwin, identities, features); } } g_slist_free_full(features, free); g_slist_free_full(identities, (GDestroyNotify)_identity_destroy); } return 0; } static int _last_activity_response_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *from = xmpp_stanza_get_from(stanza); if (!from) { cons_show_error("Invalid last activity response received."); log_info("Received last activity response with no from attribute."); return 0; } const char *type = xmpp_stanza_get_type(stanza); // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); if (from) { cons_show_error("Last activity request failed for %s: %s", from, error_message); } else { cons_show_error("Last activity request failed: %s", error_message); } free(error_message); return 0; } xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); if (!query) { cons_show_error("Invalid last activity response received."); log_info("Received last activity response with no query element."); return 0; } const char *seconds_str = xmpp_stanza_get_attribute(query, "seconds"); if (!seconds_str) { cons_show_error("Invalid last activity response received."); log_info("Received last activity response with no seconds attribute."); return 0; } int seconds = atoi(seconds_str); if (seconds < 0) { cons_show_error("Invalid last activity response received."); log_info("Received last activity response with negative value."); return 0; } char *msg = xmpp_stanza_get_text(query); sv_ev_lastactivity_response(from, seconds, msg); xmpp_free(connection_get_ctx(), msg); return 0; } static int _disco_info_response_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { const char *from = xmpp_stanza_get_from(stanza); const char *type = xmpp_stanza_get_type(stanza); if (from) { log_info("Received disco#info response from: %s", from); } else { log_info("Received disco#info response"); } // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); if (from) { cons_show_error("Service discovery failed for %s: %s", from, error_message); } else { cons_show_error("Service discovery failed: %s", error_message); } free(error_message); return 0; } xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); if (query) { xmpp_stanza_t *child = xmpp_stanza_get_children(query); GSList *identities = NULL; GSList *features = NULL; while (child) { const char *stanza_name = xmpp_stanza_get_name(child); if (g_strcmp0(stanza_name, STANZA_NAME_FEATURE) == 0) { const char *var = xmpp_stanza_get_attribute(child, STANZA_ATTR_VAR); if (var) { features = g_slist_append(features, strdup(var)); } } else if (g_strcmp0(stanza_name, STANZA_NAME_IDENTITY) == 0) { const char *name = xmpp_stanza_get_attribute(child, STANZA_ATTR_NAME); const char *type = xmpp_stanza_get_type(child); const char *category = xmpp_stanza_get_attribute(child, STANZA_ATTR_CATEGORY); if (name || category || type) { DiscoIdentity *identity = malloc(sizeof(struct disco_identity_t)); if (name) { identity->name = strdup(name); } else { identity->name = NULL; } if (category) { identity->category = strdup(category); } else { identity->category = NULL; } if (type) { identity->type = strdup(type); } else { identity->type = NULL; } identities = g_slist_append(identities, identity); } } child = xmpp_stanza_get_next(child); } cons_show_disco_info(from, identities, features); g_slist_free_full(features, free); g_slist_free_full(identities, (GDestroyNotify)_identity_destroy); } return 0; } static int _disco_info_response_id_handler_onconnect(xmpp_stanza_t *const stanza, void *const userdata) { const char *from = xmpp_stanza_get_from(stanza); const char *type = xmpp_stanza_get_type(stanza); if (from) { log_info("Received disco#info response from: %s", from); } else { log_info("Received disco#info response"); } // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); if (from) { log_error("Service discovery failed for %s: %s", from, error_message); } else { log_error("Service discovery failed: %s", error_message); } free(error_message); return 0; } xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); if (query) { GHashTable *features = connection_get_features(from); if (features == NULL) { log_error("No matching disco item found for %s", from); return 1; } xmpp_stanza_t *child = xmpp_stanza_get_children(query); while (child) { const char *stanza_name = xmpp_stanza_get_name(child); if (g_strcmp0(stanza_name, STANZA_NAME_FEATURE) == 0) { const char *var = xmpp_stanza_get_attribute(child, STANZA_ATTR_VAR); if (var) { g_hash_table_add(features, strdup(var)); } } child = xmpp_stanza_get_next(child); } } connection_features_received(from); return 0; } static int _http_upload_response_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { HTTPUpload *upload = (HTTPUpload *)userdata; const char *from = xmpp_stanza_get_from(stanza); const char *type = xmpp_stanza_get_type(stanza); if (from) { log_info("Received http_upload response from: %s", from); } else { log_info("Received http_upload response"); } // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); if (from) { cons_show_error("Uploading '%s' failed for %s: %s", upload->filename, from, error_message); } else { cons_show_error("Uploading '%s' failed: %s", upload->filename, error_message); } free(error_message); return 0; } xmpp_stanza_t *slot = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_SLOT); if (slot && g_strcmp0(xmpp_stanza_get_ns(slot), STANZA_NS_HTTP_UPLOAD) == 0) { xmpp_stanza_t *put = xmpp_stanza_get_child_by_name(slot, STANZA_NAME_PUT); xmpp_stanza_t *get = xmpp_stanza_get_child_by_name(slot, STANZA_NAME_GET); if (put && get) { char *put_url = xmpp_stanza_get_text(put); char *get_url = xmpp_stanza_get_text(get); upload->put_url = strdup(put_url); upload->get_url = strdup(get_url); xmpp_conn_t *conn = connection_get_conn(); xmpp_ctx_t *ctx = xmpp_conn_get_context(conn); if (put_url) xmpp_free(ctx, put_url); if (get_url) xmpp_free(ctx, get_url); pthread_create(&(upload->worker), NULL, &http_file_put, upload); upload_processes = g_slist_append(upload_processes, upload); } else { log_error("Invalid XML in HTTP Upload slot"); return 1; } } return 0; } static void _disco_items_result_handler(xmpp_stanza_t *const stanza) { log_debug("Received disco#items response"); const char *id = xmpp_stanza_get_id(stanza); const char *from = xmpp_stanza_get_from(stanza); GSList *items = NULL; if ((g_strcmp0(id, "discoitemsreq") != 0) && (g_strcmp0(id, "discoitemsreq_onconnect") != 0)) { return; } log_debug("Response to query: %s", id); xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); if (query == NULL) { return; } xmpp_stanza_t *child = xmpp_stanza_get_children(query); if (child == NULL) { return; } while (child) { const char *stanza_name = xmpp_stanza_get_name(child); if (stanza_name && (g_strcmp0(stanza_name, STANZA_NAME_ITEM) == 0)) { const char *item_jid = xmpp_stanza_get_attribute(child, STANZA_ATTR_JID); if (item_jid) { DiscoItem *item = malloc(sizeof(struct disco_item_t)); item->jid = strdup(item_jid); const char *item_name = xmpp_stanza_get_attribute(child, STANZA_ATTR_NAME); if (item_name) { item->name = strdup(item_name); } else { item->name = NULL; } items = g_slist_append(items, item); } } child = xmpp_stanza_get_next(child); } if (g_strcmp0(id, "discoitemsreq") == 0) { cons_show_disco_items(items, from); } else if (g_strcmp0(id, "discoitemsreq_onconnect") == 0) { connection_set_disco_items(items); } g_slist_free_full(items, (GDestroyNotify)_item_destroy); } void iq_send_stanza(xmpp_stanza_t *const stanza) { char *text; size_t text_size; xmpp_stanza_to_text(stanza, &text, &text_size); xmpp_conn_t *conn = connection_get_conn(); char *plugin_text = plugins_on_iq_stanza_send(text); if (plugin_text) { xmpp_send_raw_string(conn, "%s", plugin_text); free(plugin_text); } else { xmpp_send_raw_string(conn, "%s", text); } xmpp_free(connection_get_ctx(), text); } static void _iq_free_room_data(ProfRoomInfoData *roominfo) { if (roominfo) { free(roominfo->room); free(roominfo); } } static void _iq_free_affiliation_set(ProfPrivilegeSet *affiliation_set) { if (affiliation_set) { free(affiliation_set->item); free(affiliation_set->privilege); free(affiliation_set); } }